Ruby on Rails, Including a Module with Arguments

Ruby on Rails, including a module with arguments

There is no way of passing arguments when including the module. The best next thing would be to define a class method that lets you create what you need afterwards:

module Assetable
extend ActiveSupport::Concern
module ClassMethods
def total_assets(number)
number.times do |i|
attr_accessor "asset_#{i}"
attr_accessible "asset_#{i}"
end
end
end
end

class C
include Assetable
total_assets 3
end

o = C.new
o.asset_2 = "Some value."
o.asset_2 #=> "Some value."

Also be careful when overriding the included method within a concern because it's also used by ActiveSupport::Concern. You should call super within the overriden method in order to ensure proper initialization.

Pass Arguments to Included Module in Ruby?

You might want to look at the Modularity gem, which does exactly what you want.

How to pass service objects/modules as arguments in ruby

Almost. You can make your module convertable to proc and use it that way:

module PrintAny
def self.print(text)
puts text
end

def self.to_proc
method(:print).to_proc
end
end

["any"].each &PrintAny # => prints "any"

Enumerable#each requires you to pass a block, the ampersand operator (&) converts an object to block by first calling to_proc on that object. And modules are just objects, hence if they have a method to_proc, this will work.

pass a module and class name as an argument to a ruby function

If you're on rails,

You could use a combination of camelecase/classify and constantize

> model = "#{'my_database'.camelcase}::#{'here_is_some_table'.camelcase}".constantize
=> MyDatabase::HereIsSomeTable # Only work if you actually have such a constant

that'll give something like

def update_window_for_ctm_staging_extract(target_database, target_table, table_name)
model = "#{'target_database'.camelcase}::#{'target_table'.camelcase}".constantize
model.
where(src_tablename: table_name).
update(from_date_dttm: from_date_dttm, to_date_dttm: to_date_dttm, sequence_no: 1)
end

Ruby: trying to object.send a module function with parameters

I this specific instance I had trouble trying to include functions that were in another module, not within any class. I was requireing the module I was trying to send to, but when I would do ModuleName::send(:function_name) I was getting an error stating "no method exists by that name" (or something close to that error). What fixed this problem was to define the functions in the module that I was trying to "send" to as "self.function_name".

module Something
def self.my_function
puts "Silliness"
end
end

Then in the sending file/module/class:

require '../lib/something.rb'

class MyClass
def this_crazy_thing
funct = 'my_function'
Something::send(funct)
end
end

So now the above works...by simply defining the function in module Something as self. Which as per my understanding has to do with how objects are instantiated in Ruby. What got me here is that, not using the dynamic send functionality...if I knew the name of the function in the module that I wanted to call, I could call it with:

ModuleName::function_name(params)

so I expected something similar to work with send. My guess is that since I'm trying to send to an object, the function needs to be tied to the object I'm sending to, namely the module, so that it can be called as an instance of that object. Clearly I'm still wrapping my head around the fact that everything is an object in Ruby. Hopefully this at least gets someone on the right path. If someone can explain why exactly this works:

module ModuleSomething
def a_function
puts "hi"
end
end

require '../lib/modulesomething'
class MyClass
def this_thing
ModuleSomething::a_function
end
end

but not:

require 'ModuleSomething'
class MyClass
def this_string
funct = 'a_function'
ModuleSomething::send(funct)
end
end

That might help clear it up a little more as to why my solution worked. When I find out more I will add to this. Thanks guys!

Rails mailer argument error from separate module

It looks like your controller may be including your Requester module. That will override a core Rails method called request. Based on the backtrace, the failure is coming out of this line:

# ActionView::Helpers::ControllerHelper
@_request = controller.request if controller.respond_to?(:request)

request is a special method available to controllers. You'll need to either not include Requester in your controller, rename your request method, or move your request method to where you can call it without including it within the controller itself.

You may want to check what your controller inherits from. If it's something like ApplicationController, maybe that is including Requester.

If you temporarily put this code in your controller action it will show all the ancestor classes that include the module:

raise self.class.ancestors.select { |ancestor| ancestor.include?(Requester) }.map(&:name).join(', ')

How can I access params from my custom module in Rails?

See the answer in this question, Advice on using modules with Ruby on Rails. The last sentence of the accepted answer says that you're going to have to pass arguments to the module.



Related Topics



Leave a reply



Submit