mirror of
https://github.com/TracksApp/tracks.git
synced 2026-02-05 07:21:48 +01:00
move form specific accessors out of model
This commit is contained in:
parent
bad91e8d10
commit
59a29c664a
16 changed files with 279 additions and 385 deletions
|
|
@ -78,36 +78,6 @@ class RecurringTodo < ActiveRecord::Base
|
|||
# choosing between both options is done on recurrence_selector where 0 is
|
||||
# for first type and 1 for second type
|
||||
|
||||
# DAILY
|
||||
|
||||
def daily_selector=(selector)
|
||||
case selector
|
||||
when 'daily_every_x_day'
|
||||
only_work_days = false
|
||||
when 'daily_every_work_day'
|
||||
only_work_days = true
|
||||
else
|
||||
raise Exception.new, "unknown daily recurrence pattern: '#{selector}'"
|
||||
end
|
||||
end
|
||||
|
||||
def daily_every_x_days=(x)
|
||||
self.every_other1 = x if recurring_period=='daily'
|
||||
end
|
||||
|
||||
def daily_every_x_days
|
||||
every_other1
|
||||
end
|
||||
|
||||
# WEEKLY
|
||||
|
||||
def weekly_every_x_week=(x)
|
||||
self.every_other1 = x if recurring_period=='weekly'
|
||||
end
|
||||
|
||||
def weekly_every_x_week
|
||||
self.every_other1
|
||||
end
|
||||
|
||||
def switch_week_day(day, position)
|
||||
self.every_day = ' ' if self.every_day.nil?
|
||||
|
|
@ -118,128 +88,6 @@ class RecurringTodo < ActiveRecord::Base
|
|||
define_method("weekly_return_#{day}=") do |selector|
|
||||
switch_week_day(selector, number) if recurring_period=='weekly'
|
||||
end
|
||||
|
||||
define_method("on_#{day}") do
|
||||
on_xday number
|
||||
end
|
||||
end
|
||||
|
||||
def on_xday(n)
|
||||
every_day && every_day[n, 1] != ' '
|
||||
end
|
||||
|
||||
# MONTHLY
|
||||
|
||||
def monthly_selector=(selector)
|
||||
self.recurrence_selector = ( (selector=='monthly_every_x_day') ? 0 : 1) if recurring_period=='monthly'
|
||||
end
|
||||
|
||||
def monthly_every_x_day=(x)
|
||||
self.every_other1 = x if recurring_period=='monthly'
|
||||
end
|
||||
|
||||
def monthly_every_x_day
|
||||
self.every_other1
|
||||
end
|
||||
|
||||
def is_monthly_every_x_day
|
||||
recurring_period == 'monthly' && self.recurrence_selector == 0
|
||||
end
|
||||
|
||||
def is_monthly_every_xth_day
|
||||
recurring_period == 'monthly' && self.recurrence_selector == 1
|
||||
end
|
||||
|
||||
def monthly_every_x_month=(x)
|
||||
self.every_other2 = x if recurring_period=='monthly' && recurrence_selector == 0
|
||||
end
|
||||
|
||||
def monthly_every_x_month
|
||||
# in case monthly pattern is every day x, return every_other2 otherwise
|
||||
# return a default value
|
||||
self.recurrence_selector == 0 ? self.every_other2 : 1
|
||||
end
|
||||
|
||||
def monthly_every_x_month2=(x)
|
||||
self.every_other2 = x if recurring_period=='monthly' && recurrence_selector == 1
|
||||
end
|
||||
|
||||
def monthly_every_x_month2
|
||||
# in case monthly pattern is every xth day, return every_other2 otherwise
|
||||
# return a default value
|
||||
self.recurrence_selector == 1 ? self.every_other2 : 1
|
||||
end
|
||||
|
||||
def monthly_every_xth_day=(x)
|
||||
self.every_other3 = x if recurring_period=='monthly'
|
||||
end
|
||||
|
||||
def monthly_every_xth_day(default=nil)
|
||||
self.every_other3 || default
|
||||
end
|
||||
|
||||
def monthly_day_of_week=(dow)
|
||||
self.every_count = dow if recurring_period=='monthly'
|
||||
end
|
||||
|
||||
def monthly_day_of_week
|
||||
self.every_count
|
||||
end
|
||||
|
||||
# YEARLY
|
||||
|
||||
def yearly_selector=(selector)
|
||||
self.recurrence_selector = ( (selector=='yearly_every_x_day') ? 0 : 1) if recurring_period=='yearly'
|
||||
end
|
||||
|
||||
def yearly_month_of_year=(moy)
|
||||
self.every_other2 = moy if self.recurring_period=='yearly' && self.recurrence_selector == 0
|
||||
end
|
||||
|
||||
def yearly_month_of_year
|
||||
# if recurrence pattern is every x day in a month, return month otherwise
|
||||
# return a default value
|
||||
self.recurrence_selector == 0 ? self.every_other2 : Time.zone.now.month
|
||||
end
|
||||
|
||||
def yearly_month_of_year2=(moy)
|
||||
self.every_other2 = moy if self.recurring_period=='yearly' && self.recurrence_selector == 1
|
||||
end
|
||||
|
||||
def yearly_month_of_year2
|
||||
# if recurrence pattern is every xth day in a month, return month otherwise
|
||||
# return a default value
|
||||
self.recurrence_selector == 1 ? self.every_other2 : Time.zone.now.month
|
||||
end
|
||||
|
||||
def yearly_every_x_day=(x)
|
||||
self.every_other1 = x if recurring_period=='yearly'
|
||||
end
|
||||
|
||||
def yearly_every_x_day
|
||||
self.every_other1
|
||||
end
|
||||
|
||||
def yearly_every_xth_day=(x)
|
||||
self.every_other3 = x if recurring_period=='yearly'
|
||||
end
|
||||
|
||||
def yearly_every_xth_day
|
||||
self.every_other3
|
||||
end
|
||||
|
||||
def yearly_day_of_week=(dow)
|
||||
self.every_count=dow if recurring_period=='yearly'
|
||||
end
|
||||
|
||||
def yearly_day_of_week
|
||||
self.every_count
|
||||
end
|
||||
|
||||
# target
|
||||
|
||||
def recurring_target=(t)
|
||||
self.target = t
|
||||
end
|
||||
|
||||
def recurring_target_as_text
|
||||
|
|
|
|||
|
|
@ -5,34 +5,34 @@ module RecurringTodos
|
|||
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
|
||||
|
||||
@user = user
|
||||
@saved = false
|
||||
|
||||
@attributes = attributes
|
||||
@selector = get_selector(selector_key)
|
||||
@filterred_attributes = filter_attributes(@attributes)
|
||||
@mapped_attributes = map_attributes(@filterred_attributes)
|
||||
|
||||
@pattern = pattern_class.new(user)
|
||||
@pattern.attributes = @mapped_attributes
|
||||
|
||||
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]
|
||||
@recurring_todo.context = @filterred_attributes.get(:context)
|
||||
@recurring_todo.project = @filterred_attributes.get(: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]
|
||||
@recurring_todo.context = @filterred_attributes.get(:context)
|
||||
@recurring_todo.project = @filterred_attributes.get(:project)
|
||||
|
||||
@saved = @recurring_todo.save
|
||||
@recurring_todo.tag_with(@filterred_attributes[:tag_list]) if @saved && @filterred_attributes[:tag_list].present?
|
||||
@recurring_todo.tag_with(@filterred_attributes.get(:tag_list)) if @saved && @filterred_attributes.get(:tag_list).present?
|
||||
@recurring_todo.reload
|
||||
|
||||
return @saved
|
||||
|
|
@ -41,12 +41,12 @@ module RecurringTodos
|
|||
def save
|
||||
build
|
||||
@saved = @recurring_todo.save
|
||||
@recurring_todo.tag_with(@filterred_attributes[:tag_list]) if @saved && @filterred_attributes[:tag_list].present?
|
||||
@recurring_todo.tag_with(@filterred_attributes.get(:tag_list)) if @saved && @filterred_attributes.get(: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
|
||||
raise(Exception.new, @recurring_todo.valid? ? "Recurring todo was not saved yet" : "Recurring todos was not saved because of validation errors") unless @saved
|
||||
|
||||
@recurring_todo
|
||||
end
|
||||
|
|
@ -61,27 +61,29 @@ module RecurringTodos
|
|||
|
||||
def filter_attributes(attributes)
|
||||
filterred_attributes = filter_generic_attributes(attributes)
|
||||
attributes_to_filter.each{|key| filterred_attributes[key] = attributes[key] if attributes.key?(key)}
|
||||
attributes_to_filter.each{|key| filterred_attributes.set(key, attributes.get(key)) if attributes.key?(key)}
|
||||
filterred_attributes
|
||||
end
|
||||
|
||||
def filter_generic_attributes(attributes)
|
||||
{
|
||||
recurring_period: attributes["recurring_period"],
|
||||
description: attributes['description'],
|
||||
notes: attributes['notes'],
|
||||
return Tracks::AttributeHandler.new(@user, {
|
||||
recurring_period: attributes.get(:recurring_period),
|
||||
description: attributes.get(:description),
|
||||
notes: attributes.get(:notes),
|
||||
tag_list: tag_list_or_empty_string(attributes),
|
||||
start_from: attributes['start_from'],
|
||||
end_date: attributes['end_date'],
|
||||
ends_on: attributes['ends_on'],
|
||||
show_always: attributes['show_always'],
|
||||
target: attributes['target'],
|
||||
project: attributes[:project],
|
||||
context: attributes[:context],
|
||||
target: attributes['recurring_target'],
|
||||
show_from_delta: attributes['recurring_show_days_before'],
|
||||
show_always: attributes['recurring_show_always']
|
||||
}
|
||||
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)
|
||||
})
|
||||
end
|
||||
|
||||
def map_attributes
|
||||
|
|
@ -91,7 +93,7 @@ module RecurringTodos
|
|||
|
||||
# helper method to be used in mapped_attributes in subclasses
|
||||
def map(mapping, key, source_key)
|
||||
mapping[key] = mapping[source_key]
|
||||
mapping.set(key, mapping.get(source_key))
|
||||
mapping.except(source_key)
|
||||
end
|
||||
|
||||
|
|
@ -102,10 +104,12 @@ module RecurringTodos
|
|||
|
||||
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]
|
||||
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)
|
||||
|
||||
@attributes = @attributes.except(key)
|
||||
return selector
|
||||
end
|
||||
|
|
@ -118,7 +122,7 @@ module RecurringTodos
|
|||
|
||||
def tag_list_or_empty_string(attributes)
|
||||
# avoid nil
|
||||
attributes['tag_list'].blank? ? "" : attributes['tag_list'].strip
|
||||
attributes.get(:tag_list).blank? ? "" : attributes.get(:tag_list).strip
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -32,18 +32,18 @@ module RecurringTodos
|
|||
get :show_from_delta
|
||||
end
|
||||
|
||||
def build_recurring_todo(attributes)
|
||||
@recurring_todo = @user.recurring_todos.build(attributes)
|
||||
def build_recurring_todo(attribute_handler)
|
||||
@recurring_todo = @user.recurring_todos.build(attribute_handler.attributes)
|
||||
end
|
||||
|
||||
def update_recurring_todo(recurring_todo, attributes)
|
||||
recurring_todo.assign_attributes(attributes)
|
||||
def update_recurring_todo(recurring_todo, attribute_handler)
|
||||
recurring_todo.assign_attributes(attribute_handler.attributes)
|
||||
recurring_todo
|
||||
end
|
||||
|
||||
def build_from_recurring_todo(recurring_todo)
|
||||
@recurring_todo = recurring_todo
|
||||
@attributes = recurring_todo.attributes
|
||||
@attributes = Tracks::AttributeHandler.new(@user, recurring_todo.attributes)
|
||||
end
|
||||
|
||||
def validate_not_blank(object, msg)
|
||||
|
|
@ -88,9 +88,8 @@ module RecurringTodos
|
|||
@recurring_todo.errors
|
||||
end
|
||||
|
||||
def get attribute
|
||||
# handle attribute as symbol and as string
|
||||
@attributes[attribute] || @attributes[attribute.to_s]
|
||||
def get(attribute)
|
||||
@attributes.get attribute
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -12,25 +12,18 @@ module RecurringTodos
|
|||
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
|
||||
mapping.set(:only_work_days, only_work_days?(@selector))
|
||||
mapping.set(:every_other1, mapping.get(:daily_every_x_days))
|
||||
mapping.except(:daily_every_x_days)
|
||||
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
|
||||
{ 'daily_every_x_day' => false,
|
||||
'daily_every_work_day' => true}[daily_selector]
|
||||
end
|
||||
|
||||
def selector_key
|
||||
'daily_selector'
|
||||
:daily_selector
|
||||
end
|
||||
|
||||
def valid_selector?(selector)
|
||||
|
|
|
|||
|
|
@ -18,12 +18,10 @@ module RecurringTodos
|
|||
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.set(:recurrence_selector, get_recurrence_selector)
|
||||
|
||||
mapping[:recurrence_selector] = get_recurrence_selector
|
||||
|
||||
mapping
|
||||
mapping.set(:every_other2, mapping.get(get_every_other2))
|
||||
mapping.except('monthly_every_x_month').except('monthly_every_x_month2')
|
||||
end
|
||||
|
||||
def get_recurrence_selector
|
||||
|
|
@ -35,7 +33,7 @@ module RecurringTodos
|
|||
end
|
||||
|
||||
def selector_key
|
||||
'monthly_selector'
|
||||
:monthly_selector
|
||||
end
|
||||
|
||||
def valid_selector?(selector)
|
||||
|
|
|
|||
|
|
@ -6,20 +6,20 @@ module RecurringTodos
|
|||
|
||||
def initialize (user, attributes)
|
||||
@user = user
|
||||
@attributes = attributes
|
||||
@attributes = Tracks::AttributeHandler.new(@user, attributes)
|
||||
|
||||
parse_dates
|
||||
parse_project
|
||||
parse_context
|
||||
|
||||
@builder = create_builder(attributes['recurring_period'])
|
||||
@builder = create_builder(@attributes.get(: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
|
||||
raise Exception.new("Unknown recurrence selector in recurring_period (#{selector})")
|
||||
raise Exception.new("Unknown recurrence selector in :recurring_period (#{selector})")
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -53,72 +53,15 @@ module RecurringTodos
|
|||
private
|
||||
|
||||
def parse_dates
|
||||
%w{end_date start_from}.each {|date| @attributes[date] = @user.prefs.parse_date(@attributes[date])}
|
||||
%w{end_date start_from}.each {|date| @attributes.parse_date date }
|
||||
end
|
||||
|
||||
def parse_project
|
||||
@project, @new_project_created = parse(:project, @user.projects, project_name)
|
||||
@project, @new_project_created = @attributes.parse_collection(:project, @user.projects, @attributes.project_name)
|
||||
end
|
||||
|
||||
def parse_context
|
||||
@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)
|
||||
else
|
||||
# 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
|
||||
end
|
||||
|
||||
return object, new_object_created
|
||||
end
|
||||
|
||||
def specified_by_name?(object_type)
|
||||
self.send("#{object_type}_specified_by_name?")
|
||||
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
|
||||
@attributes['project_name'].try(:strip)
|
||||
end
|
||||
|
||||
def context_name
|
||||
@attributes['context_name'].try(:strip)
|
||||
@context, @new_context_created = @attributes.parse_collection(:context, @user.contexts, @attributes.context_name)
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -19,10 +19,10 @@ module RecurringTodos
|
|||
end
|
||||
|
||||
def map_day(mapping, key, source_key, index)
|
||||
mapping[key] ||= ' ' # avoid nil
|
||||
mapping[source_key] ||= ' ' # avoid nil
|
||||
mapping.set_if_nil(key, ' ') # avoid nil
|
||||
mapping.set_if_nil(source_key, ' ') # avoid nil
|
||||
|
||||
mapping[key] = mapping[key][0, index] + mapping[source_key] + mapping[key][index+1, mapping[key].length]
|
||||
mapping.set(key, mapping.get(key)[0, index] + mapping.get(source_key) + mapping.get(key)[index+1, mapping.get(key).length])
|
||||
mapping
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -13,24 +13,22 @@ 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.set(:recurrence_selector, get_recurrence_selector)
|
||||
mapping.set(:every_other2, mapping.get(get_every_other2))
|
||||
|
||||
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
|
||||
mapping.except(:yearly_month_of_year).except(:yearly_month_of_year2)
|
||||
end
|
||||
|
||||
def selector_key
|
||||
'yearly_selector'
|
||||
:yearly_selector
|
||||
end
|
||||
|
||||
def valid_selector?(selector)
|
||||
%w{yearly_every_x_day yearly_every_xth_day}.include?(selector)
|
||||
%w{yearly_every_x_day yearly_every_xth_day}.include?(selector.to_s)
|
||||
end
|
||||
|
||||
def get_recurrence_selector
|
||||
|
|
@ -38,12 +36,7 @@ module RecurringTodos
|
|||
end
|
||||
|
||||
def get_every_other2
|
||||
case get_recurrence_selector
|
||||
when 0
|
||||
'yearly_month_of_year'
|
||||
when 1
|
||||
'yearly_month_of_year2'
|
||||
end
|
||||
{ 0 => :yearly_month_of_year, 1 => :yearly_month_of_year2 }[get_recurrence_selector]
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue