move mapping of form attributes to model attributes from pattern to builder to make

pattern independent of form
This commit is contained in:
Reinier Balt 2014-02-07 22:55:52 +01:00
parent 1f36c27af8
commit 776a046465
22 changed files with 463 additions and 446 deletions

View file

@ -77,13 +77,10 @@ class RecurringTodosController < ApplicationController
end
def destroy
@number_of_todos = @recurring_todo.todos.count
# remove all references to this recurring todo
@todos = @recurring_todo.todos
@number_of_todos = @todos.size
@todos.each do |t|
t.recurring_todo_id = nil
t.save
end
@recurring_todo.clear_todos_association
# delete the recurring todo
@saved = @recurring_todo.destroy
@ -100,7 +97,7 @@ class RecurringTodosController < ApplicationController
else
notify :error, t('todos.error_deleting_recurring', :description => @recurring_todo.description)
end
redirect_to :action => 'index'
redirect_to :action => 'index'
end
format.js do

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -9,13 +9,21 @@ class RecurringTodosControllerTest < ActionController::TestCase
def test_destroy_recurring_todo
login_as(:admin_user)
rc = RecurringTodo.find(1)
todo = rc.todos.first
xhr :post, :destroy, :id => 1, :_source_view => 'todo'
begin
rc = RecurringTodo.find(1)
rescue
rc = nil
end
assert_nil rc
assert_nil rc, "rc should be deleted"
assert_nil todo.reload.recurring_todo_id, "todo should be unlinked from deleted recurring_todo"
end
def test_new_recurring_todo
@ -362,7 +370,6 @@ class RecurringTodosControllerTest < ActionController::TestCase
def test_update_recurring_todo
login_as(:admin_user)
rt = recurring_todos(:call_bill_gates_every_day)
current_descr = rt.description
put :update,
"recurring_todo" => {

View file

@ -5,6 +5,16 @@ module RecurringTodos
class AbstractRecurringTodosBuilderTest < ActiveSupport::TestCase
fixtures :users
class TestRepeatPattern < AbstractRepeatPattern
def selector_key
'test'
end
def valid_selector?(selector)
true
end
end
def setup
@admin = users(:admin_user)
end
@ -23,7 +33,7 @@ module RecurringTodos
}
assert_raise(Exception, "should have exception since we are using abstract builder") do
builder = AbstractRecurringTodosBuilder.new(@admin, attributes)
builder = AbstractRecurringTodosBuilder.new(@admin, attributes, DailyRepeatPattern)
end
end
@ -101,7 +111,6 @@ module RecurringTodos
assert_equal "bar, foo", rt.tag_list
end
def test_saved_should_raise_exception_on_validation_errors
attributes = {
'recurring_period' => "daily",
@ -125,6 +134,32 @@ module RecurringTodos
assert_raise(Exception, "should have exception since there is no saved recurring todo"){ builder.saved_recurring_todo }
end
def test_map_removes_mapped_key
attributes = { :source => "value"}
arp = WeeklyRecurringTodosBuilder.new(@admin, attributes)
attributes = arp.map(attributes, :target, :source)
assert_equal "value", attributes[:target]
assert_nil attributes[:source]
assert !attributes.key?(:source)
end
def test_get_selector_removes_selector_from_hash
attributes = { :selector => "weekly" }
arp = WeeklyRecurringTodosBuilder.new(@admin, attributes)
assert "weekly", arp.get_selector(:selector)
assert !arp.attributes.key?(:selector)
end
def test_get_selector_raises_exception_when_missing_selector
attributes = { }
arp = WeeklyRecurringTodosBuilder.new(@admin, attributes)
assert_raise(Exception, "should raise exception when recurrence selector is missing"){ arp.get_selector(:selector) }
end
end
end

View file

@ -5,42 +5,10 @@ module RecurringTodos
class AbstractRepeatPatternTest < ActiveSupport::TestCase
fixtures :users
class TestRepeatPattern < AbstractRepeatPattern
def valid_selector?(selector)
true
end
end
def setup
@admin = users(:admin_user)
end
def test_map_removes_mapped_key
attributes = { :source => "value"}
arp = AbstractRepeatPattern.new(@admin, attributes)
attributes = arp.map(attributes, :target, :source)
assert_equal "value", attributes[:target]
assert_nil attributes[:source]
assert !attributes.key?(:source)
end
def test_get_selector_removes_selector_from_hash
attributes = { :selector => "weekly" }
arp = TestRepeatPattern.new(@admin, attributes)
assert "weekly", arp.get_selector(:selector)
assert !arp.attributes.key?(:selector)
end
def test_get_selector_raises_exception_when_missing_selector
attributes = { }
arp = TestRepeatPattern.new(@admin, attributes)
assert_raise(Exception, "should raise exception when recurrence selector is missing"){ arp.get_selector(:selector) }
end
end
end

View file

@ -30,6 +30,50 @@ module RecurringTodos
assert_equal "a repeating todo", result[:description], "description should be preserved"
end
def test_valid_selector
attributes = {
'recurring_period' => 'daily'
}
# should not raise
%w{daily_every_x_day daily_every_work_day}.each do |selector|
attributes['daily_selector'] = selector
DailyRecurringTodosBuilder.new(@admin, attributes)
end
# should raise
attributes = {
'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 = {
'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"
attributes = {
'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]
end
end
end

View file

@ -9,50 +9,6 @@ module RecurringTodos
@admin = users(:admin_user)
end
def test_valid_selector
attributes = {
'recurring_period' => 'daily'
}
# should not raise
%w{daily_every_x_day daily_every_work_day}.each do |selector|
attributes['daily_selector'] = selector
DailyRepeatPattern.new(@admin, attributes)
end
# should raise
attributes = {
'recurring_period' => 'daily',
'daily_selector' => 'wrong value'
}
# should raise
assert_raise(Exception, "should have exception since daily_selector has wrong value"){ DailyRepeatPattern.new(@admin, attributes) }
end
def test_mapping_of_attributes
attributes = {
'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 = DailyRepeatPattern.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"
attributes = {
'recurring_period' => 'daily',
'description' => 'a repeating todo', # generic
'daily_selector' => 'daily_every_work_day', # daily specific --> mapped to only_work_days=true
}
pattern = DailyRepeatPattern.new(@admin, attributes)
assert_equal true, pattern.mapped_attributes[:only_work_days]
end
end
end

View file

@ -30,6 +30,66 @@ module RecurringTodos
assert_equal 5, result[:every_other1], "should be preserved"
end
def test_valid_selector
attributes = {
'recurring_period' => 'monthly'
}
# should not raise
%w{monthly_every_x_day monthly_every_xth_day}.each do |selector|
attributes['monthly_selector'] = selector
MonthlyRecurringTodosBuilder.new(@admin, attributes)
end
# should raise
attributes = {
'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 = {
'recurring_period' => 'monthly',
'description' => 'a repeating todo', # generic
'monthly_selector' => 'monthly_every_x_day', # monthly specific
'monthly_every_x_day' => '5', # mapped to :every_other1
'monthly_every_xth_day' => '7', # mapped to :every_other3
'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"
builder.build
assert builder.pattern.every_x_day?, "every_x_day? should say true for selector monthly_every_x_day"
attributes = {
'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)"
builder.build
assert builder.pattern.every_xth_day?, "every_xth_day? should say true for selector monthly_every_xth_day"
end
end
end

View file

@ -8,67 +8,7 @@ module RecurringTodos
def setup
@admin = users(:admin_user)
end
def test_valid_selector
attributes = {
'recurring_period' => 'monthly'
}
# should not raise
%w{monthly_every_x_day monthly_every_xth_day}.each do |selector|
attributes['monthly_selector'] = selector
MonthlyRepeatPattern.new(@admin, attributes)
end
# should raise
attributes = {
'recurring_period' => 'monthly',
'monthly_selector' => 'wrong value'
}
# should raise
assert_raise(Exception, "should have exception since monthly_selector has wrong value"){ MonthlyRepeatPattern.new(@admin, attributes) }
end
def test_mapping_of_attributes
attributes = {
'recurring_period' => 'monthly',
'description' => 'a repeating todo', # generic
'monthly_selector' => 'monthly_every_x_day', # monthly specific
'monthly_every_x_day' => '5', # mapped to :every_other1
'monthly_every_xth_day' => '7', # mapped to :every_other3
'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
}
pattern = MonthlyRepeatPattern.new(@admin, attributes)
assert_equal 0, pattern.mapped_attributes[:recurrence_selector], "selector should be 0 for monthly_every_x_day"
assert_equal '5', pattern.mapped_attributes[:every_other1], "every_other1 should be set to monthly_every_x_days"
assert_equal '10', pattern.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', pattern.mapped_attributes[:every_other3], "every_other3 should be set to monthly_every_xth_day"
assert_equal 3, pattern.mapped_attributes[:every_count], "every_count should be set to monthly_day_of_week"
pattern.build_recurring_todo
assert pattern.every_x_day?, "every_x_day? should say true for selector monthly_every_x_day"
attributes = {
'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
}
pattern = MonthlyRepeatPattern.new(@admin, attributes)
assert_equal 1, pattern.mapped_attributes[:recurrence_selector], "selector should be 1 for monthly_every_xth_day"
assert_equal '20', pattern.mapped_attributes[:every_other2], "every_other2 should be set to monthly_every_x_month2 when selector is monthly_every_xth_day (=0)"
pattern.build_recurring_todo
assert pattern.every_xth_day?, "every_xth_day? should say true for selector monthly_every_xth_day"
end
end
end

View file

@ -43,6 +43,45 @@ module RecurringTodos
assert w.attributes_to_filter.include?('weekly_return_monday'), "attributes_to_filter should return generated weekly_return_xyz"
end
def test_mapping_of_attributes
attributes = {
'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'"
end
def test_map_day
attributes = {
'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"
# 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
end
pattern = WeeklyRecurringTodosBuilder.new(@admin, attributes)
assert_equal 'smtwtfs', pattern.mapped_attributes[: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"
end
end
end

View file

@ -8,45 +8,7 @@ module RecurringTodos
def setup
@admin = users(:admin_user)
end
def test_mapping_of_attributes
attributes = {
'recurring_period' => 'weekly',
'description' => 'a repeating todo', # generic
'weekly_every_x_week' => '5', # mapped to every_other1
'weekly_return_monday' => 'm'
}
pattern = WeeklyRepeatPattern.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'"
end
def test_map_day
attributes = {
'recurring_period' => 'weekly',
'description' => 'a repeating todo', # generic
'weekly_every_x_week' => '5' # mapped to every_other1
}
pattern = WeeklyRepeatPattern.new(@admin, attributes)
assert_equal ' ', pattern.mapped_attributes[: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
end
pattern = WeeklyRepeatPattern.new(@admin, attributes)
assert_equal 'smtwtfs', pattern.mapped_attributes[:every_day], "all days should be filled in :every_day"
# remove wednesday
attributes = attributes.except('weekly_return_wednesday')
pattern = WeeklyRepeatPattern.new(@admin, attributes)
assert_equal 'smt tfs', pattern.mapped_attributes[:every_day], "only wednesday should be empty in :every_day"
end
end
end

View file

@ -24,13 +24,65 @@ 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"
end
def test_valid_selector
attributes = {
'recurring_period' => 'yearly'
}
# should not raise
%w{yearly_every_x_day yearly_every_xth_day}.each do |selector|
attributes['yearly_selector'] = selector
YearlyRecurringTodosBuilder.new(@admin, attributes)
end
# should raise
attributes = {
'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) }
end
def test_mapping_of_attributes
attributes = {
'recurring_period' => 'yearly',
'description' => 'a repeating todo', # generic
'yearly_selector' => 'yearly_every_x_day', # yearly specific
'yearly_every_x_day' => '5', # mapped to every_other1
'yearly_every_xth_day' => '7', # mapped to every_other3
'yearly_day_of_week' => '3', # mapped to every_count
'yearly_month_of_year' => '1', # mapped to evert_other2 because yearly_selector is yearly_every_x_day
'yearly_month_of_year2' => '2' # ignored because yearly_selector is yearly_every_x_day
}
pattern = YearlyRecurringTodosBuilder.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"
attributes = {
'recurring_period' => 'yearly',
'description' => 'a repeating todo', # generic
'yearly_selector' => 'yearly_every_xth_day', # daily specific --> mapped to only_work_days=false
'yearly_month_of_year' => '1', # ignored because yearly_selector is yearly_every_xth_day
'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"
end
end
end

View file

@ -9,58 +9,6 @@ module RecurringTodos
@admin = users(:admin_user)
end
def test_valid_selector
attributes = {
'recurring_period' => 'yearly'
}
# should not raise
%w{yearly_every_x_day yearly_every_xth_day}.each do |selector|
attributes['yearly_selector'] = selector
YearlyRepeatPattern.new(@admin, attributes)
end
# should raise
attributes = {
'recurring_period' => 'yearly',
'yearly_selector' => 'wrong value'
}
# should raise
assert_raise(Exception, "should have exception since yearly_selector has wrong value"){ YearlyRepeatPattern.new(@admin, attributes) }
end
def test_mapping_of_attributes
attributes = {
'recurring_period' => 'yearly',
'description' => 'a repeating todo', # generic
'yearly_selector' => 'yearly_every_x_day', # yearly specific
'yearly_every_x_day' => '5', # mapped to every_other1
'yearly_every_xth_day' => '7', # mapped to every_other3
'yearly_day_of_week' => '3', # mapped to every_count
'yearly_month_of_year' => '1', # mapped to evert_other2 because yearly_selector is yearly_every_x_day
'yearly_month_of_year2' => '2' # ignored because yearly_selector is yearly_every_x_day
}
pattern = YearlyRepeatPattern.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"
attributes = {
'recurring_period' => 'yearly',
'description' => 'a repeating todo', # generic
'yearly_selector' => 'yearly_every_xth_day', # daily specific --> mapped to only_work_days=false
'yearly_month_of_year' => '1', # ignored because yearly_selector is yearly_every_xth_day
'yearly_month_of_year2' => '2' # mapped to evert_other2 because yearly_selector is yearly_every_xth_day
}
pattern = YearlyRepeatPattern.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"
end
end
end