Skip to content

Instantly share code, notes, and snippets.

View christopherstyles's full-sized avatar

Chris Styles christopherstyles

View GitHub Profile
@christopherstyles
christopherstyles / with_cache_shared_example.rb.rb
Last active October 2, 2024 22:13
Enable cache in shared example
# spec/support/enable_cache.rb
RSpec.shared_examples "with cache" do
around do |example|
original_cache_store = Rails.cache
Rails.cache = ActiveSupport::Cache::MemoryStore.new
ActionController::Base.perform_caching = true
Rails.cache.clear
example.run

Keybase proof

I hereby claim:

  • I am christopherstyles on github.
  • I am cstyles (https://keybase.io/cstyles) on keybase.
  • I have a public key ASDMo_KnIYYDOPwEH-1nHS3kcLZ-1WGlaaqDUQ7VnnFriQo

To claim this, I am signing this object:

@christopherstyles
christopherstyles / active_admin_filter_for_has_many_through.rb
Created December 15, 2015 23:29
ActiveAdmin filter, for a has_many :through association
# app/models/user.rb
class User < ActiveRecord::Base
has_many :permissions
has_many :roles, through: :permissions
scope :by_role, lambda { |role_ids|
joins(:permissions).where(permissions: { role_id: Array.wrap(role_ids) })
}
ransacker :role,
@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
@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 / 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 / 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 / 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 / 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 / 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