Sha256: 155869b6332969d26c3e1c0f630a527e51d550800084863e2277c0c52187849b

Contents?: true

Size: 1.5 KB

Versions: 11

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(options ={})
    self.find(:all, options)
  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

11 entries across 11 versions & 2 rubygems

Version Path
radiant-1.0.0 ruby-debug/ruby/1.8/gems/radiant-archive-extension-1.0.7/app/models/archive_finder.rb
radiant-archive-extension-1.0.7 app/models/archive_finder.rb
radiant-archive-extension-1.0.6 app/models/archive_finder.rb
radiant-archive-extension-1.0.5 app/models/archive_finder.rb
radiant-archive-extension-1.0.4 app/models/archive_finder.rb
radiant-archive-extension-1.0.3 app/models/archive_finder.rb
radiant-archive-extension-1.0.2 app/models/archive_finder.rb
radiant-archive-extension-1.0.1 app/models/archive_finder.rb
radiant-archive-extension-1.0.0 app/models/archive_finder.rb
radiant-1.0.0.rc2 vendor/extensions/archive/app/models/archive_finder.rb
radiant-1.0.0.rc1 vendor/extensions/archive/app/models/archive_finder.rb