Permalink
Browse files

Merge pull request #5412 from tilsammans/stored_attributes

Added `stored_attributes` hash which contains the attributes stored using
ActiveRecord::Store. This allows you to retrieve the list of attributes
you've defined.

    class User < ActiveRecord::Base
      store :settings, accessors: [:color, :homepage]
    end

    User.stored_attributes[:settings] # [:color, :homepage]
  • Loading branch information...
2 parents df2104e + 8593964 commit 69f19b292ad4b228f2bc5f89732a8ef3b704fab6 @carlosantoniodasilva carlosantoniodasilva committed Jun 19, 2012
Showing with 32 additions and 2 deletions.
  1. +12 −0 activerecord/CHANGELOG.md
  2. +15 −1 activerecord/lib/active_record/store.rb
  3. +5 −1 activerecord/test/cases/store_test.rb
View
@@ -1,5 +1,17 @@
## Rails 4.0.0 (unreleased) ##
+* Added `stored_attributes` hash which contains the attributes stored using
+ ActiveRecord::Store. This allows you to retrieve the list of attributes
+ you've defined.
+
+ 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
portion of your models. So, instead of:
@@ -1,4 +1,6 @@
+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.
@@ -33,17 +35,27 @@ module ActiveRecord
# class SuperUser < User
# store_accessor :settings, :privileges, :servants
# end
+ #
+ # The stored attribute names can be retrieved using +stored_attributes+.
+ #
+ # User.stored_attributes[:settings] # [:color, :homepage]
module Store
extend ActiveSupport::Concern
+ included do
+ class_attribute :stored_attributes
+ self.stored_attributes = {}
+ end
+
module ClassMethods
def store(store_attribute, options = {})
serialize store_attribute, IndifferentCoder.new(options[:coder])
store_accessor(store_attribute, options[:accessors]) if options.has_key? :accessors
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
@@ -55,6 +67,8 @@ def store_accessor(store_attribute, *keys)
send(store_attribute)[key]
end
end
+
+ self.stored_attributes[store_attribute] = keys
end
end
@@ -13,7 +13,7 @@ class StoreTest < ActiveRecord::TestCase
assert_equal 'black', @john.color
assert_nil @john.homepage
end
-
+
test "writing store attributes through accessors" do
@john.color = 'red'
@john.homepage = '37signals.com'
@@ -111,4 +111,8 @@ class StoreTest < ActiveRecord::TestCase
@john.is_a_good_guy = false
assert_equal false, @john.is_a_good_guy
end
+
+ test "stored attributes are returned" do
+ assert_equal [:color, :homepage], Admin::User.stored_attributes[:settings]
+ end
end

0 comments on commit 69f19b2

Please sign in to comment.