Created
November 24, 2013 15:59
-
-
Save cupakromer/7628688 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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