Module: Locomotive::BaseHelper

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

Instance Method Summary collapse

Instance Method Details

#account_logo_tagObject

Tag helpers ##



86
87
88
# File 'app/helpers/locomotive/base_helper.rb', line 86

def 
  image_tag 'locomotive/logo-white.png'
end

#application_domainObject

sites



183
184
185
186
187
# File 'app/helpers/locomotive/base_helper.rb', line 183

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

#application_nameObject



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

def application_name
  Locomotive.config.name
end

#backbone_view_class_nameObject



152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'app/helpers/locomotive/base_helper.rb', line 152

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

  (parts = controller.controller_path.split('/')).shift
  name  = parts.map(&:camelize).join('.')

  "Locomotive.Views.#{name}.#{action}View"
end

#base_cache_keyObject



265
266
267
# File 'app/helpers/locomotive/base_helper.rb', line 265

def base_cache_key
  base_cache_key_without_site + [ current_site._id, current_site.handle]
end

#base_cache_key_for_sidebarObject



269
270
271
# File 'app/helpers/locomotive/base_helper.rb', line 269

def base_cache_key_for_sidebar
  base_cache_key + [current_membership.role]
end

#base_cache_key_without_siteObject

cache keys



261
262
263
# File 'app/helpers/locomotive/base_helper.rb', line 261

def base_cache_key_without_site
  [Locomotive::VERSION, locale]
end

#body_classObject



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

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

  [self.controller.controller_name, action].map(&:dasherize).join(' ')
end

#cache_key_for_sidebarObject



273
274
275
# File 'app/helpers/locomotive/base_helper.rb', line 273

def cache_key_for_sidebar
  base_cache_key_for_sidebar + ['sidebar', current_site.last_modified_at.to_i, current_content_locale]
end

#cache_key_for_sidebar_content_typesObject



283
284
285
286
287
# File 'app/helpers/locomotive/base_helper.rb', line 283

def cache_key_for_sidebar_content_types
  count          = current_site.content_types.count
  max_updated_at = current_site.content_types.max(:updated_at).try(:utc).try(:to_s, :number).to_i
  base_cache_key_for_sidebar + ['content_types', count, max_updated_at]
end

#cache_key_for_sidebar_pagesObject



277
278
279
280
281
# File 'app/helpers/locomotive/base_helper.rb', line 277

def cache_key_for_sidebar_pages
  count          = current_site.pages.count
  max_updated_at = current_site.pages.max(:updated_at).try(:utc).try(:to_s, :number).to_i
  base_cache_key_for_sidebar + ['pages', count, max_updated_at, current_content_locale]
end

#date_moment_formatObject

Dates



212
213
214
# File 'app/helpers/locomotive/base_helper.rb', line 212

def date_moment_format
  datetime_moment_format(I18n.t('date.formats.default'))
end

#datetime_moment_format(format = nil) ⇒ Object



216
217
218
219
220
221
222
223
# File 'app/helpers/locomotive/base_helper.rb', line 216

def datetime_moment_format(format = nil)
  (format || I18n.t('time.formats.default'))
    .gsub('%d', 'DD')
    .gsub('%m', 'MM')
    .gsub('%Y', 'YYYY')
    .gsub('%H', 'HH')
    .gsub('%M', 'mm')
end

#decorated_steam_content_entry(content_entry) ⇒ Object

Steam helpers



291
292
293
294
295
296
297
# File 'app/helpers/locomotive/base_helper.rb', line 291

def decorated_steam_content_entry(content_entry)
  Locomotive::Steam::Decorators::I18nDecorator.new(
    content_entry.to_steam,
    current_content_locale,
    current_site.default_locale
  )
end

#decorated_steam_page(page, locale = nil) ⇒ Object



299
300
301
302
303
304
305
# File 'app/helpers/locomotive/base_helper.rb', line 299

def decorated_steam_page(page, locale = nil)
  Locomotive::Steam::Decorators::PageDecorator.new(
    page.to_steam,
    locale || current_content_locale,
    current_site.default_locale
  )
end

#document_stamp(document) ⇒ String

Display the name of the account (+ avatar) who created or updated the document (content_entry, translation, …etc) as well as the date when it occured.

Parameters:

  • document (Object)

    The model

Returns:

  • (String)

    The html output



245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'app/helpers/locomotive/base_helper.rb', line 245

def document_stamp(document)
  distance  = distance_of_time_in_words_to_now(document.updated_at)
  update    = document.updated_at && document.updated_at != document.created_at

  if  = (document.updated_by || document.created_by)
    profile = (, '40x40#')
    key     = update ? :updated_by : :created_by
    t(key, scope: 'locomotive.shared.list', distance: distance, who: profile)
  else
    key = update ? :updated_at : :created_at
    t(key, scope: 'locomotive.shared.list', distance: distance)
  end
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:



229
230
231
232
233
234
235
236
# File 'app/helpers/locomotive/base_helper.rb', line 229

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

#enable_registration?Boolean

Sessions

Returns:



