Cannot access method from included module - ruby-on-rails

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

Related

Rails 4 engine nested module not being included with uninitialized constant (NameError)

I have a nested module set up as a base helper method which is not getting included in the app that uses the engine.
in lib/mol/blog/blog.rb
require "mol/blog/engine"
module Mol
module Blog
module Categories
def self.included(base)
base.helper_method :categories
end
def categories
Mol::Cms::Category.all
end
end
end
end
In the engine's spec/dummy application_controller
class ApplicationController < ActionController::Base
protect_from_forgery
include Mol::Blog::Categories
end
This works fine and the categories appear as expected. However, when I try to use the engine in a different app, the Categories module is not being included.
in the app's application controller
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
include Mol::Blog::Categories
end
In the rails console, the error is uninitialized constant Mol::Blog::Category (NameError)
Mol::Blog is defined, no error or anything. Why is the Categories module not being recognised?
Your app does not know about Mol::Blog::Categories module. You should require it in the "mol/blog/engine.rb" file or in the "mol/blog.rb" file.
# mol/blog.rb
require "mol/blog/categories"
module Mol
module Blog
# ...
end
end

How to use helper method of my engine generator

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

Call a class method with a multi level Module structure in Ruby

I have some modules to be included in my controller classes. These modules define before_filter:
module BasicFeatures
def filter_method
...
end
def self.included(base)
base.before_filter(:filter_method)
...
end
end
module AdvancedFeatures
include BasicFeatures
...
end
And the classes:
class BasicController < ApplicationController
include BasicFeatures
end
class AdvancedController < ApplicationController
include AdvancedFeatures
end
When BasicFeatures module is included in AdvancedFeatures module, there are no before_filter methods in it.
The AdvancedController didn't get the before_filter call.
I need both my controllers to get the before_filter without any code duplication. I don't know if I am using the best approach so, I'm open to any suggestion.
This is why ActiveSupport::Concern was created.
module BasicFeatures
extend ActiveSupport::Concern
included do
before_filter :require_user
end
def this_is_an_instance_method
'foo'
end
module ClassMethods
def this_is_a_class_method
'bar'
end
end
end
class SomeClass
include BasicFeatures
end
SomeClass.new.this_is_an_instance_method #=> 'foo'
You can also nest them — that is, create concerns that include concerns — and everything will work as expected. And here are the docs.
You can try this. Instead of including the module in AdvancedFeatures, You can include the BasicFeatures module on the class including AdvancedFeatures
module BasicFeatures
def filter_method
#code....
end
#some others basic methods...
def self.included(base)
base.before_filter(:filter_method)
#some other class method calls
end
end
module AdvancedFeatures
def self.included klass
klass.class_eval do
include BasicFeatures
end
end
#some advanced methods
end

Providing a before_filter in a rubygem

