Sha256: 2308ea2dbe828fcd35f05e2b439d5a1d384e007f078efb0fa88b74d641a9000f

Contents?: true

Size: 1.5 KB

Versions: 2

Compression:

Stored size: 1.5 KB

Contents

class ArchiveFinder
  
  def initialize(&block)
    @block = block
  end
  
  def find(method, options = {})
    @block.call(method, options)
  end
  
  # stub for page tag finding
  def all(method, options ={})
    self.find(:all)
  end
  
  class << self
    def year_finder(finder, year)
      new do |method, options|
        start = Time.local(year)
        finish = start.next_year
        add_condition(options, "published_at >= ? and published_at < ?", start, finish)
        finder.find(method, options)
      end
    end
    
    def month_finder(finder, year, month)
      new do |method, options|
        start = Time.local(year, month)
        finish = start.next_month
        add_condition(options, "published_at >= ? and published_at < ?", start, finish)
        finder.find(method, options)
      end
    end
    
    def day_finder(finder, year, month, day)
      new do |method, options|
        start = Time.local(year, month, day)
        finish = start.tomorrow
        add_condition(options, "published_at >= ? and published_at < ?", start, finish)
        finder.find(method, options)
      end
    end
    
    private
      
      def concat_conditions(a, b)
        sql = "(#{ [a.shift, b.shift].compact.join(") AND (") })"
        params = a + b
        [sql, *params]
      end
      
      def add_condition(options, *condition)
        old = options[:conditions] || []
        conditions = concat_conditions(old, condition)
        options[:conditions] = conditions
        options
      end
      
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
radiantcms-couchrest_model-0.1.4 vendor/extensions/archive/app/models/archive_finder.rb
radiant-0.9.1 vendor/extensions/archive/app/models/archive_finder.rb