Module: Locomotive::BaseHelper

Defined in:
app/helpers/locomotive/base_helper.rb

Instance Method Summary collapse

Instance Method Details

#application_domainObject

sites



148
149
150
151
152
# File 'app/helpers/locomotive/base_helper.rb', line 148

def application_domain
  domain = Locomotive.config.domain
  domain += ":#{request.port}" if request.port != 80
  domain
end

#backbone_view_class_nameObject



94
95
96
97
98
99
100
101
102
103
# File 'app/helpers/locomotive/base_helper.rb', line 94

def backbone_view_class_name
  action = case controller.action_name
  when 'create' then 'New'
  when 'update' then 'Edit'
  else
    controller.action_name
  end.camelize

  "Locomotive.Views.#{controller.controller_name.camelize}.#{action}View"
end

#backbone_view_dataObject



105
106
107
# File 'app/helpers/locomotive/base_helper.rb', line 105

def backbone_view_data
  content_for?(:backbone_view_data) ? content_for(:backbone_view_data) : ''
end

#empty_collection?(collection) ⇒ Boolean

MongoDB crashes when performing a query on a big collection where there is a sort without an index on the fields to sort.

Returns:



207
208
209
210
211
212
213
214
# File 'app/helpers/locomotive/base_helper.rb', line 207

def empty_collection?(collection)
  # criteria ?
  if collection.respond_to?(:without_sorting)
    collection.without_sorting.empty?
  else
    collection.empty?
  end
end

#flag_tag(locale, size = '24x24') ⇒ String

Display the image of the flag representing the locale.

Parameters:

  • locale (String / Symbol)

    The locale (fr, en, …etc)

  • size (String) (defaults to: '24x24')

    The width x height (by default, 24x24)

Returns:

  • (String)

    The HTML image tag with the path to the matching flag.



138
139
140
# File 'app/helpers/locomotive/base_helper.rb', line 138

def flag_tag(locale, size = '24x24')
  image_tag("locomotive/icons/flags/#{locale}.png", class: "flag flag-#{locale}", size: size)
end

#flash_messageObject



83
84
85
86
87
88
89
90
91
92
# File 'app/helpers/locomotive/base_helper.rb', line 83

def flash_message
  if not flash.empty?
    first_key = flash.keys.first
     :div, flash[first_key],
      id: "flash-#{first_key}",
      class: 'application-message'
  else
    ''
  end
end

#inputs_folded?(resource) ⇒ Boolean

Returns:



13
14
15
# File 'app/helpers/locomotive/base_helper.rb', line 13

def inputs_folded?(resource)
  resource.persisted? && resource.errors.empty?
end

Like link_to but instead of passing a label, we pass the name of an Font Awesome icon. If the name is a Symbol, we append “icon-” to the dasherized version of the name.

Parameters:

  • name (String / Symbol)

    The class name or a symbol

  • *args (Array)

Returns:

  • (String)

    The HTML <a> tag



27
28
29
30
31
# File 'app/helpers/locomotive/base_helper.rb', line 27

def link_to_icon(name, *args, &block)
  name = name.is_a?(Symbol) ? "icon-#{name.to_s.dasherize}" : name
  icon = (:i, '', class: name)
  link_to(icon, *args, &block).html_safe
end

#local_action_button(text, url, options = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'app/helpers/locomotive/base_helper.rb', line 64

def local_action_button(text, url, options = {})
  text = text.is_a?(Symbol) ? t(".#{text}") : text

  icon = options.delete(:icon) || :exclamation_sign
  icon = icon.is_a?(Symbol) ? "icon-#{icon.to_s.dasherize}" : icon

  link_to(url, options) do
    (:i, '', class: icon) + text
  end
end


75
76
77
78
79
80
81
# File 'app/helpers/locomotive/base_helper.rb', line 75

def locale_picker_link
  if current_site.locales.size > 1 && localized?
     :div, render('locomotive/shared/locale_picker_link'), class: 'action'
  else
    nil
  end
end

#localize(object, options = nil) ⇒ Object Also known as: l



195
196
197
198
199
200
# File 'app/helpers/locomotive/base_helper.rb', line 195

def localize(object, options = nil)
  if respond_to?(:current_site) && current_site && object.respond_to?(:in_time_zone)
    object = object.in_time_zone(current_site.timezone)
  end
  I18n.localize(object, options)
end

