Module: Locomotive::BaseHelper

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

Instance Method Summary collapse

Instance Method Details

#account_logo_tagObject

Tag helpers ##



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

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

#application_domainObject

sites



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

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



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

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



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

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

#base_cache_key_for_sidebarObject



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

def base_cache_key_for_sidebar
  base_cache_key + [current_membership.role]
end

#base_cache_key_without_siteObject

cache keys



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

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



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

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



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

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



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

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



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

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

#datetime_moment_format(format = nil) ⇒ Object



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

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

#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



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

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:



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

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:



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

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.



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

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



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

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



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

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



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

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



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

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



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

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) ⇒ Object



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

def help(text)
  if text.present?
     :p, text, class: 'text'
  else
    ''
  end
end

#icon_tag(name) ⇒ Object



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

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


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

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



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

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



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

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



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

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.



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

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



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

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



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

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



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

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


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

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


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

def sidebar_logo_tag
  image_tag 'locomotive/logo.png'
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