Skip to content

Commit

Permalink
Ported PR rails#4856 to 3-2-stable.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Walker committed Aug 9, 2012
1 parent 2e98e0f commit 2a6039a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
2 changes: 2 additions & 0 deletions activerecord/lib/active_record/store.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ def store(store_attribute, options = {})
def store_accessor(store_attribute, *keys) def store_accessor(store_attribute, *keys)
Array(keys).flatten.each do |key| Array(keys).flatten.each do |key|
define_method("#{key}=") do |value| define_method("#{key}=") do |value|
send("#{store_attribute}=", {}) unless send(store_attribute).is_a?(Hash)
send(store_attribute)[key] = value send(store_attribute)[key] = value
send("#{store_attribute}_will_change!") send("#{store_attribute}_will_change!")
end end


define_method(key) do define_method(key) do
send("#{store_attribute}=", {}) unless send(store_attribute).is_a?(Hash)
send(store_attribute)[key] send(store_attribute)[key]
end end
end end
Expand Down
11 changes: 10 additions & 1 deletion activerecord/test/cases/store_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


class StoreTest < ActiveRecord::TestCase class StoreTest < ActiveRecord::TestCase
setup do setup do
@john = Admin::User.create(:name => 'John Doe', :color => 'black') @john = Admin::User.create(:name => 'John Doe', :color => 'black', :remember_login => true)
end end


test "reading store attributes through accessors" do test "reading store attributes through accessors" do
Expand All @@ -31,4 +31,13 @@ class StoreTest < ActiveRecord::TestCase
@john.color = 'red' @john.color = 'red'
assert @john.settings_changed? assert @john.settings_changed?
end end

test "object initialization with not nullable column" do
assert_equal true, @john.remember_login
end

test "writing with not nullable column" do
@john.remember_login = false
assert_equal false, @john.remember_login
end
end end
1 change: 1 addition & 0 deletions activerecord/test/models/admin/user.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,5 @@
class Admin::User < ActiveRecord::Base class Admin::User < ActiveRecord::Base
belongs_to :account belongs_to :account
store :settings, :accessors => [ :color, :homepage ] store :settings, :accessors => [ :color, :homepage ]
store :preferences, :accessors => [ :remember_login ]
end end
3 changes: 2 additions & 1 deletion activerecord/test/schema/schema.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ def create_table(*args, &block)


create_table :admin_users, :force => true do |t| create_table :admin_users, :force => true do |t|
t.string :name t.string :name
t.text :settings t.text :settings, :null => true
t.text :preferences, :null => false, :default => ""
t.references :account t.references :account
end end


Expand Down

0 comments on commit 2a6039a

Please sign in to comment.