move form specific accessors out of model

This commit is contained in:
Reinier Balt 2014-02-09 21:48:52 +01:00
parent bad91e8d10
commit 59a29c664a
16 changed files with 279 additions and 385 deletions

View file

@ -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

View file

@ -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) }

View file

@ -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

View file

@ -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"

View file

@ -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

View file

@ -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_<weekday> 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_<weekday> 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

View file

@ -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