mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #4856 from ihid/store_null_bug
Allow store to be a not null column.
This commit is contained in:
commit
2abaa19e77
4 changed files with 16 additions and 3 deletions
|
@ -36,11 +36,13 @@ module ActiveRecord
|
|||
def store_accessor(store_attribute, *keys)
|
||||
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
|
||||
|
|
|
@ -4,14 +4,14 @@ require 'models/admin/user'
|
|||
|
||||
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'
|
||||
|
@ -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,4 +1,5 @@
|
|||
class Admin::User < ActiveRecord::Base
|
||||
belongs_to :account
|
||||
store :settings, :accessors => [ :color, :homepage ]
|
||||
store :preferences, :accessors => [ :remember_login ]
|
||||
end
|
||||
|
|
|
@ -37,7 +37,8 @@ ActiveRecord::Schema.define do
|
|||
|
||||
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
|
||||
|
||||
|
|
Loading…
Reference in a new issue