Skip to content

Instantly share code, notes, and snippets.

@jrhorn424
Forked from cupakromer/Refactor1.rb
Created November 24, 2013 21:36
Show Gist options
  • Save jrhorn424/7632718 to your computer and use it in GitHub Desktop.
Save jrhorn424/7632718 to your computer and use it in GitHub Desktop.
class Article < ActiveRecord::Base
def self.states(*states)
states.each do |state|
define_method "#{state}?" do
self.state == state
end
define_singleton_method "all_#{state}" do
where("state = ?", state)
end
end
end
states :draft, :published, :spam
end
module Statable
def states(*states)
states.each do |state|
define_method "#{state}?" do
self.state == state
end
define_singleton_method "all_#{state}" do
where("state = ?", state)
end
end
end
end
class Article < ActiveRecord::Base
extend Statable
states :draft, :published, :spam
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment