Class: Enki::Base::Post
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Enki::Base::Post
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
paginated, paginated_pages
Instance Attribute Details
#minor_edit ⇒ Object
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_natural ⇒ Object
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_limit ⇒ Object
55
56
57
|
# File 'app/models/enki/base/post.rb', line 55
def self.default_limit
DEFAULT_LIMIT
end
|
.find_all_grouped_by_month ⇒ Object
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
|
.find_by_permalink(year, month, day, slug, options = {}) ⇒ Object
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 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_published ⇒ Object
37
38
39
|
# File 'app/models/enki/base/post.rb', line 37
def self.only_published
where(['published_at < ?', Time.zone.now])
end
|
.published_desc ⇒ Object
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
145
146
147
|
# File 'app/models/enki/base/post.rb', line 145
def
Post.update_all(["approved_comments_count = ?", self..count], ["id = ?", self.id])
end
|
#destroy_with_undo ⇒ Object
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_slug ⇒ Object
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
72
73
74
|
# File 'app/models/enki/base/post.rb', line 72
def minor_edit?
self.minor_edit == "1"
end
|
#month ⇒ Object
130
131
132
|
# File 'app/models/enki/base/post.rb', line 130
def month
published_at.beginning_of_month
end
|
#published? ⇒ Boolean
76
77
78
|
# File 'app/models/enki/base/post.rb', line 76
def published?
published_at?
end
|
#set_dates ⇒ Object
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_natural ⇒ Object
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
|