Module: Locomotive::BaseHelper
- Defined in:
- app/helpers/locomotive/base_helper.rb
Instance Method Summary collapse
-
#application_domain ⇒ Object
sites.
- #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.
-
#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
Tag helpers ##.
- #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
- #title(title = nil) ⇒ Object
Instance Method Details
#application_domain ⇒ Object
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_name ⇒ Object
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_key ⇒ Object
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_sidebar ⇒ Object
250 251 252 |
# File 'app/helpers/locomotive/base_helper.rb', line 250 def base_cache_key + [current_membership.role] end |
#base_cache_key_without_site ⇒ Object
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_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
254 255 256 |
# File 'app/helpers/locomotive/base_helper.rb', line 254 def + ['sidebar', current_site.last_modified_at.to_i, current_content_locale] end |
#cache_key_for_sidebar_content_types ⇒ Object
264 265 266 267 268 |
# File 'app/helpers/locomotive/base_helper.rb', line 264 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
258 259 260 261 262 |
# File 'app/helpers/locomotive/base_helper.rb', line 258 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
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.
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 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.
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.
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_message ⇒ Object
98 99 100 101 102 103 104 105 106 107 |
# File 'app/helpers/locomotive/base_helper.rb', line 98 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
109 110 111 112 113 |
# File 'app/helpers/locomotive/base_helper.rb', line 109 def flash.map do |(key, )| [flash_key_to_bootstrap_alert(key), ] 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 content_tag(: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(' ') content_tag(: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? content_tag :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) content_tag :i, '', class: ['fa', name].join(' ') end |
#locale_picker_link(&block) ⇒ Object
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, = 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
53 54 55 56 57 58 |
# File 'app/helpers/locomotive/base_helper.rb', line 53 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
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.
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.
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_current_section_class ⇒ Object
Sidebar
38 39 40 41 42 43 44 45 |
# File 'app/helpers/locomotive/base_helper.rb', line 38 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
47 48 49 |
# File 'app/helpers/locomotive/base_helper.rb', line 47 def (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 content_tag(:h1, title) else @content_for_title = title '' end end end |