Module: Locomotive::BaseHelper

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

Instance Method Summary collapse

Instance Method Details

#application_domainObject

sites



126
127
128
129
130
# File 'app/helpers/locomotive/base_helper.rb', line 126

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

#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.



116
117
118
# File 'app/helpers/locomotive/base_helper.rb', line 116

def flag_tag(locale, size = '24x24')
  image_tag("locomotive/icons/flags/#{locale}.png", :class => 'flag', :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

#manage_domains?Boolean

Returns:



140
141
142
# File 'app/helpers/locomotive/base_helper.rb', line 140

def manage_domains?
  Locomotive.config.manage_domains?
end

#manage_subdomain?Boolean

Returns:



136
137
138
# File 'app/helpers/locomotive/base_helper.rb', line 136

def manage_subdomain?
  Locomotive.config.manage_subdomain?
end

#manage_subdomain_or_domains?Boolean

Returns:



132
133
134
# File 'app/helpers/locomotive/base_helper.rb', line 132

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

#multi_sites?Boolean

Returns:



144
145
146
# File 'app/helpers/locomotive/base_helper.rb', line 144

def multi_sites?
  Locomotive.config.multi_sites?
end

#nocoffee_tagObject



120
121
122
# File 'app/helpers/locomotive/base_helper.rb', line 120

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.



177
178
179
# File 'app/helpers/locomotive/base_helper.rb', line 177

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



158
159
160
161
162
163
164
165
166
# File 'app/helpers/locomotive/base_helper.rb', line 158

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

#public_page_url(page, options = {}) ⇒ Object



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

def public_page_url(page, options = {})
  if content = options.delete(:content)
    File.join(current_site_public_url, page.fullpath.gsub('content_type_template', ''), content._slug)
  else
    File.join(current_site_public_url, page.fullpath)
  end
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