mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Update prefix and allow suffix options for store accessors
This commit is contained in:
parent
2f76256127
commit
e0bc5b4da1
4 changed files with 59 additions and 5 deletions
|
@ -33,12 +33,16 @@ module ActiveRecord
|
|||
# store :settings, accessors: [ :color, :homepage ], coder: JSON
|
||||
# store :parent, accessors: [ :name ], coder: JSON, prefix: true
|
||||
# store :spouse, accessors: [ :name ], coder: JSON, prefix: :partner
|
||||
# store :settings, accessors: [ :two_factor_auth ], suffix: true
|
||||
# store :settings, accessors: [ :login_retry ], suffix: :config
|
||||
# end
|
||||
#
|
||||
# u = User.new(color: 'black', homepage: '37signals.com', parent_name: 'Mary', partner_name: 'Lily')
|
||||
# u.color # Accessor stored attribute
|
||||
# u.parent_name # Accessor stored attribute with prefix
|
||||
# u.partner_name # Accessor stored attribute with custom prefix
|
||||
# u.two_factor_auth_settings # Accessor stored attribute with suffix
|
||||
# u.login_retry_config # Accessor stored attribute with custom suffix
|
||||
# u.settings[:country] = 'Denmark' # Any attribute, even if not specified with an accessor
|
||||
#
|
||||
# # There is no difference between strings and symbols for accessing custom attributes
|
||||
|
@ -49,11 +53,12 @@ module ActiveRecord
|
|||
# class SuperUser < User
|
||||
# store_accessor :settings, :privileges, :servants
|
||||
# store_accessor :parent, :birthday, prefix: true
|
||||
# store_accessor :settings, :secret_question, suffix: :config
|
||||
# end
|
||||
#
|
||||
# The stored attribute names can be retrieved using {.stored_attributes}[rdoc-ref:rdoc-ref:ClassMethods#stored_attributes].
|
||||
#
|
||||
# User.stored_attributes[:settings] # [:color, :homepage]
|
||||
# User.stored_attributes[:settings] # [:color, :homepage, :two_factor_auth, :login_retry]
|
||||
#
|
||||
# == Overwriting default accessors
|
||||
#
|
||||
|
@ -86,10 +91,10 @@ module ActiveRecord
|
|||
module ClassMethods
|
||||
def store(store_attribute, options = {})
|
||||
serialize store_attribute, IndifferentCoder.new(store_attribute, options[:coder])
|
||||
store_accessor(store_attribute, options[:accessors], prefix: options[:prefix]) if options.has_key? :accessors
|
||||
store_accessor(store_attribute, options[:accessors], options.slice(:prefix, :suffix)) if options.has_key? :accessors
|
||||
end
|
||||
|
||||
def store_accessor(store_attribute, *keys, prefix: nil)
|
||||
def store_accessor(store_attribute, *keys, prefix: nil, suffix: nil)
|
||||
keys = keys.flatten
|
||||
|
||||
accessor_prefix =
|
||||
|
@ -101,14 +106,25 @@ module ActiveRecord
|
|||
else
|
||||
""
|
||||
end
|
||||
accessor_suffix =
|
||||
case suffix
|
||||
when String, Symbol
|
||||
"_#{suffix}"
|
||||
when TrueClass
|
||||
"_#{store_attribute}"
|
||||
else
|
||||
""
|
||||
end
|
||||
|
||||
_store_accessors_module.module_eval do
|
||||
keys.each do |key|
|
||||
define_method("#{accessor_prefix}#{key}=") do |value|
|
||||
accessor_key = "#{accessor_prefix}#{key}#{accessor_suffix}"
|
||||
|
||||
define_method("#{accessor_key}=") do |value|
|
||||
write_store_attribute(store_attribute, key, value)
|
||||
end
|
||||
|
||||
define_method("#{accessor_prefix}#{key}") do
|
||||
define_method(accessor_key) do
|
||||
read_store_attribute(store_attribute, key)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -214,4 +214,38 @@ class StoreTest < ActiveRecord::TestCase
|
|||
second_dump = YAML.dump(loaded)
|
||||
assert_equal @john, YAML.load(second_dump)
|
||||
end
|
||||
|
||||
test "read store attributes through accessors with default suffix" do
|
||||
@john.configs[:two_factor_auth] = true
|
||||
assert_equal true, @john.two_factor_auth_configs
|
||||
end
|
||||
|
||||
test "write store attributes through accessors with default suffix" do
|
||||
@john.two_factor_auth_configs = false
|
||||
assert_equal false, @john.configs[:two_factor_auth]
|
||||
end
|
||||
|
||||
test "read store attributes through accessors with custom suffix" do
|
||||
@john.configs[:login_retry] = 3
|
||||
assert_equal 3, @john.login_retry_config
|
||||
end
|
||||
|
||||
test "write store attributes through accessors with custom suffix" do
|
||||
@john.login_retry_config = 5
|
||||
assert_equal 5, @john.configs[:login_retry]
|
||||
end
|
||||
|
||||
test "read accessor without pre/suffix in the same store as other pre/suffixed accessors still works" do
|
||||
@john.configs[:secret_question] = "What is your high school?"
|
||||
assert_equal "What is your high school?", @john.secret_question
|
||||
end
|
||||
|
||||
test "write accessor without pre/suffix in the same store as other pre/suffixed accessors still works" do
|
||||
@john.secret_question = "What was the Rails version when you first worked on it?"
|
||||
assert_equal "What was the Rails version when you first worked on it?", @john.configs[:secret_question]
|
||||
end
|
||||
|
||||
test "prefix/suffix do not affect stored attributes" do
|
||||
assert_equal [:secret_question, :two_factor_auth, :login_retry], Admin::User.stored_attributes[:configs]
|
||||
end
|
||||
end
|
||||
|
|
|
@ -22,6 +22,9 @@ class Admin::User < ActiveRecord::Base
|
|||
store :parent, accessors: [:birthday, :name], prefix: true
|
||||
store :spouse, accessors: [:birthday], prefix: :partner
|
||||
store_accessor :spouse, :name, prefix: :partner
|
||||
store :configs, accessors: [ :secret_question ]
|
||||
store :configs, accessors: [ :two_factor_auth ], suffix: true
|
||||
store_accessor :configs, :login_retry, suffix: :config
|
||||
store :preferences, accessors: [ :remember_login ]
|
||||
store :json_data, accessors: [ :height, :weight ], coder: Coder.new
|
||||
store :json_data_empty, accessors: [ :is_a_good_guy ], coder: Coder.new
|
||||
|
|
|
@ -23,6 +23,7 @@ ActiveRecord::Schema.define do
|
|||
t.string :settings, null: true, limit: 1024
|
||||
t.string :parent, null: true, limit: 1024
|
||||
t.string :spouse, null: true, limit: 1024
|
||||
t.string :configs, null: true, limit: 1024
|
||||
# MySQL does not allow default values for blobs. Fake it out with a
|
||||
# big varchar below.
|
||||
t.string :preferences, null: true, default: "", limit: 1024
|
||||
|
|
Loading…
Reference in a new issue