Skip to content

Commit

Permalink
SettingObject: Renamed #update to #update_attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
ledermann committed Mar 2, 2013
1 parent 65b90dd commit 2c7457d
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 53 deletions.
15 changes: 12 additions & 3 deletions lib/app/models/setting_object.rb
Expand Up @@ -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)
Expand All @@ -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
2 changes: 1 addition & 1 deletion spec/queries_spec.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/serialize_spec.rb
Expand Up @@ -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
Expand Down
124 changes: 88 additions & 36 deletions spec/setting_object_spec.rb
Expand Up @@ -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
14 changes: 2 additions & 12 deletions spec/settings_spec.rb
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 2c7457d

Please sign in to comment.