Rails: Run a controller action from code - ruby-on-rails

I would like to automate a task in my Rails app, create a rake task, almost all the code I need is in a controller action, I would like to just call that controller action instead of write the "same" code in my task and save some lines of code. Is that possible?

Well, for example. Say you have usecase to auto renew Stripe customers subscriptions from both Rake tasks and using client side . Now, you will write a PORO class like below:
class AutoRenewSubscription
attr_reader :coupon, :email
def initialize args = {}
#email = args[:email]
#coupon = args[:coupon]
#....
end
def run!
user_current_plan.toggle!(:auto_renew)
case action
when :resume
resume_subscription
when :cancel
cancel_subscription
end
end
#... some more code as you need.
end
You can put the class inside app/services/auto_renew_subscription.rb folder. Now this class is available globally. So, call it inside the controller like :
class SubscriptionController < ApplicationController
def create
#.. some logic
AutoRenewSubscription.new(
coupon: "VXTYRE", email: 'some#email.com'
).run!
end
end
And do call it from your rake task also :
desc "This task is to auto renew user subscriptions"
task :auto_renew => :environment do
puts "auto renew."
AutoRenewSubscription.new(
coupon: "VXTYRE", email: 'some#email.com'
).run!
end
This is what I think good way to solve your issue. Hope you will like my idea. :)

Related

How to count all objects in Database via Active Record

What I'd like to do is count all objects in the database. I started with something like this:
p ["TOTAL COUNT", ApplicationRecord.subclasses.sum(&:count)]
But while experimenting I found...
[5] pry(main)> ApplicationRecord.subclasses.count => 6
Which I expected to return a lot more than that. I can inspect the subclasses and find that some are missing.
Then I found....
[8] pry(main)> ActiveRecord::Base.descendants.count => 10
Which added a few more. Again I can inspect them individually, and I noticed a few were missing. Here is an example of one that is missing...
class MerchantsPrincipal < ApplicationRecord
end
class Principal < MerchantsPrincipal
end
How can I make sure those are also included?
This is not the answer for your question, but a suggestion for you in order to speed up your test suite.
You can do some caching with FactoryGirl, something like this:
class RecordCache
def self.[](key)
all.fetch(key)
end
def self.register(name, object)
all[name] = object
end
def self.setup!
register :admin_user, FactoryGirl.create(:user, is_admin: true)
end
private
def all
#all ||= {}
end
end
Then you need to call RecordCache.setup! in your test_helper.rb before running your test suite.
After that, you will be able to ask this RecordCache to provide the instance instead of making FactoryGirl create it again:
FactoryGirl.define do
factory :post do
# title content etc.
user { RecordCache[:admin_user] }
end
end
So that every time you call FactoryGirl.create(:post), it does not create another user. This brings some concerns, as the same record is cached through the app and should not be modified. But if you want a specific user for a specific context, you can still do:
FactoryGirl.create(:post, user: FactoryGirl.create(:user, :super_admin))

Rspec controller test - how to stub and/or test custom objects

