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 :settings, accessors: [ :color, :homepage ], coder: JSON
|
||||||
# store :parent, accessors: [ :name ], coder: JSON, prefix: true
|
# store :parent, accessors: [ :name ], coder: JSON, prefix: true
|
||||||
# store :spouse, accessors: [ :name ], coder: JSON, prefix: :partner
|
# store :spouse, accessors: [ :name ], coder: JSON, prefix: :partner
|
||||||
|
# store :settings, accessors: [ :two_factor_auth ], suffix: true
|
||||||
|
# store :settings, accessors: [ :login_retry ], suffix: :config
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
# u = User.new(color: 'black', homepage: '37signals.com', parent_name: 'Mary', partner_name: 'Lily')
|
# u = User.new(color: 'black', homepage: '37signals.com', parent_name: 'Mary', partner_name: 'Lily')
|
||||||
# u.color # Accessor stored attribute
|
# u.color # Accessor stored attribute
|
||||||
# u.parent_name # Accessor stored attribute with prefix
|
# u.parent_name # Accessor stored attribute with prefix
|
||||||
# u.partner_name # Accessor stored attribute with custom 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
|
# 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
|
# # There is no difference between strings and symbols for accessing custom attributes
|
||||||
|
@ -49,11 +53,12 @@ module ActiveRecord
|
||||||
# class SuperUser < User
|
# class SuperUser < User
|
||||||
# store_accessor :settings, :privileges, :servants
|
# store_accessor :settings, :privileges, :servants
|
||||||
# store_accessor :parent, :birthday, prefix: true
|
# store_accessor :parent, :birthday, prefix: true
|
||||||
|
# store_accessor :settings, :secret_question, suffix: :config
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
# The stored attribute names can be retrieved using {.stored_attributes}[rdoc-ref:rdoc-ref:ClassMethods#stored_attributes].
|
# 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
|
# == Overwriting default accessors
|
||||||
#
|
#
|
||||||
|
@ -86,10 +91,10 @@ module ActiveRecord
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
def store(store_attribute, options = {})
|
def store(store_attribute, options = {})
|
||||||
serialize store_attribute, IndifferentCoder.new(store_attribute, options[:coder])
|
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
|
end
|
||||||
|
|
||||||
def store_accessor(store_attribute, *keys, prefix: nil)
|
def store_accessor(store_attribute, *keys, prefix: nil, suffix: nil)
|
||||||
keys = keys.flatten
|
keys = keys.flatten
|
||||||
|
|
||||||
accessor_prefix =
|
accessor_prefix =
|
||||||
|
@ -101,14 +106,25 @@ module ActiveRecord
|
||||||
else
|
else
|
||||||
""
|
""
|
||||||
end
|
end
|
||||||
|
accessor_suffix =
|
||||||
|
case suffix
|
||||||
|
when String, Symbol
|
||||||
|
"_#{suffix}"
|
||||||
|
when TrueClass
|
||||||
|
"_#{store_attribute}"
|
||||||
|
else
|
||||||
|
""
|
||||||
|
end
|
||||||
|
|
||||||
_store_accessors_module.module_eval do
|
_store_accessors_module.module_eval do
|
||||||
keys.each do |key|
|
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)
|
write_store_attribute(store_attribute, key, value)
|
||||||
end
|
end
|
||||||
|
|
||||||
define_method("#{accessor_prefix}#{key}") do
|
define_method(accessor_key) do
|
||||||
read_store_attribute(store_attribute, key)
|
read_store_attribute(store_attribute, key)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -214,4 +214,38 @@ class StoreTest < ActiveRecord::TestCase
|
||||||
second_dump = YAML.dump(loaded)
|
second_dump = YAML.dump(loaded)
|
||||||
assert_equal @john, YAML.load(second_dump)
|
assert_equal @john, YAML.load(second_dump)
|
||||||
end
|
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
|
end
|
||||||
|
|
|
@ -22,6 +22,9 @@ class Admin::User < ActiveRecord::Base
|
||||||
store :parent, accessors: [:birthday, :name], prefix: true
|
store :parent, accessors: [:birthday, :name], prefix: true
|
||||||
store :spouse, accessors: [:birthday], prefix: :partner
|
store :spouse, accessors: [:birthday], prefix: :partner
|
||||||
store_accessor :spouse, :name, 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 :preferences, accessors: [ :remember_login ]
|
||||||
store :json_data, accessors: [ :height, :weight ], coder: Coder.new
|
store :json_data, accessors: [ :height, :weight ], coder: Coder.new
|
||||||
store :json_data_empty, accessors: [ :is_a_good_guy ], 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 :settings, null: true, limit: 1024
|
||||||
t.string :parent, null: true, limit: 1024
|
t.string :parent, null: true, limit: 1024
|
||||||
t.string :spouse, 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
|
# MySQL does not allow default values for blobs. Fake it out with a
|
||||||
# big varchar below.
|
# big varchar below.
|
||||||
t.string :preferences, null: true, default: "", limit: 1024
|
t.string :preferences, null: true, default: "", limit: 1024
|
||||||
|
|
Loading…
Reference in a new issue