Class: Atreides::Post
- Includes:
- Base::AasmStates, Base::Taggable, Base::Validation, Extendable
- Defined in:
- app/models/atreides/post.rb
Instance Attribute Summary collapse
-
#fb_me ⇒ Object
Behaviours.
-
#tumblr_me ⇒ Object
Behaviours.
-
#tw_me ⇒ Object
Behaviours.
-
#url ⇒ Object
Behaviours.
Class Method Summary collapse
- .base_class ⇒ Object
- .next(post) ⇒ Object
- .next_month(month = Date.today.month) ⇒ Object
- .previous(post) ⇒ Object
- .previous_month(month = Date.today.month) ⇒ Object
Instance Method Summary collapse
-
#first_body ⇒ Object
(also: #body)
Return the first found text/video/photo.
- #first_photo ⇒ Object (also: #photo)
- #first_video ⇒ Object (also: #video)
- #next ⇒ Object
- #post_types ⇒ Object
- #previous ⇒ Object
- #push_publication ⇒ Object
- #push_to_social ⇒ Object
- #related_posts ⇒ Object
- #set_diary ⇒ Object
- #setup_social_network ⇒ Object
- #slug=(value) ⇒ Object
-
#thumbnail(size = :thumb) ⇒ Object
Return the first found photo.
-
#to_param ⇒ Object
Instance Methods.
- #update_facebook_id(facebook_id) ⇒ Object
- #update_tumblr_id(tumblr_id) ⇒ Object
- #update_twitter_id(twitter_id) ⇒ Object
Methods included from Base::Validation
Methods included from Base::AasmStates
Methods included from Base::Taggable
Methods inherited from Base
Instance Attribute Details
#fb_me ⇒ Object
Behaviours
27 28 29 |
# File 'app/models/atreides/post.rb', line 27 def fb_me @fb_me end |
#tumblr_me ⇒ Object
Behaviours
27 28 29 |
# File 'app/models/atreides/post.rb', line 27 def tumblr_me @tumblr_me end |
#tw_me ⇒ Object
Behaviours
27 28 29 |
# File 'app/models/atreides/post.rb', line 27 def tw_me @tw_me end |
#url ⇒ Object
Behaviours
27 28 29 |
# File 'app/models/atreides/post.rb', line 27 def url @url end |
Class Method Details
.base_class ⇒ Object
137 138 139 |
# File 'app/models/atreides/post.rb', line 137 def base_class self end |
.next(post) ⇒ Object
113 114 115 116 117 118 |
# File 'app/models/atreides/post.rb', line 113 def next(post) with_exclusive_scope { # Due to problems with the default_scope ordering the 'live' scope must come AFTER the order scope where("published_at >= ? and id != ?", post.published_at, post.id).order("published_at asc, id asc").live.first } end |
.next_month(month = Date.today.month) ⇒ Object
127 128 129 130 |
# File 'app/models/atreides/post.rb', line 127 def next_month(month = Date.today.month) p = self.live.first(:conditions => ["EXTRACT(MONTH from published_at) > ?", month.to_i], :order => "published_at asc, id desc") p ? p.published_at.month : nil end |
.previous(post) ⇒ Object
120 121 122 123 124 125 |
# File 'app/models/atreides/post.rb', line 120 def previous(post) with_exclusive_scope { # Due to problems with the default_scope ordering the 'live' scope must come AFTER the order scope where("published_at <= ? and id != ?", post.published_at, post.id).order("published_at desc, id desc").live.first } end |
.previous_month(month = Date.today.month) ⇒ Object
132 133 134 135 |
# File 'app/models/atreides/post.rb', line 132 def previous_month(month = Date.today.month) p = self.live.first(:conditions => ["EXTRACT(MONTH from published_at) < ?", month.to_i], :order => "published_at desc, id desc") p ? p.published_at.month : nil end |
Instance Method Details
#first_body ⇒ Object Also known as: body
Return the first found text/video/photo
172 173 174 |
# File 'app/models/atreides/post.rb', line 172 def first_body (part = parts.where(:content_type => :text).detect{|p| p.body? }) ? part.body : "" end |
#first_photo ⇒ Object Also known as: photo
177 178 179 |
# File 'app/models/atreides/post.rb', line 177 def first_photo (part = parts.where(:content_type => :photos).detect{|p| !p.photos.first.nil? }) ? part.photos.first : nil end |
#first_video ⇒ Object Also known as: video
182 183 184 |
# File 'app/models/atreides/post.rb', line 182 def first_video (part = parts.where(:content_type => :videos).detect{|p| !p.videos.first.nil? }) ? part.videos.first : nil end |
#next ⇒ Object
150 151 152 |
# File 'app/models/atreides/post.rb', line 150 def next @_next ||= self.class.next(self) end |
#post_types ⇒ Object
218 219 220 |
# File 'app/models/atreides/post.rb', line 218 def post_types @post_types ||= parts.map {|part| part.content_type} end |
#previous ⇒ Object
154 155 156 |
# File 'app/models/atreides/post.rb', line 154 def previous @_previous ||= self.class.previous(self) end |
#push_publication ⇒ Object
98 99 100 101 |
# File 'app/models/atreides/post.rb', line 98 def push_publication # If have just been saved as published then do do_publish to post to twitter/fb etc do_publish if (state_changed? and published?) or (!state_changed? and published_at_changed?) end |
#push_to_social ⇒ Object
103 104 105 106 |
# File 'app/models/atreides/post.rb', line 103 def # Atreides::Post to social networks if published? and (tw_me or fb_me or tumblr_me) end |
#related_posts ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'app/models/atreides/post.rb', line 158 def # Don't recalc twice return @related_posts unless @related_posts.nil? max = 6 @related_posts = [] @related_posts += Atreides::Post.live.tagged_with(tag_list, :match_all => :true).all(:conditions => ["posts.id not in (?)", [id]], :limit => max) rescue [] @related_posts += Atreides::Post.live.tagged_with(tag_list, :any => :true).all(:conditions => ["posts.id not in (?)", @related_posts.map(&:id)+[id]], :limit => max - @related_posts.size) if @related_posts.size < max rescue [] @related_posts.uniq! @related_posts += Atreides::Post.live.all(:conditions => ["posts.id not in (?)", @related_posts.map(&:id)+[id]], :limit => max - @related_posts.size) if @related_posts.size < max rescue [] @related_posts end |
#set_diary ⇒ Object
88 89 90 91 92 93 |
# File 'app/models/atreides/post.rb', line 88 def set_diary # Set diary as default location if pending? or new_record? and respond_to?(:location_list) and location_list.empty? location_list << Settings..posts.location.first end end |
#setup_social_network ⇒ Object
78 79 80 81 82 83 84 85 86 |
# File 'app/models/atreides/post.rb', line 78 def # Setup social network callback flags begin self.tw_me ||= ((Settings.default_cross_post_to?(:twitter) && pending?) or tw_delayed_job_id?) unless twitter_id? self.fb_me ||= ((Settings.default_cross_post_to?(:facebook) && pending?) or fb_delayed_job_id?) unless facebook_id? self.tumblr_me ||= ((Settings.default_cross_post_to?(:tumblr) && pending?) or tumblr_delayed_job_id?) unless tumblr_id? rescue end end |
#slug=(value) ⇒ Object
222 223 224 225 |
# File 'app/models/atreides/post.rb', line 222 def slug=(value) # Do not let id based slugs through write_attribute(:slug, value.to_s.match(/^\d+$/) ? nil : value) end |
#thumbnail(size = :thumb) ⇒ Object
Return the first found photo
188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'app/models/atreides/post.rb', line 188 def thumbnail(size = :thumb) # Find the first thumbnail image url = if first_photo first_photo.image.url(size) elsif first_video first_video.thumb_url end # FIXME: THIS NEEDS TO BE MOVED TO A VIEW/PRESENTER/DECORATOR # asset_path('atreides/missing_thumb.png') url || "/images/missing_thumb.png" end |
#to_param ⇒ Object
Instance Methods
145 146 147 148 |
# File 'app/models/atreides/post.rb', line 145 def to_param # Used for urls like /posts/:id/:slug id.to_s end |
#update_facebook_id(facebook_id) ⇒ Object
208 209 210 211 |
# File 'app/models/atreides/post.rb', line 208 def update_facebook_id(facebook_id) self.facebook_id = facebook_id save end |
#update_tumblr_id(tumblr_id) ⇒ Object
213 214 215 216 |
# File 'app/models/atreides/post.rb', line 213 def update_tumblr_id(tumblr_id) self.tumblr_id = tumblr_id save end |
#update_twitter_id(twitter_id) ⇒ Object
203 204 205 206 |
# File 'app/models/atreides/post.rb', line 203 def update_twitter_id(twitter_id) self.twitter_id = twitter_id save end |