Module: Locomotive::BaseHelper

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

Instance Method Summary collapse

Instance Method Details

#application_domainObject

sites



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

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

#backbone_view_class_nameObject



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

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



246
247
248
# File 'app/helpers/locomotive/base_helper.rb', line 246

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

#base_cache_key_for_sidebarObject



250
251
252
# File 'app/helpers/locomotive/base_helper.rb', line 250

def base_cache_key_for_sidebar
  base_cache_key + [current_membership.role]
end

#base_cache_key_without_siteObject

cache keys



242
243
244
# File 'app/helpers/locomotive/base_helper.rb', line 242

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



254
255
256
# File 'app/helpers/locomotive/base_helper.rb', line 254

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



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

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



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

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



193
194
195
# File 'app/helpers/locomotive/base_helper.rb', line 193

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

#datetime_moment_format(format = nil) ⇒ Object



197
198
199
200
201
202
203
204
# File 'app/helpers/locomotive/base_helper.rb', line 197

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



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

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:



210
211
212
213
214
215
216
217
# File 'app/helpers/locomotive/base_helper.rb', line 210

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.



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

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



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

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



98
99
100
101
102
103
104
105
106
107
# File 'app/helpers/locomotive/base_helper.rb', line 98

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



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

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



60
61
62
63
64
# File 'app/helpers/locomotive/base_helper.rb', line 60

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



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

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



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

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

#icon_tag(name) ⇒ Object

Tag helpers ##



75
76
77
# File 'app/helpers/locomotive/base_helper.rb', line 75

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


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

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



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

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



53
54
55
56
57
58
# File 'app/helpers/locomotive/base_helper.rb', line 53

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



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

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.



179
180
181
# File 'app/helpers/locomotive/base_helper.rb', line 179

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



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

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



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

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



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

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


47
48
49
# File 'app/helpers/locomotive/base_helper.rb', line 47

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

#title(title = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/helpers/locomotive/base_helper.rb', line 14

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