Skip to content
This repository
tree: 426d2ae6fc
Fetching contributors…

Cannot retrieve contributors at this time

file 88 lines (62 sloc) 3.66 kb
1 2 3 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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
$:.unshift File.dirname(__FILE__)

require 'category'
require 'ware'

class Interview < Resource
        
        attr_accessor :id, :slug, :name, :summary, :credit_name, :credit_url, :answers, :is_published, :published_on, :date_create, :date_update
        attr_accessor :categories, :wares
        
        def initialize()
                categories, wares = []
        end
        
        def self.fetch(query, options = {})
                interviews = super(query, options)
                
                interviews.each do |interview|
                        interview.categories = Category.for_interview(interview.id)
                        interview.wares = Ware.for_interview(interview.id) unless options[:summary]
                end
                
                interviews
        end
        
        def self.all(options = {})
                options = {:summary => false}.merge!(options)
                self.fetch("SELECT * FROM interviews ORDER BY published_on DESC", options)
        end
        
        def self.recent(options = {})
                options = {:summary => false}.merge!(options)
                fields = options[:summary] ? "id, slug, name, summary, is_published, published_on" : "*"
                
                self.fetch("SELECT #{fields} FROM interviews WHERE is_published=1 ORDER BY published_on DESC LIMIT 10", options)
        end
        
        def self.with_slug(slug, options = {})
                slug = self.escape(slug)
                result = self.fetch("SELECT * FROM interviews WHERE slug='#{slug}'", options)
                
                result.length < 1 ? false : result[0]
        end
        
        def self.by_year(year, options = {})
                year = self.escape(year)
                
                options = {:summary => false}.merge!(options)
                fields = options[:summary] ? "id, slug, name, summary, is_published, published_on" : "*"
                
                self.fetch("SELECT * FROM interviews WHERE year(published_on) = '#{year}' AND is_published=1 ORDER BY published_on DESC", options)
        end
        
        def self.for_category_slug(slug, options = {})
                slug = self.escape(slug)
                
                options = {:summary => false}.merge!(options)
                fields = options[:summary] ? "i.id, i.slug, i.name, i.summary, i.is_published, i.published_on" : "i.*"
                
                query = "SELECT #{fields} FROM interviews AS i, interview_categories AS ic, categories AS c WHERE ic.interview_id=i.id AND ic.category_id=c.id AND c.slug = '#{slug}' AND i.is_published=1 ORDER BY i.published_on DESC"
                query << " LIMIT #{options[:limit]}" if options[:limit]
                
                self.fetch(query, options)
        end

        def self.counts()
                Interview.query("SELECT year(published_on) AS year, count(*) AS count FROM interviews WHERE is_published=1 GROUP BY year ORDER BY year DESC")
        end
        
        def self.years()
                Interview.query("SELECT year(published_on) AS year FROM interviews WHERE is_published=1 GROUP BY year ORDER BY year DESC")
        end
        
        def to_markdown
                markdown = self.answers
                
                if self.wares.length > 0
                        markdown += "\n\n"
                        
                        self.wares.each do |ware|
                                markdown += "[#{ware.slug}]: #{ware.url} \"#{ware.description}\"\n"
                        end
                end
                
                markdown
        end

end
Something went wrong with that request. Please try again.