How to use helper method of my engine generator - ruby-on-rails

I needs use helper method, but an error is returned.
This is my helper module
# app/helpers/my_engine/application_helper.rb
module MyEngine
module ApplicationHelper
def app_name
Rails.application.class.parent_name.underscore
end
def engine_name
Module.nesting.last.name.underscore
end
end
end
This is my generator
# lib/generators/my_engine/my_gen_generator.rb
module MyEngine
class InstallGenerator < ::Rails::Generators::Base
include MyEngine::ApplicationHelper
desc "Desc"
p engine_name
end
end
The error returned is: Error: undefined local variable or method engine_name...

Use extend instead of include
Here's a link

Related

How to reuse a method from a helper inside of a lib module in Rails?

Let's say I have a method in my ApplicationHelper:
module ApplicationHelper
def a
do something
end
end
and I have a module in my lib folder:
module MyModule
def do_another_thing
# How can I use the method a here?
# I know that I can include the `ApplicationHelper` here, but I don't want to pollute my module with unrelated functions
end
end
You can mark that a method as module_function like so:
module ApplicationHelper
def a
do something
end
module_function :a
end
and then use it like ApplicationHelper.a

Add methods to a Rails engine model from a Rails plugin

I'm writing a Rails plugin to extend a Rails engine. Namely MyPlugin has MyEngine as a dependency.
On my Rails engine I have a MyEngine::Foo model.
I'd like to add new methods to this model so I created a file in my plugin app/models/my_engine/foo.rb which has the following code:
module MyEngine
class Foo
def sayhi
puts "hi"
end
end
end
If I enter the Rails console on the plugin dummy application I can find MyEngine::Foo, but runnning MyEngine::Foo.new.sayhi returns
NoMethodError: undefined method `sayhi'
Why MyPlugin cannot see the updates to MyEngine::Foo model? Where am I wrong?
Ok, found out. To make MyPlugin aware and able to modify MyEngine models the engine must be required on the plugin engine.rb like so:
require "MyEngine"
module MyPlugin
class Engine < ::Rails::Engine
isolate_namespace MyPlugin
# You can also inherit the ApplicationController from MyEngine
config.parent_controller = 'MyEngine::ApplicationController'
end
end
In order to extend MyEngine::Foo model I then had to create a file lib/my_engine/foo_extension.rb:
require 'active_support/concern'
module FooExtension
extend ActiveSupport::Concern
def sayhi
puts "Hi!"
end
class_methods do
def sayhello
puts "Hello!"
end
end
end
::MyEngine::Foo(:include, FooExtension)
And require it in config/initializers/my_engine_extensions.rb
require 'my_engine/foo_extension'
Now from MyPlugin I can:
MyEngine::Foo.new.sayhi
=> "Hi!"
MyEngine::Foo.sayhello
=> "Hello!"
See ActiveSupport Concern documentation for more details.

Ruby on Rails module include module

I want to include a module in a rails helper(is also a module).
The helper is:
module SportHelper
.....
end
And the module is:
module Formula
def say()
....
end
end
Now, I want to use the method say in SportHelper. What should I do?
If I write like this:
module SportHelper
def speak1()
require 'formula'
extend Formula
say()
end
def speak2()
require 'formula'
extend Formula
say()
end
end
This will work, but I don't want to do so, I just want to add the methods on the helper module,not every methods.
You need just to include this module in your helper:
require 'formula'
module SportHelper
include Formula
def speak1
say
end
def speak2
say
end
end
Maybe you don't need this line require 'formula', if it's already in the load path. For check this you can inspect $LOAD_PATH variable. For more information see this answer.
Basic difference between extend and include is that include is for adding methods to an instance of a class and extend is for adding class methods.
module Foo
def foo
puts 'heyyyyoooo!'
end
end
class Bar
include Foo
end
Bar.new.foo # heyyyyoooo!
Bar.foo # NoMethodError: undefined method ‘foo’ for Bar:Class
class Baz
extend Foo
end
Baz.foo # heyyyyoooo!
Baz.new.foo # NoMethodError: undefined method ‘foo’ for #<Baz:0x1e708>
And if you use extend inside the object method, it will adding methods to an instance of a class, but they would be available only inside this one method.
I think directly include should work
module SportHelper
include SportHelper
.........
end
end
I tested like below:
module A
def test
puts "aaaa"
end
end
module B
include A
def test1
test
end
end
class C
include B
end
c = C.new()
c.test1 #=> aaaa
It should work.

Cannot access method from included module

I'm learning ruby on rails 4, and want to use concern in ActiveSupport. Here's how I do it:
controllers/concerns/do_things_controller.rb
# file controllers/concerns/do_things_controller.rb
require 'active_support/concern'
module DoThings
extend ActiveSupport::Concern
def do_something
puts 'something'
end
included do
helper_method :do_something
end
end
controllers/application_controller.rb
# file controllers/application_controller.rb
class ApplicationController < ActionController::Base
require 'concerns/do_things_controller'
include DoThings
end
And in views/layouts/application.html.haml I call do_something, it shows error:
undefined method `do_something'
Thanks

How to define a method from a gem to use within a model?

I'm trying to build a gem and I want to define a method my_method inside the gem and use it inside a model.
Example:
class MyModel < ActiveRecord::Base
my_method
end
My gem:
#lib/my_gem.rb
require "my_gem/model_inclusions"
module MyGem
end
#lib/my_gem/model_inclusions.rb
module MyGem
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def my_method
end
end
end
When I try the example it gives me undefined method 'my_method' for <Class:0x00000045434> (NoMethodError)
module NumberInternationalizer
def my_method
...
end
end
ActiveRecord::Base.send :extend, NumberInternationalizer

Resources