Skip to content

Instantly share code, notes, and snippets.

@madsheep
madsheep / discipline.rb
Last active December 27, 2015 14:09
Ultimate solution for long running specs. It's not that rails are slow, or rspec is slow, or ruby is slow. It's your code that is slow and/or poorly designed. The solution to slow tests problem is not optimizing them once the are too slow - it's about not letting them get slugish in the first place.
# in your spec_helper.rb
RSpec.configure do |config|
config.around(:each) do |example|
timeout(0.2) { example.run }
end
end
@madsheep
madsheep / sessions.rb
Last active August 29, 2015 13:57
log-in service example
# session controller
# receives callbacks with oauth
class SessionsController < ApplicationController
skip_before_filter :login_required
def create
login.run!
redirect_to root_path
end
@madsheep
madsheep / membership.rb
Last active August 29, 2015 14:00
Example 1 - simple model.
class Membership
belongs_to :user
belongs_to :group
after_save :send_email_notification, if: ->(m) { m.accepted_changed? && m.accepted? }
def send_email_nofication
NotificationMailer.accepted(self, user).deliver!
end
@madsheep
madsheep / membership.rb
Last active August 29, 2015 14:00
Still simple
class Membership
belongs_to :user
belongs_to :group
after_save :send_notifications, if: ->(m) { m.accepted_changed? && m.accepted? }
def send_notifications
NotificationMailer.accepted(self, user).deliver!
NotificationMailer.accepted_admin(self, group.admin).deliver!
@madsheep
madsheep / membership.rb
Last active August 29, 2015 14:00
Less simple
class Membership
belongs_to :user
belogns_to :group
after_save :send_notifications, if: ->(m) { m.accepted_changed? && m.accepted? }
def send_notifications
NotificationMailer.accepted(self, user).deliver!
PushNotifcations::NewMembership.new(self, user).dispatch!
class Membership
belongs_to :user
belogns_to :group
after_save :send_notifications, if: ->(m) { m.accepted_changed? && m.accepted? }
def send_notifications
NotificationMailer.accepted(self, user).deliver!
PushNotifcations::NewMembership.new(self, user).dispatch!
@madsheep
madsheep / membership.rb
Last active August 29, 2015 14:00
less and less simple
class Membership
belongs_to :user
belogns_to :group
after_save :send_notifications, if: ->(m) { m.accepted_changed? && m.accepted? }
def send_notifications
Resque.enqueue MembershipNotificationsWorker, self.id
end
@madsheep
madsheep / membership.rb
Created April 29, 2014 12:29
not simple at all
class Membership
belongs_to :user
belogns_to :group
after_save :send_notifications, if: ->(m) { m.accepted_changed? && m.accepted? }
def send_notifications
Resque.enqeue(NotificationMailerWorker, self.id, user.id)
Resque.enqeue(PushNotifcations::NewMembershipWorker, self.id, user.id)
@madsheep
madsheep / membership.rb
Created May 19, 2014 09:54
not simple at all
class Membership
belongs_to :user
belogns_to :group
after_save :send_notifications, if: ->(m) { m.accepted_changed? && m.accepted? }
def send_notifications
Resque.enqeue(NotificationMailerWorker, self.id, user.id)
Resque.enqeue(PushNotifcations::NewMembershipWorker, self.id, user.id)
@madsheep
madsheep / nginx.conf
Last active August 29, 2015 14:07
sample nginx ssl config
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;