Skip to content
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

Closed
wants to merge 9 commits into from
Closed

Only initialise merit when action_controller_base is loaded #297

wants to merge 9 commits into from

Conversation

jamesjefferies
Copy link
Contributor

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

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
@jamesjefferies
Copy link
Contributor Author

This fixes #296 and possibly #294

@truongnmt
Copy link

truongnmt commented Jun 29, 2018

Nice, but why i'm still get warning on run rails c:
screen shot 2018-06-29 at 16 27 51

I edited directly on merit gem path and test on my application.

@jamesjefferies
Copy link
Contributor Author

@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 - :-/

@truongnmt
Copy link

@jamesjefferies here you go, this is how I edit it, a bit hacky
screen shot 2018-06-29 at 17 00 23
Then after I ran rails c I got that message :|

@jamesjefferies
Copy link
Contributor Author

@truongnmt I can see spring is running there - have you stopped and started spring (or got rid of it entirely? ;))

@truongnmt
Copy link

@jamesjefferies how do you know I'm running spring :))
Yeah LGTM 👍

@jamesjefferies
Copy link
Contributor Author

@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..

@tute
Copy link
Member

tute commented Jun 29, 2018

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! 😃

@tute
Copy link
Member

tute commented Jul 2, 2018

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!

@jamesjefferies
Copy link
Contributor Author

jamesjefferies commented Jul 2, 2018 via email

@tute
Copy link
Member

tute commented Jul 3, 2018

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.

@jamesjefferies
Copy link
Contributor Author

jamesjefferies commented Jul 5, 2018 via email

@truongnmt
Copy link

Hmm how it's going then ...

@redtachyons
Copy link

run_once option is added to prevent loading this hook twice, I think it is safe to remove since we are separating it here, Can you please verify that as well

@tute
Copy link
Member

tute commented Jul 13, 2018

run_once option is added to prevent loading this hook twice, I think it is safe to remove since we are separating it here, Can you please verify that as well

Thanks for the suggestion. That was the first attempt of a solution, but it produces the warning messages shown in #297 (comment)

@hcyildirim
Copy link

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

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

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.

@jamesjefferies
Copy link
Contributor Author

@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...

@hcyildirim
Copy link

@jamesjefferies it's working, thank you!

To ensure the tests all work in API mode
require 'rails/test_help'
require 'minitest/rails'
require 'mocha/mini_test'
require 'mocha/minitest'

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.

if API_ONLY
require File.expand_path('../dummy/config/environment_api_only.rb', __FILE__)
else
require File.expand_path('../dummy/config/environment.rb', __FILE__)

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.

@@ -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__)

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.

@@ -2,6 +2,8 @@
ENV['RAILS_ENV'] = 'test'
RUBYOPT="-w $RUBYOPT"

API_ONLY=ARGV.include?('-api-only=true')

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__)

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

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.

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)

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'

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']

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.

@jamesjefferies
Copy link
Contributor Author

jamesjefferies commented Jul 20, 2018

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

Copy link
Member

@tute tute left a 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!

@jamesjefferies
Copy link
Contributor Author

@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..

if API_ONLY
require File.expand_path("dummy/config/environment_api_only.rb", __dir__)
else
require File.expand_path("dummy/config/environment.rb", __dir__)

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.

require 'minitest/rails'
require 'mocha/mini_test'
if API_ONLY
require File.expand_path("dummy/config/environment_api_only.rb", __dir__)

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.

RUBYOPT="-w $RUBYOPT"
RUBYOPT = "-w $RUBYOPT"

API_ONLY = ARGV.include?('-api-only=true')

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.

@@ -1,6 +1,8 @@
# Configure Rails Envinronment
ENV['RAILS_ENV'] = 'test'
RUBYOPT="-w $RUBYOPT"
RUBYOPT = "-w $RUBYOPT"

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__)

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

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.

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/

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"

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.

@jamesjefferies
Copy link
Contributor Author

@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 :)

@tute tute closed this in 3371034 Jul 23, 2018
@tute
Copy link
Member

tute commented Jul 23, 2018

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! 😃 🎉 👍

@tute
Copy link
Member

tute commented Jul 23, 2018

Released version 3.0.2 with this fix! https://github.com/merit-gem/merit/releases/tag/v3.0.2

Thank you all again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants