add some more tests

This commit is contained in:
Reinier Balt 2014-02-10 19:39:39 +01:00
parent b23338eaa2
commit 5de96d7eda
8 changed files with 281 additions and 65 deletions

View file

@ -21,31 +21,16 @@ module RecurringTodos
# build does not add tags. For tags, the recurring todos needs to be saved
def build
@recurring_todo = @pattern.build_recurring_todo(@mapped_attributes)
end
def update(recurring_todo)
@recurring_todo = @pattern.update_recurring_todo(recurring_todo, @mapped_attributes)
@saved = @recurring_todo.save
@recurring_todo.tag_with(@filterred_attributes.get(:tag_list)) if @saved && @filterred_attributes.get(:tag_list).present?
@recurring_todo.reload
return @saved
save_recurring_todo
end
def save
build
@saved = @recurring_todo.save
@recurring_todo.tag_with(@filterred_attributes.get(:tag_list)) if @saved && @filterred_attributes.get(:tag_list).present?
return @saved
end
def save_collection(collection, collection_id)
# save object (project or context) and add its id to @mapped_attributes and remove the object from the attributes
object = @mapped_attributes.get(collection)
object.save
@mapped_attributes.set(collection_id, object.id)
@mapped_attributes.except(collection)
save_recurring_todo
end
def save_project
@ -71,29 +56,31 @@ module RecurringTodos
end
def filter_attributes(attributes)
# get pattern independend attributes
filterred_attributes = filter_generic_attributes(attributes)
attributes_to_filter.each{|key| filterred_attributes.set(key, attributes.get(key)) if attributes.key?(key)}
# append pattern specific attributes
attributes_to_filter.each{|key| filterred_attributes[key]= attributes[key] if attributes.key?(key)}
filterred_attributes
end
def filter_generic_attributes(attributes)
return Tracks::AttributeHandler.new(@user, {
recurring_period: attributes.get(:recurring_period),
description: attributes.get(:description),
notes: attributes.get(:notes),
recurring_period: attributes[:recurring_period],
description: attributes[:description],
notes: attributes[:notes],
tag_list: tag_list_or_empty_string(attributes),
start_from: attributes.get(:start_from),
end_date: attributes.get(:end_date),
ends_on: attributes.get(:ends_on),
show_always: attributes.get(:show_always),
target: attributes.get(:target),
project: attributes.get(:project),
context: attributes.get(:context),
project_id: attributes.get(:project_id),
context_id: attributes.get(:context_id),
target: attributes.get(:recurring_target),
show_from_delta: attributes.get(:recurring_show_days_before),
show_always: attributes.get(:recurring_show_always)
start_from: attributes[:start_from],
end_date: attributes[:end_date],
ends_on: attributes[:ends_on],
target: attributes[:target],
project: attributes[:project],
context: attributes[:context],
project_id: attributes[:project_id],
context_id: attributes[:context_id],
target: attributes[:recurring_target],
show_from_delta: attributes[:recurring_show_days_before],
show_always: attributes[:recurring_show_always]
})
end
@ -103,8 +90,9 @@ module RecurringTodos
end
# helper method to be used in mapped_attributes in subclasses
# changes name of key from source_key to key
def map(mapping, key, source_key)
mapping.set(key, mapping.get(source_key))
mapping[key] = mapping[source_key]
mapping.except(source_key)
end
@ -117,7 +105,7 @@ module RecurringTodos
return nil if key.nil?
raise Exception.new, "recurrence selector pattern (#{key}) not given" unless @attributes.selector_key_present?(key)
selector = @attributes.get(key)
selector = @attributes[key]
raise Exception.new, "unknown recurrence selector pattern: '#{selector}'" unless valid_selector?(selector)
@ -131,9 +119,28 @@ module RecurringTodos
private
def save_recurring_todo
@saved = @recurring_todo.save
save_tags if @saved
return @saved
end
def save_tags
@recurring_todo.tag_with(@filterred_attributes[:tag_list]) if @filterred_attributes[:tag_list].present?
@recurring_todo.reload
end
def save_collection(collection, collection_id)
# save object (project or context) and add its id to @mapped_attributes and remove the object from the attributes
object = @mapped_attributes[collection]
object.save
@mapped_attributes[collection_id] = object.id
@mapped_attributes.except(collection)
end
def tag_list_or_empty_string(attributes)
# avoid nil
attributes.get(:tag_list).blank? ? "" : attributes.get(:tag_list).strip
attributes[:tag_list].blank? ? "" : attributes[:tag_list].strip
end
end

View file

@ -46,6 +46,10 @@ module RecurringTodos
@attributes = Tracks::AttributeHandler.new(@user, recurring_todo.attributes)
end
def valid?
@recurring_todo.valid?
end
def validate_not_blank(object, msg)
errors[:base] << msg if object.blank?
end
@ -80,7 +84,7 @@ module RecurringTodos
validate_not_nil(show_always, "Please select when to show the action")
validate_not_blank(show_from_delta, "Please fill in the number of days to show the todo before the due date") unless show_always
else
raise Exception.new, "unexpected value of recurrence target selector '#{target}'"
errors[:base] << "Unexpected value of recurrence target selector '#{target}'"
end
end
@ -89,7 +93,7 @@ module RecurringTodos
end
def get(attribute)
@attributes.get attribute
@attributes[attribute]
end
end

View file

@ -12,15 +12,12 @@ module RecurringTodos
parse_project
parse_context
@builder = create_builder(@attributes.get(:recurring_period))
@builder = create_builder(@attributes[:recurring_period])
end
def create_builder(selector)
if %w{daily weekly monthly yearly}.include?(selector)
return eval("RecurringTodos::#{selector.capitalize}RecurringTodosBuilder.new(@user, @attributes)")
else
raise Exception.new("Unknown recurrence selector in :recurring_period (#{selector})")
end
raise "Unknown recurrence selector in :recurring_period (#{selector})" unless valid_selector? selector
eval("RecurringTodos::#{selector.capitalize}RecurringTodosBuilder.new(@user, @attributes)")
end
def build
@ -50,18 +47,26 @@ module RecurringTodos
@builder.attributes
end
def pattern
@builder.pattern
end
private
def valid_selector?(selector)
%w{daily weekly monthly yearly}.include?(selector)
end
def parse_dates
%w{end_date start_from}.each {|date| @attributes.parse_date date }
end
def parse_project
@project, @new_project_created = @attributes.parse_collection(:project, @user.projects, @attributes.project_name)
@project, @new_project_created = @attributes.parse_collection(:project, @user.projects)
end
def parse_context
@context, @new_context_created = @attributes.parse_collection(:context, @user.contexts, @attributes.context_name)
@context, @new_context_created = @attributes.parse_collection(:context, @user.contexts)
end
end