diff --git a/app/models/recurring_todo.rb b/app/models/recurring_todo.rb index 25eb512a..9fa34198 100644 --- a/app/models/recurring_todo.rb +++ b/app/models/recurring_todo.rb @@ -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 diff --git a/app/models/recurring_todos/abstract_recurring_todos_builder.rb b/app/models/recurring_todos/abstract_recurring_todos_builder.rb index 740926e9..e2d610e7 100644 --- a/app/models/recurring_todos/abstract_recurring_todos_builder.rb +++ b/app/models/recurring_todos/abstract_recurring_todos_builder.rb @@ -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 diff --git a/app/models/recurring_todos/abstract_repeat_pattern.rb b/app/models/recurring_todos/abstract_repeat_pattern.rb index c063ea45..cf2f6a76 100644 --- a/app/models/recurring_todos/abstract_repeat_pattern.rb +++ b/app/models/recurring_todos/abstract_repeat_pattern.rb @@ -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 diff --git a/app/models/recurring_todos/daily_recurring_todos_builder.rb b/app/models/recurring_todos/daily_recurring_todos_builder.rb index 6ce83809..fb8a3289 100644 --- a/app/models/recurring_todos/daily_recurring_todos_builder.rb +++ b/app/models/recurring_todos/daily_recurring_todos_builder.rb @@ -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) diff --git a/app/models/recurring_todos/monthly_recurring_todos_builder.rb b/app/models/recurring_todos/monthly_recurring_todos_builder.rb index 39aaefb5..a70cd257 100644 --- a/app/models/recurring_todos/monthly_recurring_todos_builder.rb +++ b/app/models/recurring_todos/monthly_recurring_todos_builder.rb @@ -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) diff --git a/app/models/recurring_todos/recurring_todos_builder.rb b/app/models/recurring_todos/recurring_todos_builder.rb index 4d808fd9..569baa3d 100644 --- a/app/models/recurring_todos/recurring_todos_builder.rb +++ b/app/models/recurring_todos/recurring_todos_builder.rb @@ -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 diff --git a/app/models/recurring_todos/weekly_recurring_todos_builder.rb b/app/models/recurring_todos/weekly_recurring_todos_builder.rb index 46a67055..17ec13ba 100644 --- a/app/models/recurring_todos/weekly_recurring_todos_builder.rb +++ b/app/models/recurring_todos/weekly_recurring_todos_builder.rb @@ -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 diff --git a/app/models/recurring_todos/yearly_recurring_todos_builder.rb b/app/models/recurring_todos/yearly_recurring_todos_builder.rb index d34c49f3..091509d6 100644 --- a/app/models/recurring_todos/yearly_recurring_todos_builder.rb +++ b/app/models/recurring_todos/yearly_recurring_todos_builder.rb @@ -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 diff --git a/lib/tracks/attribute_handler.rb b/lib/tracks/attribute_handler.rb new file mode 100644 index 00000000..c9901c93 --- /dev/null +++ b/lib/tracks/attribute_handler.rb @@ -0,0 +1,104 @@ +module Tracks + + class AttributeHandler + attr_reader :attributes + + def initialize(user, attributes) + @user = user + @orig_attributes = attributes + @attributes = normalize(attributes) + end + + def get(attribute) + @attributes[attribute.to_sym] + end + + def set(key, value) + @attributes[key.to_sym] = value + end + + def set_if_nil(key, value) + @attributes[key.to_sym] ||= value + end + + def except(key) + AttributeHandler.new(@user, @attributes.except(key.to_sym)) + end + + def key?(key) + @attributes.key?(key.to_sym) + end + + def selector_key_present?(key) + @attributes.key?(key.to_sym) + end + + def parse_date(date) + set(date, @user.prefs.parse_date(get(date))) + end + + def parse_collection(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' } + get 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 get(: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 get(:context_id).present? + return false if context_name.blank? + true + end + + def project_name + get(:project_name).try(:strip) + end + + def context_name + get(:context_name).try(:strip) + end + + def normalize(attributes) + # make sure the hash keys are all symbols + Hash[attributes.map{|k,v| [k.to_sym,v]}] + end + + end + +end \ No newline at end of file diff --git a/test/models/recurring_todo_test.rb b/test/models/recurring_todo_test.rb index 18ee005d..9279d5f1 100644 --- a/test/models/recurring_todo_test.rb +++ b/test/models/recurring_todo_test.rb @@ -352,9 +352,9 @@ class RecurringTodoTest < ActiveSupport::TestCase end def test_set_every_n_days_from_form_input - todo = RecurringTodo.new({ + builder = RecurringTodos::RecurringTodosBuilder.new(users(:admin_user), { :description => "Task every 2 days", - :context => Context.first, + :context_id => Context.first.id, :recurring_target => "show_from_date", :start_from => "01/01/01", :ends_on => "no_end_date", @@ -362,14 +362,17 @@ class RecurringTodoTest < ActiveSupport::TestCase :daily_selector => "daily_every_x_day", :daily_every_x_days => 2, }) + builder.save + todo = builder.saved_recurring_todo + assert todo.valid?, todo.errors.full_messages assert_equal 2, todo.every_other1 end def test_set_every_n_weeks_from_form_input - todo = RecurringTodo.new({ + builder = RecurringTodos::RecurringTodosBuilder.new(users(:admin_user), { :description => "Task every 3 weeks", - :context => Context.first, + :context_id => Context.first.id, :recurring_target => "show_from_date", :start_from => "01/01/01", :ends_on => "no_end_date", @@ -377,15 +380,18 @@ class RecurringTodoTest < ActiveSupport::TestCase :weekly_every_x_week => 3, :weekly_return_monday => "m", }) + builder.save + todo = builder.saved_recurring_todo + assert todo.valid?, todo.errors.full_messages assert_equal 3, todo.every_other1 - assert todo.on_monday + assert todo.pattern.on_monday end def test_set_every_n_months_from_form_input - todo = RecurringTodo.new({ + builder = RecurringTodos::RecurringTodosBuilder.new(users(:admin_user), { :description => "Task every 4 months", - :context => Context.first, + :context_id => Context.first.id, :recurring_target => "show_from_date", :start_from => "01/01/01", :ends_on => "no_end_date", @@ -394,14 +400,17 @@ class RecurringTodoTest < ActiveSupport::TestCase :monthly_every_x_day => 1, :monthly_every_x_month => 4, }) + builder.save + todo = builder.saved_recurring_todo + assert todo.valid?, todo.errors.full_messages assert_equal 4, todo.every_other2 end def test_set_yearly_from_form_input - todo = RecurringTodo.new({ + builder = RecurringTodos::RecurringTodosBuilder.new(users(:admin_user), { :description => "Task every year in May", - :context => Context.first, + :context_id => Context.first.id, :recurring_target => "show_from_date", :start_from => "01/01/01", :ends_on => "no_end_date", @@ -410,6 +419,9 @@ class RecurringTodoTest < ActiveSupport::TestCase :yearly_every_x_day => 15, :yearly_month_of_year => 5, }) + builder.save + todo = builder.saved_recurring_todo + assert todo.valid?, todo.errors.full_messages assert_equal 5, todo.every_other2 end diff --git a/test/models/recurring_todos/abstract_recurring_todos_builder_test.rb b/test/models/recurring_todos/abstract_recurring_todos_builder_test.rb index 86efb238..c2ac5575 100644 --- a/test/models/recurring_todos/abstract_recurring_todos_builder_test.rb +++ b/test/models/recurring_todos/abstract_recurring_todos_builder_test.rb @@ -20,7 +20,7 @@ module RecurringTodos end def test_filter_attributes_should_throw_exception - attributes = { + attributes = Tracks::AttributeHandler.new(@admin, { 'recurring_period' => "daily", 'description' => "test", 'tag_list' => "tag, this, that", @@ -30,7 +30,7 @@ module RecurringTodos 'show_always' => true, 'start_from' => '01/01/01', 'ends_on' => 'no_end_date' - } + }) assert_raise(Exception, "should have exception since we are using abstract builder") do builder = AbstractRecurringTodosBuilder.new(@admin, attributes, DailyRepeatPattern) @@ -46,7 +46,7 @@ module RecurringTodos } builder = RecurringTodosBuilder.new(@admin, attributes) - assert_equal "tag, this, that", builder.attributes[:tag_list] + assert_equal "tag, this, that", builder.attributes.get(:tag_list) # given attributes without tag_list attributes = { @@ -55,7 +55,7 @@ module RecurringTodos } builder = RecurringTodosBuilder.new(@admin, attributes) - assert_equal "", builder.attributes[:tag_list] + assert_equal "", builder.attributes.get(:tag_list) # given attributes with nil tag_list attributes = { @@ -65,7 +65,7 @@ module RecurringTodos } builder = RecurringTodosBuilder.new(@admin, attributes) - assert_equal "", builder.attributes[:tag_list] + assert_equal "", builder.attributes.get(:tag_list) # given attributes with empty tag_list ==> should be stripped attributes = { @@ -75,7 +75,7 @@ module RecurringTodos } builder = RecurringTodosBuilder.new(@admin, attributes) - assert_equal "", builder.attributes[:tag_list] + assert_equal "", builder.attributes.get(:tag_list) end def test_tags_should_be_saved_on_create_and_update @@ -135,18 +135,18 @@ module RecurringTodos end def test_map_removes_mapped_key - attributes = { :source => "value"} + attributes = Tracks::AttributeHandler.new(@admin, { :source => "value"}) arp = WeeklyRecurringTodosBuilder.new(@admin, attributes) attributes = arp.map(attributes, :target, :source) - assert_equal "value", attributes[:target] - assert_nil attributes[:source] + assert_equal "value", attributes.get(:target) + assert_nil attributes.get(:source) assert !attributes.key?(:source) end def test_get_selector_removes_selector_from_hash - attributes = { :selector => "weekly" } + attributes = Tracks::AttributeHandler.new(@admin, { :selector => "weekly" }) arp = WeeklyRecurringTodosBuilder.new(@admin, attributes) assert "weekly", arp.get_selector(:selector) @@ -154,7 +154,7 @@ module RecurringTodos end def test_get_selector_raises_exception_when_missing_selector - attributes = { } + attributes = Tracks::AttributeHandler.new(@admin, { }) arp = WeeklyRecurringTodosBuilder.new(@admin, attributes) assert_raise(Exception, "should raise exception when recurrence selector is missing"){ arp.get_selector(:selector) } diff --git a/test/models/recurring_todos/daily_recurring_todos_builder_test.rb b/test/models/recurring_todos/daily_recurring_todos_builder_test.rb index 8e692bac..b6463c9b 100644 --- a/test/models/recurring_todos/daily_recurring_todos_builder_test.rb +++ b/test/models/recurring_todos/daily_recurring_todos_builder_test.rb @@ -24,54 +24,54 @@ module RecurringTodos result = RecurringTodosBuilder.new(@admin, attributes).attributes - assert_nil result['bla_bla'], "bla_bla should be filtered" - assert_nil result[:bla_bla], "bla_bla should be filtered" - assert_equal false, result[:only_work_days], "daily attributes should be preserved" - assert_equal "a repeating todo", result[:description], "description should be preserved" + assert_nil result.get('bla_bla'), "bla_bla should be filtered" + assert_nil result.get(:bla_bla), "bla_bla should be filtered" + assert_equal false, result.get(:only_work_days), "daily attributes should be preserved" + assert_equal "a repeating todo", result.get(:description), "description should be preserved" end def test_valid_selector - attributes = { + attributes = Tracks::AttributeHandler.new(@admin, { 'recurring_period' => 'daily' - } + }) # should not raise %w{daily_every_x_day daily_every_work_day}.each do |selector| - attributes['daily_selector'] = selector + attributes.set('daily_selector', selector) DailyRecurringTodosBuilder.new(@admin, attributes) end # should raise - attributes = { + attributes = Tracks::AttributeHandler.new(@admin, { 'recurring_period' => 'daily', 'daily_selector' => 'wrong value' - } + }) # should raise assert_raise(Exception, "should have exception since daily_selector has wrong value"){ DailyRecurringTodosBuilder.new(@admin, attributes) } end def test_mapping_of_attributes - attributes = { + attributes = Tracks::AttributeHandler.new(@admin, { 'recurring_period' => 'daily', 'description' => 'a repeating todo', # generic 'daily_selector' => 'daily_every_x_day', # daily specific --> mapped to only_work_days=false 'daily_every_x_days' => '5' # mapped to every_other1 - } + }) pattern = DailyRecurringTodosBuilder.new(@admin, attributes) - assert_equal '5', pattern.mapped_attributes[:every_other1], "every_other1 should be set to daily_every_x_days" - assert_equal false, pattern.mapped_attributes[:only_work_days], "only_work_days should be set to false for daily_every_x_day" + assert_equal '5', pattern.mapped_attributes.get(:every_other1), "every_other1 should be set to daily_every_x_days" + assert_equal false, pattern.mapped_attributes.get(:only_work_days), "only_work_days should be set to false for daily_every_x_day" - attributes = { + attributes = Tracks::AttributeHandler.new(@admin, { 'recurring_period' => 'daily', 'description' => 'a repeating todo', # generic 'daily_selector' => 'daily_every_work_day', # daily specific --> mapped to only_work_days=true - } + }) pattern = DailyRecurringTodosBuilder.new(@admin, attributes) - assert_equal true, pattern.mapped_attributes[:only_work_days] + assert_equal true, pattern.mapped_attributes.get(:only_work_days) end end diff --git a/test/models/recurring_todos/monthly_recurring_todos_builder_test.rb b/test/models/recurring_todos/monthly_recurring_todos_builder_test.rb index acb0da99..f02299ca 100644 --- a/test/models/recurring_todos/monthly_recurring_todos_builder_test.rb +++ b/test/models/recurring_todos/monthly_recurring_todos_builder_test.rb @@ -25,34 +25,34 @@ module RecurringTodos result = RecurringTodosBuilder.new(@admin, attributes).attributes - assert_nil result['bla_bla'], "bla_bla should be filtered" - assert_nil result[:bla_bla], "bla_bla should be filtered" - assert_equal 5, result[:every_other1], "should be preserved" + assert_nil result.get('bla_bla'), "bla_bla should be filtered" + assert_nil result.get(:bla_bla), "bla_bla should be filtered" + assert_equal 5, result.get(:every_other1), "should be preserved" end def test_valid_selector - attributes = { + attributes = Tracks::AttributeHandler.new(@admin, { 'recurring_period' => 'monthly' - } + }) # should not raise %w{monthly_every_x_day monthly_every_xth_day}.each do |selector| - attributes['monthly_selector'] = selector + attributes.set('monthly_selector', selector) MonthlyRecurringTodosBuilder.new(@admin, attributes) end # should raise - attributes = { + attributes = Tracks::AttributeHandler.new(@admin, { 'recurring_period' => 'monthly', 'monthly_selector' => 'wrong value' - } + }) # should raise assert_raise(Exception, "should have exception since monthly_selector has wrong value"){ MonthlyRecurringTodosBuilder.new(@admin, attributes) } end def test_mapping_of_attributes - attributes = { + attributes = Tracks::AttributeHandler.new(@admin, { 'recurring_period' => 'monthly', 'description' => 'a repeating todo', # generic 'monthly_selector' => 'monthly_every_x_day', # monthly specific @@ -61,30 +61,30 @@ module RecurringTodos 'monthly_day_of_week' => 3, # mapped to :every_count 'monthly_every_x_month' => '10', # mapped to :every_other2 'monthly_every_x_month2' => '20' # not mapped - } + }) builder = MonthlyRecurringTodosBuilder.new(@admin, attributes) - assert_equal 0, builder.mapped_attributes[:recurrence_selector], "selector should be 0 for monthly_every_x_day" - assert_equal '5', builder.mapped_attributes[:every_other1], "every_other1 should be set to monthly_every_x_days" - assert_equal '10', builder.mapped_attributes[:every_other2], "every_other2 should be set to monthly_every_x_month when selector is monthly_every_x_day (=0)" - assert_equal '7', builder.mapped_attributes[:every_other3], "every_other3 should be set to monthly_every_xth_day" - assert_equal 3, builder.mapped_attributes[:every_count], "every_count should be set to monthly_day_of_week" + assert_equal 0, builder.mapped_attributes.get(:recurrence_selector), "selector should be 0 for monthly_every_x_day" + assert_equal '5', builder.mapped_attributes.get(:every_other1), "every_other1 should be set to monthly_every_x_days" + assert_equal '10', builder.mapped_attributes.get(:every_other2), "every_other2 should be set to monthly_every_x_month when selector is monthly_every_x_day (=0)" + assert_equal '7', builder.mapped_attributes.get(:every_other3), "every_other3 should be set to monthly_every_xth_day" + assert_equal 3, builder.mapped_attributes.get(:every_count), "every_count should be set to monthly_day_of_week" builder.build assert builder.pattern.every_x_day?, "every_x_day? should say true for selector monthly_every_x_day" - attributes = { + attributes = Tracks::AttributeHandler.new(@admin, { 'recurring_period' => 'monthly', 'description' => 'a repeating todo', # generic 'monthly_selector' => 'monthly_every_xth_day', # monthly specific 'monthly_every_x_day' => '5', # mapped to :every_other1 'monthly_every_x_month' => '10', # not mapped 'monthly_every_x_month2' => '20' # mapped to :every_other2 - } + }) builder = MonthlyRecurringTodosBuilder.new(@admin, attributes) - assert_equal 1, builder.mapped_attributes[:recurrence_selector], "selector should be 1 for monthly_every_xth_day" - assert_equal '20', builder.mapped_attributes[:every_other2], "every_other2 should be set to monthly_every_x_month2 when selector is monthly_every_xth_day (=0)" + assert_equal 1, builder.mapped_attributes.get(:recurrence_selector), "selector should be 1 for monthly_every_xth_day" + assert_equal '20', builder.mapped_attributes.get(:every_other2), "every_other2 should be set to monthly_every_x_month2 when selector is monthly_every_xth_day (=0)" builder.build assert builder.pattern.every_xth_day?, "every_xth_day? should say true for selector monthly_every_xth_day" diff --git a/test/models/recurring_todos/recurring_todos_builder_test.rb b/test/models/recurring_todos/recurring_todos_builder_test.rb index fbeff70e..e72e4633 100644 --- a/test/models/recurring_todos/recurring_todos_builder_test.rb +++ b/test/models/recurring_todos/recurring_todos_builder_test.rb @@ -35,8 +35,8 @@ module RecurringTodos 'end_date' => '05/05/05' }) - assert builder.attributes[:start_from].is_a?(ActiveSupport::TimeWithZone), "Dates should be parsed to ActiveSupport::TimeWithZone class" - assert builder.attributes[:end_date].is_a?(ActiveSupport::TimeWithZone), "Dates should be parsed to ActiveSupport::TimeWithZone class" + assert builder.attributes.get(:start_from).is_a?(ActiveSupport::TimeWithZone), "Dates should be parsed to ActiveSupport::TimeWithZone class" + assert builder.attributes.get(:end_date).is_a?(ActiveSupport::TimeWithZone), "Dates should be parsed to ActiveSupport::TimeWithZone class" end def test_exisisting_project_is_used diff --git a/test/models/recurring_todos/weekly_recurring_todos_builder_test.rb b/test/models/recurring_todos/weekly_recurring_todos_builder_test.rb index e4a715ad..963e9b37 100644 --- a/test/models/recurring_todos/weekly_recurring_todos_builder_test.rb +++ b/test/models/recurring_todos/weekly_recurring_todos_builder_test.rb @@ -24,18 +24,18 @@ module RecurringTodos result = RecurringTodosBuilder.new(@admin, attributes).attributes - assert_nil result['bla_bla'], "bla_bla should be filtered" - assert_nil result[:bla_bla], "bla_bla should be filtered" - assert_equal ' m ', result[:every_day], "weekly attributes should be preserved" - assert_equal "a repeating todo", result[:description], "description should be preserved" + assert_nil result.get('bla_bla'), "bla_bla should be filtered" + assert_nil result.get(:bla_bla), "bla_bla should be filtered" + assert_equal ' m ', result.get(:every_day), "weekly attributes should be preserved" + assert_equal "a repeating todo", result.get(:description), "description should be preserved" end def test_attributes_to_filter - attributes = { + attributes = Tracks::AttributeHandler.new(@admin, { 'recurring_period' => 'weekly', 'description' => 'a repeating todo', # generic 'weekly_return_monday' => 'm', # weekly specific - } + }) w = WeeklyRecurringTodosBuilder.new(@admin, attributes) assert_equal 9, w.attributes_to_filter.size @@ -44,41 +44,41 @@ module RecurringTodos end def test_mapping_of_attributes - attributes = { + attributes = Tracks::AttributeHandler.new(@admin, { 'recurring_period' => 'weekly', 'description' => 'a repeating todo', # generic 'weekly_every_x_week' => '5', # mapped to every_other1 'weekly_return_monday' => 'm' - } + }) pattern = WeeklyRecurringTodosBuilder.new(@admin, attributes) - assert_equal '5', pattern.mapped_attributes[:every_other1], "every_other1 should be set to weekly_every_x_week" - assert_equal ' m ', pattern.mapped_attributes[:every_day], "weekly_return_ should be mapped to :every_day in format 'smtwtfs'" + assert_equal '5', pattern.mapped_attributes.get(:every_other1), "every_other1 should be set to weekly_every_x_week" + assert_equal ' m ', pattern.mapped_attributes.get(:every_day), "weekly_return_ should be mapped to :every_day in format 'smtwtfs'" end def test_map_day - attributes = { + attributes = Tracks::AttributeHandler.new(@admin, { 'recurring_period' => 'weekly', 'description' => 'a repeating todo', # generic 'weekly_every_x_week' => '5' # mapped to every_other1 - } + }) pattern = WeeklyRecurringTodosBuilder.new(@admin, attributes) - assert_equal ' ', pattern.mapped_attributes[:every_day], "all days should be empty in :every_day" + assert_equal ' ', pattern.mapped_attributes.get(:every_day), "all days should be empty in :every_day" # add all days { sunday: 's', monday: 'm', tuesday: 't', wednesday: 'w', thursday: 't', friday: 'f', saturday: 's' }.each do |day, short| - attributes["weekly_return_#{day}"] = short + attributes.set("weekly_return_#{day}", short) end pattern = WeeklyRecurringTodosBuilder.new(@admin, attributes) - assert_equal 'smtwtfs', pattern.mapped_attributes[:every_day], "all days should be filled in :every_day" + assert_equal 'smtwtfs', pattern.mapped_attributes.get(:every_day), "all days should be filled in :every_day" # remove wednesday attributes = attributes.except('weekly_return_wednesday') pattern = WeeklyRecurringTodosBuilder.new(@admin, attributes) - assert_equal 'smt tfs', pattern.mapped_attributes[:every_day], "only wednesday should be empty in :every_day" + assert_equal 'smt tfs', pattern.mapped_attributes.get(:every_day), "only wednesday should be empty in :every_day" end diff --git a/test/models/recurring_todos/yearly_recurring_todos_builder_test.rb b/test/models/recurring_todos/yearly_recurring_todos_builder_test.rb index 6b821cc8..51db4860 100644 --- a/test/models/recurring_todos/yearly_recurring_todos_builder_test.rb +++ b/test/models/recurring_todos/yearly_recurring_todos_builder_test.rb @@ -25,28 +25,28 @@ module RecurringTodos result = RecurringTodosBuilder.new(@admin, attributes).attributes - assert_nil result['bla_bla'], "bla_bla should be filtered" - assert_nil result[:bla_bla], "bla_bla should be filtered" - assert_equal '1', result[:every_other2], "yearly attributes should be preserved" - assert_equal "a repeating todo", result[:description], "description should be preserved" + assert_nil result.get('bla_bla'), "bla_bla should be filtered" + assert_nil result.get(:bla_bla), "bla_bla should be filtered" + assert_equal '1', result.get(:every_other2), "yearly attributes should be preserved" + assert_equal "a repeating todo", result.get(:description), "description should be preserved" end def test_valid_selector - attributes = { + attributes = Tracks::AttributeHandler.new(@admin, { 'recurring_period' => 'yearly' - } + }) # should not raise %w{yearly_every_x_day yearly_every_xth_day}.each do |selector| - attributes['yearly_selector'] = selector + attributes.set(:yearly_selector, selector) YearlyRecurringTodosBuilder.new(@admin, attributes) end # should raise - attributes = { + attributes = Tracks::AttributeHandler.new(@admin, { 'recurring_period' => 'yearly', 'yearly_selector' => 'wrong value' - } + }) # should raise assert_raise(Exception, "should have exception since yearly_selector has wrong value"){ YearlyRecurringTodosBuilder.new(@admin, attributes) } @@ -64,12 +64,12 @@ module RecurringTodos 'yearly_month_of_year2' => '2' # ignored because yearly_selector is yearly_every_x_day } - pattern = YearlyRecurringTodosBuilder.new(@admin, attributes) + pattern = YearlyRecurringTodosBuilder.new(@admin, Tracks::AttributeHandler.new(@admin, attributes)) - assert_equal '5', pattern.mapped_attributes[:every_other1], "every_other1 should be set to yearly_every_x_day" - assert_equal '1', pattern.mapped_attributes[:every_other2], "every_other2 should be set to yearly_month_of_year because selector is yearly_every_x_day" - assert_equal '7', pattern.mapped_attributes[:every_other3], "every_other3 should be set to yearly_every_xth_day" - assert_equal '3', pattern.mapped_attributes[:every_count], "every_count should be set to yearly_day_of_week" + assert_equal '5', pattern.mapped_attributes.get(:every_other1), "every_other1 should be set to yearly_every_x_day" + assert_equal '1', pattern.mapped_attributes.get(:every_other2), "every_other2 should be set to yearly_month_of_year because selector is yearly_every_x_day" + assert_equal '7', pattern.mapped_attributes.get(:every_other3), "every_other3 should be set to yearly_every_xth_day" + assert_equal '3', pattern.mapped_attributes.get(:every_count), "every_count should be set to yearly_day_of_week" attributes = { 'recurring_period' => 'yearly', @@ -79,8 +79,8 @@ module RecurringTodos 'yearly_month_of_year2' => '2' # mapped to evert_other2 because yearly_selector is yearly_every_xth_day } - pattern = YearlyRecurringTodosBuilder.new(@admin, attributes) - assert_equal '2', pattern.mapped_attributes[:every_other2], "every_other2 should be set to yearly_month_of_year2 because selector is yearly_every_xth_day" + pattern = YearlyRecurringTodosBuilder.new(@admin, Tracks::AttributeHandler.new(@admin, attributes)) + assert_equal '2', pattern.mapped_attributes.get(:every_other2), "every_other2 should be set to yearly_month_of_year2 because selector is yearly_every_xth_day" end end