Module: Qcontent::Published

Defined in:
lib/qcontent/published.rb

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



4
5
6
7
8
9
10
11
12
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
44
45
# File 'lib/qcontent/published.rb', line 4

def self.included(klass)
  klass.module_eval do
    def self.active_conditions
      {}
    end

    def self.only_active(&block)
      with_scope({:find => {:conditions => active_conditions}}, &block)
    end
    
    named_scope :active_only, lambda { {:conditions => active_conditions } }  
    
  end
  return unless klass.column_names.include?('published_at')
  klass.module_eval do

    named_scope :published, :conditions => lambda {  ["? > published_at", Time.now.utc]  }
    named_scope :recent, lambda {|how_many| {:order => 'published_at DESC', :limit => how_many || 3 }}

    def published?
      published_at && Time.now.utc > published_at
    end
    alias_method :is_published?, :published?
    alias_method :is_published, :published?

    def self.published_only
      self.with_scope(:find => {:conditions => ["? > published_at", Time.now.utc]}) do
        yield if block_given?
      end
    end

    def self.most_recent(how_many = 1)
      published.find(:all,:order => 'published_at DESC', :limit => how_many) 
      (!all.empty? && how_many == 1) ? all.first : all
    end
    
    def self.active_conditions
      ["published_at <= ?", Time.now.utc]
    end

  end
end