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-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-10 19:39:39 +01:00
save_recurring_todo
2014-02-03 10:48:21 +01:00
end
2014-01-27 16:42:54 +01:00
def save
build
2014-02-10 19:39:39 +01:00
save_recurring_todo
2014-02-10 11:45:25 +01:00
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-23 15:13:53 +01:00
def errors
@recurring_todo . try ( :errors )
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-10 19:39:39 +01:00
# get pattern independend attributes
2014-02-07 22:55:52 +01:00
filterred_attributes = filter_generic_attributes ( attributes )
2014-02-10 19:39:39 +01:00
# append pattern specific attributes
attributes_to_filter . each { | key | filterred_attributes [ key ] = attributes [ 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 , {
2014-02-10 19:39:39 +01:00
recurring_period : attributes [ :recurring_period ] ,
description : attributes [ :description ] ,
notes : attributes [ :notes ] ,
2014-02-07 22:55:52 +01:00
tag_list : tag_list_or_empty_string ( attributes ) ,
2014-02-10 19:39:39 +01:00
start_from : attributes [ :start_from ] ,
end_date : attributes [ :end_date ] ,
ends_on : attributes [ :ends_on ] ,
target : attributes [ :target ] ,
project : attributes [ :project ] ,
context : attributes [ :context ] ,
project_id : attributes [ :project_id ] ,
context_id : attributes [ :context_id ] ,
target : attributes [ :recurring_target ] ,
show_from_delta : attributes [ :recurring_show_days_before ] ,
show_always : attributes [ :recurring_show_always ]
2014-02-09 21:48:52 +01:00
} )
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
2014-02-10 19:39:39 +01:00
# changes name of key from source_key to key
2014-02-07 22:55:52 +01:00
def map ( mapping , key , source_key )
2014-02-10 19:39:39 +01:00
mapping [ key ] = mapping [ 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 )
2014-02-10 19:39:39 +01:00
selector = @attributes [ key ]
2014-02-09 21:48:52 +01:00
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
2014-02-10 19:39:39 +01:00
def save_recurring_todo
@saved = @recurring_todo . save
save_tags if @saved
return @saved
end
def save_tags
@recurring_todo . tag_with ( @filterred_attributes [ :tag_list ] ) if @filterred_attributes [ :tag_list ] . present?
@recurring_todo . reload
end
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 [ collection ]
object . save
@mapped_attributes [ collection_id ] = object . id
@mapped_attributes . except ( collection )
end
2014-01-27 16:42:54 +01:00
def tag_list_or_empty_string ( attributes )
# avoid nil
2014-02-10 19:39:39 +01:00
attributes [ :tag_list ] . blank? ? " " : attributes [ :tag_list ] . strip
2014-01-27 16:42:54 +01:00
end
end
end