New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow prefix/suffix options for store accessors #29373

Merged
merged 1 commit into from Jun 13, 2018

Conversation

Projects
None yet
5 participants
@untidy-hair
Contributor

untidy-hair commented Jun 6, 2017

Summary

####Background:

class SampleModel < ApplicationRecord
  store :settings, accessors: [:color, :dependencies], coder: JSON
  has_many :dependencies
end

There was a situation where association has_many :dependencies existed
whereas we needed the same key "dependencies" (the value is array of dependency's names) as a part of "settings" JSON, too, so that the JSON can be consumed as it is from some other stuff.

We needed SampleModel#dependencies to return the association dependencies (and thus we wanted to add a method like setting_dependencies for JSON dependencies),
but the default behavior of SampleModel#dependencies above is to return JSON dependencies.

####Workarounds:

  1. remove_method, undef or alias, etc, to override
    => Didn't quite work well.
  2. has_many :my_dependencies, class: Dependency and have def dependencies; my_dependencies; end to override
    => Too much carefulness is necessary to cover other methods that has_many generates so I avoided this.
  3. Do not apply store (or store_accessor) for JSON dependencies, and add def setting_dependencies; settings[:dependencies]; end (and setter also).
    => This was the most viable option. But dependencies does not show up in SampleModel.stored_attributes (however you can add it).

But in the end, among stored accessors, only dependencies had setting_ prefix.
To be consistent and clearer, I ended up with adding setting_ for other stored accessors, too, in the similar way as No3 above. (setting_color/setting_dependencies for SampleModel above.)

This PR allows this prefix as an option out of the box.
The above was just one case but I think there will be some other cases, too, where prefix/suffix option is convenient and useful.
(Btw I referenced enum prefix for the implementation.)

store :settings, accessors: [ :two_factor_auth ], coder: JSON, _prefix: true
# => accessor will be model.settings_two_factor_auth
store_accessor :settings, :secret_question, _prefix: 'config'
# => accessor will be model.config_secret_question
store :settings, accessors: [ :login_retry ], _suffix: 'setting'
# => accessor will be model.login_retry_setting

Lastly, thank you for the great work, Rails team!

@rails-bot

This comment has been minimized.

rails-bot commented Jun 6, 2017

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @rafaelfranca (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

@gmcgibbon

Still relevant in 6.0.0.alpha. I think this is a good idea! See comments for some suggestions 👍

activerecord/lib/active_record/store.rb Outdated
def store_accessor(store_attribute, *keys)
keys = keys.flatten
def store_accessor(store_attribute, *args)
options = args.last.is_a?(Hash) ? args.pop : {}

This comment has been minimized.

@gmcgibbon

gmcgibbon May 21, 2018

Member

I think you can do extract_options! here instead.

activerecord/lib/active_record/store.rb Outdated
# # Optionally specify prefix/suffix to accessor with _prefix/_suffix option
# class User
# store :settings, accessors: [ :two_factor_auth ], coder: JSON, _prefix: true
# store_accessor :settings, :secret_question, _prefix: 'config'

This comment has been minimized.

@gmcgibbon

gmcgibbon May 21, 2018

Member

Should it be _prefix or prefix like in active support's delegate? I guess enum have underscored options but I'm not sure which is preferred.

This comment has been minimized.

@rafaelfranca

rafaelfranca May 22, 2018

Member

Without underscore, is the preffered way.

activerecord/lib/active_record/store.rb Outdated
# class User
# store :settings, accessors: [ :two_factor_auth ], coder: JSON, _prefix: true
# store_accessor :settings, :secret_question, _prefix: 'config'
# store_accessor :settings, :login_retry, _suffix: 'setting'

This comment has been minimized.

@gmcgibbon

gmcgibbon May 21, 2018

Member

Should the examples be using symbols instead of strings for _prefix and _suffix values?

@untidy-hair

This comment has been minimized.

Contributor

untidy-hair commented May 21, 2018

Thanks for the comments @gmcgibbon. Looks like another similar PR #32306 was created and merged before they had noticed this PR... @rafaelfranca (or someone), if you think it's worth adding suffix option or this PR can improve something related, I would be happy to contribute!

@gmcgibbon

This comment has been minimized.

Member

gmcgibbon commented May 22, 2018

Ah, thanks for pointing that out @untidy-hair. I'd say you're good to rebase and add the suffix option but if you want to wait for a core member's approval that's cool too 👍

@untidy-hair

This comment has been minimized.

Contributor

untidy-hair commented May 22, 2018

well, i'm not waiting for an approval but i just want to make sure they recognize this PR this time haha :P

@untidy-hair

This comment has been minimized.

Contributor

untidy-hair commented May 23, 2018

I'm going to work on this this weekend :)

@untidy-hair

This comment has been minimized.

Contributor

untidy-hair commented May 26, 2018

Rebased and updated per comments :) @gmcgibbon, @rafaelfranca please review again. Thanks!

@gmcgibbon

👍

@untidy-hair

This comment has been minimized.

Contributor

untidy-hair commented May 31, 2018

Thanks for the review @gmcgibbon! @rafaelfranca, can we merge this?

activerecord/lib/active_record/store.rb Outdated
@@ -123,7 +120,21 @@ def store_accessor(store_attribute, *keys, prefix: nil)
def _store_accessors_module # :nodoc:
@_store_accessors_module ||= begin
mod = Module.new
mod = Module.new do
def self.stored_accessor_key(store_attribute, key, options)

This comment has been minimized.

@kamipo

kamipo Jun 1, 2018

Member

Why do we define the helper method in each _store_accessors_modules?

This comment has been minimized.

@untidy-hair

untidy-hair Jun 1, 2018

Contributor

@kamipo, thanks for the review! The definition is there so that we can call the chunk of code in _store_accessors_module context, but it's true that each model that calls store/store_accessor defines the helper method inside the _store_accessors_module. Maybe I should just move that part of code directly into inside the module_eval block, unless you have any better suggestion. If you do, let me know :)

@untidy-hair

This comment has been minimized.

Contributor

untidy-hair commented Jun 5, 2018

@kamipo updated. please review again. Thanks!

activerecord/lib/active_record/store.rb Outdated
# 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, :secret_question]

This comment has been minimized.

@kamipo

kamipo Jun 9, 2018

Member

Looks like that isn't correct appeared a subclass' accessor :secret_question.

activerecord/lib/active_record/store.rb Outdated
@@ -86,29 +91,32 @@ class << self
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.delete(:accessors), options) if options.has_key? :accessors

