Permalink
Browse files

Merge pull request #7299 from iHiD/3-2-stable-store

Ported PR #4856 to 3-2-stable.
  • Loading branch information...
2 parents 2e98e0f + 5c2fc69 commit 0f9a6a7c80a2beeaace875d8a9d89a164811d729 @rafaelfranca rafaelfranca committed Aug 9, 2012
View
5 activerecord/CHANGELOG.md
@@ -1,5 +1,10 @@
## Rails 3.2.9 (unreleased)
+* Allow store to work with an empty column.
+ Fix #4840.
+
+ *Jeremy Walker*
+
* Remove prepared statement from system query in postgresql adapter.
Fix #5872.
View
2 activerecord/lib/active_record/store.rb
@@ -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
View
11 activerecord/test/cases/store_test.rb
@@ -4,7 +4,7 @@
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
View
1 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
View
5 activerecord/test/schema/schema.rb
@@ -37,7 +37,10 @@ def create_table(*args, &block)
create_table :admin_users, :force => true do |t|
t.string :name
- t.text :settings
+ t.string :settings, :null => true, :limit => 1024
+ # MySQL does not allow default values for blobs. Fake it out with a
+ # big varchar below.
+ t.string :preferences, :null => true, :default => '', :limit => 1024
t.references :account
end

0 comments on commit 0f9a6a7

Please sign in to comment.