Rails 5 Notes
Active Jobs
Create Simple Jobs that call a Service Object to do the work
class ImportLegislatorJob < ApplicationJob
queue_as :default
def perform(leg_id)
LegislatorImporter.new.import_legislators(cutoff)
end
end
app/jobs/import_legislators_job.rb
Service Objects
class LegislatorImporter
def initialize
@api_url = "http://www2.leg.state.co.us/Public/cgaAPI.nsf/api.xsp/"
end
# Import all Legislators since cutoff
def import_legislators(cutoff)
url = "#{@api_url}directory/legislators/lm/#{cutoff}"
uri = URI(url)
response = Net::HTTP.get(uri)
j = JSON.parse(response)
# loop through each entry and queue job to Import the Legislator later
j.each do |row|
ImportLegislatorJob.perform_later row["memberId"]
end
end
end
app/services/legislator_importer.rb
Configure Spring to watch your Service Objects
Add the following to config.application.rb to tell Spring to watch and autoload the services dir
Spring.watch "app/services/**"
Sass autoloading
Rails creates a folder app/assets/stylesheets/ and adds a new scss file for each of your resources. Just add SASS or CSS to these files and they will be autoloaded during development.