public
Description: railscasts.com in open source goodness!
Homepage: http://railscasts.com
Clone URL: git://github.com/ryanb/railscasts.git
railscasts / app / models / episode.rb
dcfc2d26 » ryanb 2008-05-08 generating nifty scaffold f... 1 class Episode < ActiveRecord::Base
18ce412a » ryanb 2008-07-29 adding dependent destroy op... 2 has_many :comments, :dependent => :destroy
3 has_many :taggings, :dependent => :destroy
bf761bbc » ryanb 2008-06-19 adding published named scop... 4 has_many :tags, :through => :taggings
18ce412a » ryanb 2008-07-29 adding dependent destroy op... 5 has_many :downloads, :dependent => :destroy
be705e2f » ryanb 2008-07-20 adding download model for h... 6
8b05ccd2 » ryanb 2008-07-01 adding next/prev episode li... 7 acts_as_list
bf761bbc » ryanb 2008-06-19 adding published named scop... 8
ceffcd8d » ryanb 2008-10-19 using UTC time when generat... 9 named_scope :published, lambda { {:conditions => ['published_at <= ?', Time.now.utc]} }
10 named_scope :unpublished, lambda { {:conditions => ['published_at > ?', Time.now.utc]} }
495a36de » ryanb 2008-07-21 sorting episodes in descend... 11 named_scope :recent, :order => 'position DESC'
5149eab1 » ryanb 2008-06-19 tags can now be assigned to... 12
97e1b940 » ryanb 2008-06-28 automatically generate perm... 13 validates_presence_of :published_at, :name
be705e2f » ryanb 2008-07-20 adding download model for h... 14 validates_associated :downloads, :on => :update # create automatically handles validation
97e1b940 » ryanb 2008-06-28 automatically generate perm... 15
16 before_create :set_permalink
be705e2f » ryanb 2008-07-20 adding download model for h... 17 after_update :save_downloads
04e4d4f6 » ryanb 2008-06-28 adding a validation to epis... 18
3146f0dc » ryanb 2009-04-16 don't call define_index if ... 19 # sometimes ThinkingSphinx isn't loaded for rake tasks
20 if respond_to? :define_index
21 define_index do
22 indexes :name
23 indexes position, :sortable => true
24 indexes description
25 indexes notes
26 indexes comments.content, :as => :comment_content
27 indexes tags(:name), :as => :tag_names
7cf28d3c » ryanb 2008-08-18 add thinking sphinx index d... 28
3146f0dc » ryanb 2009-04-16 don't call define_index if ... 29 has published_at
30 end
7cf28d3c » ryanb 2008-08-18 add thinking sphinx index d... 31 end
32
33 def self.search_published(query)
c2f234f9 » ryanb 2008-08-18 enhancing thinking sphinx s... 34 if APP_CONFIG['thinking_sphinx']
ceffcd8d » ryanb 2008-10-19 using UTC time when generat... 35 search(query, :conditions => { :published_at => 0..Time.now.utc.to_i },
c2f234f9 » ryanb 2008-08-18 enhancing thinking sphinx s... 36 :field_weights => { :name => 20, :description => 15, :notes => 5, :tag_names => 10 })
36484027 » ryanb 2008-08-18 adding thinking sphinx opti... 37 else
38 published.primitive_search(query)
39 end
40 rescue ThinkingSphinx::ConnectionError => e
c2f234f9 » ryanb 2008-08-18 enhancing thinking sphinx s... 41 APP_CONFIG['thinking_sphinx'] = false
36484027 » ryanb 2008-08-18 adding thinking sphinx opti... 42 raise e
7cf28d3c » ryanb 2008-08-18 add thinking sphinx index d... 43 end
44
45 def self.primitive_search(query)
46 find(:all, :conditions => primitive_search_conditions(query))
47 end
48
ae641949 » ryanb 2008-08-19 adding search to episodes a... 49 def published_month
50 published_at.beginning_of_month
1a513d2e » ryanb 2008-06-28 adding full episodes (archi... 51 end
52
be705e2f » ryanb 2008-07-20 adding download model for h... 53 def mov
54 downloads.find_by_format('mov')
55 end
56
57 def m4v
58 downloads.find_by_format('m4v')
59 end
60
5149eab1 » ryanb 2008-06-19 tags can now be assigned to... 61 def tag_names=(names)
6b477914 » ryanb 2008-06-28 fixing episode tags so they... 62 self.tags = Tag.with_names(names.split(/\s+/))
5149eab1 » ryanb 2008-06-19 tags can now be assigned to... 63 end
64
65 def tag_names
66 tags.map(&:name).join(' ')
67 end
97e1b940 » ryanb 2008-06-28 automatically generate perm... 68
69 def to_param
c9d6386f » ryanb 2009-04-16 using position in episode U... 70 [position, permalink].join('-')
97e1b940 » ryanb 2008-06-28 automatically generate perm... 71 end
72
7b94df03 » ryanb 2008-07-01 only showing next episode l... 73 def last_published?
8b05ccd2 » ryanb 2008-07-01 adding next/prev episode li... 74 self == self.class.published.last
75 end
76
be705e2f » ryanb 2008-07-20 adding download model for h... 77 def new_download_attributes=(download_attributes)
78 download_attributes.each do |attributes|
79 downloads.build(attributes)
80 end
81 end
82
83 def existing_download_attributes=(download_attributes)
84 downloads.reject(&:new_record?).each do |download|
85 attributes = download_attributes[download.id.to_s]
86 if attributes
87 download.attributes = attributes
88 else
89 downloads.delete(download)
90 end
91 end
92 end
93
c2f4d81a » ryanb 2009-02-28 moving seconds/duration fro... 94 def duration
95 if seconds
96 min, sec = *seconds.divmod(60)
97 [min, sec.to_s.rjust(2, '0')].join(':')
98 end
99 end
100
101 def duration=(duration)
102 min, sec = *duration.split(':').map(&:to_i)
103 self.seconds = min*60 + sec
104 end
105
97e1b940 » ryanb 2008-06-28 automatically generate perm... 106 private
107
7cf28d3c » ryanb 2008-08-18 add thinking sphinx index d... 108 def self.primitive_search_conditions(query)
109 query.split(/\s+/).map do |word|
110 '(' + %w[name description notes].map { |col| "#{col} LIKE #{sanitize('%' + word.to_s + '%')}" }.join(' OR ') + ')'
111 end.join(' AND ')
112 end
113
be705e2f » ryanb 2008-07-20 adding download model for h... 114 def save_downloads
115 if downloads.loaded?
116 downloads.each do |download|
117 download.save(false)
118 end
119 end
120 end
121
97e1b940 » ryanb 2008-06-28 automatically generate perm... 122 def set_permalink
123 self.permalink = name.downcase.gsub(/[^0-9a-z]+/, ' ').strip.gsub(' ', '-') if name
124 end
dcfc2d26 » ryanb 2008-05-08 generating nifty scaffold f... 125 end