We have the following sweeper in a rails application:
class AgencyEquipmentTypeSweeper < ActionController::Caching::Sweeper
observe AgencyEquipmentType
#include ExpireOptions
def after_update(agency_equipment_type)
expire_options(agency_equipment_type)
end
def after_delete(agency_equipment_type)
expire_options(agency_equipment_type)
end
def after_create(agency_equipment_type)
expire_options(agency_equipment_type)
end
def expire_options(agency_equipment_type)
Rails.cache.delete("agency_equipment_type_options/#{agency_equipment_type.agency_id}")
end
end
We'd like to extract the after_update, after_delete, and after_create callbacks to a module called "ExpireOptions"
The module should look like this (with the 'expire_options' method staying behind in the
original sweeper):
module ExpireOptions
def after_update(record)
expire_options(record)
end
def after_delete(record)
expire_options(record)
end
def after_create(record)
expire_options(record)
end
end
class AgencyEquipmentTypeSweeper < ActionController::Caching::Sweeper
observe AgencyEquipmentType
include ExpireOptions
def expire_options(agency_equipment_type)
Rails.cache.delete("agency_equipment_type_options/#{agency_equipment_type.agency_id}")
end
end
BUT the cache expirations only work if we define the methods explicitly inside the sweeper. Is there an easy way to extract those callback methods to a module, and still have them work?
Try with:
module ExpireOptions
def self.included(base)
base.class_eval do
after_update :custom_after_update
after_delete :custom_after_delete
after_create :custom_after_create
end
end
def custom_after_update(record)
expire_options(record)
end
def custom_after_delete(record)
expire_options(record)
end
def custom_after_create(record)
expire_options(record)
end
end
I would try something like:
module ExpireOptions
def after_update(record)
self.send(:expire_options, record)
end
def after_delete(record)
self.send(:expire_options, record)
end
def after_create(record)
self.send(:expire_options, record)
end
end
This should make sure it does not try to call those methods on the module, but on self which would hopefully be the calling object.
Does that help?
Related
I have the following class
class EvaluateService
def initialize
end
def get_url
end
def self.evaluate_service
#instance ||= new
end
end
class CheckController < ApplicationController
def index
get_url = EvaluateService.get_url
end
end
The problem here is that i know that i can do evaluate_service = EvaluateService.new and use the object evaluate_service.get_url and it will work fine but i also know that some frown upon the idea of initializing the service object this way and rather there is a way of initializing it via a call, send method in the service class.
Just wondering how do i do this?
I think what you're looking for is something like:
class Evaluate
def initialize(foo)
#foo = foo
end
def self.call(foo)
new(foo).call
end
def call
url
end
private
def url
# Implement me
end
end
Now you can do this in your controller:
class CheckController < ApplicationController
def index
#url = Evaluate.call(params)
end
end
The reason some prefer #call as the entry point is that it's polymorphic with lambdas. That is, anywhere you could use a lambda, you can substitute it for an instance of Evaluate, and vice versa.
There are various ways to approach this.
If the methods in EvaluateService don't need state, you could just use class methods, e.g.:
class EvaluateService
def self.get_url
# ...
end
end
class CheckController < ApplicationController
def index
#url = EvaluateService.get_url
end
end
In this scenario, EvaluateService should probably be a module.
If you want a single global EvaluateService instance, there's Singleton:
class EvaluateService
include Singleton
def get_url
# ...
end
end
class CheckController < ApplicationController
def index
#url = EvaluateService.instance.get_url
end
end
But global objects can be tricky.
Or you could use a helper method in your controller that creates a service instance (as needed) and memoizes it:
class EvaluateService
def get_url
# ...
end
end
class CheckController < ApplicationController
def index
#url = evaluate_service.get_url
end
private
def evaluate_service
#evaluate_service ||= EvaluateService.new
end
end
Maybe even move it up to your ApplicationController.
I have some helpers and private methods in a controller, and I want to have the same helpers and private methods in another controller. So I moved that code to module and tried to include the module in the second controller. But I can't seem to do it, because it says undefined method helper method for the DashboardHelper. Is there anyway to accomplish what I am trying to do?
Here is the code
module DashboardHelper
def get_date(log)
end
def get_working_hours(log)
end
helper_method :get_date, :get_working_hours
private
def employee_params
end
def identify_employee
end
def check_is_arrived
end
def calculate_time_percentage
end
end
class AccountController < ApplicationController
include DashboardHelper
end
hello gates you have to include extend ActiveSupport::Concern in your concern .
This should not be in your helper folder instead pull it somewhere in you concern folder
the end file may look like
module DashboardHelper
extend ActiveSupport::Concern
module ClassMethods
def get_date(log)
end
def get_working_hours(log)
end
helper_method :get_date, :get_working_hours
private
def employee_params
end
def identify_employee
end
def check_is_arrived
end
def calculate_time_percentage
end
end
end
I need to access a class method (defined in ClassMethods) in an instance method inside a concern.
My brain is melted and I'm sure that is a simple thing that I'm doing wrong.
I need to access comparable_opts inside comparison. How can I do it?
Follow snippets below:
Concern
# app/models/concerns/compare.rb
module Compare
extend ActiveSupport::Concern
attr_accessor :comparable_opts
module ClassMethods
attr_reader :arguable_opts
def comparable_opts
##comparable_opts
end
private
def default_opts
#default_opts ||= {fields: [:answers_count,
:answers_correct_count,
:answers_correct_rate,
:users_count]}
end
def compare(opts={})
#comparable_opts = default_opts.merge(opts)
end
end
def comparison
end
end
Model
# app/models/mock_alternative.rb
class MockAlternative < ActiveRecord::Base
include Compare
belongs_to :mock, primary_key: :mock_id, foreign_key: :mock_id
compare fields: [:answers_count, :question_answers_count, :question_answers_rate],
with: :mock_aternative_school
def question_answers_rate
self[:answers_count].to_f/self[:question_answers_count].to_f
end
end
Solution:
I've just used cattr_accessor in my method compare. Thank everyone.
module Compare
extend ActiveSupport::Concern
module ClassMethods
attr_reader :arguable_opts
def comparison_klass
"ActiveRecord::#{comparable_opts[:with].to_s.classify}".constantize
end
private
def default_opts
#default_opts ||= {fields: [:answers_count,
:answers_correct_count,
:answers_correct_rate,
:users_count]}
end
def compare(opts={})
cattr_accessor :comparable_opts
self.comparable_opts = default_opts.merge(opts)
end
end
def comparison
comparable_opts
end
end
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 have a model which specifies a before destroy callback. Something like:
Class User < ActiveRecord::Base
before_destroy :do_destroy
acts_as_destroyable
private
def do_destroy
puts "A"
end
end
module ActsAsDestroyable
def self.included(base)
base.send(:extend, ActsMethods)
end
module ActsMethods
def acts_as_destroyable(options = {})
self.send(:include, InstanceMethods)
end
module InstanceMethods
def do_something
puts "A0"
end
def self.included(base)
base.before_destroy :do_something
end
end
end
end
Now since the Destroyable module works with Users's assosiacions it needs to have its "do_domething" method executed before the "do_destroy" callback. Any ideas how can I move it up the callback queue?