Sha256: 66dc6cecf3774b9430a46fe1bf024f190c46bc25e8c6850dd6a59c5daed28f4f

Contents?: true

Size: 1.57 KB

Versions: 9

Compression:

Stored size: 1.57 KB

Contents

module Governor
  # Include this module into any class that will represent a blog article
  # post.
  module Article
    def self.included(base) #:nodoc:
      base.belongs_to :author, :polymorphic => true
      base.validates_presence_of :author, :title, :post
      
      if defined?(WillPaginate)
        base.cattr_reader :per_page
        base.class_eval "@@per_page = 10"
      end
      
      Governor::PluginManager.plugins.each do |plugin|
        plugin.include_in_model(base)
      end
      
      # Will retrieve all of the articles with a given year, month, or day. If
      # day is not specified, it will find all of the posts for a given month;
      # if month is not specified, then it will find all of the posts for a
      # given year. Specifying a page will work with will_paginate.
      def base.find_all_by_date(year, month = nil, day = nil, page = 1)
        from, to = self.time_delta(year, month, day)
        conditions = ['created_at BETWEEN ? AND ?', from, to]
        if model_class.respond_to?(:paginate)
          paginate :page => page, :conditions => conditions, :order => 'created_at DESC'
        else
          all :conditions => conditions, :order => 'created_at DESC'
        end
      end

      private
      def base.time_delta(year, month = nil, day = nil)
        from = Time.mktime(year, month || 1, day || 1)
        to = if day.present?
          from.end_of_day
        elsif month.present?
          from.end_of_month
        else
          from.end_of_year
        end
        [from, to]
      end
    end
    
    def to_s
      title
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
governor-0.5.7 lib/governor/article.rb
governor-0.5.6 lib/governor/article.rb
governor-0.5.5 lib/governor/article.rb
governor-0.5.4 lib/governor/article.rb
governor-0.5.3 lib/governor/article.rb
governor-0.5.2 lib/governor/article.rb
governor-0.5.1 lib/governor/article.rb
governor-0.5.0 lib/governor/article.rb
governor-0.4.0 lib/governor/article.rb