discourse/lib/site_content_class_methods.rb
Louis Rose 1574485443 Perform the where(...).first to find_by(...) refactoring.
This refactoring was automated using the command: bundle exec "ruby refactorings/where_dot_first_to_find_by/app.rb"
2014-05-06 14:41:59 +01:00

51 lines
1.2 KiB
Ruby

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).find_by(content_type: content_type)
result = ""
if site_content.blank?
ct = find_content_type(content_type)
result = ct.default_content.dup if ct.present?
else
result = site_content.content.dup
end
result.gsub!(/\%\{[^}]+\}/) do |m|
replacements[m[2..-2].to_sym] || m
end
result
end
def find_or_new(content_type)
site_content = SiteContent.find_by(content_type: content_type)
return site_content if site_content.present?
site_content = SiteContent.new
site_content.content_type = content_type
site_content
end
end