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.
34 35 36 |
# File 'app/models/post.rb', line 34 def invalid_emails @invalid_emails end |
Class Method Details
.by_featured_writers ⇒ Object
Scopes
42 43 44 |
# File 'app/models/post.rb', line 42 def by_featured_writers where("users.featured_writer = ?", true).includes(:user) end |
.find_featured(options = {:limit => 10}) ⇒ Object
74 75 76 |
# File 'app/models/post.rb', line 74 def find_featured( = {:limit => 10}) self.recent.by_featured_writers.limit([:limit]).all end |
.find_most_commented(limit = 10, since = 7.days.ago) ⇒ Object
78 79 80 81 82 83 84 85 86 87 |
# File 'app/models/post.rb', line 78 def find_most_commented(limit = 10, since = 7.days.ago) Post.find(:all, :select => 'posts.*, count(*) as comments_count', :joins => "LEFT JOIN comments ON comments.commentable_id = posts.id", :conditions => ['comments.commentable_type = ? AND posts.published_at > ?', 'Post', since], :group => self.columns.map{|column| self.table_name + "." + column.name}.join(","), :order => 'comments_count DESC', :limit => limit ) end |
.find_popular(options = {}) ⇒ Object
68 69 70 71 72 |
# File 'app/models/post.rb', line 68 def find_popular( = {} ) .reverse_merge! :limit => 5, :since => 7.days self.popular.since([:since]).limit([:limit]).all end |
.find_recent(options = {:limit => 5}) ⇒ Object
64 65 66 |
# File 'app/models/post.rb', line 64 def find_recent( = {:limit => 5}) recent.find :all, end |
.find_related_to(post, options = {}) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'app/models/post.rb', line 55 def (post, = {}) .reverse_merge!({:limit => 8, :order => '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
89 90 91 92 93 94 |
# File 'app/models/post.rb', line 89 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
45 46 47 |
# File 'app/models/post.rb', line 45 def popular order('posts.view_count DESC') end |
.recent ⇒ Object
51 52 53 |
# File 'app/models/post.rb', line 51 def recent order("posts.published_at DESC") end |
.since(days) ⇒ Object
48 49 50 |
# File 'app/models/post.rb', line 48 def since(days) where("posts.published_at > ?", days.ago) end |
Instance Method Details
#create_poll(poll, choices) ⇒ Object
168 169 170 171 172 173 174 175 |
# File 'app/models/post.rb', line 168 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
105 106 107 108 109 110 111 |
# File 'app/models/post.rb', line 105 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
120 121 122 123 124 |
# File 'app/models/post.rb', line 120 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
194 195 196 197 |
# File 'app/models/post.rb', line 194 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
164 165 166 |
# File 'app/models/post.rb', line 164 def image_for_excerpt first_image_in_body || user.avatar_photo_url(:medium) end |
#next_post ⇒ Object
116 117 118 |
# File 'app/models/post.rb', line 116 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
143 144 145 |
# File 'app/models/post.rb', line 143 def owner self.user end |
#poll ⇒ Object
190 191 192 |
# File 'app/models/post.rb', line 190 def poll !polls.empty? && polls.first end |
#previous_post ⇒ Object
113 114 115 |
# File 'app/models/post.rb', line 113 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
199 200 201 |
# File 'app/models/post.rb', line 199 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
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'app/models/post.rb', line 147 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
137 138 139 140 141 |
# File 'app/models/post.rb', line 137 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
126 127 128 129 |
# File 'app/models/post.rb', line 126 def tag_for_first_image_in_body image = first_image_in_body image.nil? ? '' : "<img src='#{image}' />" end |
#to_param ⇒ Object
101 102 103 |
# File 'app/models/post.rb', line 101 def to_param id.to_s << "-" << (title ? title.parameterize : '' ) end |
#transform_post ⇒ Object
transform the text and title into valid html
132 133 134 135 |
# File 'app/models/post.rb', line 132 def transform_post self.post = white_list(self.raw_post) self.title = white_list(self.title) end |
#update_poll(poll, choices) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'app/models/post.rb', line 177 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 |