I am trying to create a gem to extend Rails ActionView Renderer to print an HTML comment with the partial view name bring rendered.
I tried the normal way:
module MyGem
class Engine < ::Rails::Engine
initializer 'mygem.initialize' do
::ActiveSupport.on_load(:action_view) do
::ActionView::Renderer.send :include, MyGem::ViewRenderer
end
end
end
end
And then in mygem/lib/view_renderer.rb:
module MyGem
module ViewRenderer
module InstanceMethods
def render(context, options)
puts "here" # Just to test it was included and it doesn't print
if options.key?(:partial)
render_partial(context, options)
else
render_template(context, options)
end
end
end
def self.included(base)
base.send :include, InstanceMethods
end
end
end
However, when I use render from my views, the test line added doesn't work.
Any idea what am I doing wrong?
This is how I have done it in the past, maybe it will help.
module MyModule
def self.included(base)
def render
#do stuff
end
end
end
ActionController::Base.send :include, MyModule
Then just require the file
Related
New to Ruby\Rails, shame on me :(
I'm developing an engine for personal use (simple admin panel). What I want, is to be able to config my main app's models, like this:
class User < ActiveRecord::Base
include Entropy::Configurable
entropy_config do
form_caption 'Editing user'
end
end
And then in engine's templates do this:
<h1><%= #object.entropy_config :form_caption %></h1>
Engine's module:
module Entropy
module Configurable
def self.included(base)
## to call entropy_config in model class
base.send :extend, ClassMethods
end
def entropy_config(arg)
## ... I'm missing this part
end
module ClassMethods
##config = { ... }
def entropy_config (&block)
class_eval &block
end
def form_caption(arg)
// skipping class identification
##config[:user][:form_caption] = arg
end
end
end
end
The problem is that I can not get access to ##config from Configurable module, actually when I call entropy_config on #object. What I'm doing wrong?
First of all you've doing it wrong. Rails is on of the frameworks that pushed a lot on the MVC architecture. Having your model know about form captions is wrong. For that I would use the rails i18n gem. For the sake of the argument here's some untested code that will probably answer your question:
module Entropy
module Configurable
def self.included(base)
## to call entropy_config in model class
base.send :extend, ClassMethods
end
def entropy_config(key)
self.class.config[:user][key]
end
module ClassMethods
cattr_accessor :config
def entropy_config (&block)
self.config ||= {}
class_eval &block
end
def form_caption(arg)
// skipping class identification
self.config[:user][:form_caption] = arg
end
end
end
end
see http://apidock.com/rails/Class/cattr_accessor for more info
I have developed two plugins and they both modify the same method of the same class via alias_method_chain that result into stack level too deep error.
first plugin:
Rails.configuration.to_prepare do
require 'issue_patch'
Issue.send :include, IssuePatch
end
module IssuePatch
module InstanceMethods
def editable_custom_field_values_with_sort(user=nil)
editable_custom_field_values_without_sort(user).sort
end
end
def self.included(receiver)
receiver.send :include, InstanceMethods
receiver.class_eval do
alias_method_chain :editable_custom_field_values, :sort
end
end
end
Second plugin modify the class same way but with different feature:
Rails.configuration.to_prepare do
require 'issue_patch'
Issue.send :include, IssuePatch
end
module IssuePatch
module InstanceMethods
def editable_custom_field_values_with_some_stuff(user=nil)
editable_custom_field_values_without_some_stuff(user).select { |c| c.have_stuff? }
end
end
def self.included(receiver)
receiver.send :include, InstanceMethods
receiver.class_eval do
alias_method_chain :editable_custom_field_values, :some_stuff
end
end
end
When I trying to call this method I got:
ActionView::Template::Error (stack level too deep):
plugins/my_plugin/lib/issue_patch.rb:8
One possible but hack solution is simple monkey patch the redmine code with first plugin feature so the second plugin can alias it without any error.
So how I can fix this error?
The problem was:
I define two modules with the same name IssuePatch so the first module overwrites the second but
Issue.send :include, IssuePatch
still exists in 2 places (for each plugin) and the same module (doesn't matter which one overwrites the other) so the same module were included 2 times and the same alias_method_chain were called 2 times.
The solution: I just add separate module for each plugin and included them like this:
Issue.send :include, FirstPlugin::IssuePatch
Issue.send :include, SecondPlugin::IssuePatch
There is a module:
module ActionDispatch
module Routing
end
end
And methods:
def add_movie_path
end
def edit_movie_path
end
How I can add to module Routing this methods?
Is this only way?
Try:
module ActionDispatch
module Routing
def add_movie_path
end
def edit_movie_path
end
module_function :edit_movie_path
end
end
So that then you can do a call like it is a instance method like so:
class Make
include ActionDispatch::Routing
end
class MakeAll
def only_needs_the_one_method
ActionDispatch::Routing.edit_movie_path
end
end
You can also define it as a class method by using self.class_name and then directly access it like so:
module ActionDispatch
module Routing
def self.add_movie_path
end
def self.edit_movie_path
end
end
end
class Make
include ActionDispatch::Routing
def do_something
ActionDispatch::Routing.add_movie_path
end
end
class MakeAll
def only_needs_the_one_method
ActionDispatch::Routing.edit_movie_path
end
end
See that Modules Magic for more.
Unless I misunderstand what you're asking, how about something like:
module ActionDispatch
module Routing
def add_movie_path
end
def edit_movie_path
end
end
end
Alternatively, you could use module_eval.
Simply put your methods inside the module.
module ActionDispatch
module Routing
def add_movie_path
end
def edit_movie_path
end
end
end
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
I am writing my first Rails gem, which adds a method to ActiveRecord. I can't seem to figure out a simple way to call other methods from within the method I am adding to ActiveRecord. Is there a pattern for this I should be using?
module MyModule
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def my_class_method
# This doesn't work
some_utility_method
end
end
def some_utility_method
# Do something useful
end
end
ActiveRecord::Base.send(:include, MyModule)
Once you've included MyModule, ActiveRecord::Base will have my_class_method as a class method (equivalently, an instance method of the Class object ActiveRecord::Base), and some_utility_method as an instance method.
So, inside my_class_method, self is the Class ActiveRecord::Base, not an instance of that class; it does not have some_utility_method as an available method
Edit:
If you want a utility method private to the Module, you could do it like this:
module MyModule
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def my_class_method
# This doesn't work
MyModule::some_utility_method
end
end
def self.some_utility_method
# Do something useful
end
end
ActiveRecord::Base.send(:include, MyModule)