Class: Enki::Base::Post

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
PaginationShim
Defined in:
app/models/enki/base/post.rb

Constant Summary collapse

DEFAULT_LIMIT =
15

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PaginationShim

paginated, paginated_pages

Instance Attribute Details

#minor_editObject

Returns the value of attribute minor_edit.



67
68
69
# File 'app/models/enki/base/post.rb', line 67

def minor_edit
  @minor_edit
end

#published_at_naturalObject

Returns the value of attribute published_at_natural.



80
81
82
# File 'app/models/enki/base/post.rb', line 80

def published_at_natural
  @published_at_natural
end

Class Method Details

.build_for_preview(params) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'app/models/enki/base/post.rb', line 86

def build_for_preview(params)
  post = self.new(params)
  post.generate_slug
  post.set_dates
  post.apply_filter
  TagList.from(params[:tag_list]).each do |tag|
    post.tags << Tag.new(:name => tag)
  end
  post
end

.default_limitObject


Class Methods




55
56
57
# File 'app/models/enki/base/post.rb', line 55

def self.default_limit
  DEFAULT_LIMIT
end

.find_all_grouped_by_monthObject



111
112
113
114
115
116
117
118
119
# File 'app/models/enki/base/post.rb', line 111

def find_all_grouped_by_month
  posts = find(
    :all,
    :order      => 'posts.published_at DESC',
    :conditions => ['published_at < ?', Time.now]
  )
  month = Struct.new(:date, :posts)
  posts.group_by(&:month).inject([]) {|a, v| a << month.new(v[0], v[1])}
end


97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/models/enki/base/post.rb', line 97

def find_by_permalink(year, month, day, slug, options = {})
  begin
    day = Time.parse([year, month, day].collect(&:to_i).join("-")).midnight
    post = find_all_by_slug(slug, options).detect do |post|
      [:year, :month, :day].all? {|time|
        post.published_at.send(time) == day.send(time)
      }
    end
  rescue ArgumentError # Invalid time
    post = nil
  end
  post || raise(ActiveRecord::RecordNotFound)
end

.find_recent(options = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'app/models/enki/base/post.rb', line 41

def self.find_recent(options = {})
  tag     = options.delete(:tag)
  limit   = options.delete(:limit) || DEFAULT_LIMIT

  query = tag ? tagged_with(tag) : self
  query = query.only_published.published_desc.limit(limit)

  return options.empty? ? query : all(options)
end

.only_publishedObject



37
38
39
# File 'app/models/enki/base/post.rb', line 37

def self.only_published
  where(['published_at < ?', Time.zone.now])
end

.published_descObject


Scopes




33
34
35
# File 'app/models/enki/base/post.rb', line 33

def self.published_desc
  order('posts.published_at DESC')
end

Instance Method Details

#apply_filterObject



134
135
136
# File 'app/models/enki/base/post.rb', line 134

def apply_filter
  self.body_html = EnkiFormatter.format_as_xhtml(self.body)
end

#denormalize_comments_count!Object



145
146
147
# File 'app/models/enki/base/post.rb', line 145

def denormalize_comments_count!
  Post.update_all(["approved_comments_count = ?", self.approved_comments.count], ["id = ?", self.id])
end

#destroy_with_undoObject



122
123
124
125
126
127
128
# File 'app/models/enki/base/post.rb', line 122

def destroy_with_undo
  transaction do
    undo = DeletePostUndo.create_undo(self)
    self.destroy
    return undo
  end
end

#generate_slugObject



149
150
151
152
# File 'app/models/enki/base/post.rb', line 149

def generate_slug
  self.slug = self.title.dup if self.slug.blank?
  self.slug.slugorize!
end

#minor_edit?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'app/models/enki/base/post.rb', line 72

def minor_edit?
  self.minor_edit == "1"
end

#monthObject



130
131
132
# File 'app/models/enki/base/post.rb', line 130

def month
  published_at.beginning_of_month
end

#published?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'app/models/enki/base/post.rb', line 76

def published?
  published_at?
end

#set_datesObject



138
139
140
141
142
143
# File 'app/models/enki/base/post.rb', line 138

def set_dates
  self.edited_at = Time.now if self.edited_at.nil? || !minor_edit?
  if new_published_at = Chronic.parse(self.published_at_natural)
    self.published_at = new_published_at
  end
end

#validate_published_at_naturalObject


Instance Methods




63
64
65
# File 'app/models/enki/base/post.rb', line 63

def validate_published_at_natural
  errors.add("published_at_natural", "Unable to parse time") unless published?
end