Permalink
Browse files

Serialized attribute can be serialized in an integer column

Fix #8575

Conflicts:
	activerecord/CHANGELOG.md
	activerecord/lib/active_record/attribute_methods/serialization.rb
	activerecord/test/cases/serialized_attribute_test.rb
	activerecord/test/models/person.rb
  • Loading branch information...
1 parent bf16699 commit 885f59f6852cce670b48680fa0a1b6a4b0998291 @rafaelfranca rafaelfranca committed Dec 21, 2012
@@ -1,14 +1,19 @@
## Rails 3.2.10 (unreleased)
+* Serialized attributes can be serialized in integer columns.
+ Fix #8575.
+
+ *Rafael Mendonça França*
+
* Keep index names when using `alter_table` with sqlite3.
- Fix #3489
- Backport #8522
+ Fix #3489.
+ Backport #8522.
*Yves Senn*
* Recognize migrations placed in directories containing numbers and 'rb'.
- Fix #8492
- Backport of #8500
+ Fix #8492.
+ Backport of #8500.
*Yves Senn*
@@ -90,6 +90,14 @@ def type_cast_attribute_for_write(column, value)
end
end
+ def _field_changed?(attr, old, value)
+ if self.class.serialized_attributes.include?(attr)
+ old != value
+ else
+ super
+ end
+ end
+
def read_attribute_before_type_cast(attr_name)
if serialized_attributes.include?(attr_name)
super.unserialized_value
@@ -1457,6 +1457,14 @@ def test_serialize_attribute_via_select_method_when_time_zone_available
ActiveRecord::Base.time_zone_aware_attributes = false
end
+ def test_serialize_attribute_can_be_serialized_in_an_integer_column
+ insures = ['life']
+ person = SerializedPerson.new(:first_name => 'David', :insures => insures)
+ assert person.save
+ person = person.reload
+ assert_equal(insures, person.insures)
+ end
+
def test_quote
author_name = "\\ \001 ' \n \\n \""
topic = Topic.create('author_name' => author_name)
@@ -89,6 +89,27 @@ class TightDescendant < TightPerson; end
class RichPerson < ActiveRecord::Base
self.table_name = 'people'
-
+
has_and_belongs_to_many :treasures, :join_table => 'peoples_treasures'
end
+
+class Insure
+ INSURES = %W{life annuality}
+
+ def self.load mask
+ INSURES.select do |insure|
+ (1 << INSURES.index(insure)) & mask.to_i > 0
+ end
+ end
+
+ def self.dump insures
+ numbers = insures.map { |insure| INSURES.index(insure) }
+ numbers.inject(0) { |sum, n| sum + (1 << n) }
+ end
+end
+
+class SerializedPerson < ActiveRecord::Base
+ self.table_name = 'people'
+
+ serialize :insures, Insure
+end
@@ -479,9 +479,10 @@ def create_table(*args, &block)
t.integer :followers_count, :default => 0
t.references :best_friend
t.references :best_friend_of
+ t.integer :insures, :null => false, :default => 0
t.timestamps
end
-
+
create_table :peoples_treasures, :id => false, :force => true do |t|
t.column :rich_person_id, :integer
t.column :treasure_id, :integer

0 comments on commit 885f59f

Please sign in to comment.