mirror of
https://github.com/TracksApp/tracks.git
synced 2026-02-04 23:11:47 +01:00
move mapping of form attributes to model attributes from pattern to builder to make
pattern independent of form
This commit is contained in:
parent
1f36c27af8
commit
776a046465
22 changed files with 463 additions and 446 deletions
|
|
@ -2,13 +2,69 @@ module RecurringTodos
|
|||
|
||||
class AbstractRecurringTodosBuilder
|
||||
|
||||
def initialize(user, attributes)
|
||||
attr_reader :mapped_attributes, :pattern
|
||||
|
||||
def initialize(user, attributes, pattern_class)
|
||||
@user = user
|
||||
|
||||
@attributes = attributes
|
||||
@filterred_attributes = filter_attributes(@attributes)
|
||||
@selector = get_selector(selector_key)
|
||||
@mapped_attributes = map_attributes(@filterred_attributes)
|
||||
|
||||
@pattern = pattern_class.new(user)
|
||||
@pattern.attributes = @mapped_attributes
|
||||
|
||||
@saved = false
|
||||
end
|
||||
|
||||
# build does not add tags. For tags, the recurring todos needs to be saved
|
||||
def build
|
||||
@recurring_todo = @pattern.build_recurring_todo(@mapped_attributes)
|
||||
|
||||
@recurring_todo.context = @filterred_attributes[:context]
|
||||
@recurring_todo.project = @filterred_attributes[:project]
|
||||
end
|
||||
|
||||
def update(recurring_todo)
|
||||
@recurring_todo = @pattern.update_recurring_todo(recurring_todo, @mapped_attributes)
|
||||
@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?
|
||||
@recurring_todo.reload
|
||||
|
||||
return @saved
|
||||
end
|
||||
|
||||
def save
|
||||
build
|
||||
@saved = @recurring_todo.save
|
||||
@recurring_todo.tag_with(@filterred_attributes[:tag_list]) if @saved && @filterred_attributes[:tag_list].present?
|
||||
return @saved
|
||||
end
|
||||
|
||||
def saved_recurring_todo
|
||||
raise(Exception.new, @recurring_todo.valid? ? "Recurring todo was not saved yet" : "Recurring todos was not saved because of validation errors") if !@saved
|
||||
|
||||
@recurring_todo
|
||||
end
|
||||
|
||||
def attributes
|
||||
@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
|
||||
|
||||
def filter_generic_attributes(attributes)
|
||||
{
|
||||
recurring_period: attributes["recurring_period"],
|
||||
|
|
@ -28,53 +84,36 @@ module RecurringTodos
|
|||
}
|
||||
end
|
||||
|
||||
# build does not add tags. For tags, the recurring todos needs to be saved
|
||||
def build
|
||||
@recurring_todo = @pattern.build_recurring_todo
|
||||
|
||||
@recurring_todo.context = @filterred_attributes[:context]
|
||||
@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
|
||||
@recurring_todo.tag_with(@filterred_attributes[:tag_list]) if @saved && @filterred_attributes[:tag_list].present?
|
||||
return @saved
|
||||
end
|
||||
|
||||
def saved_recurring_todo
|
||||
if !@saved
|
||||
raise Exception.new, @recurring_todo.valid? ? "Recurring todo was not saved yet" : "Recurring todos was not saved because of validation errors"
|
||||
end
|
||||
|
||||
@recurring_todo
|
||||
end
|
||||
|
||||
def attributes
|
||||
@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)}
|
||||
def map_attributes
|
||||
# should be overwritten by subclasses to map attributes to activerecord model attributes
|
||||
@filterred_attributes
|
||||
end
|
||||
|
||||
# helper method to be used in mapped_attributes in subclasses
|
||||
def map(mapping, key, source_key)
|
||||
mapping[key] = mapping[source_key]
|
||||
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?
|
||||
raise Exception.new, "recurrence selector pattern (#{key}) not given" unless @attributes.key?(key)
|
||||
raise Exception.new, "unknown recurrence selector pattern: '#{@attributes[key]}'" unless valid_selector?(@attributes[key])
|
||||
|
||||
selector = @attributes[key]
|
||||
@attributes = @attributes.except(key)
|
||||
return selector
|
||||
end
|
||||
|
||||
def valid_selector?(selector)
|
||||
raise Exception.new, "valid_selector? should be overridden in subclass of AbstractRecurringTodosBuilder"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def tag_list_or_empty_string(attributes)
|
||||
|
|
|
|||
|
|
@ -2,48 +2,21 @@ module RecurringTodos
|
|||
|
||||
class AbstractRepeatPattern
|
||||
|
||||
def initialize(user, attributes)
|
||||
attr_accessor :attributes
|
||||
|
||||
def initialize(user)
|
||||
@user = user
|
||||
@attributes = attributes
|
||||
@filterred_attributes = nil
|
||||
end
|
||||
|
||||
def build_recurring_todo
|
||||
@recurring_todo = @user.recurring_todos.build(mapped_attributes)
|
||||
def build_recurring_todo(attributes)
|
||||
@recurring_todo = @user.recurring_todos.build(attributes)
|
||||
end
|
||||
|
||||
def update_recurring_todo(recurring_todo)
|
||||
recurring_todo.assign_attributes(mapped_attributes)
|
||||
def update_recurring_todo(recurring_todo, attributes)
|
||||
recurring_todo.assign_attributes(attributes)
|
||||
recurring_todo
|
||||
end
|
||||
|
||||
def mapped_attributes
|
||||
# should be overwritten to map attributes to activerecord model
|
||||
@attributes
|
||||
end
|
||||
|
||||
def attributes
|
||||
mapped_attributes
|
||||
end
|
||||
|
||||
def map(mapping, key, source_key)
|
||||
mapping[key] = mapping[source_key]
|
||||
mapping.except(source_key)
|
||||
end
|
||||
|
||||
def get_selector(key)
|
||||
raise Exception.new, "recurrence selector pattern (#{key}) not given" unless @attributes.key?(key)
|
||||
raise Exception.new, "unknown recurrence selector pattern: '#{@attributes[key]}'" unless valid_selector?(@attributes[key])
|
||||
|
||||
selector = @attributes[key]
|
||||
@attributes = @attributes.except(key)
|
||||
return selector
|
||||
end
|
||||
|
||||
def valid_selector?(selector)
|
||||
raise Exception.new, "valid_selector? should be overridden in subclass of AbstractRepeatPattern"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
@ -4,15 +4,39 @@ module RecurringTodos
|
|||
attr_reader :recurring_todo, :pattern
|
||||
|
||||
def initialize(user, attributes)
|
||||
super(user, attributes)
|
||||
|
||||
@pattern = DailyRepeatPattern.new(user, @filterred_attributes)
|
||||
super(user, attributes, DailyRepeatPattern)
|
||||
end
|
||||
|
||||
def attributes_to_filter
|
||||
%w{daily_selector daily_every_x_days}
|
||||
end
|
||||
|
||||
def map_attributes(mapping)
|
||||
mapping[:only_work_days] = only_work_days?(@selector)
|
||||
|
||||
mapping[:every_other1] = mapping['daily_every_x_days']
|
||||
mapping = mapping.except('daily_every_x_days')
|
||||
|
||||
mapping
|
||||
end
|
||||
|
||||
def only_work_days?(daily_selector)
|
||||
case daily_selector
|
||||
when 'daily_every_x_day'
|
||||
return false
|
||||
when 'daily_every_work_day'
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
def selector_key
|
||||
'daily_selector'
|
||||
end
|
||||
|
||||
def valid_selector?(selector)
|
||||
%w{daily_every_x_day daily_every_work_day}.include?(selector)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
@ -2,41 +2,14 @@ module RecurringTodos
|
|||
|
||||
class DailyRepeatPattern < AbstractRepeatPattern
|
||||
|
||||
def initialize(user, attributes)
|
||||
super user, attributes
|
||||
@selector = get_selector('daily_selector')
|
||||
end
|
||||
|
||||
def mapped_attributes
|
||||
mapping = @attributes
|
||||
|
||||
mapping[:only_work_days] = only_work_days?(@selector)
|
||||
|
||||
mapping[:every_other1] = mapping['daily_every_x_days']
|
||||
mapping = mapping.except('daily_every_x_days')
|
||||
|
||||
mapping
|
||||
def initialize(user)
|
||||
super user
|
||||
end
|
||||
|
||||
def every_x_days
|
||||
@recurring_todo.every_other1
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def only_work_days?(daily_selector)
|
||||
case daily_selector
|
||||
when 'daily_every_x_day'
|
||||
return false
|
||||
when 'daily_every_work_day'
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
def valid_selector?(selector)
|
||||
%w{daily_every_x_day daily_every_work_day}.include?(selector)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
@ -3,8 +3,7 @@ module RecurringTodos
|
|||
class MonthlyRecurringTodosBuilder < AbstractRecurringTodosBuilder
|
||||
|
||||
def initialize(user, attributes)
|
||||
super(user, attributes)
|
||||
@pattern = MonthlyRepeatPattern.new(user, @filterred_attributes)
|
||||
super(user, attributes, MonthlyRepeatPattern)
|
||||
end
|
||||
|
||||
def attributes_to_filter
|
||||
|
|
@ -14,6 +13,35 @@ module RecurringTodos
|
|||
}
|
||||
end
|
||||
|
||||
def map_attributes(mapping)
|
||||
mapping = map(mapping, :every_other1, 'monthly_every_x_day')
|
||||
mapping = map(mapping, :every_other3, 'monthly_every_xth_day')
|
||||
mapping = map(mapping, :every_count, 'monthly_day_of_week')
|
||||
|
||||
mapping[:every_other2] = mapping[get_every_other2]
|
||||
mapping = mapping.except('monthly_every_x_month').except('monthly_every_x_month2')
|
||||
|
||||
mapping[:recurrence_selector] = get_recurrence_selector
|
||||
|
||||
mapping
|
||||
end
|
||||
|
||||
def get_recurrence_selector
|
||||
@selector=='monthly_every_x_day' ? 0 : 1
|
||||
end
|
||||
|
||||
def get_every_other2
|
||||
get_recurrence_selector == 0 ? 'monthly_every_x_month' : 'monthly_every_x_month2'
|
||||
end
|
||||
|
||||
def selector_key
|
||||
'monthly_selector'
|
||||
end
|
||||
|
||||
def valid_selector?(selector)
|
||||
%w{monthly_every_x_day monthly_every_xth_day}.include?(selector)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
@ -2,24 +2,8 @@ module RecurringTodos
|
|||
|
||||
class MonthlyRepeatPattern < AbstractRepeatPattern
|
||||
|
||||
def initialize(user, attributes)
|
||||
super user, attributes
|
||||
@selector = get_selector('monthly_selector')
|
||||
end
|
||||
|
||||
def mapped_attributes
|
||||
mapping = @attributes
|
||||
|
||||
mapping = map(mapping, :every_other1, 'monthly_every_x_day')
|
||||
mapping = map(mapping, :every_other3, 'monthly_every_xth_day')
|
||||
mapping = map(mapping, :every_count, 'monthly_day_of_week')
|
||||
|
||||
mapping[:every_other2] = mapping[get_every_other2]
|
||||
mapping = mapping.except('monthly_every_x_month').except('monthly_every_x_month2')
|
||||
|
||||
mapping[:recurrence_selector] = get_recurrence_selector
|
||||
|
||||
mapping
|
||||
def initialize(user)
|
||||
super user
|
||||
end
|
||||
|
||||
def every_x_day?
|
||||
|
|
@ -30,20 +14,6 @@ module RecurringTodos
|
|||
@recurring_todo.recurrence_selector == 1
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def get_recurrence_selector
|
||||
@selector=='monthly_every_x_day' ? 0 : 1
|
||||
end
|
||||
|
||||
def get_every_other2
|
||||
get_recurrence_selector == 0 ? 'monthly_every_x_month' : 'monthly_every_x_month2'
|
||||
end
|
||||
|
||||
def valid_selector?(selector)
|
||||
%w{monthly_every_x_day monthly_every_xth_day}.include?(selector)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
@ -3,14 +3,36 @@ module RecurringTodos
|
|||
class WeeklyRecurringTodosBuilder < AbstractRecurringTodosBuilder
|
||||
|
||||
def initialize(user, attributes)
|
||||
super(user, attributes)
|
||||
@pattern = WeeklyRepeatPattern.new(user, @filterred_attributes)
|
||||
super(user, attributes, WeeklyRepeatPattern)
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
def map_attributes(mapping)
|
||||
mapping = map(mapping, :every_other1, 'weekly_every_x_week')
|
||||
|
||||
{ monday: 1, tuesday: 2, wednesday: 3, thursday: 4, friday: 5, saturday: 6, sunday: 0 }.each{|day, index| mapping = map_day(mapping, :every_day, "weekly_return_#{day}", index)}
|
||||
|
||||
mapping
|
||||
end
|
||||
|
||||
def map_day(mapping, key, source_key, index)
|
||||
mapping[key] ||= ' ' # avoid nil
|
||||
mapping[source_key] ||= ' ' # avoid nil
|
||||
|
||||
mapping[key] = mapping[key][0, index] + mapping[source_key] + mapping[key][index+1, mapping[key].length]
|
||||
mapping
|
||||
end
|
||||
|
||||
def selector_key
|
||||
nil
|
||||
end
|
||||
|
||||
def valid_selector?(key)
|
||||
true
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -2,26 +2,8 @@ module RecurringTodos
|
|||
|
||||
class WeeklyRepeatPattern < AbstractRepeatPattern
|
||||
|
||||
def initialize(user, attributes)
|
||||
super user, attributes
|
||||
end
|
||||
|
||||
def mapped_attributes
|
||||
mapping = @attributes
|
||||
|
||||
mapping = map(mapping, :every_other1, 'weekly_every_x_week')
|
||||
|
||||
{ monday: 1, tuesday: 2, wednesday: 3, thursday: 4, friday: 5, saturday: 6, sunday: 0 }.each{|day, index| mapping = map_day(mapping, :every_day, "weekly_return_#{day}", index)}
|
||||
|
||||
mapping
|
||||
end
|
||||
|
||||
def map_day(mapping, key, source_key, index)
|
||||
mapping[key] ||= ' ' # avoid nil
|
||||
mapping[source_key] ||= ' ' # avoid nil
|
||||
|
||||
mapping[key] = mapping[key][0, index] + mapping[source_key] + mapping[key][index+1, mapping[key].length]
|
||||
mapping
|
||||
def initialize(user)
|
||||
super user
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -3,8 +3,7 @@ module RecurringTodos
|
|||
class YearlyRecurringTodosBuilder < AbstractRecurringTodosBuilder
|
||||
|
||||
def initialize(user, attributes)
|
||||
super(user, attributes)
|
||||
@pattern = YearlyRepeatPattern.new(user, @filterred_attributes)
|
||||
super(user, attributes, YearlyRepeatPattern)
|
||||
end
|
||||
|
||||
def attributes_to_filter
|
||||
|
|
@ -13,6 +12,40 @@ module RecurringTodos
|
|||
}
|
||||
end
|
||||
|
||||
def map_attributes(mapping)
|
||||
mapping[:recurrence_selector] = get_recurrence_selector
|
||||
|
||||
mapping[:every_other2] = mapping[get_every_other2]
|
||||
mapping = mapping.except('yearly_month_of_year').except('yearly_month_of_year2')
|
||||
|
||||
mapping = map(mapping, :every_other1, 'yearly_every_x_day')
|
||||
mapping = map(mapping, :every_other3, 'yearly_every_xth_day')
|
||||
mapping = map(mapping, :every_count, 'yearly_day_of_week')
|
||||
|
||||
mapping
|
||||
end
|
||||
|
||||
def selector_key
|
||||
'yearly_selector'
|
||||
end
|
||||
|
||||
def valid_selector?(selector)
|
||||
%w{yearly_every_x_day yearly_every_xth_day}.include?(selector)
|
||||
end
|
||||
|
||||
def get_recurrence_selector
|
||||
@selector=='yearly_every_x_day' ? 0 : 1
|
||||
end
|
||||
|
||||
def get_every_other2
|
||||
case get_recurrence_selector
|
||||
when 0
|
||||
'yearly_month_of_year'
|
||||
when 1
|
||||
'yearly_month_of_year2'
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
@ -2,43 +2,8 @@ module RecurringTodos
|
|||
|
||||
class YearlyRepeatPattern < AbstractRepeatPattern
|
||||
|
||||
def initialize(user, attributes)
|
||||
super user, attributes
|
||||
@selector = get_selector('yearly_selector')
|
||||
end
|
||||
|
||||
def mapped_attributes
|
||||
mapping = @attributes
|
||||
|
||||
mapping[:recurrence_selector] = get_recurrence_selector
|
||||
|
||||
mapping[:every_other2] = mapping[get_every_other2]
|
||||
mapping = mapping.except('yearly_month_of_year').except('yearly_month_of_year2')
|
||||
|
||||
mapping = map(mapping, :every_other1, 'yearly_every_x_day')
|
||||
mapping = map(mapping, :every_other3, 'yearly_every_xth_day')
|
||||
mapping = map(mapping, :every_count, 'yearly_day_of_week')
|
||||
|
||||
mapping
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def get_recurrence_selector
|
||||
@selector=='yearly_every_x_day' ? 0 : 1
|
||||
end
|
||||
|
||||
def get_every_other2
|
||||
case get_recurrence_selector
|
||||
when 0
|
||||
'yearly_month_of_year'
|
||||
when 1
|
||||
'yearly_month_of_year2'
|
||||
end
|
||||
end
|
||||
|
||||
def valid_selector?(selector)
|
||||
%w{yearly_every_x_day yearly_every_xth_day}.include?(selector)
|
||||
def initialize(user)
|
||||
super user
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue