Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow store to be a not null column. #4856

Merged
merged 1 commit into from Feb 2, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions activerecord/lib/active_record/store.rb
Expand Up @@ -36,11 +36,13 @@ def store(store_attribute, options = {})
def store_accessor(store_attribute, *keys)
Array(keys).flatten.each do |key|
define_method("#{key}=") do |value|
send("#{store_attribute}=", {}) unless send(store_attribute).is_a?(Hash)
send(store_attribute)[key] = value
send("#{store_attribute}_will_change!")
end

define_method(key) do
send("#{store_attribute}=", {}) unless send(store_attribute).is_a?(Hash)
send(store_attribute)[key]
end
end
Expand Down
13 changes: 11 additions & 2 deletions activerecord/test/cases/store_test.rb
Expand Up @@ -4,14 +4,14 @@

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

test "reading store attributes through accessors" do
assert_equal 'black', @john.color
assert_nil @john.homepage
end

test "writing store attributes through accessors" do
@john.color = 'red'
@john.homepage = '37signals.com'
Expand All @@ -31,4 +31,13 @@ class StoreTest < ActiveRecord::TestCase
@john.color = 'red'
assert @john.settings_changed?
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
1 change: 1 addition & 0 deletions activerecord/test/models/admin/user.rb
@@ -1,4 +1,5 @@
class Admin::User < ActiveRecord::Base
belongs_to :account
store :settings, :accessors => [ :color, :homepage ]
store :preferences, :accessors => [ :remember_login ]
end
3 changes: 2 additions & 1 deletion activerecord/test/schema/schema.rb
Expand Up @@ -37,7 +37,8 @@ def create_table(*args, &block)

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

Expand Down