Skip to content

Instantly share code, notes, and snippets.

@JAMSUPREME
Last active September 9, 2016 13:43
Show Gist options
  • Save JAMSUPREME/a45222cbebeb1549eb0a94357a6df4d6 to your computer and use it in GitHub Desktop.
Save JAMSUPREME/a45222cbebeb1549eb0a94357a6df4d6 to your computer and use it in GitHub Desktop.
Abstractions v2 stuff
# Example of a few bad things (not a comprehensive list of bad ideas)
class Event < ActiveRecord::Base
# Less OK - scopes and retrieval helpers
scope :expensive, -> { where('desired_fee > 1000') }
scope :more_expensive_than, ->(cost) { where('desired_fee > ?', cost) }
def jan_events
where("strftime('%m', tentative_date) = 01")
end
# Not OK - arcane code that probably isn't reusable
def do_weird_things(title, other_event, new_price, *args)
@title = title + args.join(' ? ')
other_event.title = @title
puts "#{new_price}"
end
# Not OK - view logic
def cool_title
"COOL #{@title}"
end
end
# In our brilliance, we abstracted!
class Event < ActiveRecord::Base
include EventScopes
include EventWeirdThings
include EventHelpers
include StuffAndThings
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment