Class: Post
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Post
- Includes:
- Rakismet::Model
- Defined in:
- app/models/post.rb
Instance Attribute Summary collapse
-
#invalid_emails ⇒ Object
Returns the value of attribute invalid_emails.
Class Method Summary collapse
-
.by_featured_writers ⇒ Object
Scopes.
- .find_featured(options = {:limit => 10}) ⇒ Object
- .find_most_commented(limit = 10, since = 7.days.ago) ⇒ Object
- .find_popular(options = {}) ⇒ Object
- .find_recent(options = {:limit => 5}) ⇒ Object
- .find_related_to(post, options = {}) ⇒ Object
- .new_from_bookmarklet(params) ⇒ Object
- .popular ⇒ Object
- .recent ⇒ Object
- .since(days) ⇒ Object
Instance Method Summary collapse
- #create_poll(poll, choices) ⇒ Object
- #display_title ⇒ Object
- #first_image_in_body(size = nil, options = {}) ⇒ Object
- #has_been_favorited_by(user = nil, remote_ip = nil) ⇒ Object
- #image_for_excerpt ⇒ Object
- #next_post ⇒ Object
- #owner ⇒ Object
- #poll ⇒ Object
- #previous_post ⇒ Object
- #published_at_display(format = :published_date) ⇒ Object
- #send_to(email_addresses = '', message = '', user = nil) ⇒ Object
- #set_published_at ⇒ Object
- #tag_for_first_image_in_body ⇒ Object
- #to_param ⇒ Object
-
#transform_post ⇒ Object
transform the text and title into valid html.
- #update_poll(poll, choices) ⇒ Object
Instance Attribute Details
#invalid_emails ⇒ Object
Returns the value of attribute invalid_emails.
33 34 35 |
# File 'app/models/post.rb', line 33 def invalid_emails @invalid_emails end |
Class Method Details
.by_featured_writers ⇒ Object
Scopes
39 40 41 |
# File 'app/models/post.rb', line 39 def by_featured_writers includes(:user).where("users.featured_writer = ?", true).references(:users) end |
.find_featured(options = {:limit => 10}) ⇒ Object
71 72 73 |
# File 'app/models/post.rb', line 71 def find_featured( = {:limit => 10}) self.recent.by_featured_writers.limit([:limit]) end |
.find_most_commented(limit = 10, since = 7.days.ago) ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'app/models/post.rb', line 75 def find_most_commented(limit = 10, since = 7.days.ago) Post.select('posts.*', 'count(*) as comments_count') .joins("LEFT JOIN comments ON comments.commentable_id = posts.id") .where('comments.commentable_type = ? AND posts.published_at > ?', 'Post', since) .references('comments') .group(self.columns.map{|column| self.table_name + "." + column.name}.join(",")) .order('comments_count DESC') .limit(limit) end |
.find_popular(options = {}) ⇒ Object
65 66 67 68 69 |
# File 'app/models/post.rb', line 65 def find_popular( = {} ) .reverse_merge! :limit => 5, :since => 7.days self.popular.since([:since]).limit([:limit]) end |
.find_recent(options = {:limit => 5}) ⇒ Object
61 62 63 |
# File 'app/models/post.rb', line 61 def find_recent( = {:limit => 5}) recent.limit([:limit]) end |
.find_related_to(post, options = {}) ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'app/models/post.rb', line 52 def (post, = {}) .reverse_merge!({:limit => 8, :order => 'posts.published_at DESC', :conditions => [ 'posts.id != ? AND published_as = ?', post.id, 'live' ] }) limit([:limit]).order([:order]).where([:conditions]).tagged_with(post.tag_list, :any => true) end |
.new_from_bookmarklet(params) ⇒ Object
85 86 87 88 89 90 |
# File 'app/models/post.rb', line 85 def new_from_bookmarklet(params) self.new( :title => "#{params[:title] || params[:uri]}", :raw_post => "<a href='#{params[:uri]}'>#{params[:uri]}</a>#{params[:selection] ? "<p>#{params[:selection]}</p>" : ''}" ) end |
.popular ⇒ Object
42 43 44 |
# File 'app/models/post.rb', line 42 def popular order('posts.view_count DESC') end |
.recent ⇒ Object
48 49 50 |
# File 'app/models/post.rb', line 48 def recent order("posts.published_at DESC") end |
.since(days) ⇒ Object
45 46 47 |
# File 'app/models/post.rb', line 45 def since(days) where("posts.published_at > ?", days.ago) end |
Instance Method Details
#create_poll(poll, choices) ⇒ Object
164 165 166 167 168 169 170 171 |
# File 'app/models/post.rb', line 164 def create_poll(poll, choices) new_poll = self.polls.new(:question => poll[:question]) choices.delete('') if choices.size > 1 new_poll.save new_poll.add_choices(choices) end end |
#display_title ⇒ Object
101 102 103 104 105 106 107 |
# File 'app/models/post.rb', line 101 def display_title t = self.title if self.category t = self.category.name.upcase << ": " << t end t end |
#first_image_in_body(size = nil, options = {}) ⇒ Object
116 117 118 119 120 |
# File 'app/models/post.rb', line 116 def first_image_in_body(size = nil, = {}) doc = Hpricot( post ) image = doc.at("img") image ? image['src'] : nil end |
#has_been_favorited_by(user = nil, remote_ip = nil) ⇒ Object
190 191 192 193 |
# File 'app/models/post.rb', line 190 def has_been_favorited_by(user = nil, remote_ip = nil) f = Favorite.find_by_user_or_ip_address(self, user, remote_ip) return f end |
#image_for_excerpt ⇒ Object
160 161 162 |
# File 'app/models/post.rb', line 160 def image_for_excerpt first_image_in_body || user.avatar_photo_url(:medium) end |
#next_post ⇒ Object
112 113 114 |
# File 'app/models/post.rb', line 112 def next_post self.user.posts.except(:order).order('published_at DESC').where('published_at > ? and published_as = ?', published_at, 'live').first end |
#owner ⇒ Object
139 140 141 |
# File 'app/models/post.rb', line 139 def owner self.user end |
#poll ⇒ Object
186 187 188 |
# File 'app/models/post.rb', line 186 def poll !polls.empty? && polls.first end |
#previous_post ⇒ Object
109 110 111 |
# File 'app/models/post.rb', line 109 def previous_post self.user.posts.order('published_at DESC').where('published_at < ? and published_as = ?', published_at, 'live').first end |
#published_at_display(format = :published_date) ⇒ Object
195 196 197 |
# File 'app/models/post.rb', line 195 def published_at_display(format = :published_date) is_live? ? I18n.l(published_at, :format => format.to_sym) : 'Draft' end |
#send_to(email_addresses = '', message = '', user = nil) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'app/models/post.rb', line 143 def send_to(email_addresses = '', = '', user = nil) self.invalid_emails = [] emails = email_addresses.split(",").collect{|email| email.strip }.uniq emails.each do |email| self.invalid_emails << email unless email =~ /[\w._%-]+@[\w.-]+.[a-zA-Z]{2,4}/ end if email_addresses.blank? || !invalid_emails.empty? return false else emails = email_addresses.split(",").collect{|email| email.strip }.uniq emails.each{|email| UserNotifier.post_recommendation((user ? user.login : 'Someone'), email, self, , user).deliver } self.increment(:emailed_count).save end end |
#set_published_at ⇒ Object
133 134 135 136 137 |
# File 'app/models/post.rb', line 133 def set_published_at if self.is_live? && !self.published_at self.published_at = Time.now end end |
#tag_for_first_image_in_body ⇒ Object
122 123 124 125 |
# File 'app/models/post.rb', line 122 def tag_for_first_image_in_body image = first_image_in_body image.nil? ? '' : "<img src='#{image}' />" end |
#to_param ⇒ Object
97 98 99 |
# File 'app/models/post.rb', line 97 def to_param id.to_s << "-" << (title ? title.parameterize : '' ) end |
#transform_post ⇒ Object
transform the text and title into valid html
128 129 130 131 |
# File 'app/models/post.rb', line 128 def transform_post self.post = white_list(self.raw_post) self.title = white_list(self.title) end |
#update_poll(poll, choices) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'app/models/post.rb', line 173 def update_poll(poll, choices) return unless self.poll self.poll.update_attributes(:question => poll[:question]) choices.delete('') if choices.size > 1 self.poll.choices.destroy_all self.poll.save self.poll.add_choices(choices) else self.poll.destroy end end |