Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Ported PR #4856 to 3-2-stable.

  • Loading branch information...
commit 39b7036845ffcee2ec33bd580f9e6ba5ada55c75 1 parent f916804
Jeremy Walker iHiD authored lmars committed
2  activerecord/lib/active_record/store.rb
@@ -36,11 +36,13 @@ def store(store_attribute, options = {})
36 36 def store_accessor(store_attribute, *keys)
37 37 Array(keys).flatten.each do |key|
38 38 define_method("#{key}=") do |value|
  39 + send("#{store_attribute}=", {}) unless send(store_attribute).is_a?(Hash)
39 40 send(store_attribute)[key] = value
40 41 send("#{store_attribute}_will_change!")
41 42 end
42 43
43 44 define_method(key) do
  45 + send("#{store_attribute}=", {}) unless send(store_attribute).is_a?(Hash)
44 46 send(store_attribute)[key]
45 47 end
46 48 end
11 activerecord/test/cases/store_test.rb
@@ -4,7 +4,7 @@
4 4
5 5 class StoreTest < ActiveRecord::TestCase
6 6 setup do
7   - @john = Admin::User.create(:name => 'John Doe', :color => 'black')
  7 + @john = Admin::User.create(:name => 'John Doe', :color => 'black', :remember_login => true)
8 8 end
9 9
10 10 test "reading store attributes through accessors" do
@@ -31,4 +31,13 @@ class StoreTest < ActiveRecord::TestCase
31 31 @john.color = 'red'
32 32 assert @john.settings_changed?
33 33 end
  34 +
  35 + test "object initialization with not nullable column" do
  36 + assert_equal true, @john.remember_login
  37 + end
  38 +
  39 + test "writing with not nullable column" do
  40 + @john.remember_login = false
  41 + assert_equal false, @john.remember_login
  42 + end
34 43 end
1  activerecord/test/models/admin/user.rb
... ... @@ -1,4 +1,5 @@
1 1 class Admin::User < ActiveRecord::Base
2 2 belongs_to :account
3 3 store :settings, :accessors => [ :color, :homepage ]
  4 + store :preferences, :accessors => [ :remember_login ]
4 5 end
3  activerecord/test/schema/schema.rb
@@ -37,7 +37,8 @@ def create_table(*args, &block)
37 37
38 38 create_table :admin_users, :force => true do |t|
39 39 t.string :name
40   - t.text :settings
  40 + t.text :settings, :null => true
  41 + t.text :preferences, :null => false, :default => ""
41 42 t.references :account
42 43 end
43 44

0 comments on commit 39b7036

Please sign in to comment.
Something went wrong with that request. Please try again.