Module: Simplec::ActionView::Helper

Defined in:
lib/simplec/action_view/helper.rb

Instance Method Summary collapse

Instance Method Details

#doc(slug, options = {}) ⇒ Object

Retreive a document.

slug - matching a Document slug. options - a string matching a Subdomain name, find a document

from another subdomain

options - if true, casuse a 404 if document isn't found

Example:

-# Save as...
<%= link_to doc('/permission/general').name,
  doc('/permission/general').path,
  download: true %>

-# Display in new window...
<%= link_to doc('/permission/general').name,
  doc('/permission/general').path,
  target: '_blank' %>

Raises:

  • (ActiveRecord::RecordNotFound)

46
47
48
49
50
51
52
53
54
# File 'lib/simplec/action_view/helper.rb', line 46

def doc(slug, options={})
  @_docs ||= Hash.new
  key = "[#{options[:subdomain]}][#{slug}]"
  return @_docs[key] if @_docs[key]
  @_docs[key] = subdomain(options[:subdomain]).
    documents.find_by(slug: slug)
  raise ActiveRecord::RecordNotFound if options[:raise] && !@_docs[key]
  @_docs[key]
end

#docset(slug, options = {}) ⇒ Object

Retreive a document.

slug - matching a Document slug. options - a string matching a Subdomain name, find a document

set from another subdomain

options - if true, casuse a 404 if document set isn't found

Example:

<% docset('/faith').each do |doc| %>
  <%= link_to doc.name, doc.path %>
<% end %>

Raises:

  • (ActiveRecord::RecordNotFound)

69
70
71
72
73
74
75
76
77
# File 'lib/simplec/action_view/helper.rb', line 69

def docset(slug, options={})
  @_docsets ||= Hash.new
  key = "[#{options[:subdomain]}][#{slug}]"
  return @_docsets[key] if @_docsets[key]
  set = subdomain(options[:subdomain]).
    document_sets.find_by(slug: slug)
  raise ActiveRecord::RecordNotFound if options[:raise] && !set
  @_docsets[key] = set.documents
end

#head(page) ⇒ Object


5
6
7
8
# File 'lib/simplec/action_view/helper.rb', line 5

def head(page)
  content_for :title, page.title
  content_for :meta_description, page.meta_description
end

#meta_description_tagObject


14
15
16
# File 'lib/simplec/action_view/helper.rb', line 14

def meta_description_tag
  tag :meta, name: 'description', content: content_for(:meta_description)
end

#page_field(f, options = {}) ⇒ Object


23
24
25
# File 'lib/simplec/action_view/helper.rb', line 23

def page_field(f, options={})
  render "simplec/fields/#{options[:type]}", options.merge(f: f)
end

#subpages(page_or_path, options = {}) ⇒ Object

page_or_path - a page object or a path of a page options - a string matching a Subdomain name, find a page

from another subdomain

options - if true, casuse a 404 if a page set isn't found

Example:

<% subpages('/give-volunteer', conditions: ->{ order(title: :asc) }).each do |page| %>
  ... do something with page ...
<% end%>

89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/simplec/action_view/helper.rb', line 89

def subpages(page_or_path, options={})
  if options[:conditions] && !options[:conditions].respond_to?(:call)
    raise ArgumentError, <<-ERROR
    #{options[:conditions]} was passed as :conditions but is not callable.
"Pass a callable instead: `conditions: -> { where(approved: true) }`
    ERROR
  end

  @_subpages ||= Hash.new # TODO apply new conditions after cache
  key = "[#{options[:subdomain]}][#{page_or_path}]"

  unless @_subpages[key]
    page = page_or_path.is_a?(Page) ? page_or_path : nil
    page ||= subdomain(options[:subdomain]).pages.find_by(path: page_or_path)

    unless page
      raise ActiveRecord::RecordNotFound if options[:raise]
      return @_subpages[key] = Array.new
    end

    @_subpages[key] = page.subpages
  end

  @_subpages[key].respond_to?(:merge) && options[:conditions] ?
    @_subpages[key].merge(options[:conditions]) : @_subpages[key]
end

#template(page) ⇒ Object


18
19
20
21
# File 'lib/simplec/action_view/helper.rb', line 18

def template(page)
  _template = page.type.demodulize.downcase
  render "pages/#{_template}"
end

#titleObject


10
11
12
# File 'lib/simplec/action_view/helper.rb', line 10

def title
  content_for :title
end