Below is passing!
Controller code:
class OrdersController
def create
...
#order.save
end
end
Spec code:
describe OrdersController do
it "should call save method" do
Order.any_instance.should_receive(:save)
post :create
end
end
But if only it were that easy... I have some custom job objects that are executed after the save, so the code actually looks like this:
Controller code:
class OrdersController
def create
...
#order.save
RoadrunnerEmailAlert.new.async.perform(#order.id, true)
CalendarInvite.new.async.perform(#order.id)
RoadrunnerTwilioAlert.new.async.perform(#order.id)
end
end
I would love to test that the custom objects are receiving the chain of methods with the right parameters, but not sure how, short of creating something in the spec code like this:
before do
class RoadrunnerEmailAlert
def async
end
end
end
But that's so contrived, it certainly isn't right... advice appreciated!
In case this helps other people... this is a very comprehensive answer.
Context & design notes
The async framework is Sucker Punch gem
(http://brandonhilkert.com/blog/why-i-wrote-the-sucker-punch-gem/).
Back then, this was the easiest thing for me to use after looking at
Delayed Job, Sidekick, etc
Basically it works like this: in Controller reference a Job that then references anything else (in my case, some POROs)
If I were really rigidly testing, I'd want to test that A) the Controller calls the Job appropriately and passes the right parameters, and B) the Job calls the appropriate POROs and passes the right parameters. But instead, I just tested that the Controller calls the appropriate POROs and passes the right parameters, i.e., the Jobs are already working.
Controller code
#order.save
RoadrunnerEmailAlert.new.async.perform(#order.id, true)
CalendarInvite.new.async.perform(#order.id)
RoadrunnerTwilioAlert.new.async.perform(#order.id)
Job code
# app/jobs/roadrunner_email_alert.rb
class RoadrunnerEmailAlert
include SuckerPunch::Job
def perform(order_id, require_tos)
ActiveRecord::Base.connection_pool.with_connection do
OrderMailer.success_email(order_id, require_tos).deliver
end
end
end
# app/jobs/calendar_invite.rb
class CalendarInvite
include SuckerPunch::Job
def perform(order_id)
ActiveRecord::Base.connection_pool.with_connection do
CreateCalendar.new(order_id).perform
end
end
end
# app/jobs/roadrunner_twilio_alert.rb
class RoadrunnerTwilioAlert
include SuckerPunch::Job
def perform(order_id)
ActiveRecord::Base.connection_pool.with_connection do
CreateAlert.new(order_id).perform
end
end
end
Test code
The really big thing here that I don't know why I keep forgetting (but only in testing) is class vs. instance of class. For the POROs, since I'm instantiating the object, I needed to test 2 different "layers" (first that the object is instantiated appropriately, second that the instantiated object is acted upon appropriately).
require 'sucker_punch/testing/inline'
describe "Controller code" do
before do
OrderMailer.any_instance.stub(:success_email)
mock_calendar = CreateCalendar.new(1)
CreateCalendar.stub(:new).and_return(mock_calendar)
CreateCalendar.any_instance.stub(:perform)
mock_alert = CreateAlert.new(1)
CreateAlert.stub(:new).and_return(mock_alert)
CreateAlert.any_instance.stub(:perform)
end
it "should call appropriate async jobs" do
expect_any_instance_of(OrderMailer).to receive(:success_email).with(1, true)
expect(CreateCalendar).to receive(:new).with(1)
expect_any_instance_of(CreateCalendar).to receive(:perform)
expect(CreateAlert).to receive(:new).with(1)
expect_any_instance_of(CreateAlert).to receive(:perform)
post :create
end
end

rails - Delayed jobs without perform method

After make a big class with many method, I want all of these method be called in a delayed jobs.
But the practice of the Delayed::job, is that you have to create a class with a perform method, like that :
class Me < Struct.new(:something)
def perform
puts "GO"
end
end
and call it like :
Delayed::Job.enqueue Me.new(1)
But the problem is that my class as already many method like this type
class NameHandler
def self.init
ap "TODO : Delayed jobs "
end
def self.action_one
...
end
def self.action_two
...
end
etc.
end
and I want to call it like :
Delayed::Job.enqueue NameHandler.action_one params...
Theres is an best practice for that ? Or I have to follow the classic Delayed::job way and lose many times ?
In the README it has a number of ways:
Me.new.delay.action_one
or
class NameHandler
handle_asynchronously :action_one
def action_one
end
def self.action_one
new.action_one
end
end
NameHandler.action_one

Resque starts jobs, but do nothing

I'm using resque to do some (long time) job. And I have a few classes with the same mixed-in module for queuing. Class Service substitutes in tests, that's why it standalone and (maybe too much) complicated. So the story is when I call
Campaign.perform(user_id)
directly, everything works fine, but when I try to use queue:
Resque.enqueue(Campaign, user_id)
Job created, but seems like do nothing. At least, nothing saves into the database. Which is main task of Campaign class. I can see in resque-web-interface that jobs creates and finished, and finished (to fast, almost just after create) but no result.
I'm new in Resque and not really sure it calls it all (confused how to debug it).
Does anybody have similar problem? thanks for any help.
Module:
module Synchronisable
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def perform(user_id)
save_objects("#{self.name}::Service".constantize.get_objects(user_id))
end
protected
def save_objects(objects)
raise ArgumentError "should be implemented"
end
end
class Service
def self.get_objects(user)
raise ArgumentError "should be implemented"
end
end
end
One of the classes:
class Campaign < ActiveRecord::Base
include Synchronisable
#queue = :app
class << self
protected
def save_objects(objects)
#some stuff to save objects
end
end
class Service
def self.get_objects(user_id)
#some stuff to get objects
end
end
end
This is a very old question so not sure how rails folder structure was back then but I had the same problem and issue was with inheritance. Seems if you are using Resque your job classes shouldn't inherit from ApplicationJob.
so if your code was like this in (app/jobs/campaign_job.rb):
class Campaign < ApplicationJob
#queue = :a_job_queue
def self.perform
#some background job
end
end
then remove the inheritance i.e "< ApplicationJob"
These jobs are almost certainly failing, due to an Exception. What is resque-web showing you on the Failures tab? You can also get this from the Rails console with:
Resque.info
or
Resque::Failure.all(0)
You should run your worker like this:
nohup QUEUE=* rake resque:work & &> log/resque_worker_QUEUE.log
This will output everything you debug to "log/resque_worker_QUEUE.log" and you will be able to find out what's wrong with your Campaign class.
Try this:
env TERM_CHILD=1 COUNT=2 "QUEUE=*" bundle exec rake resque:workers

How would you test observers with rSpec in a Ruby on Rails application?

Suppose you have an ActiveRecord::Observer in one of your Ruby on Rails applications - how do you test this observer with rSpec?
You are on the right track, but I have run into a number of frustrating unexpected message errors when using rSpec, observers, and mock objects. When I am spec testing my model, I don't want to have to handle observer behavior in my message expectations.
In your example, there isn't a really good way to spec "set_status" on the model without knowledge of what the observer is going to do to it.
Therefore, I like to use the "No Peeping Toms" plugin. Given your code above and using the No Peeping Toms plugin, I would spec the model like this:
describe Person do
it "should set status correctly" do
#p = Person.new(:status => "foo")
#p.set_status("bar")
#p.save
#p.status.should eql("bar")
end
end
You can spec your model code without having to worry that there is an observer out there that is going to come in and clobber your value. You'd spec that separately in the person_observer_spec like this:
describe PersonObserver do
it "should clobber the status field" do
#p = mock_model(Person, :status => "foo")
#obs = PersonObserver.instance
#p.should_receive(:set_status).with("aha!")
#obs.after_save
end
end
If you REALLY REALLY want to test the coupled Model and Observer class, you can do it like this:
describe Person do
it "should register a status change with the person observer turned on" do
Person.with_observers(:person_observer) do
lambda { #p = Person.new; #p.save }.should change(#p, :status).to("aha!)
end
end
end
99% of the time, I'd rather spec test with the observers turned off. It's just easier that way.
Disclaimer: I've never actually done this on a production site, but it looks like a reasonable way would be to use mock objects, should_receive and friends, and invoke methods on the observer directly
Given the following model and observer:
class Person < ActiveRecord::Base
def set_status( new_status )
# do whatever
end
end
class PersonObserver < ActiveRecord::Observer
def after_save(person)
person.set_status("aha!")
end
end
I would write a spec like this (I ran it, and it passes)
describe PersonObserver do
before :each do
#person = stub_model(Person)
#observer = PersonObserver.instance
end
it "should invoke after_save on the observed object" do
#person.should_receive(:set_status).with("aha!")
#observer.after_save(#person)
end
end
no_peeping_toms is now a gem and can be found here: https://github.com/patmaddox/no-peeping-toms
If you want to test that the observer observes the correct model and receives the notification as expected, here is an example using RR.
your_model.rb:
class YourModel < ActiveRecord::Base
...
end
your_model_observer.rb:
class YourModelObserver < ActiveRecord::Observer
def after_create
...
end
def custom_notification
...
end
end
your_model_observer_spec.rb:
before do
#observer = YourModelObserver.instance
#model = YourModel.new
end
it "acts on the after_create notification"
mock(#observer).after_create(#model)
#model.save!
end
it "acts on the custom notification"
mock(#observer).custom_notification(#model)
#model.send(:notify, :custom_notification)
end

Resources