Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Remember the stored attributes in a config attribute. #5412

Closed
wants to merge 14 commits into from

5 participants

@tilsammans

This allows you to retrieve the list of attributes you've defined.
Usable for e.g. selects in the view, or interators based on the
attributes you wish to store in the serialized column.

It follows the serialize API, with its serialized_attributes hash.

tilsammans added some commits
@tilsammans tilsammans Remember the stored attributes in a config attribute.
This allows you to retrieve the list of attributes you've defined.
Usable for e.g. selects in the view, or interators based on the
attributes you wish to store in the serialized column.
9e42171
@tilsammans tilsammans terser docu b5eaaab
@tilsammans tilsammans move this setter after the iterator 983e15e
@tilsammans tilsammans remove white line 88bdfee
@carlosantoniodasilva

My thoughts are there a constant with the list of attributes would probably do in this case. Anyway, in case it goes in, I'd have to ask you to squash your commits into one. Thanks!

/cc @rafaelfranca @josevalim

@tilsammans

Will gladly squash the commits. And I will wait a couple of days for the others to chime in with regards to the chosen solution.

@josevalim
Owner

Why are we using config_attribute instead of class_attribute?

@rafaelfranca
Owner

@josevalim good question. We have this code using config_attribute too.

I think this pull request will be useful. :+1:

@tilsammans could you try to use class_attibute in your code?

@josevalim
Owner
@jonleighton
Collaborator

@josevalim there is some documentation here

The point was to create something similar to class_attribute that can be used whether or not a module is included in a class or a module. I.e. the following wouldn't work:

module Foo
  included do
    class_attribute :foo
  end
end

module ActiveRecord::Model
  include Foo # we're not in a class, so class_attribute isn't defined
end

The reason that it's in AM is because some AM modules use it.

I'm happy to hear idea for other, nicer solutions :)

@josevalim
Owner

@jonleighton If you extend ActiveSupport::Concern in ActiveRecord::Model, the modules should be lazily included. This means class_attribute should work fine. At least this is how it was designed to work. /cc @wycats

@jonleighton
Collaborator

Hmm good point. I will check that. IIRC there was some issue with relying on that behaviour but I can't remember the details so it's worth revisiting.

@jonleighton
Collaborator

@josevalim I think part of it was that I wanted to make it possible to set config stuff on AR::Model and have it inherited down to classes that include it, and to AR::Base. But it's on my list to revisit this so I'll think about it some more then.

@carlosantoniodasilva

@tilsammans your proposed pull request is ok to be merged in, but it'd need minor changes before. Could you please:

  • Change from config_attribute to class_attribute, and make sure you add a require to it.
  • Add a Changelog entry.
  • Squash the commits into one.

Thanks!

carlosantoniodasilva and others added some commits
@carlosantoniodasilva carlosantoniodasilva Merge pull request #6327 from nashby/string-interpolation
remove backported string interpolation
bc4ebaf
@tilsammans tilsammans Remember the stored attributes in a config attribute.
This allows you to retrieve the list of attributes you've defined.
Usable for e.g. selects in the view, or interators based on the
attributes you wish to store in the serialized column.
26150ed
@tilsammans tilsammans terser docu fa64aa9
@tilsammans tilsammans move this setter after the iterator 8758b53
@tilsammans tilsammans remove white line 1e5b10f
@tilsammans tilsammans Use class_attribute and not config_attribute.
Also require the needed files.
3ff1102
@tilsammans tilsammans added changelog entry 9e0c0f1
@tilsammans tilsammans Revert "Use class_attribute and not config_attribute."
This reverts commit 3ff1102b229032040e07261e821f200d1ce8ef2f.
2766d09
@tilsammans tilsammans require active_support/concern f5379e6
@tilsammans

ActiveRecord::Store is a Module, not a Class, so class_attribute will not work. What to do?

@carlosantoniodasilva

@tilsammans well, ok for config_attribute for now, and thanks for the changelog entry.

Your pull request is not applying cleanly anymore, can you please rebase from current master, and squash your commits into one, so we can merge? Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 13, 2012
  1. @tilsammans

    Remember the stored attributes in a config attribute.

    tilsammans authored
    This allows you to retrieve the list of attributes you've defined.
    Usable for e.g. selects in the view, or interators based on the
    attributes you wish to store in the serialized column.
  2. @tilsammans

    terser docu

    tilsammans authored
  3. @tilsammans
  4. @tilsammans

    remove white line

    tilsammans authored
Commits on May 15, 2012
  1. @carlosantoniodasilva

    Merge pull request #6327 from nashby/string-interpolation

    carlosantoniodasilva authored
    remove backported string interpolation
  2. @tilsammans

    Remember the stored attributes in a config attribute.

    tilsammans authored
    This allows you to retrieve the list of attributes you've defined.
    Usable for e.g. selects in the view, or interators based on the
    attributes you wish to store in the serialized column.
  3. @tilsammans

    terser docu

    tilsammans authored
  4. @tilsammans
  5. @tilsammans

    remove white line

    tilsammans authored
  6. @tilsammans

    Use class_attribute and not config_attribute.

    tilsammans authored
    Also require the needed files.
  7. @tilsammans

    added changelog entry

    tilsammans authored
  8. @tilsammans

    Revert "Use class_attribute and not config_attribute."

    tilsammans authored
    This reverts commit 3ff1102b229032040e07261e821f200d1ce8ef2f.
  9. @tilsammans
  10. @tilsammans
This page is out of date. Refresh to see the latest.
View
4 activerecord/CHANGELOG.md
@@ -1,5 +1,9 @@
## 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 custom coders support for ActiveRecord::Store. Now you can set
your custom coder like this:
View
13 activerecord/lib/active_record/store.rb
@@ -1,3 +1,5 @@
+require 'active_support/concern'
+
module ActiveRecord
# Store gives you a thin wrapper around serialize for the purpose of storing hashes in a single column.
# It's like a simple key/value store backed into your record when you don't care about being able to
@@ -30,9 +32,18 @@ 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
+ config_attribute :stored_attributes
+ self.stored_attributes = {}
+ end
+
module ClassMethods
def store(store_attribute, options = {})
serialize store_attribute, options.fetch(:coder, Hash)
@@ -52,6 +63,8 @@ def store_accessor(store_attribute, *keys)
send(store_attribute)[key.to_s]
end
end
+
+ self.stored_attributes[store_attribute] = keys.flatten
end
end
end
View
6 activerecord/test/cases/store_test.rb
@@ -11,7 +11,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'
@@ -74,4 +74,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
Something went wrong with that request. Please try again.