Module: Homura::LayoutHelper

Defined in:
app/helpers/homura/layout_helper.rb

Constant Summary collapse

DEFAULT_PAGE_TITLE_KEY =
:'homura.default_page_title'
DEFAULT_PAGE_DESCRIPTION_KEY =
:'homura.default_page_description'

Instance Method Summary collapse

Instance Method Details

Get/Set canonical link for the page.

Raises:

  • will raise ‘canonical link should be a full url’ if not an url



53
54
55
56
57
58
59
60
61
62
# File 'app/helpers/homura/layout_helper.rb', line 53

def canonical_link(url = nil)
  if url
    if url !~ %r(^https?://)
      raise 'canonical link should be a full url'
    else
      @canonical_link = url
    end
  end
  @canonical_link
end


64
65
66
67
68
69
70
71
72
# File 'app/helpers/homura/layout_helper.rb', line 64

def canonical_link_tag
  if @canonical_link.blank?
    logger.warn "in #{controller_action_full_name}:"
    logger.warn 'No canonical link specified, please use `canonical_link` helper.'
    return
  else
    tag(:link, rel: 'canonical_link', href: @canonical_link)
  end
end

#controller_action_full_nameObject



6
7
8
# File 'app/helpers/homura/layout_helper.rb', line 6

def controller_action_full_name
  [params[:controller], params[:action]].join('#')
end

#og_properties(properties = {}) ⇒ Object

Set the Open Graph properties.

Examples

og_properties type: 'website',
              image: [image_path('hello.png'),
                      image_path('world.png')]

the page_title and page_description helpers are also set og properties, so you don’t need to do that again.



84
85
86
87
# File 'app/helpers/homura/layout_helper.rb', line 84

def og_properties(properties = {})
  @og_properties ||= {site_name: t('homura.layout.site_name')}
  @og_properties = @og_properties.merge(properties)
end

#og_properties_tagsObject



89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/helpers/homura/layout_helper.rb', line 89

def og_properties_tags
  og_properties.map { |property, content|
    case content
    when Array
      content.map { |item|
        tag(:meta, property: "og:#{property}", content: item)
      }.inject(&:+)
    else
      tag(:meta, property: "og:#{property}", content: content)
    end
  }.inject(&:+)
end

#page_classesObject



10
11
12
# File 'app/helpers/homura/layout_helper.rb', line 10

def page_classes
  [params[:controller].gsub('/', '_'), params[:action]].join(' ')
end

#page_description(text = nil) ⇒ Object



31
32
33
34
35
36
37
38
# File 'app/helpers/homura/layout_helper.rb', line 31

def page_description(text = nil)
  if text.present?
    @page_description = text
    og_properties description: text
  end
  @page_description ||
    t('.page_description', default: [DEFAULT_PAGE_DESCRIPTION_KEY, ''])
end

#page_description_tagObject



40
41
42
43
44
45
46
47
48
# File 'app/helpers/homura/layout_helper.rb', line 40

def page_description_tag
  if @page_description.blank?
    logger.warn "in #{controller_action_full_name}:"
    logger.warn 'No page description specified, please use `page_description` helper.'
  end
  if page_description.present?
    tag(:meta, name: :description, content: page_description)
  end
end

#page_title(text = nil) ⇒ Object



14
15
16
17
18
19
20
21
# File 'app/helpers/homura/layout_helper.rb', line 14

def page_title(text = nil)
  if text.present?
    @page_title = text
    og_properties title: text
  end
  @page_title || t('.page_title', default: [DEFAULT_PAGE_TITLE_KEY,
                                            controller_action_full_name])
end

#page_title_tagObject



23
24
25
26
27
28
29
# File 'app/helpers/homura/layout_helper.rb', line 23

def page_title_tag
  if @page_title.blank?
    logger.warn "in #{controller_action_full_name}:"
    logger.warn 'No page title specified, please use `page_title` helper.'
  end
  (:title, t('homura.layout.title', title: page_title))
end