Skip to content
Browse files

SettingObject: Renamed #update to #update_attributes

  • Loading branch information...
1 parent 65b90dd commit 2c7457db393aa84f9d4a3d9d21b0d2bed4becae4 @ledermann committed Mar 2, 2013
Showing with 104 additions and 53 deletions.
  1. +12 −3 lib/app/models/setting_object.rb
  2. +1 −1 spec/queries_spec.rb
  3. +1 −1 spec/serialize_spec.rb
  4. +88 −36 spec/setting_object_spec.rb
  5. +2 −12 spec/settings_spec.rb
View
15 lib/app/models/setting_object.rb
@@ -13,9 +13,14 @@ class SettingObject < ActiveRecord::Base
serialize :value, Hash
- def update!(value_attributes)
- self.value = self.value.merge(value_attributes.stringify_keys)
- save! if self.value_changed?
+ def update_attributes(attributes)
+ merge_value(attributes)
+ self.value_changed? ? super(:value => self.value) : true
+ end
+
+ def update_attributes!(attributes)
+ merge_value(attributes)
+ self.value_changed? ? super(:value => self.value) : true
end
def method_missing(method_name, *args, &block)
@@ -33,5 +38,9 @@ def method_missing(method_name, *args, &block)
def target_class
target_type.constantize
end
+
+ def merge_value(attributes)
+ self.value = self.value.merge(attributes.stringify_keys)
+ end
end
end
View
2 spec/queries_spec.rb
@@ -94,7 +94,7 @@
it "should update settings by one SQL query" do
expect {
- user.settings(:dashboard).update! :foo => 'bar'
+ user.settings(:dashboard).update_attributes! :foo => 'bar'
}.to perform_queries(1)
end
end
View
2 spec/serialize_spec.rb
@@ -25,7 +25,7 @@
describe 'updated settings' do
it 'should be serialized' do
- user.settings(:dashboard).update! :smart => true
+ user.settings(:dashboard).update_attributes! :smart => true
dashboard_settings = user.setting_objects.where(:var => 'dashboard').first
calendar_settings = user.setting_objects.where(:var => 'calendar').first
View
124 spec/setting_object_spec.rb
@@ -4,47 +4,99 @@
let(:user) { User.create! :name => 'Mr. Pink' }
let(:setting_object) { RailsSettings::SettingObject.new :var => 'dashboard', :target => user }
- it "should return nil for unknown keys" do
- setting_object.foo.should eq(nil)
- setting_object.bar.should eq(nil)
- end
+ describe "Getter and Setter" do
+ it "should return nil for unknown attribute" do
+ setting_object.foo.should eq(nil)
+ setting_object.bar.should eq(nil)
+ end
+
+ it "should return defaults" do
+ setting_object.theme.should eq('blue')
+ setting_object.view.should eq('monthly')
+ setting_object.filter.should eq(false)
+ end
+
+ it "should store to hash" do
+ setting_object.foo = 42
+ setting_object.bar = 'hello'
+
+ setting_object.value.should eq({'foo' => 42, 'bar' => 'hello'})
+ end
+
+ it "should set and return attributes" do
+ setting_object.theme = 'pink'
+ setting_object.foo = 42
+ setting_object.bar = 'hello'
- it "should return defaults" do
- setting_object.theme.should eq('blue')
- setting_object.view.should eq('monthly')
- setting_object.filter.should eq(false)
+ setting_object.theme.should eq('pink')
+ setting_object.foo.should eq(42)
+ setting_object.bar.should eq('hello')
+ end
end
-
- it "should set and return keys" do
- setting_object.theme = 'pink'
- setting_object.foo = 42
- setting_object.bar = 'hello'
-
- setting_object.theme.should eq('pink')
- setting_object.foo.should eq(42)
- setting_object.bar.should eq('hello')
+
+ describe "update_attributes" do
+ it 'should save' do
+ setting_object.update_attributes('foo' => 42).should be_true
+ setting_object.reload
+
+ setting_object.foo.should eq(42)
+ end
+
+ it "should not save for unchanged attributes" do
+ setting_object.theme = 'white'
+ setting_object.save!
+ setting_object.reload
+
+ setting_object.should_not_receive(:save)
+ setting_object.update_attributes :theme => 'white'
+ end
+
+ it "should not save for blank Hash" do
+ setting_object.should_not_receive(:save)
+ setting_object.update_attributes({}).should be_true
+ end
end
- it "should store to hash" do
- setting_object.foo = 42
- setting_object.bar = 'hello'
-
- setting_object.value.should eq({'foo' => 42, 'bar' => 'hello'})
+ describe "update_attributes!" do
+ it 'should save' do
+ setting_object.update_attributes!('foo' => 42).should be_true
+ setting_object.reload
+
+ setting_object.foo.should eq(42)
+ end
+
+ it "should not save for unchanged attributes" do
+ setting_object.theme = 'white'
+ setting_object.save!
+ setting_object.reload
+
+ setting_object.should_not_receive(:save)
+ setting_object.update_attributes! :theme => 'white'
+ end
+
+ it "should not save for blank Hash" do
+ setting_object.should_not_receive(:save!)
+ setting_object.update_attributes!({}).should be_true
+ end
end
-
- it "should save" do
- setting_object.foo = 42
- setting_object.bar = 'hello'
- setting_object.save!
-
- setting_object.should_not be_new_record
- setting_object.id.should_not be_zero
+
+ describe "save!" do
+ it "should save" do
+ setting_object.foo = 42
+ setting_object.bar = 'hello'
+ setting_object.save!
+
+ setting_object.should_not be_new_record
+ setting_object.id.should_not be_zero
+ end
end
-
- it "should not validate for unknown var" do
- setting_object.var = "unknown-var"
-
- setting_object.should_not be_valid
- setting_object.errors[:var].should be_present
+
+ describe "validation" do
+ it "should not validate for unknown var" do
+ setting_object.var = "unknown-var"
+
+ setting_object.should_not be_valid
+ setting_object.errors[:var].should be_present
+ end
end
end
View
14 spec/settings_spec.rb
@@ -123,7 +123,7 @@
end
it "should update settings" do
- user.settings(:dashboard).update! :smart => true
+ user.settings(:dashboard).update_attributes :smart => true
user.reload
user.settings(:dashboard).smart.should eq(true)
@@ -150,7 +150,7 @@
end
it "should update settings" do
- user.settings(:dashboard).update! :smart => true
+ user.settings(:dashboard).update_attributes! :smart => true
user.reload
user.settings(:dashboard).smart.should eq(true)
@@ -166,16 +166,6 @@
user.settings(:dashboard).smart.should eq(true)
end
- it "should not update settings for unchanged attributes" do
- RailsSettings::SettingObject.any_instance.should_not_receive(:save!)
- user.settings(:dashboard).update! :theme => 'white'
- end
-
- it "should not update settings for blank Hash" do
- RailsSettings::SettingObject.any_instance.should_not_receive(:save!)
- user.settings(:dashboard).update!({})
- end
-
it "should destroy settings with nil" do
expect {
user.settings = nil

0 comments on commit 2c7457d

Please sign in to comment.
Something went wrong with that request. Please try again.