2014-01-27 16:42:54 +01:00
|
|
|
module RecurringTodos
|
|
|
|
|
|
|
|
|
|
class AbstractRecurringTodosBuilder
|
|
|
|
|
|
2014-02-07 22:55:52 +01:00
|
|
|
attr_reader :mapped_attributes, :pattern
|
|
|
|
|
|
|
|
|
|
def initialize(user, attributes, pattern_class)
|
2014-01-27 16:42:54 +01:00
|
|
|
@user = user
|
2014-02-07 22:55:52 +01:00
|
|
|
|
2014-01-27 16:42:54 +01:00
|
|
|
@attributes = attributes
|
2014-02-03 10:48:21 +01:00
|
|
|
@filterred_attributes = filter_attributes(@attributes)
|
2014-02-07 22:55:52 +01:00
|
|
|
@selector = get_selector(selector_key)
|
|
|
|
|
@mapped_attributes = map_attributes(@filterred_attributes)
|
2014-01-27 16:42:54 +01:00
|
|
|
|
2014-02-07 22:55:52 +01:00
|
|
|
@pattern = pattern_class.new(user)
|
|
|
|
|
@pattern.attributes = @mapped_attributes
|
|
|
|
|
|
|
|
|
|
@saved = false
|
2014-01-27 16:42:54 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# build does not add tags. For tags, the recurring todos needs to be saved
|
|
|
|
|
def build
|
2014-02-07 22:55:52 +01:00
|
|
|
@recurring_todo = @pattern.build_recurring_todo(@mapped_attributes)
|
2014-01-27 16:42:54 +01:00
|
|
|
|
|
|
|
|
@recurring_todo.context = @filterred_attributes[:context]
|
|
|
|
|
@recurring_todo.project = @filterred_attributes[:project]
|
|
|
|
|
end
|
|
|
|
|
|
2014-02-03 10:48:21 +01:00
|
|
|
def update(recurring_todo)
|
2014-02-07 22:55:52 +01:00
|
|
|
@recurring_todo = @pattern.update_recurring_todo(recurring_todo, @mapped_attributes)
|
2014-02-03 10:48:21 +01:00
|
|
|
@recurring_todo.context = @filterred_attributes[:context]
|
|
|
|
|
@recurring_todo.project = @filterred_attributes[:project]
|
|
|
|
|
|
|
|
|
|
@saved = @recurring_todo.save
|
|
|
|
|
@recurring_todo.tag_with(@filterred_attributes[:tag_list]) if @saved && @filterred_attributes[:tag_list].present?
|
2014-02-07 22:55:52 +01:00
|
|
|
@recurring_todo.reload
|
|
|
|
|
|
2014-02-03 10:48:21 +01:00
|
|
|
return @saved
|
|
|
|
|
end
|
|
|
|
|
|
2014-01-27 16:42:54 +01:00
|
|
|
def save
|
|
|
|
|
build
|
|
|
|
|
@saved = @recurring_todo.save
|
|
|
|
|
@recurring_todo.tag_with(@filterred_attributes[:tag_list]) if @saved && @filterred_attributes[:tag_list].present?
|
|
|
|
|
return @saved
|
|
|
|
|
end
|
|
|
|
|
|
2014-02-07 22:55:52 +01:00
|
|
|
def saved_recurring_todo
|
|
|
|
|
raise(Exception.new, @recurring_todo.valid? ? "Recurring todo was not saved yet" : "Recurring todos was not saved because of validation errors") if !@saved
|
2014-01-27 16:42:54 +01:00
|
|
|
|
|
|
|
|
@recurring_todo
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def attributes
|
|
|
|
|
@pattern.attributes
|
|
|
|
|
end
|
|
|
|
|
|
2014-02-03 10:48:21 +01:00
|
|
|
def attributes_to_filter
|
|
|
|
|
raise Exception.new, "attributes_to_filter should be overridden"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def filter_attributes(attributes)
|
2014-02-07 22:55:52 +01:00
|
|
|
filterred_attributes = filter_generic_attributes(attributes)
|
|
|
|
|
attributes_to_filter.each{|key| filterred_attributes[key] = attributes[key] if attributes.key?(key)}
|
|
|
|
|
filterred_attributes
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def filter_generic_attributes(attributes)
|
|
|
|
|
{
|
|
|
|
|
recurring_period: attributes["recurring_period"],
|
|
|
|
|
description: attributes['description'],
|
|
|
|
|
notes: attributes['notes'],
|
|
|
|
|
tag_list: tag_list_or_empty_string(attributes),
|
|
|
|
|
start_from: attributes['start_from'],
|
|
|
|
|
end_date: attributes['end_date'],
|
|
|
|
|
ends_on: attributes['ends_on'],
|
|
|
|
|
show_always: attributes['show_always'],
|
|
|
|
|
target: attributes['target'],
|
|
|
|
|
project: attributes[:project],
|
|
|
|
|
context: attributes[:context],
|
|
|
|
|
target: attributes['recurring_target'],
|
|
|
|
|
show_from_delta: attributes['recurring_show_days_before'],
|
|
|
|
|
show_always: attributes['recurring_show_always']
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def map_attributes
|
|
|
|
|
# should be overwritten by subclasses to map attributes to activerecord model attributes
|
2014-02-03 10:48:21 +01:00
|
|
|
@filterred_attributes
|
|
|
|
|
end
|
|
|
|
|
|
2014-02-07 22:55:52 +01:00
|
|
|
# helper method to be used in mapped_attributes in subclasses
|
|
|
|
|
def map(mapping, key, source_key)
|
|
|
|
|
mapping[key] = mapping[source_key]
|
|
|
|
|
mapping.except(source_key)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# should return period specific selector like yearly_selector or daily_selector
|
|
|
|
|
def selector_key
|
|
|
|
|
raise Exception.new, "selector_key should be overridden in subclass of AbstractRecurringTodosBuilder"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def get_selector(key)
|
|
|
|
|
return nil if key.nil?
|
|
|
|
|
raise Exception.new, "recurrence selector pattern (#{key}) not given" unless @attributes.key?(key)
|
|
|
|
|
raise Exception.new, "unknown recurrence selector pattern: '#{@attributes[key]}'" unless valid_selector?(@attributes[key])
|
|
|
|
|
|
|
|
|
|
selector = @attributes[key]
|
|
|
|
|
@attributes = @attributes.except(key)
|
|
|
|
|
return selector
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def valid_selector?(selector)
|
|
|
|
|
raise Exception.new, "valid_selector? should be overridden in subclass of AbstractRecurringTodosBuilder"
|
|
|
|
|
end
|
|
|
|
|
|
2014-01-27 16:42:54 +01:00
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def tag_list_or_empty_string(attributes)
|
|
|
|
|
# avoid nil
|
|
|
|
|
attributes['tag_list'].blank? ? "" : attributes['tag_list'].strip
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
end
|