diff --git a/app/models/my_projects_overview.rb b/app/models/my_projects_overview.rb index 495e4833e4..3b07c87596 100644 --- a/app/models/my_projects_overview.rb +++ b/app/models/my_projects_overview.rb @@ -21,13 +21,19 @@ class MyProjectsOverview < ActiveRecord::Base "top" => [], "hidden" => [] } - def after_initialize - hs = attributes - DEFAULTS.each_pair do |k, v| - if hs[k].blank? - update_attribute(k, v.to_yaml) + def initialize(attributes = nil) + super + + if attributes.nil? + DEFAULTS.each_pair do |k, v| self.send("#{k}=", v) end + else + not_provided = DEFAULTS.keys - attributes.keys.collect(&:to_s) + + not_provided.each do |k| + self.send("#{k}=", []) + end end end diff --git a/spec/models/my_projects_overview_spec.rb b/spec/models/my_projects_overview_spec.rb index 54dc7eef26..ea8bb9ed24 100644 --- a/spec/models/my_projects_overview_spec.rb +++ b/spec/models/my_projects_overview_spec.rb @@ -8,6 +8,31 @@ describe MyProjectsOverview do @overview = MyProjectsOverview.create(:project_id => @project.id) end + it 'sets default elements for new records if no elements are provided' do + o = MyProjectsOverview.new + o.left.should =~ ["wiki", "projectdetails", "issuetracking"] + o.right.should =~ ["members", "news"] + o.top.should =~ [] + o.hidden.should =~ [] + end + + it 'does not set default elements if elements are provided' do + o = MyProjectsOverview.new :left => ["members"] + o.left.should =~ ["members"] + o.right.should =~ [] + o.top.should =~ [] + o.hidden.should =~ [] + end + + + it 'does not enforce default elements' do + @overview.right = [] + @overview.save! + + @overview.reload + @overview.right.should =~ [] + end + it 'creates a new custom element' do @overview.new_custom_element.should_not be_nil end