Module: Homura::LayoutHelper

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

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



49
50
51
52
53
54
55
56
57
58
# File 'app/helpers/homura/layout_helper.rb', line 49

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


60
61
62
63
64
65
66
67
68
# File 'app/helpers/homura/layout_helper.rb', line 60

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



3
4
5
# File 'app/helpers/homura/layout_helper.rb', line 3

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.



80
81
82
83
# File 'app/helpers/homura/layout_helper.rb', line 80

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

#og_properties_tagsObject



85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/helpers/homura/layout_helper.rb', line 85

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



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

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

#page_description(text = nil) ⇒ Object



28
29
30
31
32
33
34
# File 'app/helpers/homura/layout_helper.rb', line 28

def page_description(text = nil)
  if text.present?
    @page_description = text
    og_properties description: text
  end
  @page_description
end

#page_description_tagObject



36
37
38
39
40
41
42
43
44
# File 'app/helpers/homura/layout_helper.rb', line 36

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.'
    return
  else
    tag(:meta, name: :description, content: @page_description)
  end
end

#page_title(text = nil) ⇒ Object



11
12
13
14
15
16
17
# File 'app/helpers/homura/layout_helper.rb', line 11

def page_title(text = nil)
  if text.present?
    @page_title = text
    og_properties title: text
  end
  @page_title
end

#page_title_tagObject



19
20
21
22
23
24
25
26
# File 'app/helpers/homura/layout_helper.rb', line 19

def page_title_tag
  if @page_title.blank?
    @page_title = controller_action_full_name
    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