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-02-09 21:48:52 +01:00
@user = user
@saved = false
2014-02-07 22:55:52 +01:00
2014-02-09 21:48:52 +01:00
@attributes = attributes
@selector = get_selector ( selector_key )
2014-02-03 10:48:21 +01:00
@filterred_attributes = filter_attributes ( @attributes )
2014-02-09 21:48:52 +01:00
@mapped_attributes = map_attributes ( @filterred_attributes )
2014-01-27 16:42:54 +01:00
2014-02-09 21:48:52 +01:00
@pattern = pattern_class . new ( user )
@pattern . attributes = @mapped_attributes
2014-02-07 22:55:52 +01:00
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
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
@saved = @recurring_todo . save
2014-02-09 21:48:52 +01:00
@recurring_todo . tag_with ( @filterred_attributes . get ( :tag_list ) ) if @saved && @filterred_attributes . get ( :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
2014-02-09 21:48:52 +01:00
@recurring_todo . tag_with ( @filterred_attributes . get ( :tag_list ) ) if @saved && @filterred_attributes . get ( :tag_list ) . present?
2014-01-27 16:42:54 +01:00
return @saved
end
2014-02-10 11:45:25 +01:00
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 )
end
def save_project
save_collection ( :project , :project_id )
end
def save_context
save_collection ( :context , :context_id )
end
2014-02-07 22:55:52 +01:00
def saved_recurring_todo
2014-02-09 21:48:52 +01:00
raise ( Exception . new , @recurring_todo . valid? ? " Recurring todo was not saved yet " : " Recurring todos was not saved because of validation errors " ) unless @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 )
2014-02-09 21:48:52 +01:00
attributes_to_filter . each { | key | filterred_attributes . set ( key , attributes . get ( key ) ) if attributes . key? ( key ) }
2014-02-07 22:55:52 +01:00
filterred_attributes
end
def filter_generic_attributes ( attributes )
2014-02-09 21:48:52 +01:00
return Tracks :: AttributeHandler . new ( @user , {
recurring_period : attributes . get ( :recurring_period ) ,
description : attributes . get ( :description ) ,
notes : attributes . get ( :notes ) ,
2014-02-07 22:55:52 +01:00
tag_list : tag_list_or_empty_string ( attributes ) ,
2014-02-09 21:48:52 +01:00
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 )
} )
2014-02-07 22:55:52 +01:00
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 )
2014-02-09 21:48:52 +01:00
mapping . set ( key , mapping . get ( source_key ) )
2014-02-07 22:55:52 +01:00
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?
2014-02-09 21:48:52 +01:00
raise Exception . new , " recurrence selector pattern ( #{ key } ) not given " unless @attributes . selector_key_present? ( key )
selector = @attributes . get ( key )
raise Exception . new , " unknown recurrence selector pattern: ' #{ selector } ' " unless valid_selector? ( selector )
2014-02-07 22:55:52 +01:00
@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
2014-02-09 21:48:52 +01:00
attributes . get ( :tag_list ) . blank? ? " " : attributes . get ( :tag_list ) . strip
2014-01-27 16:42:54 +01:00
end
end
end