2019-12-18 09:49:57 -06:00
|
|
|
# typed: true
|
2014-01-27 16:42:54 +01:00
|
|
|
module RecurringTodos
|
|
|
|
|
|
|
|
class RecurringTodosBuilder
|
|
|
|
|
|
|
|
attr_reader :builder, :project, :context, :tag_list, :user
|
|
|
|
|
|
|
|
def initialize (user, attributes)
|
|
|
|
@user = user
|
2014-02-09 21:48:52 +01:00
|
|
|
@attributes = Tracks::AttributeHandler.new(@user, attributes)
|
2014-01-27 16:42:54 +01:00
|
|
|
|
|
|
|
parse_dates
|
|
|
|
parse_project
|
|
|
|
parse_context
|
|
|
|
|
2014-02-10 19:39:39 +01:00
|
|
|
@builder = create_builder(@attributes[:recurring_period])
|
2014-01-27 16:42:54 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_builder(selector)
|
2014-02-10 19:39:39 +01:00
|
|
|
raise "Unknown recurrence selector in :recurring_period (#{selector})" unless valid_selector? selector
|
|
|
|
eval("RecurringTodos::#{selector.capitalize}RecurringTodosBuilder.new(@user, @attributes)")
|
2014-01-27 16:42:54 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def build
|
|
|
|
@builder.build
|
|
|
|
end
|
|
|
|
|
2014-02-03 10:48:21 +01:00
|
|
|
def update(recurring_todo)
|
|
|
|
@builder.update(recurring_todo)
|
|
|
|
end
|
|
|
|
|
2014-01-27 16:42:54 +01:00
|
|
|
def save
|
2014-02-10 11:45:25 +01:00
|
|
|
@builder.save_project if @new_project_created
|
|
|
|
@builder.save_context if @new_context_created
|
2014-01-27 16:42:54 +01:00
|
|
|
|
|
|
|
return @builder.save
|
|
|
|
end
|
|
|
|
|
|
|
|
def saved_recurring_todo
|
|
|
|
@builder.saved_recurring_todo
|
|
|
|
end
|
|
|
|
|
|
|
|
def recurring_todo
|
|
|
|
@builder.recurring_todo
|
|
|
|
end
|
|
|
|
|
|
|
|
def attributes
|
|
|
|
@builder.attributes
|
|
|
|
end
|
|
|
|
|
2014-02-10 19:39:39 +01:00
|
|
|
def pattern
|
|
|
|
@builder.pattern
|
|
|
|
end
|
|
|
|
|
2014-02-23 15:13:53 +01:00
|
|
|
def errors
|
|
|
|
@builder.errors
|
|
|
|
end
|
|
|
|
|
2014-01-27 16:42:54 +01:00
|
|
|
private
|
|
|
|
|
2014-02-10 19:39:39 +01:00
|
|
|
def valid_selector?(selector)
|
|
|
|
%w{daily weekly monthly yearly}.include?(selector)
|
|
|
|
end
|
|
|
|
|
2014-01-27 16:42:54 +01:00
|
|
|
def parse_dates
|
2014-02-09 21:48:52 +01:00
|
|
|
%w{end_date start_from}.each {|date| @attributes.parse_date date }
|
2014-01-27 16:42:54 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def parse_project
|
2014-02-10 19:39:39 +01:00
|
|
|
@project, @new_project_created = @attributes.parse_collection(:project, @user.projects)
|
2014-01-27 16:42:54 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def parse_context
|
2014-02-10 19:39:39 +01:00
|
|
|
@context, @new_context_created = @attributes.parse_collection(:context, @user.contexts)
|
2014-01-27 16:42:54 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|