forked from publify/publify
/
content_base.rb
116 lines (96 loc) · 2.73 KB
/
content_base.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
module ContentBase
def self.included base
base.extend ClassMethods
end
def blog
@blog ||= Blog.default
end
attr_accessor :just_changed_published_status
alias_method :just_changed_published_status?, :just_changed_published_status
# Set the text filter for this object.
def text_filter= filter
filter_object = filter.to_text_filter
if filter_object
self.text_filter_id = filter_object.id
else
self.text_filter_id = filter.to_i
end
end
def really_send_notifications
interested_users.each do |value|
send_notification_to_user(value)
end
return true
end
def send_notification_to_user(user)
notify_user_via_email(user)
end
# Return HTML for some part of this object.
def html(field = :all)
if field == :all
generate_html(:all, content_fields.map{|f| self[f].to_s}.join("\n\n"))
elsif html_map(field)
generate_html(field)
else
raise "Unknown field: #{field.inspect} in content.html"
end
end
# Generate HTML for a specific field using the text_filter in use for this
# object.
def generate_html(field, text = nil)
text ||= self[field].to_s
prehtml = html_preprocess(field, text).to_s
html = (text_filter || default_text_filter).filter_text_for_content(blog, prehtml, self) || prehtml
html_postprocess(field,html).to_s
end
# Post-process the HTML. This is a noop by default, but Comment overrides it
# to enforce HTML sanity.
def html_postprocess(field, html)
html
end
def html_preprocess(field, html)
html
end
def html_map field
content_fields.include? field
end
def excerpt_text(length = 160)
if respond_to?(:excerpt) and (excerpt || "") != ""
text = generate_html(:excerpt, excerpt)
else
text = html(:all)
end
text = text.strip_html
return text.slice(0, length) +
(text.length > length ? '...' : '');
end
def invalidates_cache?(on_destruction = false)
@invalidates_cache ||= if on_destruction
just_changed_published_status? || published?
else
(changed? && published?) || just_changed_published_status?
end
end
def publish!
self.published = true
self.save!
end
# The default text filter. Generally, this is the filter specified by blog.text_filter,
# but comments may use a different default.
def default_text_filter
blog.text_filter_object
end
module ClassMethods
def content_fields *attribs
class_eval "def content_fields; #{attribs.inspect}; end"
end
def find_published(what = :all, options = {})
with_scope(:find => where(:published => true).order(default_order)) do
find what, options
end
end
def default_order
'published_at DESC'
end
end
end