Module: Acts::As::Publishable::ClassMethods

Defined in:
lib/acts_as_publishable/acts_as_publishable.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_publishable(*args) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/acts_as_publishable/acts_as_publishable.rb', line 13

def acts_as_publishable(*args)
  unless args.include?(:draft) && args.include?(:live)
    raise "you must specify :draft and :live in list of publish_states for acts_as_publishable"
  end

  cattr_accessor :publish_states
  self.publish_states = args.collect{|state| state.to_s.downcase.to_sym }
  include InstanceMethods
  
  scope :live, lambda { where(:published_as => 'live') }
  scope :draft, lambda { where(:published_as => 'draft') }
  default_scope -> { live }

  # class << self
  #   alias_method_chain :find, :published_as
  # end

  # create methods for each publish state
  self.publish_states.each do |status|

    define_method("is_#{status}?") do
      self.published_as == status.to_s
    end

    define_method("save_as_#{status}") do
      self.published_as = status.to_s
      save
    end

  end
end