#manage_domains?Boolean

Returns:



162
163
164
# File 'app/helpers/locomotive/base_helper.rb', line 162

def manage_domains?
  Locomotive.config.manage_domains?
end

#manage_subdomain?Boolean

Returns:



158
159
160
# File 'app/helpers/locomotive/base_helper.rb', line 158

def manage_subdomain?
  Locomotive.config.manage_subdomain?
end

#manage_subdomain_or_domains?Boolean

Returns:



154
155
156
# File 'app/helpers/locomotive/base_helper.rb', line 154

def manage_subdomain_or_domains?
  Locomotive.config.manage_subdomain? || Locomotive.config.manage_domains?
end

#multi_sites?Boolean

Returns:



166
167
168
# File 'app/helpers/locomotive/base_helper.rb', line 166

def multi_sites?
  Locomotive.config.multi_sites?
end

#nocoffee_tagObject



142
143
144
# File 'app/helpers/locomotive/base_helper.rb', line 142

def nocoffee_tag
  link_to 'noCoffee', 'http://www.nocoffee.fr', id: 'nocoffee'
end

#not_the_default_current_locale?Boolean

For a localized site, tell if the current content locale does not match the default locale of the site. It is used by the page / snippet forms to determine if we have to display the warning message letting the designer know that the template is only editable in the default locale.

Returns:

  • (Boolean)

    True if it matches the condition above.



191
192
193
# File 'app/helpers/locomotive/base_helper.rb', line 191

def not_the_default_current_locale?
  current_site.localized? && current_content_locale.to_s != current_site.default_locale.to_s
end

#options_for_membership_roles(options = {}) ⇒ Object

memberships



172
173
174
175
176
177
178
179
180
# File 'app/helpers/locomotive/base_helper.rb', line 172

def options_for_membership_roles(options = {})
  list = (unless options[:skip_admin]
    Locomotive::Ability::ROLES.map { |r| [t("locomotive.memberships.roles.#{r}"), r] }
  else
    (Locomotive::Ability::ROLES - ['admin']).map { |r| [t("locomotive.memberships.roles.#{r}"), r] }
  end)

  options_for_select(list)
end

#required_once(label, &block) ⇒ Object

Execute the code only once during the request time. It avoids duplicated dom elements in the rendered rails page.

Parameters:

  • label (String / Symbol)

    Unique identifier of the block



38
39
40
41
42
43
44
45
# File 'app/helpers/locomotive/base_helper.rb', line 38

def required_once(label, &block)
  symbol = :"@block_#{label.to_s.underscore}"

  if instance_variable_get(symbol).blank?
    yield
    instance_variable_set(symbol, true)
  end
end

#submenu_entry(name, url, options = {}, &block) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/helpers/locomotive/base_helper.rb', line 47

def submenu_entry(name, url, options = {}, &block)
  default_options = { i18n: true, css: name.dasherize.downcase }
  default_options.merge!(options)

  css = "#{'on' if name == sections(:sub)} #{options[:css]}"

  label_link = default_options[:i18n] ? t("locomotive.shared.menu.#{name}") : name
  if block_given?
    popup = (:div, capture(&block), class: 'popup', style: 'display: none')
    text  = (:span, preserve(label_link) + (:i, '', class: 'icon-caret-down'))
    link  = link_to(text + (:em), url, class: css)
    (:li, link + popup, class: 'hoverable')
  else
    (:li, link_to((:span, label_link), url, class: css))
  end
end

#title(title = nil) ⇒ Object



4
5
6
7
8
9
10
11
# File 'app/helpers/locomotive/base_helper.rb', line 4

def title(title = nil)
  if title.nil?
    @content_for_title
  else
    @content_for_title = title
    ''
  end
end

#to_json(object) ⇒ String

Build the json version of a object. If the object owns a presenter then that presenter is used instead of the default object to_json method. Furthermore, if the presenter owns a as_json_for_html_view method, then it is called instead of the default as_json method. A html_safe is processed at the end.

Returns:

  • (String)

    The json representation of the object



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/helpers/locomotive/base_helper.rb', line 117

def to_json(object)
  if object.respond_to?(:to_presenter)
    presenter = object.to_presenter

    if presenter.respond_to?(:as_json_for_html_view)
      presenter.as_json_for_html_view
    else
      presenter.as_json
    end.to_json
  else
    object.to_json
  end.html_safe
end