forked from discourse/discourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsite_content_class_methods.rb
51 lines (38 loc) · 1.2 KB
/
site_content_class_methods.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
module SiteContentClassMethods
def content_types
@types || []
end
def find_content_type(ct)
SiteContent.content_types.find {|t| t.content_type == ct.to_sym}
end
def add_content_type(content_type, opts=nil)
opts ||= {}
@types ||= []
format = opts[:format] || :markdown
@types << SiteContentType.new(content_type, format, opts)
end
def content_for(content_type, replacements=nil)
replacements ||= {}
replacements = {site_name: SiteSetting.title}.merge!(replacements)
replacements = SiteSetting.settings_hash.merge!(replacements)
site_content = SiteContent.select(:content).where(content_type: content_type).first
result = ""
if site_content.blank?
ct = find_content_type(content_type)
result = ct.default_content if ct.present?
else
result = site_content.content
end
result.gsub!(/\%\{[^}]+\}/) do |m|
replacements[m[2..-2].to_sym] || m
end
result
end
def find_or_new(content_type)
site_content = SiteContent.where(content_type: content_type).first
return site_content if site_content.present?
site_content = SiteContent.new
site_content.content_type = content_type
site_content
end
end