This comment has been minimized.

@kamipo

kamipo Jun 9, 2018

Member
store_accessor(store_attribute, options[:accessors], options.slice(:prefix, :suffix)) if options.has_key? :accessors`
activerecord/lib/active_record/store.rb Outdated
else
""
end
def store_accessor(store_attribute, *args)

This comment has been minimized.

@kamipo

kamipo Jun 9, 2018

Member
def store_accessor(store_attribute, *keys, prefix: nil, suffix: nil)
activerecord/lib/active_record/store.rb Outdated
_store_accessors_module.module_eval do
keys.each do |key|
define_method("#{accessor_prefix}#{key}=") do |value|
prefix = if options[:prefix] == true

This comment has been minimized.

@kamipo

kamipo Jun 9, 2018

Member

The prefix/suffix is just enough calculated once, there is no reason to do for each keys.

@untidy-hair

This comment has been minimized.

Contributor

untidy-hair commented Jun 12, 2018

@kamipo thanks for the comments. updated.

@kamipo kamipo merged commit e0bc5b4 into rails:master Jun 13, 2018

2 checks passed

codeclimate All good!
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

kamipo added a commit that referenced this pull request Jun 13, 2018

Merge pull request #29373 from untidy-hair/store_accessor_enhance
Allow prefix/suffix options for store accessors
@kamipo

This comment has been minimized.

Member

kamipo commented Jun 13, 2018

Thanks!

@untidy-hair untidy-hair deleted the untidy-hair:store_accessor_enhance branch Jun 16, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment