use new model to handle updating of recurring todos

This commit is contained in:
Reinier Balt 2014-02-03 10:48:21 +01:00
parent 00af159be7
commit c2c67f1640
12 changed files with 166 additions and 104 deletions

View file

@ -5,16 +5,11 @@ module RecurringTodos
def initialize(user, attributes)
@user = user
@attributes = attributes
@filterred_attributes = filter_attributes(attributes)
@filterred_attributes = filter_attributes(@attributes)
@saved = false
end
def filter_attributes(attributes)
raise Exception.new, "filter_attributes should be overridden"
end
def filter_generic_attributes(attributes)
attributes['tag_list'] =
{
recurring_period: attributes["recurring_period"],
description: attributes['description'],
@ -41,6 +36,16 @@ module RecurringTodos
@recurring_todo.project = @filterred_attributes[:project]
end
def update(recurring_todo)
@recurring_todo = @pattern.update_recurring_todo(recurring_todo)
@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?
return @saved
end
def save
build
@saved = @recurring_todo.save
@ -60,6 +65,16 @@ module RecurringTodos
@pattern.attributes
end
def attributes_to_filter
raise Exception.new, "attributes_to_filter should be overridden"
end
def filter_attributes(attributes)
@filterred_attributes = filter_generic_attributes(attributes)
attributes_to_filter.each{|key| @filterred_attributes[key] = attributes[key] if attributes.key?(key)}
@filterred_attributes
end
private
def tag_list_or_empty_string(attributes)

View file

@ -3,15 +3,22 @@ module RecurringTodos
class AbstractRepeatPattern
def initialize(user, attributes)
@attributes = attributes
@user = user
@attributes = attributes
@filterred_attributes = nil
end
def build_recurring_todo
@recurring_todo = @user.recurring_todos.build(mapped_attributes)
end
def update_recurring_todo(recurring_todo)
recurring_todo.assign_attributes(mapped_attributes)
recurring_todo
end
def mapped_attributes
# should be overwritten to map attributes to activerecord model
@attributes
end

View file

@ -5,13 +5,12 @@ module RecurringTodos
def initialize(user, attributes)
super(user, attributes)
@pattern = DailyRepeatPattern.new(user, @filterred_attributes)
end
def filter_attributes(attributes)
@filterred_attributes = filter_generic_attributes(attributes)
%w{daily_selector daily_every_x_days}.each{|key| @filterred_attributes[key] = attributes[key] if attributes.key?(key)}
@filterred_attributes
def attributes_to_filter
%w{daily_selector daily_every_x_days}
end
end

View file

@ -7,17 +7,11 @@ module RecurringTodos
@pattern = MonthlyRepeatPattern.new(user, @filterred_attributes)
end
def filter_attributes(attributes)
@filterred_attributes = filter_generic_attributes(attributes)
def attributes_to_filter
%w{
monthly_selector monthly_every_x_day monthly_every_x_month
monthly_every_x_month2 monthly_every_xth_day monthly_day_of_week
}.each do |key|
@filterred_attributes[key] = attributes[key] if attributes.key?(key)
end
@filterred_attributes
}
end
end

View file

@ -19,7 +19,7 @@ module RecurringTodos
if %w{daily weekly monthly yearly}.include?(selector)
return eval("RecurringTodos::#{selector.capitalize}RecurringTodosBuilder.new(@user, @attributes)")
else
raise Exception.new("Unknown recurrence selector (#{selector})")
raise Exception.new("Unknown recurrence selector in recurring_period (#{selector})")
end
end
@ -27,6 +27,10 @@ module RecurringTodos
@builder.build
end
def update(recurring_todo)
@builder.update(recurring_todo)
end
def save
@project.save if @new_project_created
@context.save if @new_context_created

View file

@ -7,16 +7,11 @@ module RecurringTodos
@pattern = WeeklyRepeatPattern.new(user, @filterred_attributes)
end
def filter_attributes(attributes)
@filterred_attributes = filter_generic_attributes(attributes)
weekly_attributes = %w{weekly_selector weekly_every_x_week}
%w{monday tuesday wednesday thursday friday saturday sunday}.each{|day| weekly_attributes << "weekly_return_#{day}"}
weekly_attributes.each{|key| @filterred_attributes[key] = attributes[key] if attributes.key?(key)}
@filterred_attributes
def attributes_to_filter
%w{weekly_selector weekly_every_x_week} + %w{monday tuesday wednesday thursday friday saturday sunday}.map{|day| "weekly_return_#{day}" }
end
end
end

View file

@ -7,16 +7,10 @@ module RecurringTodos
@pattern = YearlyRepeatPattern.new(user, @filterred_attributes)
end
def filter_attributes(attributes)
@filterred_attributes = filter_generic_attributes(attributes)
%w{ yearly_selector yearly_month_of_year yearly_month_of_year2
yearly_every_x_day yearly_every_xth_day yearly_day_of_week
}.each do |key|
@filterred_attributes[key] = attributes[key] if attributes.key?(key)
end
@filterred_attributes
def attributes_to_filter
%w{ yearly_selector yearly_month_of_year yearly_month_of_year2
yearly_every_x_day yearly_every_xth_day yearly_day_of_week
}
end
end