Class: Atreides::Post

Inherits:
Base
  • Object
show all
Includes:
Base::AasmStates, Base::Taggable, Base::Validation, Extendable
Defined in:
app/models/atreides/post.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Base::Validation

included

Methods included from Base::AasmStates

included

Methods included from Base::Taggable

included

Methods inherited from Base

#dom_id

Instance Attribute Details

#fb_meObject

Behaviours



27
28
29
# File 'app/models/atreides/post.rb', line 27

def fb_me
  @fb_me
end

#tumblr_meObject

Behaviours



27
28
29
# File 'app/models/atreides/post.rb', line 27

def tumblr_me
  @tumblr_me
end

#tw_meObject

Behaviours



27
28
29
# File 'app/models/atreides/post.rb', line 27

def tw_me
  @tw_me
end

#urlObject

Behaviours



27
28
29
# File 'app/models/atreides/post.rb', line 27

def url
  @url
end

Class Method Details

.base_classObject



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_bodyObject 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_photoObject 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_videoObject 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

#nextObject



150
151
152
# File 'app/models/atreides/post.rb', line 150

def next
  @_next ||= self.class.next(self)
end

#post_typesObject



218
219
220
# File 'app/models/atreides/post.rb', line 218

def post_types
  @post_types ||= parts.map {|part| part.content_type}
end

#previousObject



154
155
156
# File 'app/models/atreides/post.rb', line 154

def previous
  @_previous ||= self.class.previous(self)
end

#push_publicationObject



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_socialObject



103
104
105
106
# File 'app/models/atreides/post.rb', line 103

def push_to_social
  # Atreides::Post to social networks
  social_cross_posts if published? and (tw_me or fb_me or tumblr_me)
end


158
159
160
161
162
163
164
165
166
167
168
169
# File 'app/models/atreides/post.rb', line 158

def related_posts
  # 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_diaryObject



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.tags.posts.location.first
  end
end

#setup_social_networkObject



78
79
80
81
82
83
84
85
86
# File 'app/models/atreides/post.rb', line 78

def setup_social_network
  # 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_paramObject

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