mirror of
https://github.com/github/rails.git
synced 2026-01-09 14:48:01 -05:00
Ported PR #4856 to 3-2-stable.
This commit is contained in:
@@ -36,11 +36,13 @@ module ActiveRecord
|
||||
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
|
||||
|
||||
@@ -4,7 +4,7 @@ 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
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user