43
44
45
# File 'app/helpers/locomotive/base_helper.rb', line 43

def enable_registration?
  current_site.nil? && Locomotive.config.enable_registration
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.



173
174
175
# File 'app/helpers/locomotive/base_helper.rb', line 173

def flag_tag(locale, size = '24x24')
  %(<i class="flag flag-#{locale} flag-#{size.gsub('x', '-')}"></i>).html_safe
end

#flash_key_to_bootstrap_alert(key) ⇒ Object



143
144
145
146
147
148
149
150
# File 'app/helpers/locomotive/base_helper.rb', line 143

def flash_key_to_bootstrap_alert(key)
  case key.to_sym
  when :notice          then :success
  when :alert, :error   then :danger
  else
    :info
  end
end

#flash_messageObject



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

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

#flash_messages_to_jsonObject



128
129
130
131
132
# File 'app/helpers/locomotive/base_helper.rb', line 128

def flash_messages_to_json
  flash.map do |(key, message)|
    [flash_key_to_bootstrap_alert(key), message]
  end.to_json
end

#form_nav_tab(name, first = false, &block) ⇒ Object



71
72
73
74
75
# File 'app/helpers/locomotive/base_helper.rb', line 71

def form_nav_tab(name, first = false, &block)
  active = (first && params[:active_tab].blank?) || params[:active_tab] == name.to_s

  (:li, capture(&block), class: active ? 'active' : '')
end

#form_tab_pane(name, first = false, &block) ⇒ Object



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

def form_tab_pane(name, first = false, &block)
  active  = (first && params[:active_tab].blank?) || params[:active_tab] == name.to_s
  css     = ['tab-pane', active ? 'active' : nil].compact.join(' ')

  (:div, capture(&block), id: name, class: css)
end

#help(text = nil) ⇒ Object



32
33
34
35
36
37
38
39
# File 'app/helpers/locomotive/base_helper.rb', line 32

def help(text = nil)
  if text.nil?
    @content_for_help
  else
    @content_for_help = (:p, text, class: 'text')
    ''
  end
end

#icon_tag(name) ⇒ Object



94
95
96
# File 'app/helpers/locomotive/base_helper.rb', line 94

def icon_tag(name)
   :i, '', class: ['fa', name].join(' ')
end


112
113
114
115
# File 'app/helpers/locomotive/base_helper.rb', line 112

def locale_picker_link(&block)
  return '' if current_site.locales.size == 1
  render partial: 'locomotive/shared/locale_picker_link', locals: { url_block: block_given? ? block : nil }
end

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



202
203
204
205
206
207
# File 'app/helpers/locomotive/base_helper.rb', line 202

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

#locomotive_form_for(object, *args, &block) ⇒ Object

Form helpers



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

def locomotive_form_for(object, *args, &block)
  options = args.extract_options!
  options[:wrapper] = :locomotive
  (options[:data] ||= {})[:blank_required_fields_message] = t(:blank_required_fields, scope: 'simple_form')
  simple_form_for(object, *(args << options.merge(builder: Locomotive::FormBuilder)), &block)
end

#nocoffee_tagObject



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

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.



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

def not_the_default_current_locale?
  current_site.localized? && current_content_locale.to_s != current_site.default_locale.to_s
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



103
104
105
106
107
108
109
110
# File 'app/helpers/locomotive/base_helper.rb', line 103

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

#set_error_from_flash(resource, attribute) ⇒ Object



134
135
136
137
138
139
140
141
# File 'app/helpers/locomotive/base_helper.rb', line 134

def set_error_from_flash(resource, attribute)
  if !flash.empty? && flash.alert
    flash.alert.tap do |msg|
      resource.errors.add(attribute.to_sym, msg)
      flash.delete(:alert)
    end
  end
end

Sidebar



49
50
51
52
53
54
55
56
# File 'app/helpers/locomotive/base_helper.rb', line 49

def sidebar_current_section_class
  case self.controller.controller_name
  when 'pages', 'editable_elements' then :pages
  when 'content_types', 'content_entries', 'public_submission_accounts', 'select_options' then :content_types
  else
    self.controller.controller_name
  end
end


58
59
60
# File 'app/helpers/locomotive/base_helper.rb', line 58

def sidebar_link_class(section)
  ['sidebar-link', section].join(' ')
end


90
91
92
# File 'app/helpers/locomotive/base_helper.rb', line 90

def sidebar_logo_tag
  image_tag 'locomotive/logo.png'
end

#steam_url_builderObject



307
308
309
310
311
# File 'app/helpers/locomotive/base_helper.rb', line 307

def steam_url_builder
  return @steam_url_builder if @steam_url_builder

  @steam_url_builder = Locomotive::Steam::Services.build_simple_instance(current_site).url_builder
end

#title(title = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/helpers/locomotive/base_helper.rb', line 18

def title(title = nil)
  if title.nil?
    @content_for_title
  else
    if request.xhr?
      @content_for_title = '' # won't raise an exception if a layout is applied.
      concat (:h1, title)
    else
      @content_for_title = title
      ''
    end
  end
end