fix: only set default blocks on initializing a new overview and only when no blocks are provided

pull/6827/head
Jens Ulferts 13 years ago
parent 7be0043fb9
commit 9d16f8def3
  1. 16
      app/models/my_projects_overview.rb
  2. 25
      spec/models/my_projects_overview_spec.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

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

Loading…
Cancel
Save