How to Rspec system that ActionCable broadcast messages appear in the view - ruby-on-rails-5

I want to test that messages that are broadcast when some background jobs are completed are actually appearing in the view.
I have unit tests for this which work fine. I would actually like to ensure that the JS gets run so that the view is updated with the correct message.
So far I have not been able to find any way to do this.
Here is the test I have where I would like to add the expectation for the broadcast message:
require 'rails_helper'
require 'sidekiq/testing'
RSpec.describe 'sending a quote request', js: true do
let(:quote_request_form) { build(:quote_request_form) }
before do
create(:job_rate, :proofreading)
create(:proofreader_with_work_events)
end
it 'shows the user their quotation' do
visit new_quote_request_path
fill_in("quote_request_form_name", with: quote_request_form.name)
fill_in("quote_request_form_email", with: quote_request_form.email)
attach_file('customFile','/Users/mitchellgould/RailsProjects/ProvenWordNew/spec/test_documents/quote_request_form/1.docx', make_visible: true)
click_on "Submit"
Sidekiq::Testing.inline! do
page.execute_script("$('#invisible-recaptcha-form').submit()")
expect(current_path).to eq(quote_confirm_path)
#add expectation here:
expect(page).to have_content("Calculating Time Required")
page.execute_script("window.location.pathname = '#{quotation_path(Quotation.first)}'")
expect(current_path).to eq(quotation_path(Quotation.first))
expect(page).to have_content("Here is your quotation")
end
end
end
Here is my .coffee file:
$(document).on 'turbolinks:load', ->
if $("meta[name='current_user']").length > 0
App.notification = App.cable.subscriptions.create "NotificationChannel",
connected: ->
# Called when the subscription is ready for use on the server
disconnected: ->
# Called when the subscription has been terminated by the server
received: (data) ->
$('.background_message').html(data.content)
if data.head == 302 && data.path
window.location.pathname = data.path
else if App.notification
App.quotation.unsubscribe()
delete App.notification
Here is one of the background jobs that broadcasts a message when its done:
class CreateParagraphDetailsJob < ApplicationJob
queue_as :default
after_perform :broadcast_message, :calculate_proofreading_job_duration
def perform(document, proofreading_job_id, current_user_id)
document.create_paragraph_details
end
private
def calculate_proofreading_job_duration
CalculateDurationJob.set(wait: 1.seconds).perform_later proofreading_job_id, current_user_id
end
def broadcast_message
ActionCable.server.broadcast "notification_channel_user_#{current_user_id}", content: "Analyzed writing quality of paragraphs"
end
def document
self.arguments.first
end
def proofreading_job_id
self.arguments.second
end
def current_user_id
self.arguments.last
end
end
Any ideas on how to do this?

Related

Devise async with Mandrill mailer wrong number of arguments (1 for 2)

