提交 8593964d 编写于 作者: C Carlos Antonio da Silva

Refactor and use class_attribute

上级 757f7231
## Rails 4.0.0 (unreleased) ##
* Added a `stored_attributes` hash which contains the attributes stored
using ActiveRecord::Store. This allows you to retrieve the list of
attributes you've defined.
* Added `stored_attributes` hash which contains the attributes stored using
ActiveRecord::Store. This allows you to retrieve the list of attributes
you've defined.
*Joost Baaij*
class User < ActiveRecord::Base
store :settings, accessors: [:color, :homepage]
end
User.stored_attributes[:settings] # [:color, :homepage]
*Joost Baaij & Carlos Antonio da Silva*
* `composed_of` was removed. You'll have to write your own accessor
and mutator methods if you'd like to use value objects to represent some
......
require 'active_support/concern'
require 'active_support/core_ext/hash/indifferent_access'
require 'active_support/core_ext/class/attribute'
module ActiveRecord
# Store gives you a thin wrapper around serialize for the purpose of storing hashes in a single column.
......@@ -42,7 +43,7 @@ module Store
extend ActiveSupport::Concern
included do
config_attribute :stored_attributes
class_attribute :stored_attributes
self.stored_attributes = {}
end
......@@ -53,7 +54,8 @@ def store(store_attribute, options = {})
end
def store_accessor(store_attribute, *keys)
keys.flatten.each do |key|
keys = keys.flatten
keys.each do |key|
define_method("#{key}=") do |value|
initialize_store_attribute(store_attribute)
send(store_attribute)[key] = value
......@@ -66,7 +68,7 @@ def store_accessor(store_attribute, *keys)
end
end
self.stored_attributes[store_attribute] = keys.flatten
self.stored_attributes[store_attribute] = keys
end
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册