2013-04-04 12:59:44 -04:00
|
|
|
module SiteContentClassMethods
|
|
|
|
|
|
|
|
def content_types
|
|
|
|
@types || []
|
|
|
|
end
|
|
|
|
|
2013-04-05 15:21:55 -04:00
|
|
|
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)
|
2013-04-04 12:59:44 -04:00
|
|
|
opts ||= {}
|
|
|
|
@types ||= []
|
2013-04-05 15:21:55 -04:00
|
|
|
format = opts[:format] || :markdown
|
2013-04-04 12:59:44 -04:00
|
|
|
@types << SiteContentType.new(content_type, format, opts)
|
|
|
|
end
|
|
|
|
|
|
|
|
def content_for(content_type, replacements=nil)
|
|
|
|
replacements ||= {}
|
2013-04-05 16:47:20 -04:00
|
|
|
replacements = {site_name: SiteSetting.title}.merge!(replacements)
|
2013-04-05 15:21:55 -04:00
|
|
|
replacements = SiteSetting.settings_hash.merge!(replacements)
|
2013-04-04 12:59:44 -04:00
|
|
|
|
|
|
|
site_content = SiteContent.select(:content).where(content_type: content_type).first
|
|
|
|
|
2013-04-05 15:21:55 -04:00
|
|
|
result = ""
|
|
|
|
if site_content.blank?
|
|
|
|
ct = find_content_type(content_type)
|
2014-02-17 19:14:35 -05:00
|
|
|
result = ct.default_content.dup if ct.present?
|
2013-04-05 15:21:55 -04:00
|
|
|
else
|
2014-02-17 19:14:35 -05:00
|
|
|
result = site_content.content.dup
|
2013-04-05 15:21:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
result.gsub!(/\%\{[^}]+\}/) do |m|
|
|
|
|
replacements[m[2..-2].to_sym] || m
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
2013-04-04 12:59:44 -04:00
|
|
|
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
|