Module: Locomotive::BaseHelper
- Defined in:
- app/helpers/locomotive/base_helper.rb
Instance Method Summary collapse
-
#account_logo_tag ⇒ Object
Tag helpers ##.
-
#application_domain ⇒ Object
sites.
- #application_name ⇒ Object
- #backbone_view_class_name ⇒ Object
- #base_cache_key ⇒ Object
- #base_cache_key_for_sidebar ⇒ Object
-
#base_cache_key_without_site ⇒ Object
cache keys.
- #body_class ⇒ Object
- #cache_key_for_sidebar ⇒ Object
- #cache_key_for_sidebar_content_types ⇒ Object
- #cache_key_for_sidebar_pages ⇒ Object
-
#date_moment_format ⇒ Object
Dates.
- #datetime_moment_format(format = nil) ⇒ Object
-
#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.
-
#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.
-
#enable_registration? ⇒ Boolean
Sessions.
-
#flag_tag(locale, size = '24x24') ⇒ String
Display the image of the flag representing the locale.
- #flash_key_to_bootstrap_alert(key) ⇒ Object
- #flash_message ⇒ Object
- #flash_messages_to_json ⇒ Object
- #form_nav_tab(name, first = false, &block) ⇒ Object
- #form_tab_pane(name, first = false, &block) ⇒ Object
- #help(text) ⇒ Object
- #icon_tag(name) ⇒ Object
- #locale_picker_link(&block) ⇒ Object
- #localize(object, options = nil) ⇒ Object (also: #l)
-
#locomotive_form_for(object, *args, &block) ⇒ Object
Form helpers.
- #nocoffee_tag ⇒ Object
-
#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.
-
#required_once(label, &block) ⇒ Object
Execute the code only once during the request time.
- #set_error_from_flash(resource, attribute) ⇒ Object
-
#sidebar_current_section_class ⇒ Object
Sidebar.
- #sidebar_link_class(section) ⇒ Object
- #sidebar_logo_tag ⇒ Object
- #title(title = nil) ⇒ Object
Instance Method Details
#account_logo_tag ⇒ Object
Tag helpers ##
85 86 87 |
# File 'app/helpers/locomotive/base_helper.rb', line 85 def account_logo_tag image_tag 'locomotive/logo-white.png' end |
#application_domain ⇒ Object
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_name ⇒ Object
14 15 16 |
# File 'app/helpers/locomotive/base_helper.rb', line 14 def application_name Locomotive.config.name end |
#backbone_view_class_name ⇒ Object
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_key ⇒ Object
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_sidebar ⇒ Object
268 269 270 |
# File 'app/helpers/locomotive/base_helper.rb', line 268 def base_cache_key + [current_membership.role] end |
#base_cache_key_without_site ⇒ Object
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_class ⇒ Object
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_sidebar ⇒ Object
272 273 274 |
# File 'app/helpers/locomotive/base_helper.rb', line 272 def + ['sidebar', current_site.last_modified_at.to_i, current_content_locale] end |
#cache_key_for_sidebar_content_types ⇒ Object
282 283 284 285 286 |
# File 'app/helpers/locomotive/base_helper.rb', line 282 def count = current_site.content_types.count max_updated_at = current_site.content_types.max(:updated_at).try(:utc).try(:to_s, :number).to_i + ['content_types', count, max_updated_at] end |
#cache_key_for_sidebar_pages ⇒ Object
276 277 278 279 280 |
# File 'app/helpers/locomotive/base_helper.rb', line 276 def count = current_site.pages.count max_updated_at = current_site.pages.max(:updated_at).try(:utc).try(:to_s, :number).to_i + ['pages', count, max_updated_at, current_content_locale] end |
#date_moment_format ⇒ Object
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.
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 account = (document.updated_by || document.created_by) profile = account_avatar_and_name(account, '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.
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
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.
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_message ⇒ Object
116 117 118 119 120 121 122 123 124 125 |
# File 'app/helpers/locomotive/base_helper.rb', line 116 def if not flash.empty? first_key = flash.keys.first content_tag :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_json ⇒ Object
127 128 129 130 131 |
# File 'app/helpers/locomotive/base_helper.rb', line 127 def flash.map do |(key, )| [flash_key_to_bootstrap_alert(key), ] 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 content_tag(: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(' ') content_tag(: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? content_tag :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) content_tag :i, '', class: ['fa', name].join(' ') end |
#locale_picker_link(&block) ⇒ Object
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, = 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, ) 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) = args. [:wrapper] = :locomotive ([:data] ||= {})[:blank_required_fields_message] = t(:blank_required_fields, scope: 'simple_form') simple_form_for(object, *(args << .merge(builder: Locomotive::FormBuilder)), &block) end |
#nocoffee_tag ⇒ Object
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.
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.
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_current_section_class ⇒ Object
Sidebar
48 49 50 51 52 53 54 55 |
# File 'app/helpers/locomotive/base_helper.rb', line 48 def 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 |
#sidebar_link_class(section) ⇒ Object
57 58 59 |
# File 'app/helpers/locomotive/base_helper.rb', line 57 def (section) ['sidebar-link', section].join(' ') end |
#sidebar_logo_tag ⇒ Object
89 90 91 |
# File 'app/helpers/locomotive/base_helper.rb', line 89 def 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 content_tag(:h1, title) else @content_for_title = title '' end end end |