Module: Locomotive::BaseHelper
- Defined in:
- app/helpers/locomotive/base_helper.rb
Instance Method Summary collapse
-
#application_domain ⇒ Object
sites.
- #backbone_view_class_name ⇒ Object
- #backbone_view_data ⇒ Object
-
#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_message ⇒ Object
- #inputs_folded?(resource) ⇒ Boolean
-
#link_to_icon(name, *args, &block) ⇒ String
Like link_to but instead of passing a label, we pass the name of an Font Awesome icon.
- #local_action_button(text, url, options = {}) ⇒ Object
- #locale_picker_link ⇒ Object
- #localize(object, options = nil) ⇒ Object (also: #l)
- #manage_domains? ⇒ Boolean
- #manage_subdomain? ⇒ Boolean
- #manage_subdomain_or_domains? ⇒ Boolean
- #multi_sites? ⇒ Boolean
- #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.
-
#options_for_membership_roles(options = {}) ⇒ Object
memberships.
-
#required_once(label, &block) ⇒ Object
Execute the code only once during the request time.
- #submenu_entry(name, url, options = {}, &block) ⇒ Object
- #title(title = nil) ⇒ Object
-
#to_json(object) ⇒ String
Build the json version of a object.
Instance Method Details
#application_domain ⇒ Object
sites
148 149 150 151 152 |
# File 'app/helpers/locomotive/base_helper.rb', line 148 def application_domain domain = Locomotive.config.domain domain += ":#{request.port}" if request.port != 80 domain end |
#backbone_view_class_name ⇒ Object
94 95 96 97 98 99 100 101 102 103 |
# File 'app/helpers/locomotive/base_helper.rb', line 94 def backbone_view_class_name action = case controller.action_name when 'create' then 'New' when 'update' then 'Edit' else controller.action_name end.camelize "Locomotive.Views.#{controller.controller_name.camelize}.#{action}View" end |
#backbone_view_data ⇒ Object
105 106 107 |
# File 'app/helpers/locomotive/base_helper.rb', line 105 def backbone_view_data content_for?(:backbone_view_data) ? content_for(:backbone_view_data) : '' 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.
207 208 209 210 211 212 213 214 |
# File 'app/helpers/locomotive/base_helper.rb', line 207 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.
138 139 140 |
# File 'app/helpers/locomotive/base_helper.rb', line 138 def flag_tag(locale, size = '24x24') image_tag("locomotive/icons/flags/#{locale}.png", class: "flag flag-#{locale}", size: size) end |
#flash_message ⇒ Object
83 84 85 86 87 88 89 90 91 92 |
# File 'app/helpers/locomotive/base_helper.rb', line 83 def if not flash.empty? first_key = flash.keys.first content_tag :div, flash[first_key], id: "flash-#{first_key}", class: 'application-message' else '' end end |
#inputs_folded?(resource) ⇒ Boolean
13 14 15 |
# File 'app/helpers/locomotive/base_helper.rb', line 13 def inputs_folded?(resource) resource.persisted? && resource.errors.empty? end |
#link_to_icon(name, *args, &block) ⇒ String
Like link_to but instead of passing a label, we pass the name of an Font Awesome icon. If the name is a Symbol, we append “icon-” to the dasherized version of the name.
27 28 29 30 31 |
# File 'app/helpers/locomotive/base_helper.rb', line 27 def link_to_icon(name, *args, &block) name = name.is_a?(Symbol) ? "icon-#{name.to_s.dasherize}" : name icon = content_tag(:i, '', class: name) link_to(icon, *args, &block).html_safe end |
#local_action_button(text, url, options = {}) ⇒ Object
64 65 66 67 68 69 70 71 72 73 |
# File 'app/helpers/locomotive/base_helper.rb', line 64 def (text, url, = {}) text = text.is_a?(Symbol) ? t(".#{text}") : text icon = .delete(:icon) || :exclamation_sign icon = icon.is_a?(Symbol) ? "icon-#{icon.to_s.dasherize}" : icon link_to(url, ) do content_tag(:i, '', class: icon) + text end end |
#locale_picker_link ⇒ Object
75 76 77 78 79 80 81 |
# File 'app/helpers/locomotive/base_helper.rb', line 75 def locale_picker_link if current_site.locales.size > 1 && localized? content_tag :div, render('locomotive/shared/locale_picker_link'), class: 'action' else nil end end |
#localize(object, options = nil) ⇒ Object Also known as: l
195 196 197 198 199 200 |
# File 'app/helpers/locomotive/base_helper.rb', line 195 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 |
#manage_domains? ⇒ Boolean
162 163 164 |
# File 'app/helpers/locomotive/base_helper.rb', line 162 def manage_domains? Locomotive.config.manage_domains? end |
#manage_subdomain? ⇒ Boolean
158 159 160 |
# File 'app/helpers/locomotive/base_helper.rb', line 158 def manage_subdomain? Locomotive.config.manage_subdomain? end |
#manage_subdomain_or_domains? ⇒ Boolean
154 155 156 |
# File 'app/helpers/locomotive/base_helper.rb', line 154 def manage_subdomain_or_domains? Locomotive.config.manage_subdomain? || Locomotive.config.manage_domains? end |
#multi_sites? ⇒ Boolean
166 167 168 |
# File 'app/helpers/locomotive/base_helper.rb', line 166 def multi_sites? Locomotive.config.multi_sites? end |
#nocoffee_tag ⇒ Object
142 143 144 |
# File 'app/helpers/locomotive/base_helper.rb', line 142 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.
191 192 193 |
# File 'app/helpers/locomotive/base_helper.rb', line 191 def not_the_default_current_locale? current_site.localized? && current_content_locale.to_s != current_site.default_locale.to_s end |
#options_for_membership_roles(options = {}) ⇒ Object
memberships
172 173 174 175 176 177 178 179 180 |
# File 'app/helpers/locomotive/base_helper.rb', line 172 def ( = {}) list = (unless [:skip_admin] Locomotive::Ability::ROLES.map { |r| [t("locomotive.memberships.roles.#{r}"), r] } else (Locomotive::Ability::ROLES - ['admin']).map { |r| [t("locomotive.memberships.roles.#{r}"), r] } end) (list) 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.
38 39 40 41 42 43 44 45 |
# File 'app/helpers/locomotive/base_helper.rb', line 38 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 |
#submenu_entry(name, url, options = {}, &block) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'app/helpers/locomotive/base_helper.rb', line 47 def (name, url, = {}, &block) = { i18n: true, css: name.dasherize.downcase } .merge!() css = "#{'on' if name == sections(:sub)} #{[:css]}" label_link = [:i18n] ? t("locomotive.shared.menu.#{name}") : name if block_given? popup = content_tag(:div, capture(&block), class: 'popup', style: 'display: none') text = content_tag(:span, preserve(label_link) + content_tag(:i, '', class: 'icon-caret-down')) link = link_to(text + content_tag(:em), url, class: css) content_tag(:li, link + popup, class: 'hoverable') else content_tag(:li, link_to(content_tag(:span, label_link), url, class: css)) end end |
#title(title = nil) ⇒ Object
4 5 6 7 8 9 10 11 |
# File 'app/helpers/locomotive/base_helper.rb', line 4 def title(title = nil) if title.nil? @content_for_title else @content_for_title = title '' end end |
#to_json(object) ⇒ String
Build the json version of a object. If the object owns a presenter then that presenter is used instead of the default object to_json method. Furthermore, if the presenter owns a as_json_for_html_view method, then it is called instead of the default as_json method. A html_safe is processed at the end.
117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'app/helpers/locomotive/base_helper.rb', line 117 def to_json(object) if object.respond_to?(:to_presenter) presenter = object.to_presenter if presenter.respond_to?(:as_json_for_html_view) presenter.as_json_for_html_view else presenter.as_json end.to_json else object.to_json end.html_safe end |