Adding a helper method with a gem
Create a module somewhere for your helper methods:
module MyHelper
def mymethod
end
end
Mix it into ActionView::Base (such as in init.rb
or lib/your_lib_file.rb
)
ActionView::Base.send :include, MyHelper
Add method to a gem's helpers
What's the point of altering the gem with additional methods? If it's a view helper, simply create an appropriate file in the app/helpers
directory of your app and add your custom helper there.
The custom helper will be available to all views.
How to include my helpers into my gem and load them into rails app
Last time I did it with rails 2.
I've placed helper module in lib directory with following line added in init.rb which is placed in root directory of a gem/plugin:
ActionController::Base.helper TestHelper
It makes TestHelper module methods available for template. Here is how above helper method works: helper method
Creating a Gem that adds a rails form helper
After searching, I found a different gem that adds a FormBuilder method. I used their repo as a guide to structure my own. For others with this questions, you can view my repo and their repo here respectively:
https://github.com/dyeje/rails_json_field
https://github.com/Brantron/john_hancock
How to override controller helper method loaded from a gem
This solved my issue. Simple:
# /rails/app/helpers/controllers/page_title_helper.rb
module Controllers
module PageTitleHelper
include OsmUiHelper
alias old_page_title page_title
# rubocop:disable Style/OptionHash
def page_title(options = {})
company_id = settings ? settings.company_id : 0
if Feature.on?('omnibar_enabled', company_id)
# New logic here
else
# Use the old page_title in the gem
old_page_title(options)
end
end
# rubocop:enable Style/OptionHash
end
end
How do I extract Rails view helpers into a gem?
In my opinion, a full Engine is overkill for this task. You could instead just create a Railtie which includes your helpers into ActionView::Base when it initializes.
# lib/my_gem/view_helpers.rb
module MyGem
module ViewHelpers
def pre(text)
content_tag :pre, text
end
def another_helper
# super secret stuff
end
end
end
# lib/my_gem/railtie.rb
require 'my_gem/view_helpers'
module MyGem
class Railtie < Rails::Railtie
initializer "my_gem.view_helpers" do
ActiveSupport.on_load(:action_view) { include MyGem::ViewHelpers }
end
end
end
# lib/my_gem.rb
require 'my_gem/railtie' if defined?(Rails)
How do I make functions in a gem available to Sinatra views?
You’re mainly just missing the call to Sinatra.register
(in cool_gem/sinatra.rb
):
require 'sinatra/base'
require 'cool_gem/helper_functions'
module CoolGem
# you could just put this directly in the CoolGem module if you wanted,
# rather than have a Sinatra sub-module
module Sinatra
def self.registered(app)
#no need to create another module here
app.helpers CoolGem::HelperFunctions
end
end
end
# this is what you're missing:
Sinatra.register CoolGem::Sinatra
Now any classic style Sinatra app that requires cool_gem
will have the helpers available. If you use the modular style you’ll also need to call register CoolGem::Sinatra
inside the Sinatra::Base
subclass.
In this case, if you are just providing some helper methods, an easier way might be to just use the helpers
method (again in cool_gem/sinatra.rb
):
require 'sinatra/base'
require 'cool_gem/helper_functions'
Sinatra.helpers CoolGem::HelperFunctions
Now the methods will be available in classic style apps, and modular style apps will need to call helpers CoolGem::HelperFunctions
. This is a bit simpler, but if you are adding methods to the DSL context you will need to use registered
as above.
Related Topics
Have Devise Create a Subdomain on Registration
Change HTML Form Id Generated by Form_For Rails 3.1
Rails: Update Model Attribute Without Invoking Callbacks
Carrierwave Fog Amazon S3 Images Not Displaying
Is There a Ruby Http Client Library with a Response Cache
Running Parallel Selenium Tests with Capybara
Chef and Erb Templates. How to Use Boolean Code Blocks
Is There a Workaround to Open Urls Containing Underscores in Ruby
How to Pass <Arguments> to Irb If I Don't Specify <Programfile>
"Certificate Verify Failed" Error When Installing Ruby Gems on Windows
Vagrant Install Plugin from Github
Active Record with Delegate and Conditions
Does Ruby Support Unicode and How Does It Work
Trying to Understand Use of Self.Method_Name VS. Classname.Method_Name in Ruby