aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2012-05-13 15:02:31 -0700
committerRafael Mendonça França <rafaelmfranca@gmail.com>2012-05-13 15:02:31 -0700
commitf443e119cd448b78bcb1584d7a9bd8b5775b5577 (patch)
tree417f2008e72dd69dc20dd34b51b7414d4cb80138 /activerecord/test
parentd3c83a4e8ef02560cdc5d1e22938c4fa1919355b (diff)
parent3c0bf0435cfbc7a349faec5e1b5631e506b5dc70 (diff)
downloadrails-f443e119cd448b78bcb1584d7a9bd8b5775b5577.tar.gz
rails-f443e119cd448b78bcb1584d7a9bd8b5775b5577.tar.bz2
rails-f443e119cd448b78bcb1584d7a9bd8b5775b5577.zip
Merge pull request #5807 from Antiarchitect/store-improvement
Custom coders support for ActiveRecord::Store.
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/cases/store_test.rb36
-rw-r--r--activerecord/test/models/admin/user.rb2
-rw-r--r--activerecord/test/schema/schema.rb2
3 files changed, 39 insertions, 1 deletions
diff --git a/activerecord/test/cases/store_test.rb b/activerecord/test/cases/store_test.rb
index 40520d6da2..e1d0f1f799 100644
--- a/activerecord/test/cases/store_test.rb
+++ b/activerecord/test/cases/store_test.rb
@@ -4,7 +4,7 @@ require 'models/admin/user'
class StoreTest < ActiveRecord::TestCase
setup do
- @john = Admin::User.create(:name => 'John Doe', :color => 'black', :remember_login => true)
+ @john = Admin::User.create(:name => 'John Doe', :color => 'black', :remember_login => true, :height => 'tall', :is_a_good_guy => true)
end
test "reading store attributes through accessors" do
@@ -40,4 +40,38 @@ class StoreTest < ActiveRecord::TestCase
@john.remember_login = false
assert_equal false, @john.remember_login
end
+
+ test "reading store attributes through accessors encoded with JSON" do
+ assert_equal 'tall', @john.height
+ assert_nil @john.weight
+ end
+
+ test "writing store attributes through accessors encoded with JSON" do
+ @john.height = 'short'
+ @john.weight = 'heavy'
+
+ assert_equal 'short', @john.height
+ assert_equal 'heavy', @john.weight
+ end
+
+ test "accessing attributes not exposed by accessors encoded with JSON" do
+ @john.json_data['somestuff'] = 'somecoolstuff'
+ @john.save
+
+ assert_equal 'somecoolstuff', @john.reload.json_data['somestuff']
+ end
+
+ test "updating the store will mark it as changed encoded with JSON" do
+ @john.height = 'short'
+ assert @john.json_data_changed?
+ end
+
+ test "object initialization with not nullable column encoded with JSON" do
+ assert_equal true, @john.is_a_good_guy
+ end
+
+ test "writing with not nullable column encoded with JSON" do
+ @john.is_a_good_guy = false
+ assert_equal false, @john.is_a_good_guy
+ end
end
diff --git a/activerecord/test/models/admin/user.rb b/activerecord/test/models/admin/user.rb
index d0e628bd50..ad30039304 100644
--- a/activerecord/test/models/admin/user.rb
+++ b/activerecord/test/models/admin/user.rb
@@ -2,4 +2,6 @@ class Admin::User < ActiveRecord::Base
belongs_to :account
store :settings, :accessors => [ :color, :homepage ]
store :preferences, :accessors => [ :remember_login ]
+ store :json_data, :accessors => [ :height, :weight ], :coder => JSON
+ store :json_data_empty, :accessors => [ :is_a_good_guy ], :coder => JSON
end
diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb
index cef08cd99c..464fdba555 100644
--- a/activerecord/test/schema/schema.rb
+++ b/activerecord/test/schema/schema.rb
@@ -41,6 +41,8 @@ ActiveRecord::Schema.define do
# MySQL does not allow default values for blobs. Fake it out with a
# big varchar below.
t.string :preferences, :null => false, :default => '', :limit => 1024
+ t.text :json_data, :null => true
+ t.text :json_data_empty, :null => false, :default => ""
t.references :account
end