Skip to content

Only initialise merit when action_controller_base is loaded - #297

Closed
jamesjefferies wants to merge 9 commits into
tute:masterfrom
jamesjefferies:initialise-merit-when-action-controller-base-is-loaded
Closed

jamesjefferies wants to merge 9 commits into
tute:masterfrom
jamesjefferies:initialise-merit-when-action-controller-base-is-loaded

Conversation

@jamesjefferies

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

This fixes #296 and possibly #294

@truongnmt

truongnmt commented Jun 29, 2018 •

Copy link
Copy Markdown

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
Copy Markdown
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
Copy Markdown

@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
Copy Markdown
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
Copy Markdown

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

@jamesjefferies

Copy link
Copy Markdown
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

tute commented Jun 29, 2018

Copy link
Copy Markdown
Owner

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

tute commented Jul 2, 2018

Copy link
Copy Markdown
Owner

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

jamesjefferies commented Jul 2, 2018 via email

Copy link
Copy Markdown
Contributor Author

@tute

tute commented Jul 3, 2018

Copy link
Copy Markdown
Owner

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

jamesjefferies commented Jul 5, 2018 via email

Copy link
Copy Markdown
Contributor Author

@truongnmt

Copy link
Copy Markdown

Hmm how it's going then ...

@redtachyons

Copy link
Copy Markdown

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

tute commented Jul 13, 2018

Copy link
Copy Markdown
Owner

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
Copy Markdown

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
Comment thread lib/merit.rb Outdated
end

def action_controller_hook
Rails.application.config.api_only ? :action_controller_api : :action_controller_base

Copy link
Copy Markdown

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]

Comment thread lib/merit.rb Outdated
end
end

private

Copy link
Copy Markdown

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
Copy Markdown
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
Copy Markdown

@jamesjefferies it's working, thank you!

To ensure the tests all work in API mode
Comment thread test/test_helper.rb Outdated
require 'rails/test_help'
require 'minitest/rails'
require 'mocha/mini_test'
require 'mocha/minitest'

Copy link
Copy Markdown

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.

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

Copy link
Copy Markdown

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.

Comment thread test/test_helper.rb Outdated

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

Copy link
Copy Markdown

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.

Comment thread test/test_helper.rb Outdated
ENV['RAILS_ENV'] = 'test'
RUBYOPT="-w $RUBYOPT"

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

Copy link
Copy Markdown

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

Copy link
Copy Markdown

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

Copy link
Copy Markdown

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.

Copy link
Copy Markdown

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)

Copy link
Copy Markdown

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'

Copy link
Copy Markdown

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

Copy link
Copy Markdown

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

jamesjefferies commented Jul 20, 2018 •

Copy link
Copy Markdown
Contributor Author

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

@tute tute left a comment

Copy link
Copy Markdown
Owner

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
Copy Markdown
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..

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

Copy link
Copy Markdown

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.

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

Copy link
Copy Markdown

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.

Comment thread test/test_helper.rb Outdated
RUBYOPT="-w $RUBYOPT"
RUBYOPT = "-w $RUBYOPT"

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

Copy link
Copy Markdown

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.

Comment thread test/test_helper.rb Outdated
# Configure Rails Envinronment
ENV['RAILS_ENV'] = 'test'
RUBYOPT="-w $RUBYOPT"
RUBYOPT = "-w $RUBYOPT"

Copy link
Copy Markdown

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

Copy link
Copy Markdown

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

Copy link
Copy Markdown

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.

Copy link
Copy Markdown

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/

Copy link
Copy Markdown

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"

Copy link
Copy Markdown

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
Copy Markdown
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

tute commented Jul 23, 2018

Copy link
Copy Markdown
Owner

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

tute commented Jul 23, 2018

Copy link
Copy Markdown
Owner

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