rails 4 use application helpers inside initializers
This is one way to do it:
# lib/browser_util.rb
module BrowserUtil
def self.supported?(browser)
# your code ...
end
end
and wrap that from ApplicationHelper for use in views
module ApplicationHelper
def is_browser_supported?
BrowserUtil.supported?(browser)
end
end
in middleware
Rails.configuration.middleware.use Browser::Middleware do
unless BrowserUtil.supported?(browser)
redirect_to :controller => 'error', :action => 'browser-upgrade-required'
end
end
UPDATE: it does not need to be in a separate module (BrowserUtil)
module ApplicationHelper
def self.foo
"FOO"
end
def foo
ApplicationHelper.foo
end
end
in middleware use
ApplicationHelper.foo
in views it would use the included method
foo
Accessing helpers and models from rails engine initializer
After I used required for every class, I ended with ActiveRecord::ConnectionNotEstablished
, and it seems that not everything is loaded and available at that point when the GLOBAL
object is created.
So I moved the code that was using the models in a separate init
method. Then, I added an after initialize event:
config.after_initialize do
MyEngine.GLOBAL.init
end
How to run an initializer that calls Rails.application?
According to The Rails Initialization Process of the Rails official reference, the last file to be executed in the initialization sequence is config/environment.rb
(Note that although the reference states the last one is config/application.rb
, the file should have been in default require-d in the earlier process).
In Rails 6 (or maybe earlier ones, too?), the last line in config/environment.rb
is
Rails.application.initialize!
So, if you write your own statements after the line in the file config/environment.rb
, your Rails app must have been fully initialized by the time your statements are executed.
The reason why it's possible to use helper method like current_user, authenticate_user!, and so on without including any module
The answer is devise included its helper methods into ActionController on behalf of you when Rails on_load
# devise/rails.rb
module Devise
class Engine < ::Rails::Engine
# ...
initializer "devise.url_helpers" do
Devise.include_helpers(Devise::Controllers)
end
# ...
end
# devise.rb
# ...
def self.include_helpers(scope)
ActiveSupport.on_load(:action_controller) do
include scope::Helpers if defined?(scope::Helpers)
include scope::UrlHelpers
end
ActiveSupport.on_load(:action_view) do
include scope::UrlHelpers
end
end
# ...
I saw many 3rd gems using on_load to include their methods (or themselves) into Rails core, maybe it's a typical way to do that (allows Rails to lazily load a lot of components and thus making the app boot faster). If you install some gems and you could use their methods on your model/controller/view then those gems did the same thing devise
did above.
About those methods current_user
, authenticate_user!
... they are dynamic methods devise will generate when it did include scope::Helpers
into Rails (see code above and the link).
Accessing helpers from the parent app in an isolated Rails engine
The RailsAdmin Engine is also isolated, but they have the same configuration options as you would like to implement. They have configurable before_filters for both authentication and authorization. Have a look at this.
As far as I can tell, they just subclass the parent controller like this::ApplicationController
or instead you can configure one (ref).
For your controller you could just create your own EngineController
, that inherits from RocketPant::Base
and maybe just create a method there that calls the configured authentication method directly via send
on the parent controller.
As the RocketPant::Base
Class does not inherit from ApplicationController::Base
I guess you have to find some custom way around this and can't go the normal ways for Rails Engines. Maybe you could also try to file an issue to the rocket_pant repo, to add the helper
method. As far as I read they soon want to inherit from ApplicationController::Base
anyway in 2.0 (ref).
Strange Missing Helper File Error in Rails
Seems the helper file has been deleted at some stage.
In the helpers folder create a new file, rename it underwrite.rb
and place the following code inside it.
module UnderwriteHelper
end
Related Topics
I Want to Return a Single Result Using .Find() with Ruby on Rails
Encrypt Data Bag from Inside of Ruby Without Relying on Knife
How to Display Error Messages in a Multi-Model Form with Transaction
Ruby, Value Bucketing, Beautify Code
Regex to Extract Boundary and Content Type Out of Mail Headers
Delete from Database with Specific Details in Ruby
Ancestry Gem in Rails and Mutli Nesting
How to Write the JSON Schema If JSON Has Multiple Data Set
Devise Password Reset from Rails Console
Print Unicode Escape Codes from Variable
Replacing an Element in Nested Array Ruby
Pow Not Loading Gem Properly While Rails S Works
Randomly Showing Nomethoderror: Undefined Method 'Empty' for 0:Fixnum