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
|
|
|
|
|
@attributes = attributes
|
|
|
|
|
|
|
|
|
|
parse_dates
|
|
|
|
|
parse_project
|
|
|
|
|
parse_context
|
|
|
|
|
|
|
|
|
|
@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
|
2014-02-03 10:48:21 +01:00
|
|
|
raise Exception.new("Unknown recurrence selector in recurring_period (#{selector})")
|
2014-01-27 16:42:54 +01:00
|
|
|
end
|
|
|
|
|
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
|
|
|
|
|
@project.save if @new_project_created
|
|
|
|
|
@context.save if @new_context_created
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def parse_dates
|
|
|
|
|
%w{end_date start_from}.each {|date| @attributes[date] = @user.prefs.parse_date(@attributes[date])}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def parse_project
|
2014-01-27 20:28:57 +01:00
|
|
|
@project, @new_project_created = parse(:project, @user.projects, project_name)
|
2014-01-27 16:42:54 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def parse_context
|
2014-01-27 20:28:57 +01:00
|
|
|
@context, @new_context_created = parse(:context, @user.contexts, context_name)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def parse(object_type, relation, name)
|
|
|
|
|
object = nil
|
|
|
|
|
new_object_created = false
|
|
|
|
|
|
|
|
|
|
if specified_by_name?(object_type)
|
|
|
|
|
# find or create context or project by given name
|
|
|
|
|
object, new_object_created = find_or_create_by_name(relation, name)
|
2014-01-27 16:42:54 +01:00
|
|
|
else
|
2014-01-27 20:28:57 +01:00
|
|
|
# find context or project by its id
|
|
|
|
|
object = attribute_with_id_of(object_type).present? ? relation.find(attribute_with_id_of(object_type)) : nil
|
|
|
|
|
end
|
|
|
|
|
@attributes[object_type] = object
|
|
|
|
|
return object, new_object_created
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def attribute_with_id_of(object_type)
|
|
|
|
|
map = { project: 'project_id', context: 'context_id' }
|
|
|
|
|
@attributes[map[object_type]]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def find_or_create_by_name(relation, name)
|
|
|
|
|
new_object_created = false
|
|
|
|
|
|
|
|
|
|
object = relation.where(:name => name).first
|
|
|
|
|
unless object
|
|
|
|
|
object = relation.build(:name => name)
|
|
|
|
|
new_object_created = true
|
2014-01-27 16:42:54 +01:00
|
|
|
end
|
2014-01-27 20:28:57 +01:00
|
|
|
|
|
|
|
|
return object, new_object_created
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def specified_by_name?(object_type)
|
|
|
|
|
self.send("#{object_type}_specified_by_name?")
|
2014-01-27 16:42:54 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def project_specified_by_name?
|
|
|
|
|
return false if @attributes['project_id'].present?
|
|
|
|
|
return false if project_name.blank?
|
|
|
|
|
return false if project_name == 'None'
|
|
|
|
|
true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def context_specified_by_name?
|
|
|
|
|
return false if @attributes['context_id'].present?
|
|
|
|
|
return false if context_name.blank?
|
|
|
|
|
true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def project_name
|
2014-01-27 20:28:57 +01:00
|
|
|
@attributes['project_name'].try(:strip)
|
2014-01-27 16:42:54 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def context_name
|
2014-01-27 20:28:57 +01:00
|
|
|
@attributes['context_name'].try(:strip)
|
2014-01-27 16:42:54 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
end
|