Class: Plum::PagesController

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

Constant Summary collapse

HOMEPAGE_SLUG =
"home".freeze

Instance Method Summary collapse

Instance Method Details

#homeObject



5
6
7
8
9
10
11
12
13
14
# File 'app/controllers/plum/pages_controller.rb', line 5

def home
  @site_settings = SiteSetting.instance(current_site)
  @entry = Entry.for_site(current_site).live
                .includes(:content_type, terms: :taxonomy)
                .find_by(slug: HOMEPAGE_SLUG)

  template = @entry ? "entries/#{@entry.content_type.handle}" : "index"
  html = Plum::LiquidRenderer.render_template(template, build_context(@entry).to_h)
  render html: html.html_safe, layout: false
end

#searchObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/controllers/plum/pages_controller.rb', line 16

def search
  query = params[:q].to_s.strip
  context = build_context
  results = Entry.for_site(current_site).live.search(query)
                 .includes(:content_type)
                 .order(published_at: :desc)
                 .limit(50)

  context_hash = context.to_h.merge(
    "search" => {
      "query" => query,
      "results" => results.map { |e| context.send(:entry_context, e) },
      "total" => results.size
    }
  )
  html = Plum::LiquidRenderer.render_template("search", context_hash)
  render html: html.html_safe, layout: false
end

#showObject



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
# File 'app/controllers/plum/pages_controller.rb', line 35

def show
  slug = params[:slug]

  @content_type = ContentType.for_site(current_site).find_by(handle: slug)
  if @content_type
    return render_collection(@content_type)
  end

  if slug.include?("/")
    parts = slug.split("/", 2)
    content_type = ContentType.for_site(current_site).find do |candidate|
      candidate.route_prefix == parts[0]
    end
    if content_type
      @entry = Entry.for_site(current_site).live
                    .includes(:content_type, terms: :taxonomy)
                    .find_by!(content_type: content_type, slug: parts[1])
      html = Plum::LiquidRenderer.render_template("entries/#{content_type.handle}", build_context(@entry).to_h)
      return render html: html.html_safe, layout: false
    end

    taxonomy = Taxonomy.for_site(current_site).find_by(slug: parts[0])
    if taxonomy
      term = taxonomy.terms.find_by!(slug: parts[1])
      return render_term_page(taxonomy, term)
    end
  else
    taxonomy = Taxonomy.for_site(current_site).find_by(slug: slug)
    return render_taxonomy_index(taxonomy) if taxonomy
  end

  @entry = Entry.for_site(current_site).live
                .includes(:content_type, terms: :taxonomy)
                .find_by!(slug: slug)
  template = "entries/#{@entry.content_type.handle}"
  html = Plum::LiquidRenderer.render_template(template, build_context(@entry).to_h)
  render html: html.html_safe, layout: false
rescue ActiveRecord::RecordNotFound
  render file: Rails.public_path.join("404.html"), status: :not_found, layout: false
end