Tags

, ,

## Before
class Article < ActiveRecord::Base
 
  def self.all_published
    where("state = ?", "published")
  end
 
  def self.all_draft
    where("state = ?", "draft")
  end
 
  def self.all_spam
     where("state = ?", "spam")
  end
 
  def published?
    self.state == 'published'
  end
 
  def draft?
    self.state == 'draft'
  end
 
  def spam?
    self.state == 'spam'
  end
end

## After
class Article < ActiveRecord::Base
  STATES = ['draft', 'published', 'spam']
 
  class <<self
    STATES.each do |state_name|
      define_method "all_#{state_name}" do
        where("state = ?", state_name)
      end
    end
  end
 
  STATES.each do |state_name|
    define_method "#{state_name}?" do
      self.state == state_name
    end
  end
 
end
Advertisements