• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

avonderluft / occams / #30

23 Sep 2023 03:50PM UTC coverage: 34.256% (-63.8%) from 98.056%
#30

push

web-flow
Merge pull request #11 from avonderluft/version108

Switch CI updates from Buildkite to Github actions

953 of 2782 relevant lines covered (34.26%)

8.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/lib/occams/render_methods.rb
1
# frozen_string_literal: true
2

3
module Occams::RenderMethods
×
4
  def self.included(base)
×
5
    # If application controller doesn't have template associated with it
6
    # CMS will attempt to find one. This is so you don't have to explicitly
7
    # call render cms_page: '/something'
8
    base.rescue_from 'ActionView::MissingTemplate' do |e|
×
9
      render cms_page: request.path
×
10
    rescue Occams::MissingPage, Occams::MissingSite
×
11
      raise e
×
12
    end
×
13
  end
×
14

15
  # Now you can render cms_page simply by calling:
16
  #   render cms_page: '/path/to/page'
17
  # This way application controllers can use CMS content while populating
18
  # instance variables that can be used in partials (that are included by
19
  # by the cms page and/or layout)
20
  #
21
  # Or how about not worrying about setting up CMS pages and rendering
22
  # application view using a CMS layout?
23
  #   render cms_layout: 'layout_slug', cms_fragments: {
24
  #     fragment_identifier_a: 'content text',
25
  #     fragment_identifier_b: {template: 'path/to/template' },
26
  #     fragment_identifier_c: {partial:  'path/to/partial' }
27
  #   }
28
  #
29
  # This way you are populating page block content and rendering
30
  # an instantialized CMS page.
31
  #
32
  # Site is loaded automatically based on the request. However you can force
33
  # it by passing :cms_site parameter with site's slug. For example:
34
  #   render cms_page: '/path/to/page', cms_site: 'default'
35
  #
36
  def render(options = {}, locals = {}, &block)
×
37
    return super unless options.is_a?(Hash)
×
38

39
    if (site_identifier = options.delete(:cms_site)) && !(@cms_site = Occams::Cms::Site.find_by_identifier(site_identifier))
×
40
      raise Occams::MissingSite, site_identifier
×
41
    end
×
42

43
    if ((page_path = options.delete(:cms_page)) || (layout_identifier = options.delete(:cms_layout))) && !@cms_site ||= Occams::Cms::Site.find_site(
×
44
      request.host_with_port.downcase, request.fullpath
×
45
    )
×
46
      raise Occams::MissingSite, "#{request.host.downcase}/#{request.fullpath}"
×
47
    end
×
48

49
    if page_path
×
50
      render_cms_page(page_path, options, locals, &block)
×
51
    elsif layout_identifier
×
52
      render_cms_layout(layout_identifier, options, locals, &block)
×
53
    else
×
54
      super
×
55
    end
×
56
  end
×
57

58
  def render_cms_page(path, options = {}, locals = {}, &block)
×
59
    path.gsub!(%r{^/#{@cms_site.path}}, '') if @cms_site.path.present?
×
60

61
    unless (@cms_page = @cms_site.pages.find_by_full_path(path))
×
62
      raise Occams::MissingPage, path
×
63
    end
×
64

65
    @cms_page.translate!
×
66

67
    @cms_layout = @cms_page.layout
×
68
    if (cms_fragments = options.delete(:cms_fragments)).present?
×
69
      cms_fragments.each do |identifier, value|
×
70
        content = value.is_a?(Hash) ? render_to_string(value.merge(layout: false)) : value.to_s
×
71
        page_fragment = @cms_page.fragments.detect { |f| f.identifier == identifier.to_s } ||
×
72
                        @cms_page.fragments.build(identifier: identifier.to_s)
×
73
        page_fragment.content = content
×
74
      end
×
75
    end
×
76
    cms_app_layout = @cms_layout.app_layout
×
77
    options[:layout] ||= cms_app_layout.blank? ? nil : cms_app_layout
×
78
    options[:inline] = @cms_page.render
×
79

80
    render(options, locals, &block)
×
81
  end
×
82

83
  def render_cms_layout(identifier, options = {}, locals = {}, &block)
×
84
    unless (@cms_layout = @cms_site.layouts.find_by_identifier(identifier))
×
85
      raise Occams::MissingLayout, identifier
×
86
    end
×
87

88
    cms_app_layout = @cms_layout.app_layout
×
89
    cms_page = @cms_site.pages.build(layout: @cms_layout)
×
90
    cms_fragments =
×
91
      options.delete(:cms_fragments) || { content: render_to_string({ layout: false }.merge(options)) }
×
92

93
    cms_fragments.each do |frag_identifier, value|
×
94
      content = value.is_a?(Hash) ? render_to_string(value.merge(layout: false)) : value.to_s
×
95
      cms_page.fragments.build(identifier: frag_identifier.to_s, content: content)
×
96
    end
×
97
    options[:layout] ||= cms_app_layout.blank? ? nil : cms_app_layout
×
98
    options[:inline] = cms_page.render
×
99

100
    render(options, locals, &block)
×
101
  end
×
102
end
×
103

104
ActiveSupport.on_load :action_controller_base do
×
105
  include Occams::RenderMethods
×
106
end
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc