-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Fix generators (@dgynn); load Railtie only with Rails, ensures caching configured #1352
Conversation
ActiveModel::Serializer.serializers_cache.clear | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if maybe this shouldn't be in an initializer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does, however, make it easier to conditionally disable including the module into action controller. e.g. https://github.com/rails-api/active_model_serializers/pull/592/files and #1295 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having the include ::ActionController::Serialization
in an initializer is good for being able to conditionally disable it based on whatever gets decided from #1295. And having the on_load(:action_controller)
is good for lazy-loading in case ActionController::Base
is never loaded. Those both seem good.
The to_prepare
block does not need to be in the initializer. It could be outside the initializers on the Railtie itself with just...
config.to_prepare do
ActiveModel::Serializer.serializers_cache.clear
end
Using an initializer, and a load hook (:action_controller
), and then running another load hook (:active_model_serializers
) to set the logger seems like overly complicated. Depending on load ordering, someone could have an initializer that explicitly sets ActiveModelSerializers.logger
but then gets overwritten.
Is there any other reason to have the :active_model_serializers
lazy-load hook? Could that logic just get folded in where the run_load_hooks
call is? Would you ever expect someone else to hook into that? And if so, would it only be after :action_controller has been loaded?
I'm still poking around a bit to see if I've got the order right on some of these.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using an initializer, and a load hook (:action_controller), and then running another load hook (:active_model_serializers) to set the logger seems like overly complicated. Depending on load ordering, someone could have an initializer that explicitly sets ActiveModelSerializers.logger but then gets overwritten.
Is there any other reason to have the :active_model_serializers lazy-load hook?
Since the logger isn't necessarily derived from the controller, I'm just being paranoid that someone might configure the logger before the controller was loaded. This makes sure the user has the final say.. but I could me misthinking that
cc @rails-api/ams |
sh(Gem.ruby, '-w', "-I#{dir}/lib", "-I#{dir}/test", file) or fail 'Failures' # rubocop:disable Style/AndOr | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see commit message, but basically this is adapted from ActiveJob Rakefile
b63f6ca
to
a7e16ad
Compare
- rvm: jruby-9000 | ||
env: JRUBY_OPTS='--server -Xcompile.invokedynamic=false -Xcli.debug=true --debug' | ||
- rvm: jruby-9.0.4.0 | ||
env: JRUBY_OPTS='-Xcompat.version=2.0 --server -Xcompile.invokedynamic=false -Xcli.debug=true --debug' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this change belong here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, from the jruby pr. Had a failute
B mobile phone
On Dec 29, 2015, at 4:15 PM, Lucas Hosseini [email protected] wrote:
In .travis.yml:
@@ -33,8 +33,8 @@ matrix:
include:
- rvm: 2.2
env: CAPTURE_STDERR=true
- rvm: jruby-9000
- env: JRUBY_OPTS='--server -Xcompile.invokedynamic=false -Xcli.debug=true --debug'
- rvm: jruby-9.0.4.0
- env: JRUBY_OPTS='-Xcompat.version=2.0 --server -Xcompile.invokedynamic=false -Xcli.debug=true --debug'
Does this change belong here?—
Reply to this email directly or view it on GitHub.
I'm not familiar enough with railties. Would you mind summarizing what this PR does? |
@beauby Updated the description from the commit messages #1352 (comment). Thanks for the nudge |
de0189c
to
a2f30a1
Compare
end | ||
|
||
if ENV['RAILS_VERSION'].to_s > '4.0' && RUBY_ENGINE == 'ruby' | ||
task default: [:isolated, :test, :rubocop] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isolated tests as set up here break on 4.0 and on JRuby
I've pushed up a PR to your branch that shows the changes I'd suggest. I did not update all the tests or change the comment about cache setup. Feel free to edit or disregard. :) BTW, you might be interested in a tool I'm working on that inspects Rails configuration info. Working on that is how I came across the generators issue initially. |
require 'active_model/serializable_resource' | ||
require 'active_model/serializer/version' | ||
class << self; attr_accessor :logger; end | ||
self.logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the mattr_accessor(:logger) { ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT)) }
is more legible than this one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe mattr_accessor does not support blocks in ActiveSupport 4.0.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See pr description. It didn't work
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
- use hook_for to hook in the serializer and remove load_generators - move generators so they can be found by rails - move to_prepare block to railtie config This commit improves the way the generators are loaded and how they extend the resource generator. * The initializer block has been changed to a `generator` block which is only executed when generators are needed. * The call to `app.load_generators` has been removed. There is no need to load *all* generators. * The `resource_override.rb` has been changed to use `hook_for` to extend the resource generator. * The directory for the generators has been moved to match the way Rails looks to load generators. With `hook_for` it would now be possible for a user to pass `--no-serializer` to skip that option. The `--serialize` option also now shows up in the generator help with `rails g resource --help`. These changes follow the way the Draper gem extends the `controller` generator.
Isolated Testing - Rake test inspired by https://github.com/rails/rails/blob/v5.0.0.beta1/activejob/Rakefile - Isolated unit inspired by - https://github.com/rails/rails/blob/v5.0.0.beta1/railties/test/isolation/abstract_unit.rb - https://github.com/rails/rails/blob/v5.0.0.beta1/activemodel/test/cases/railtie_test.rb Misc - Turns out `mattr_accessor(:logger) { ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT)) }` was always nil until the Railtie was loaded, since mattr_accessor block defaults don't really work on modules, but on the classes that include them. - Commented on important on Rails being required first for caching to work. - In isolated tests, `active_support/core_ext/object/with_options` is required.
this uses the configuration settings rather than calling ActionController::Base to get the configured values. after the "action_controller.set_configs" initializer has run, the configuration option holds the value Base will get when it loads.
@maurogeorge @dgynn updated |
LGTM. I only recommend take a look at the @dgynn comments #1352 (comment) #1352 (comment) that worked on this part of the project 👍 |
@maurogeorge I merged in his code... but I'm tired.. besides the value in re-reading the comments, was there more work to do that I missed? |
This looks good. I do think the comment in the Railtie about caching can now be removed but that should be verified. |
Can I give you commit to my fork to finish it? I'm on vacation Re comment there are tests for it B mobile phone
|
this also changes the action_controller load hook to not trigger loading of the ActionController::Base
I've updated the Railtie to combine the initializers and update the comment. I believe this initializer ordering should resolve the caching issue from #923 and also not trigger too-early loading of ActionController::Base. I'd say this is ready to be merged. |
@dgynn looks good. pr description still good summary? |
@@ -0,0 +1,36 @@ | |||
require 'rails/railtie' | |||
require 'action_controller' | |||
require 'action_controller/railtie' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This still necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The AMS railtie does require that the action_controller/railtie has been loaded so that Rails.configuration.action_controller
exists. But there isn't any normal that wouldn't have already been required. It should have been loaded by rails/all or even active_record/railtie (see the comment here https://github.com/rails/rails/blob/master/activerecord/lib/active_record/railtie.rb#L5).
Ok, anything than needs to be resolved before merging this? I'll add an issue with label 'pr followup' for what we've discussed |
@bf4 This looks good to merge to me. |
Fix generators (@dgynn); load Railtie only with Rails, ensures caching configured
merged. Let the heavens be glad; let the earth rejoice |
Fixing the generators (@dgynn)
Only load generators when needed
This commit improves the way the generators are loaded and how
they extend the resource generator.
generator
block which is only executed when generators are needed.app.load_generators
has been removed. There is no need to load all generators.resource_override.rb
has been changed to usehook_for
to extend the resource generator.With
hook_for
it would now be possible for a user to pass--no-serializer
to skip that option.The
--serialize
option also now shows up in the generator help withrails g resource --help
.These changes follow the way the Draper gem extends the
controller
generator.Putting Rails Code in the Railties (@bf4)
akak let people use AMS without requiring 'rails' or 'action{pack,view}'
Only call railtie when Rails is defined; assume controller loaded
Isolated Testing
Misc
Turns out
mattr_accessor(:logger) { ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT)) }
was always nil until the Railtie was loaded, since mattr_accessor
block defaults don't really work on modules, but on the classes that
include them.
Should resolve 0.10.0.rc3 outside of rails? #1389 where Railties was being too-eagerly loaded.
Commented on importance of Rails being required first for caching to
work. Ref Cache not working #923 (comment)
In isolated tests,
active_support/core_ext/object/with_options
is required.