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



9
10
11
12
13
14
15
16
17
18
19
# File 'app/controllers/plum/pages_controller.rb', line 9

def home
  @site_settings = SiteSetting.instance(current_site)
  @entry = Entry.for_site(current_site).live
                .where(locale: requested_locale)
                .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

#localized_homeObject



21
22
23
24
25
26
27
# File 'app/controllers/plum/pages_controller.rb', line 21

def localized_home
  raise ActiveRecord::RecordNotFound unless current_site.locales.include?(params[:locale])

  home
rescue ActiveRecord::RecordNotFound
  render file: Rails.public_path.join("404.html"), status: :not_found, layout: false
end

#searchObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/plum/pages_controller.rb', line 29

def search
  query = params[:q].to_s.strip
  context = build_context
  results = Entry.for_site(current_site).live.search(query)
                 .where(locale: requested_locale)
                 .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



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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/controllers/plum/pages_controller.rb', line 49

def show
  raise ActiveRecord::RecordNotFound unless current_site.locales.include?(requested_locale)

  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
                    .where(locale: requested_locale)
                    .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
                .where(locale: requested_locale)
                .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