I'm using delayed_job to handle background jobs. I recently came across the devse_async gem to integrated delayed_job and devise.
I am receiving a wrong number of arguments (1 for 2) error when delayed_job processes the email job in the queue. I should note I'm using Mandrill's API to send these emails.
delayed job console
Starting job worker
[Worker(host:MacBook-Pro.local pid:21007)] Job Devise::Async::Backend::DelayedJob#perform (id=1) RUNNING
[Worker(host:MacBook-Pro.local pid:21007)] Job Devise::Async::Backend::DelayedJob#perform (id=1) FAILED (0 prior attempts) with ArgumentError: wrong number of arguments (1 for 2)
[Worker(host:MacBook-Pro.local pid:21007)] 1 jobs processed at 30.3564 j/s, 1 failed
trace
wrong number of arguments (1 for 2)
/Users/Sites/app/mailers/devise_mailer.rb:6:in `confirmation_instructions'
devise_mailer
class DeviseMailer < MandrillMailer::TemplateMailer
include Devise::Controllers::UrlHelpers
include Devise::Mailers::Helpers
default from: 'no-reply#foobar.com'
def confirmation_instructions(record, token)
#resource = record
# Route mailer to send the proper subject and template
if #resource.pending_reconfirmation?
confirmation_template = 'Reconfirmation Instructions'
confirmation_subject = 'Confirm your new email'
else
confirmation_template = 'Confirmation Instructions'
confirmation_subject = 'Confirmation Email'
end
# Include proper greeting in email based on User type
recipient_name = nil
if #resource.type == "Business"
recipient_name = record.business_name
else
recipient_name = record.first_name
end
puts "sending confirmation email"
host = ActionMailer::Base.default_url_options[:host]
mandrill_mail template: confirmation_template,
subject: confirmation_subject,
from_name: 'foobar',
to: { email: 'contact#foobar.ca' },
vars: {
'FNAME' => recipient_name,
'LIST_COMPANY' => "foobar",
'HTML_LIST_ADDRESS_HTML' => "foobar",
'CONFIRMATION_LINK' => "%s/users/confirmation?confirmation_token=#{record.confirmation_token}" % ENV['MAIL_HOST']
# "http://0.0.0.0:3000/users/confirmation?confirmation_token=#{record.confirmation_token}"
},
async: true
end
end
registrations_controller.rb
def new
build_resource({})
resource.build_supp_form
respond_with self.resource
end
def create
super
end
The use of database encrypted tokens was introduced in devise 3.1 (https://github.com/plataformatec/devise/blob/master/CHANGELOG.md#310---2013-09-05), so your confirmation_instructions mailer method doesn't expect any token as the second parameter.
In fact you're not using that parameter anywhere in your method, notice you call record.confirmation_token.
Just remove the second parameter in the method signature and you should be good to go:
def confirmation_instructions(record)
...
end

Carrierwave: set image path and skip the upload

I would like to set some images without uploading. (They already exist, or another task saves them...)
If I try (in rails console):
user = User.last
user.picture = '/some/picture.jpg'
user.save
user.picture # nil
The only way to do that is to set remote_picture_url, and then delete the upload (which is stupid)
Is there any method in carrierwave that lets you modify only the filename ?
class User < ActiveRecord::Base
attr_accessible :picture
# Don't want to write to the database, but want to be able to check
attr_writer :skip
# set a default value
def skip
#skip ||= false
end
mount_uploader :image, PictureUploader
# Make sure that the skip callback comes after the mount_uploader
skip_callback :save, :before, :store_picture!, if: :skip_saving?
# Other callbacks which might be triggered depending on the usecase
#skip_callback :save, :before, :write_picture_identifier, id: :skip_saving?
def skip_saving?
skip
end
end
class PictureUploader < Carrierwave::Uploader::Base
# You could also implement filename=
def set_filename(name)
#filename = name
end
end
Assuming you have the setup above, in your console:
user = User.last
user.picture.set_filename('/some/picture.jpg')
user.skip = true
# Save will now skip the callback store_picture!
user.save
user.picture # /some/picture.jpg
It should be noted that if you're in the console and you update an existing record that has an attached file (ie user.picture.file) it will show the old url/location. If you quit the console (assuming you're not in sandbox mode) and come back and query the same object it will have the updated url/location.

How to stop a helper method from applying to a specific controller?

I have a helper_method that allows links to escape from a subdomain. However it is impacting my videos_controller, as it essentially seems to negate the 'current_event' method when not in the events controlller.
I've tried several dozen different ways over the last 4 days to make it so I can still escape my links from the subdomain, but still allow the videos_controller to work.
I think the best way to achieve this is to exclude the videos_controller from the helper method, but I'm not sure how (or if it is actually the best way forward - I'm obviously a noob!) Any suggestions please?! Relevant code below:
module UrlHelper
def url_for(options = nil)
if request.subdomain.present? and request.subdomain.downcase != 'www' and !options.nil? and options.is_a?(Hash) and options.has_key? :only_path and options[:only_path]
options[:only_path] = false
end
super
end
end
Videos_controller
def new
if current_event?
#video = current_event.videos.new
else
#video = Video.new
end
end
def create
if current_event.present?
#video = current_event.videos.new(params[:video])
#video.user_id = current_user.id
key = get_key_from_the_cloud
#video.key = key
else
#video = current_user.videos.new(params[:video])
#video.user_id = current_user.id
key = get_key_from_the_cloud
#video.key = key
end
if #video.save
flash[:success] = "Video uploaded!"
redirect_to root_url(subdomain: => current_event.name)
else
flash[:error] = "#{#video.errors.messages}"
render :new
end
end
current_event method
def current_event
if request.subdomain.present?
#event = Event.find_by_name(request.subdomain)
end
end
Did you take a look at this post yet?
You might want to create a new function test that only does something like
module UrlHelper
def test
puts "Test is called"
end
end
If that works you know its not including that fails but it has to be the method.
Otherwise you know the module is not included and you can narrow down the search.

ActiveRecord: Is it possible to get the number of DB queries executed in addition to total time in the Rails log?

For every request, I get this in the logs:
Completed 200 OK in 854ms (Views: 1.0ms | ActiveRecord: 17.0ms)
Is it possible to get it to also include the number of queries?
Something like:
Completed 200 OK in 854ms (Views: 1.0ms | ActiveRecord: 17.0ms | Queries: 10)
Ideally, I'd like all the "cached" ones to show up in that count too. Ie, even if the "cache" is saving me from "N+1" queries from hitting the DB, I still want to know I have a problem.
I'm fine with monkeypatching / manually editing something, since I really want this just for my dev box.
(If this can be made civilizedly so I can have it in production, that's even better, but if not, I'm fine with just having a manually modified Rails in my own machine)
Thanks!
Daniel
I know the ThinkingSphinx gem does something quite like this, adding the time spent running Sphinx queries to the summary in the log. You can probably do something similar ( maybe by making your own gem, since I bet other people would appreciate this functionality) to make the number of queries appear.
I haven't really looked hard at how it works, but it looks like modifications to ActionController and LogSubscriber are responsible:
lib/thinking_sphinx/action_controller.rb:
module ThinkingSphinx
module ActionController
extend ActiveSupport::Concern
protected
attr_internal :query_runtime
def cleanup_view_runtime
log_subscriber = ThinkingSphinx::ActiveRecord::LogSubscriber
query_runtime_pre_render = log_subscriber.reset_runtime
runtime = super
query_runtime_post_render = log_subscriber.reset_runtime
self.query_runtime = query_runtime_pre_render + query_runtime_post_render
runtime - query_runtime_post_render
end
def append_info_to_payload(payload)
super
payload[:query_runtime] = query_runtime
end
module ClassMethods
def log_process_action(payload)
messages, query_runtime = super, payload[:query_runtime]
messages << ("Sphinx: %.1fms" % query_runtime.to_f) if query_runtime
messages
end
end
end
end
lib/thinking_sphinx/active_record/log_subscriber.rb:
require 'active_support/log_subscriber'
module ThinkingSphinx
module ActiveRecord
class LogSubscriber < ActiveSupport::LogSubscriber
def self.runtime=(value)
Thread.current['thinking_sphinx_query_runtime'] = value
end
def self.runtime
Thread.current['thinking_sphinx_query_runtime'] ||= 0
end
def self.reset_runtime
rt, self.runtime = runtime, 0
rt
end
def initialize
super
#odd_or_even = false
end
def query(event)
self.class.runtime += event.duration
return unless logger.debug?
identifier = color('Sphinx Query (%.1fms)' % event.duration, GREEN, true)
query = event.payload[:query]
query = color query, nil, true if odd?
debug " #{identifier} #{query}"
end
def message(event)
return unless logger.debug?
identifier = color 'Sphinx', GREEN, true
message = event.payload[:message]
message = color message, nil, true if odd?
debug " #{identifier} #{message}"
end
def odd?
#odd_or_even = !#odd_or_even
end
def logger
return #logger if defined? #logger
self.logger = ::ActiveRecord::Base.logger
end
def logger=(logger)
#logger = logger
end
attach_to :thinking_sphinx
end
end
end
I hope this helps.

Rails 3: Trying to extend Action Mailer with a module

Trying to rewrite an old alias_method_chain to add a filter on outgoing emails, and it isn't working. I'm pretty sure I've leaving something out/missing something, but I don't know what.
This file is in /lib/outgoing_mail_filter.rb, which is loaded with config/initializers/required.rb
Here's the old code that worked under Rails 2:
class ActionMailer::Base
def deliver_with_recipient_filter!(mail = #mail)
unless 'production' == Rails.env
mail.to = mail.to.to_a.delete_if do |to|
!(to.ends_with?('some_domain.com'))
end
end
unless mail.to.blank?
deliver_without_recipient_filter!(mail)
end
end
alias_method_chain 'deliver!'.to_sym, :recipient_filter
end
And here's my current attempt at re-writing it:
class ActionMailer::Base
module RecipientFilter
def deliver(mail = #mail)
super
unless 'production' == Rails.env
mail.to = mail.to.to_a.delete_if do |to|
!(to.ends_with?('some_domain.com'))
end
end
unless mail.to.blank?
deliver(mail)
end
end
end
include RecipientFilter
end
When I run my tests, it doesn't even look like this is being called or anything. Any help is appreciated
I'm using mail_safe to rewrite emails in the development environment, highly recommended. You could look into it for inspiration if it doesn't fit your bill, the code is very simple.
The following code is extracted from /lib/mail_safe/rails3_hook.rb and should do what you want:
require 'mail'
module MailSafe
class MailInterceptor
def self.delivering_email(mail)
# replace the following line with your code
# and don't forget to return the mail object at the end
MailSafe::AddressReplacer.replace_external_addresses(mail) if mail
end
::Mail.register_interceptor(self)
end
end
Alternate version, registering with ActionMailer::Base instead of Mail (thanks to Kevin Whitaker for letting me know it's possible):
module MailSafe
class MailInterceptor
def self.delivering_email(mail)
# replace the following line with your code
# and don't forget to return the mail object at the end
MailSafe::AddressReplacer.replace_external_addresses(mail) if mail
end
::ActionMailer::Base.register_interceptor(self)
end
end