提交 46873aed 编写于 作者: M Matt Jones

refactor store_accessor

上级 01ef633f
## Rails 4.0.0 (unreleased) ##
* You can now override the generated accessor methods for stored attributes
and reuse the original behavior with `read_store_attribute` and `write_store_attribute`,
which are counterparts to `read_attribute` and `write_attribute`.
*Matt Jones*
* Accept belongs_to (including polymorphic) association keys in queries
The following queries are now equivalent:
......@@ -38,7 +44,7 @@
*Jan Bernacki*
* Fix bug when call `store_accessor` multiple times.
* Fix bug when calling `store_accessor` multiple times.
Fixes #7532.
*Matt Jones*
......
......@@ -37,6 +37,28 @@ module ActiveRecord
# The stored attribute names can be retrieved using +stored_attributes+.
#
# User.stored_attributes[:settings] # [:color, :homepage]
#
# == Overwriting default accessors
#
# All stored values are automatically available through accessors on the Active Record
# object, but sometimes you want to specialize this behavior. This can be done by overwriting
# the default accessors (using the same name as the attribute) and calling
# <tt>read_store_attribute(store_attribute_name, attr_name)</tt> and
# <tt>write_store_attribute(store_attribute_name, attr_name, value)</tt> to actually
# change things.
#
# class Song < ActiveRecord::Base
# # Uses a stored integer to hold the volume adjustment of the song
# store :settings, accessors: [:volume_adjustment]
#
# def volume_adjustment=(decibels)
# write_store_attribute(:settings, :volume_adjustment, decibels.to_i)
# end
#
# def volume_adjustment
# read_store_attribute(:settings, :volume_adjustment).to_i
# end
# end
module Store
extend ActiveSupport::Concern
......@@ -55,15 +77,11 @@ def store_accessor(store_attribute, *keys)
keys = keys.flatten
keys.each do |key|
define_method("#{key}=") do |value|
attribute = initialize_store_attribute(store_attribute)
if value != attribute[key]
send :"#{store_attribute}_will_change!"
attribute[key] = value
end
write_store_attribute(store_attribute, key, value)
end
define_method(key) do
initialize_store_attribute(store_attribute)[key]
read_store_attribute(store_attribute, key)
end
end
......@@ -72,6 +90,20 @@ def store_accessor(store_attribute, *keys)
end
end
protected
def read_store_attribute(store_attribute, key)
attribute = initialize_store_attribute(store_attribute)
attribute[key]
end
def write_store_attribute(store_attribute, key, value)
attribute = initialize_store_attribute(store_attribute)
if value != attribute[key]
send :"#{store_attribute}_will_change!"
attribute[key] = value
end
end
private
def initialize_store_attribute(store_attribute)
attribute = send(store_attribute)
......
......@@ -29,6 +29,12 @@ class StoreTest < ActiveRecord::TestCase
assert_equal 'graeters', @john.reload.settings[:icecream]
end
test "overriding a read accessor" do
@john.settings[:phone_number] = '1234567890'
assert_equal '(123) 456-7890', @john.phone_number
end
test "updating the store will mark it as changed" do
@john.color = 'red'
assert @john.settings_changed?
......@@ -54,6 +60,12 @@ class StoreTest < ActiveRecord::TestCase
assert_equal false, @john.remember_login
end
test "overriding a write accessor" do
@john.phone_number = '(123) 456-7890'
assert_equal '1234567890', @john.settings[:phone_number]
end
test "preserve store attributes data in HashWithIndifferentAccess format without any conversion" do
@john.json_data = HashWithIndifferentAccess.new(:height => 'tall', 'weight' => 'heavy')
@john.height = 'low'
......@@ -124,7 +136,7 @@ class StoreTest < ActiveRecord::TestCase
end
test "all stored attributes are returned" do
assert_equal [:color, :homepage, :favorite_food], Admin::User.stored_attributes[:settings]
assert_equal [:color, :homepage, :favorite_food, :phone_number], Admin::User.stored_attributes[:settings]
end
test "stores_attributes are class level settings" do
......
class Admin::User < ActiveRecord::Base
belongs_to :account
store :settings, :accessors => [ :color, :homepage ]
store_accessor :settings, :favorite_food
store_accessor :settings, :favorite_food, :phone_number
store :preferences, :accessors => [ :remember_login ]
store :json_data, :accessors => [ :height, :weight ], :coder => JSON
store :json_data_empty, :accessors => [ :is_a_good_guy ], :coder => JSON
def phone_number
read_store_attribute(:settings, :phone_number).gsub(/(\d{3})(\d{3})(\d{4})/,'(\1) \2-\3')
end
def phone_number=(value)
write_store_attribute(:settings, :phone_number, value && value.gsub(/[^\d]/,''))
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册