-
Notifications
You must be signed in to change notification settings - Fork 197
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
Only initialise merit when action_controller_base is loaded #297
Only initialise merit when action_controller_base is loaded #297
Conversation
Previously merit was initialised when action_controller was loaded, which if you are using the Rails API & Web functions meant that it was loaded once, but could be loaded by the API OR Web bits being initialised. Thanks to the Rails PR listed below, you can now hook in to the web part only being initialised using action_controller_base rails/rails#28402
@truongnmt are you 100% sure you're running the code with my PR in it? I can't see how you'd get that message with the run_once parameter set - :-/ |
@jamesjefferies here you go, this is how I edit it, a bit hacky |
@truongnmt I can see spring is running there - have you stopped and started spring (or got rid of it entirely? ;)) |
@jamesjefferies how do you know I'm running spring :)) |
@truongnmt it was in the log message you attached - honestly, I find Spring more trouble than it's worth when working out what the heck is going on! @tute all the tests pass on Rails 5.2 for this PR - I'm suspecting it might break on other supported versions of Rails - not sure what your strategy is for older versions - I think run_once is only supported on later versions of Rails too.. |
CI is green, so supported versions of current version of merit are fixed! :-) Pretty sure this will go in as is, but will take a few days to review, understand the changes, and see if we can test it. Thank you both for collaborating and helping with this! 😃 |
If there are API only implementations of merit, this won't work either. What do you think of either adding a conditional to use the proper hook according to the api-only config, or making it configurable? Thanks! |
I'd have thought it unlikely for merit to be used without a web component,
but it's difficult to say isn't it! Maybe it should be something set in the
config in the initialiser..
…On 2 July 2018 at 22:15, Tute Costa ***@***.***> wrote:
If there are API only implementations of merit, this won't work either.
What do you think of either adding a conditional to use the proper hook
according to the api-only config, or making it configurable? Thanks!
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#297 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ABnqobtqblbXlYWZ8dxnIbcgLoDK8icGks5uCo1pgaJpZM4U8tYT>
.
--
James Jefferies
ShedCode Limited
Sheffield, South Yorkshire
Twitter: @shedcode @jamesjefferies
Email: [email protected]
Web: shedcode.co.uk jamesjefferies.com
Phone: 07595 252069
|
We can use def action_controller_hook
if Rails.application.config.api_only
# api one
else
# the current one
end
end If we do this we'll want to test it. A situation where this might be useful is in single page apps, where for instance an Ember.js connects to a Rails backend using |
That all makes sense to me - happy to do this, but can’t do it right now! Obviously will do a PR if/when I get chance
… On 3 Jul 2018, at 14:20, Tute Costa ***@***.***> wrote:
We can use Rails.application.config.api_only as a query, and automate it:
def action_controller_hook
if Rails.application.config.api_only
# api one
else
# the current one
end
end
If we do this we'll want to test it.
A situation where this might be useful is in single page apps, where for instance an Ember.js connects to a Rails backend using merit.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
|
Hmm how it's going then ... |
|
Thanks for the suggestion. That was the first attempt of a solution, but it produces the warning messages shown in #297 (comment) |
Hi, I am using the rails 5.2 api-only configuration. And I had a chance to test the pr that james created. But nothing is happening, as long as the point rules are defined. I may have misunderstood, but i guess it does not solve the api-only problem. If I can help you, I'm ready for it. But I don't have much knowledge about ruby. Thanks for pr and gem. |
the action controller API loading to instantiate, else it uses the standard action controller base. If this change isn't made, then merit doesn't get instantiated for API only applications
lib/merit.rb
Outdated
@@ -113,5 +115,9 @@ def extend_orm_with_has_merit | |||
Mongoid::Document.send :include, Merit | |||
end | |||
end | |||
|
|||
def action_controller_hook | |||
Rails.application.config.api_only ? :action_controller_api : :action_controller_base |
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.
Metrics/LineLength: Line is too long. [90/80]
lib/merit.rb
Outdated
@@ -96,6 +96,8 @@ class Engine < Rails::Engine | |||
end | |||
end | |||
|
|||
private |
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.
Layout/AccessModifierIndentation: Indent access modifiers like private.
@ccoeder please try again now - I've made the amend which @tute suggested. Existing tests pass, I've not written any specific tests for these use cases though... |
@jamesjefferies it's working, thank you! |
To ensure the tests all work in API mode
test/test_helper.rb
Outdated
require 'rails/test_help' | ||
require 'minitest/rails' | ||
require 'mocha/mini_test' | ||
require 'mocha/minitest' |
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.
Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
test/test_helper.rb
Outdated
if API_ONLY | ||
require File.expand_path('../dummy/config/environment_api_only.rb', __FILE__) | ||
else | ||
require File.expand_path('../dummy/config/environment.rb', __FILE__) |
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.
Style/ExpandPathArguments: Use expand_path('dummy/config/environment.rb', dir) instead of expand_path('../dummy/config/environment.rb', FILE).
Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
test/test_helper.rb
Outdated
@@ -18,10 +20,15 @@ | |||
SimpleCov.start 'rubygem' | |||
end | |||
|
|||
require File.expand_path('../dummy/config/environment.rb', __FILE__) | |||
if API_ONLY | |||
require File.expand_path('../dummy/config/environment_api_only.rb', __FILE__) |
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.
Style/ExpandPathArguments: Use expand_path('dummy/config/environment_api_only.rb', dir) instead of expand_path('../dummy/config/environment_api_only.rb', FILE).
Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
test/test_helper.rb
Outdated
@@ -2,6 +2,8 @@ | |||
ENV['RAILS_ENV'] = 'test' | |||
RUBYOPT="-w $RUBYOPT" | |||
|
|||
API_ONLY=ARGV.include?('-api-only=true') |
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.
Layout/SpaceAroundOperators: Surrounding space missing for operator =.
Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
@@ -0,0 +1,5 @@ | |||
# Load the rails application | |||
require File.expand_path('../application_api_only', __FILE__) |
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.
Style/ExpandPathArguments: Use expand_path('application_api_only', dir) instead of expand_path('../application_api_only', FILE).
Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
@@ -0,0 +1,5 @@ | |||
# Load the rails application |
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.
Style/FrozenStringLiteralComment: Missing magic comment # frozen_string_literal: true.
# Initialize configuration defaults for originally generated Rails version. | ||
config.load_defaults 5.2 | ||
|
||
# Settings in config/environments/* take precedence over those specified 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.
Metrics/LineLength: Line is too long. [82/80]
|
||
# Require the gems listed in Gemfile, including any gems | ||
# you've limited to :test, :development, or :production. | ||
Bundler.require#(*Rails.groups) |
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.
Layout/LeadingCommentSpace: Missing space after #.
Layout/SpaceBeforeComment: Put a space before an end-of-line comment.
@@ -0,0 +1,42 @@ | |||
require_relative 'boot' |
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.
Style/FrozenStringLiteralComment: Missing magic comment # frozen_string_literal: true.
Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
module Dummy | ||
class Application < Rails::Application | ||
# Initialize configuration defaults for originally generated Rails version. | ||
config.load_defaults ENV['RAILS_VERSION'] if ENV['RAILS_VERSION'] |
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.
Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
Ok - I've added some tests which switch the dummy app in to API only mode - it runs all the tests again (in API only mode) apart from the navigation tests. I can see that Hound is being a bit picky about a few things - let me know if you'd like me to make those changes @tute @redtachyons |
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 is awesome, thank you! Given only the configuration option should change (AFAIK), and not the entire environment.rb
file, what do you think of implementing it in the original one as:
config.api_only = ARGV.include?('-api-only=true')
Thanks for your great work!
Regarding Hound, I'd follow most of its recommendations yes. It's not a big deal, but it's nice to have consistency. Thanks!
@tute I think I wanted to keep the API switch in the rake file and test helper rather than in the dummy app. I originally had two apps, but then reduced to one. I wanted the API only one not to load any of the extra 'full web' dependencies of the stack for a better comparison, as well as switching the api_only flag. Will look at hound now.. |
test/test_helper.rb
Outdated
if API_ONLY | ||
require File.expand_path("dummy/config/environment_api_only.rb", __dir__) | ||
else | ||
require File.expand_path("dummy/config/environment.rb", __dir__) |
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.
Layout/ExtraSpacing: Unnecessary spacing detected.
test/test_helper.rb
Outdated
require 'minitest/rails' | ||
require 'mocha/mini_test' | ||
if API_ONLY | ||
require File.expand_path("dummy/config/environment_api_only.rb", __dir__) |
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.
Layout/ExtraSpacing: Unnecessary spacing detected.
test/test_helper.rb
Outdated
RUBYOPT="-w $RUBYOPT" | ||
RUBYOPT = "-w $RUBYOPT" | ||
|
||
API_ONLY = ARGV.include?('-api-only=true') |
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.
Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
test/test_helper.rb
Outdated
@@ -1,6 +1,8 @@ | |||
# Configure Rails Envinronment | |||
ENV['RAILS_ENV'] = 'test' | |||
RUBYOPT="-w $RUBYOPT" | |||
RUBYOPT = "-w $RUBYOPT" |
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.
Style/MutableConstant: Freeze mutable objects assigned to constants.
@@ -0,0 +1,6 @@ | |||
# frozen_string_literal: true | |||
# Load the rails application | |||
require File.expand_path("application_api_only", __dir__) |
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.
Layout/ExtraSpacing: Unnecessary spacing detected.
@@ -0,0 +1,6 @@ | |||
# frozen_string_literal: true | |||
# Load the rails application |
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.
Layout/EmptyLineAfterMagicComment: Add an empty line after magic comments.
# Initialize configuration defaults for originally generated Rails version. | ||
config.load_defaults 5.2 if ENV['RAILS_VERSION'] =~ /^5.2/ | ||
|
||
# Settings in config/environments/* take precedence over those specified 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.
Metrics/LineLength: Line is too long. [82/80]
module Dummy | ||
class Application < Rails::Application | ||
# Initialize configuration defaults for originally generated Rails version. | ||
config.load_defaults 5.2 if ENV['RAILS_VERSION'] =~ /^5.2/ |
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.
Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
@@ -0,0 +1,43 @@ | |||
# frozen_string_literal: true | |||
require_relative "boot" |
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.
Layout/EmptyLineAfterMagicComment: Add an empty line after magic comments.
@tute got there in the end with Hound! Sorry, for the extra commits, I hadn't twigged it was using rubocop else I'd have patched it all locally :) |
Updated dependencies on a commit prior to this PR, and then squashed your commits together and applied some minor adjustments in 3371034. Thanks for your work on this, @jamesjefferies! 😃 🎉 👍 |
Released version 3.0.2 with this fix! https://github.com/merit-gem/merit/releases/tag/v3.0.2 Thank you all again. |
Previously merit was initialised when action_controller was loaded,
which if you are using the Rails API & Web functions meant that it was
loaded once, but could be loaded by the API OR Web bits being
initialised. Thanks to the Rails PR listed below, you can now hook in to
the web part only being initialised using action_controller_base
rails/rails#28402