Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Allow new settings on Sidebars

If a new setting is added to an existing sidebar, old instances will
correctly use the provided default value.
  • Loading branch information...
commit b46041e86c67038b68b5e153f4585a11ed9e8941 1 parent 9164798
@mvz mvz authored
Showing with 44 additions and 5 deletions.
  1. +14 −5 app/models/sidebar.rb
  2. +30 −0 spec/models/sidebar_spec.rb
View
19 app/models/sidebar.rb
@@ -151,14 +151,23 @@ def purge
end
def setting(key, default=nil, options = { })
- return if instance_methods.include?(key.to_s)
- fields << Field.build(key.to_s, default, options)
- fieldmap.update(key.to_s => fields.last)
+ key = key.to_s
+
+ return if instance_methods.include?(key)
+
+ fields << Field.build(key, default, options)
+ fieldmap.update(key => fields.last)
+
self.send(:define_method, key) do
- self.config[key.to_s]
+ if config.has_key? key
+ config[key]
+ else
+ default
+ end
end
+
self.send(:define_method, "#{key}=") do |newval|
- self.config[key.to_s] = newval
+ config[key] = newval
end
end
View
30 spec/models/sidebar_spec.rb
@@ -40,4 +40,34 @@
AmazonSidebar.new.content_partial.should == "/amazon_sidebar/content"
end
end
+
+ describe "::setting" do
+ let(:dummy_sidebar) do
+ Class.new(Sidebar) do
+ setting :foo, "default-foo"
+ end
+ end
+
+ it "creates a reader method with default value on instances" do
+ dummy = dummy_sidebar.new
+ dummy.foo.should eq "default-foo"
+ end
+
+ it "creates a writer method on instances" do
+ dummy = dummy_sidebar.new
+ dummy.foo = "adjusted-foo"
+ dummy.foo.should eq "adjusted-foo"
+ end
+
+ it "provides the default value to instances created earlier" do
+ dummy = dummy_sidebar.new
+
+ dummy_sidebar.instance_eval do
+ setting :bar, "default-bar"
+ end
+
+ dummy.config.should_not have_key("bar")
+ dummy.bar.should eq "default-bar"
+ end
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.