Skip to content

Instantly share code, notes, and snippets.

@RedSoxFan22
Last active August 29, 2015 14:23
Show Gist options
  • Save RedSoxFan22/fe6bc6f2480b3a3dbc97 to your computer and use it in GitHub Desktop.
Save RedSoxFan22/fe6bc6f2480b3a3dbc97 to your computer and use it in GitHub Desktop.
function disableButton(){
var button = document.getElementById("submit");
setTimeout(function() [button.disabled = true;}, 1);
}
SQL
IN: let's you pass in multiple things. similar to Ruby include
removing N+1 queries is the best way to speed up runtimes
.joins:
AREL:
INNER JOIN clause: doesn't work for polymorphic tables .
10:45
WHERE a.answer IS NULL
Mailers 11:00
1. Work in the background (asynchronously - meaning not time with the time frame in whcih the response comes back to the user)
2. mail ActiveJob is a framework for interafcing with background processes. Background processes is Que approach ...FIFO...First In First Out.
Using Delayed Job 11:12
add to gemfile 'delayed_job_active_record'
generate delayed job rails generate delayed_job:active_record
rake db:migrate
We need to tell Rails in tha config which gem we're using, 11:37: config.active_job.queue_adapter = :delayed_job
Make a job: rails generate job PotatoMaker
*****tell it to explicitly include the jobs in config 11:47*******
add gem 'daemons'
(in potato_maker_job.rb: def perform(name, species, genus))
Turn on delayed job
Mailers have views 12:12 rails g mailer PotatoMailer subscribe unsubscribe daily_digest
class ApplicationMailer <ActionMailer::Base
default from: "danai@potatomasters.com" (where all the emails are being sent from)
layout 'mailer' (telling it to use the mailer layout)
end
instance variable are set in mailers (html.erb) the same way they are set in controllers. instance variable are set here and passed to the views.
How do you make a mailer mail? 12:23 Config 12:26 In console, PotatoMailer.subscribe
(2. Send mail
Mailer method:
def todo_list(todo_list, destination)
@user = todo_list.user
@todo_list = todo_list
mail(to: destination, subject: "#{@user.first_name} sent you a todo list")
end)
HOMEWORK
Your task tonight is to offload the generation of this index page to a background job. Instead of the index page showing the list, it should ask the user for an e-mail address. The user should be able to enter his or her e-mail address, then be immediately presented with a confirmation notice stating that the information will be sent to that e-mail address.
Your controller action which receives this e-mail address should queue up a message to be delivered later via a background process. When the background job is picked up off the queue, that process should do the work of querying the database and generating the report. The report should then be e-mailed to that e-mail address. Email address kicks off the job. Form needs to send you to a page throug the router.
1. index page should ask for email address
2. user enter email
3. modal for confirmation message on submit, will need boostrap to do modal
4. submit also trigger controller action
5. mailer (job?) action queue up perform_later method?
6. perform_later method: query the database for index page, send via email
7. deliver_later to send it to delayed jobs?
8. disable button so it can't be submitted more than once
CONTROLLER>>>>MAILER>>>>VIEWS
Add gems 'delayed_job_active_record' & 'daemon'
bundle install
in config/application.rb: in config.active_jo...
rails generate delayed_job: active_record
rake db:migrate
rails generate job JobName
def thank_you (*args)
Family.create (
SendStatsJob.perform_later
end
Add gems and config/application
rails generate delayed_job:active_record
rake db:migrate
rails generate job JobName
bin/delayed_job start
Somewhere in our code: JobName.perform_later(params[:something_important]
...when you are done coding:
bin/delayed_job stop
rails g mailer MailerNameMailer action_name other_action name
Modify views and mailers as you see fit
Add gmail style config to environment/development.rb: http://guides.rubyonrails.org/action_mail
Somewhere in our code: MailerNameMailer.other_action_name.deliver_now
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment