Skip to content

Commit

Permalink
Use instance_accessor: false instead of instance_writer.
Browse files Browse the repository at this point in the history
  • Loading branch information
kennyj committed Aug 21, 2012
1 parent 55d943c commit 57bef99
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 11 deletions.
4 changes: 4 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,5 +1,9 @@
## Rails 4.0.0 (unreleased) ## ## Rails 4.0.0 (unreleased) ##


* `serialized_attributes` and `_attr_readonly` become class method only. Instance reader methods are deprecated.

*kennyj*

* Round usec when comparing timestamp attributes in the dirty tracking. * Round usec when comparing timestamp attributes in the dirty tracking.
Fixes #6975. Fixes #6975.


Expand Down
Expand Up @@ -6,7 +6,7 @@ module Serialization
included do included do
# Returns a hash of all the attributes that have been specified for serialization as # Returns a hash of all the attributes that have been specified for serialization as
# keys and their class restriction as values. # keys and their class restriction as values.
class_attribute :serialized_attributes, instance_writer: false class_attribute :serialized_attributes, instance_accessor: false
self.serialized_attributes = {} self.serialized_attributes = {}
end end


Expand Down Expand Up @@ -41,6 +41,11 @@ def serialize(attr_name, class_name = Object)
end end
end end


def serialized_attributes
ActiveSupport::Deprecation.warn("Instance level serialized_attributes method is deprecated, please use class level method.")
defined?(@serialized_attributes) ? @serialized_attributes : self.class.serialized_attributes
end

class Type # :nodoc: class Type # :nodoc:
def initialize(column) def initialize(column)
@column = column @column = column
Expand Down Expand Up @@ -114,7 +119,7 @@ def type_cast_attribute_for_write(column, value)
end end


def read_attribute_before_type_cast(attr_name) def read_attribute_before_type_cast(attr_name)
if serialized_attributes.include?(attr_name) if self.class.serialized_attributes.include?(attr_name)
super.unserialized_value super.unserialized_value
else else
super super
Expand Down
7 changes: 6 additions & 1 deletion activerecord/lib/active_record/readonly_attributes.rb
Expand Up @@ -4,7 +4,7 @@ module ReadonlyAttributes
extend ActiveSupport::Concern extend ActiveSupport::Concern


included do included do
class_attribute :_attr_readonly, instance_writer: false class_attribute :_attr_readonly, instance_accessor: false
self._attr_readonly = [] self._attr_readonly = []
end end


Expand All @@ -20,5 +20,10 @@ def readonly_attributes
self._attr_readonly self._attr_readonly
end end
end end

def _attr_readonly
ActiveSupport::Deprecation.warn("Instance level _attr_readonly method is deprecated, please use class level method.")
defined?(@_attr_readonly) ? @_attr_readonly : self.class._attr_readonly
end
end end
end end
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/store.rb
Expand Up @@ -41,7 +41,7 @@ module Store
extend ActiveSupport::Concern extend ActiveSupport::Concern


included do included do
class_attribute :stored_attributes, instance_writer: false class_attribute :stored_attributes, instance_accessor: false
self.stored_attributes = {} self.stored_attributes = {}
end end


Expand Down
6 changes: 6 additions & 0 deletions activerecord/test/cases/base_test.rb
Expand Up @@ -604,6 +604,12 @@ def test_readonly_attributes
assert_equal "changed", post.body assert_equal "changed", post.body
end end


def test_attr_readonly_is_class_level_setting
post = ReadonlyTitlePost.new
assert_raise(NoMethodError) { post._attr_readonly = [:title] }
assert_deprecated { post._attr_readonly }
end

def test_non_valid_identifier_column_name def test_non_valid_identifier_column_name
weird = Weird.create('a$b' => 'value') weird = Weird.create('a$b' => 'value')
weird.reload weird.reload
Expand Down
7 changes: 3 additions & 4 deletions activerecord/test/cases/serialization_test.rb
Expand Up @@ -53,9 +53,8 @@ def test_serialize_should_allow_attribute_except_filtering
end end


def test_serialized_attributes_are_class_level_settings def test_serialized_attributes_are_class_level_settings
assert_raise NoMethodError do topic = Topic.new
topic = Topic.new assert_raise(NoMethodError) { topic.serialized_attributes = [] }
topic.serialized_attributes = [] assert_deprecated { topic.serialized_attributes }
end
end end
end end
5 changes: 2 additions & 3 deletions activerecord/test/cases/store_test.rb
Expand Up @@ -122,9 +122,8 @@ class StoreTest < ActiveRecord::TestCase
end end


test "stores_attributes are class level settings" do test "stores_attributes are class level settings" do
assert_raise NoMethodError do assert_raise(NoMethodError) { @john.stored_attributes = Hash.new }
@john.stored_attributes = {} assert_raise(NoMethodError) { @john.stored_attributes }
end
end end


end end
2 changes: 2 additions & 0 deletions guides/source/upgrading_ruby_on_rails.textile
Expand Up @@ -44,6 +44,8 @@ The <tt>delete</tt> method in collection associations can now receive <tt>Fixnum


Rails 4.0 has changed how orders get stacked in +ActiveRecord::Relation+. In previous versions of rails new order was applied after previous defined order. But this is no long true. Check "ActiveRecord Query guide":active_record_querying.html#ordering for more information. Rails 4.0 has changed how orders get stacked in +ActiveRecord::Relation+. In previous versions of rails new order was applied after previous defined order. But this is no long true. Check "ActiveRecord Query guide":active_record_querying.html#ordering for more information.


Rails 4.0 has changed <tt>serialized_attributes</tt> and <tt>_attr_readonly</tt> to class methods only. Now you shouldn't use instance methods, it's deprecated. You must change them, e.g. <tt>self.serialized_attributes</tt> to <tt>self.class.serialized_attributes</tt>.

h4(#active_model4_0). Active Model h4(#active_model4_0). Active Model


Rails 4.0 has changed how errors attach with the <tt>ActiveModel::Validations::ConfirmationValidator</tt>. Now when confirmation validations fail the error will be attached to <tt>:#{attribute}_confirmation</tt> instead of <tt>attribute</tt>. Rails 4.0 has changed how errors attach with the <tt>ActiveModel::Validations::ConfirmationValidator</tt>. Now when confirmation validations fail the error will be attached to <tt>:#{attribute}_confirmation</tt> instead of <tt>attribute</tt>.
Expand Down

0 comments on commit 57bef99

Please sign in to comment.