Class: RailsPages::PagesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/rails_pages/pages_controller.rb

Instance Method Summary collapse

Instance Method Details

#showObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/rails_pages/pages_controller.rb', line 5

def show
  raise "No Rails.root set" if Rails.root.nil?
  file_base = File.join(Rails.root, 'app', 'pages', params[:id])

  data_types = {
    md: proc { |text| Redcarpet::Markdown.new(Redcarpet::Render::XHTML.new).render(text) },
    markdown: proc { |text| Redcarpet::Markdown.new(Redcarpet::Render::XHTML.new).render(text) }
  }

  files = []
  files += Dir.glob(file_base+'.'+I18n.locale.to_s+'.{'+data_types.keys.join(',')+'}')
  files += Dir.glob(file_base+'.{'+data_types.keys.join(',')+'}')
  file = files.first
  if file.nil?
    Rails.logger.error file+' not found'
    raise ActionController::RoutingError.new('Not Found')
  end
  type = File.extname(file)
  type = type[1..-1]

  @html = data_types[type.to_sym].call(File.read(file))
  render layout: 'application'
end