Skip to content

Instantly share code, notes, and snippets.

View christopherstyles's full-sized avatar

Chris Styles christopherstyles

View GitHub Profile
@christopherstyles
christopherstyles / service_objects_verb_first.rb
Last active August 29, 2015 14:04
Service objects, verb-first
# Illustrate the usage of service objects named in the verb-first form.
# a test user
user = User.where(name: 'Oleg').first
SubscribeUserToList.call(user)
# => "Oleg was added to the mailing list."
UnsubscribeUserFromList.call(user)
# => "Oleg was removed from the mailing list."
@christopherstyles
christopherstyles / service_objects_noun_first.rb
Last active August 29, 2015 14:04
Service objects, noun-first
# Illustrate the usage of service objects named in the noun-first form.
# a test user
user = User.where(name: 'Oleg').first
UserListSubscription.subscribe(oleg, list)
# => "Oleg was added to the mailing list."
UserListSubscription.unsubscribe(oleg, list)
# => "Oleg was removed from the mailing list."
@christopherstyles
christopherstyles / archive_users.rake
Created July 27, 2014 21:42
Calling services from a rake task
namespace :archive do
task :users do
users.each do |user|
UnsubscribeUserFromList.call(user)
ArchiveUserComments.call(user)
end
end
end
@christopherstyles
christopherstyles / archive_user_worker.rb
Last active August 29, 2015 14:04
Calling a service from a background job
class ArchiveUserWorker
include Sidekiq::Worker
def perform(user)
UnsubscribeUserFromList.call(user)
end
end
@christopherstyles
christopherstyles / console_service.rb
Created July 27, 2014 21:48
Calling services from the console
[1] pry(main)> user = User.find(99)
[2] pry(main)> RemoveUserComments.call(user)
=> "User #99 was removed."
@christopherstyles
christopherstyles / services.rb
Last active August 29, 2015 14:04
Calling service objects from other services
class ArchiveInactiveUsers
def self.call(users)
users.each do |user|
UnsubscribeUserFromList.call(user)
SendGoodbyeEmail.call(user)
RemoveUserComment.call(user)
end
end
end
@christopherstyles
christopherstyles / profile.rb
Created August 12, 2015 20:29
Profile an RSpec example
#!/usr/bin/env ruby
$:.unshift 'spec'
require 'rspec'
require 'rails_helper'
spec = ARGV[0]
# -----------------------------------------------------------------------------
@christopherstyles
christopherstyles / benchmark.rb
Created August 12, 2015 20:33
Benchmark an RSpec example
#!/usr/bin/env ruby
$:.unshift 'spec'
require 'rspec'
require 'rails_helper'
require 'benchmark/ips'
spec = ARGV[0]
@christopherstyles
christopherstyles / item.rb
Created October 21, 2015 20:08
Requiring STI dependencies
module YourNamespace
class Item < ActiveRecord::Base
end
end
YourNamespace.require_sti_dependencies(:item)
@christopherstyles
christopherstyles / admin_user.rb
Created December 15, 2015 23:15
ActiveAdmin filter, for a has_many :through association
# admin/user.rb
ActiveAdmin.register User do
filter :role_in_all, as: :select,
multiple: true,
collection: -> { Role.order(name: :asc).all }
end