Skip to content

Commit

Permalink
Allow store to be a not null column.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Walker committed Feb 2, 2012
1 parent 56ba210 commit 58d10e2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 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
13 changes: 11 additions & 2 deletions activerecord/test/cases/store_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@


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
assert_equal 'black', @john.color assert_equal 'black', @john.color
assert_nil @john.homepage assert_nil @john.homepage
end end

test "writing store attributes through accessors" do test "writing store attributes through accessors" do
@john.color = 'red' @john.color = 'red'
@john.homepage = '37signals.com' @john.homepage = '37signals.com'
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 58d10e2

Please sign in to comment.