class User def rectify if self.payments.find { |p| p.status == "pending" } self.payments.select { |p| p.status == "pending" } do |p| p.apply_to!(self) end end if self.bills.find { |b| b.due_date < Time.now } self.bills.select { |b| b.due_date < Time.now }.each do |b| b.submit_to_collections! end end if self.bills.find { |b| b.payment_date.nil? } self.bills.select { |b| b.payment_date.nil? }.each do |b| b.send_to self end end end end
class User def rectify process_pending_payments notify_collections_about_overdue_bills send_me_my_remaining_bills end end
class User def process_pending_payments #... end def notify_collections_about_overdue_bills if self.bills.find { |b| b.due_date < Time.now } self.bills.select { |b| b.due_date < Time.now }.each do |b| b.submit_to_collections! end end end def send_me_my_remaining_bills # ... end end
class User # previously just 'rectify' def rectify_account process_pending_payments notify_collections_about_overdue_bills send_me_my_remaining_bills end end
def notify_collections_about_overdue_bills if self.bills.find { |b| b.due_date < Time.now } self.bills.select { |b| b.due_date < Time.now }.each do |b| b.submit_to_collections! end end end
class CollectionClaim def self.file_against(bill) self.new(bill) end def initialize # biz logic would start here end end
class User def notify_collections_about_overdue_bills if self.bills.find { |b| b.due_date < Time.now } self.bills.select { |b| b.due_date < Time.now }.each do |b| CollectionsClaim.file_against bill end end end end
class User def notify_collections_about_overdue_bills past_due_bills.each do |bill| CollectionsClaim.file_against bill end end private def past_due_bills self.bills.select { |bill| b.past_due? } end end class Bill def past_due? due_date < Time.now end end
class User # Elided for your sanity # Yes, this is (mostly) real code # Yes, I helped write it a year or so ago # Yes, I hate my past self at times include User::Associations include User::Validations include User::Search include User::DefaultSettings include User::DefaultPrivacies include User::Permissions include User::Invitations include User::OAuthProvisioning # even more includes.... HALP! end
require 'delegate' class UserWithNormalPermissions < SimpleDelegator ALLOWED_METHODS = %w[do_something] def method_missing(args={}) unless ALLOWED_METHODS.include?(args[0]) fail PermissionError, ... end # Call "super" or else delegation doesn't happen! super end end
def some_controller_action user = UserWithNormalPermissions.new( User.find(...) ) user.do_something rescue PermissionError => p # ... end
%div.poor_template - if some_condition = render_this - elsif another_condition = render_that - else = render_those
module SomeHelper def render_whatever_through_a_helper if some_nasty_condition render_this elsif another_nasty_condition render_that else render_those end end end
%div.better_templating = render_whatever_through_a_helper
module EmployerHelper def redirect_to_not_allowed_if_not_employer end def city_province_country_string(model) end def welcome_text end def employer_videos_create_job end def employer_videos_read_results end def employer_videos_customize_jobs end end
class SomeConditionPresenter def render #... end end class AnotherConditionPresenter # Same interface as above end class DefaultConditionPresenter # Get the idea? end
module FooHelper def conditional_presenter klass = if some_nasty_condition SomeConditionPresenter elsif another_nasty_condition AnotherConditionPresenter else DefaultConditionPresenter end klass.new end end
module Resque class Worker # Unfortunately have to override Resque::Worker's +run_hook+ # method to call hook on [MySpecialSnowflake gem] rather on # Resque directly. Any suggestions on how to make this more # flexible are more than welcome. def run_hook(name, *args) # Me: 4 hours of my life... gone # FFFFFFFUUUUUUUUUUUUUUUU return unless hook = MySpecialSnowflake.send(name) # ... end end end
# Unfortunately have to override Resque::Worker's +run_hook+ # method to call hook on [MySpecialSnowflake gem] rather on # Resque directly. Any suggestions on how to make this more # flexible are more than welcome.
module Resque def self.hook_responders @hook_responders ||= [self] @hook_responders end def self.register_hook_responder(responder) hook_responsers << responder end end
module Resque class Worker def run_hook(name, *args) hook = Resque.hook_responders.find { |r| r.send(name) } return unless hook # ... end end end
def busy_method please_dont_write_longs_lines(of: code, that: go, on: and) followed_by_more(really: hard, to: read, crap: that, hurts_my: eyes) because_i_will_find_you(and: hurt, you: for: writing, this: shit) end
class EXCITINGController < ApplicationController def timeline; end def tag; end def search; end def detail; end # and on, and on, and on... end
class VeryBoringController < ApplicationController def index; end def new; end def create; end def update; end def show; end def destroy; end end
class UserTeam belongs_to :user belongs_to :team end class CandidateEmployerJob end
class TeamMembership belongs_to :user belongs_to :team end class Applicant end
/