I have a gem that contains a method designed to be run as a before_filter in Rails:
before_filter :method_in_gem
It is up to the developer when they want to call this before_filter in their application (i.e I don't want to enforce it on them in any way)
How can I expose this method in a way that the controller is able to pick it up? I have my method in gem_name/lib/controllers.rb
If it's relevant, my gem is being created with bundler.
try the following
module ModuleName
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def meth(args)
before_filter :bf_method
include ModuleName::InstanceMethods
end
end
module InstanceMethods
def bf_method
# ...
end
end
end
then just include the Module in your controller
class ApplicationController < ActionController::Base
include ModuleName
end

How can I extend ApplicationController in a gem?

I thought I'd come up with a slick way to extend ApplicationController in a Rails 3.x gem.
In my gem's lib/my_namespace/my_controller.rb, I had:
class MyNamespace::MyController < ApplicationController
before_filter :some_method
after_filter :another_method
def initialize
# getting classname of the subclass to use for lookup of the associated model, etc.
# and storing the model_class in an instance variable
# ...
end
# define :some_method, :another_method, etc.
# ...
private
attr_accessor :subclass_defined_during_initialize # etc.
# etc.
end
but when the Gem is loaded, app/controllers/application_controller.rb is not yet loaded, so it fails:
/path/to/rvm/gemset/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:251:
in `require': cannot load such file -- my_gem_name/application_controller (LoadError)
As a workaround, I had defined ApplicationController in my gem's lib/gem_namespace/application_controller.rb as:
class ApplicationController < ActionController::Base
end
I assumed that even though I had defined it there, it would be redefined in my Rails 3 application's app/controllers/application_controller.rb, such that both controllers in the application that extended ApplicationController and controllers that extended MyNamespace::MyController would directly or indirectly extend the ApplicationController defined in app/controllers/application_controller.rb.
However, we noticed that after loading the gem, controllers that extend ApplicationController were unable to access methods defined in app/controllers/application_controller.rb. Also, the ApplicationHelper (app/helpers/application_helper.rb) module was no longer being loaded by other helper modules.
How can I extend ApplicationController within the controller in my gem for the purpose of defining a before_filter and after_filter to and use initialize to access the class's name to determine the associated model's class that it could then store and use within its methods?
Update 2012/10/22:
Here's what I came up with:
In lib/your_gem_name/railtie.rb:
module YourGemsModuleName
class Railtie < Rails::Railtie
initializer "your_gem_name.action_controller" do
ActiveSupport.on_load(:action_controller) do
puts "Extending #{self} with YourGemsModuleName::Controller"
# ActionController::Base gets a method that allows controllers to include the new behavior
include YourGemsModuleName::Controller # ActiveSupport::Concern
end
end
end
and in lib/your_gem_name/controller.rb:
module YourGemsModuleName
module Controller
extend ActiveSupport::Concern
# note: don't specify included or ClassMethods if unused
included do
# anything you would want to do in every controller, for example: add a class attribute
class_attribute :class_attribute_available_on_every_controller, instance_writer: false
end
module ClassMethods
# notice: no self.method_name here, because this is being extended because ActiveSupport::Concern was extended
def make_this_controller_fantastic
before_filter :some_instance_method_available_on_every_controller # to be available on every controller
after_filter :another_instance_method_available_on_every_controller # to be available on every controller
include FantasticStuff
end
end
# instance methods to go on every controller go here
def some_instance_method_available_on_every_controller
puts "a method available on every controller!"
end
def another_instance_method_available_on_every_controller
puts "another method available on every controller!"
end
module FantasticStuff
extend ActiveSupport::Concern
# note: don't specify included or ClassMethods if unused
included do
class_attribute :class_attribute_only_available_on_fantastic_controllers, instance_writer: false
end
module ClassMethods
# class methods available only if make_this_controller_fantastic is specified in the controller
def some_fanastic_class_method
put "a fantastic class method!"
end
end
# instance methods available only if make_this_controller_fantastic is specified in the controller
def some_fantastic_instance_method
puts "a fantastic instance method!"
end
def another_fantastic_instance_method
puts "another fantastic instance method!"
end
end
end
end
For this specific kind of functionality I would recommend creating a module in your gem and include that module in your Application Controller
class ApplicationController < ActionController::Base
include MyCoolModule
end
To add before filters, etc (add this to your module)
def self.included(base)
base.send(:before_filter, my_method)
end
Update: you may be able to just do base.before_filter :my_method which is cleaner.
Here is a Gist
that shows how to access the class of the subclass and store it in an instance variable and access it in the before and after filters. It uses the include method.
Truth is much much simpler and flexible.
Add to lib/engine.rb this: class Engine < Rails::Engine; end
And then simply use:
ActionController::Base.class_eval do
include SomethingFromMineGemModule
# or:
def hello_from_gem
'Hey people!'
end
end
I was able to reference ApplicationController with an initializer callback.
gem code that subclasses/references ApplicationController:
class GemApplicationController < ApplicationController
before_filter :method_to_call
def method_to_call
#your code here
end
end
gem code callback to create subclassed controller:
module GemName
def self.load_gem_application_controller
require "path/to/gem_application_controller"
end
end
rails_app/config/initializers/gem_name.rb
GemName.load_gem_application_controller
Then have controllers that use this functionality subclass GemApplicationController
class SpecialCaseController < GemApplicationController
# this will inherit from the gem's controller,
# which inherits from the rails_app ApplicationController
end

Resources