Module: Spree::Admin::BaseHelper
- Defined in:
- app/helpers/spree/admin/base_helper.rb
Instance Method Summary collapse
-
#allowed_file_types_for_upload ⇒ Array<String>
returns the allowed file types for upload, according to the active storage configuration.
-
#available_countries ⇒ Array<Spree::Country>
The available countries for checkout.
-
#available_countries_iso ⇒ Array<String>
The available countries for checkout.
-
#clipboard_button(options = {}) ⇒ String
renders a clipboard button.
-
#clipboard_component(text, options = {}) ⇒ String
renders a clipboard component.
-
#display_on_options(model = nil) ⇒ Array<Array<String, String>>
returns the available display on options, eg backend, frontend, both.
- #enterprise_edition? ⇒ Boolean
-
#error_message_on(object, method, _options = {}) ⇒ String
render an error message for a form field.
-
#flag_emoji(iso) ⇒ String
returns the flag emoji for a country.
-
#format_preference_value(key, value) ⇒ String
Formats a preference value for display Handles special cases like zone_ids, user_ids, country_id that should show names instead of IDs.
-
#icon(icon_name, options = {}) ⇒ String
render an icon, using the tabler icons library.
- #preference_field(object, form, key, i18n_scope: '') ⇒ Object
-
#preference_field_for(form, field, options) ⇒ String
render a form field for a preference, according to the type of the preference (number, decimal, boolean, string, password, text) see https://spreecommerce.org/docs/developer/customization/model-preferences.
-
#preference_field_options(options) ⇒ Hash
returns the options for a preference field, according to the type of the preference (number, decimal, boolean, string, password, text, datetime).
-
#preference_fields(object, form, i18n_scope: '') ⇒ String
renders all the preference fields for an object.
-
#progress_bar_component(value, options = {}) ⇒ String
renders a progress bar component.
- #render_admin_partials(section, options = {}) ⇒ Object
-
#required_span_tag ⇒ String
renders a red dot with a * to indicate that a field is required.
-
#settings_area? ⇒ Boolean
check if the current controller is a settings controller this is used to display different sidebar navigation for settings pages.
- #show_spree_updater_notice? ⇒ Boolean
-
#spree_date(date, options = {}) ⇒ String
returns the local date for a given date.
-
#spree_time(time, options = {}) ⇒ String
returns the local time for a given time.
-
#spree_time_ago(time, options = {}) ⇒ String
returns the local time ago for a given time.
- #spree_update_available? ⇒ Boolean
-
#spree_updater ⇒ Spree::Admin::Updater
The spree updater.
- #tooltip(text = nil, &block) ⇒ Object
- #updater_notice_dismissed? ⇒ Boolean
Instance Method Details
#allowed_file_types_for_upload ⇒ Array<String>
returns the allowed file types for upload, according to the active storage configuration
305 306 307 |
# File 'app/helpers/spree/admin/base_helper.rb', line 305 def allowed_file_types_for_upload Rails.application.config.active_storage.web_image_content_types end |
#available_countries ⇒ Array<Spree::Country>
Returns the available countries for checkout.
45 46 47 |
# File 'app/helpers/spree/admin/base_helper.rb', line 45 def available_countries @available_countries ||= available_countries_iso.map { |iso| Spree::Country.new(iso: iso) } end |
#available_countries_iso ⇒ Array<String>
Returns the available countries for checkout.
40 41 42 |
# File 'app/helpers/spree/admin/base_helper.rb', line 40 def available_countries_iso @available_countries_iso ||= current_store.countries_available_for_checkout.pluck(:iso) end |
#clipboard_button(options = {}) ⇒ String
renders a clipboard button
247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'app/helpers/spree/admin/base_helper.rb', line 247 def ( = {}) [:class] ||= 'btn btn-light btn-sm btn-clipboard' [:icon_class] ||= 'mr-0 text-sm' [:type] ||= 'button' [:data] ||= {} [:data][:action] = 'clipboard#copy' [:data][:clipboard_target] = 'button' [:data][:controller] = 'tooltip' [:aria_label] ||= Spree.t('admin.copy_to_clipboard') # screen-reader label content_tag(:button, ) do icon('copy', class: [:icon_class]) + tooltip(Spree.t('admin.copy_to_clipboard')) end end |
#clipboard_component(text, options = {}) ⇒ String
renders a clipboard component
271 272 273 274 275 276 277 278 279 280 |
# File 'app/helpers/spree/admin/base_helper.rb', line 271 def clipboard_component(text, = {}) [:data] ||= {} [:data][:controller] = 'clipboard' [:data][:clipboard_success_content_value] ||= raw(icon('check', class: [:icon_class])) content_tag(:span, data: [:data], class: [:class]) do hidden_field_tag(:clipboard_source, text, data: { clipboard_target: 'source' }) + (class: [:button_class], icon_class: [:icon_class]) end end |
#display_on_options(model = nil) ⇒ Array<Array<String, String>>
returns the available display on options, eg backend, frontend, both
51 52 53 54 55 56 57 |
# File 'app/helpers/spree/admin/base_helper.rb', line 51 def (model = nil) model ||= Spree::DisplayOn model::DISPLAY.map do |display_on| [Spree.t("admin.display_on_options.#{display_on}"), display_on] end end |
#enterprise_edition? ⇒ Boolean
10 11 12 |
# File 'app/helpers/spree/admin/base_helper.rb', line 10 def enterprise_edition? defined?(SpreeEnterprise) end |
#error_message_on(object, method, _options = {}) ⇒ String
render an error message for a form field
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'app/helpers/spree/admin/base_helper.rb', line 64 def (object, method, = {}) object = convert_to_model(object) obj = if object.respond_to?(:errors) object else # Handle nested attributes like "variant[prices_attributes][0]" # by extracting the base object name object_name = object.to_s.split('[').first instance_variable_get("@#{object_name}") if object_name.present? end if obj && obj.errors[method].present? errors = safe_join(obj.errors[method], '<br />'.html_safe) content_tag(:span, errors, class: 'formError') else '' end end |
#flag_emoji(iso) ⇒ String
returns the flag emoji for a country
111 112 113 |
# File 'app/helpers/spree/admin/base_helper.rb', line 111 def flag_emoji(iso) ::Country.new(iso).emoji_flag end |
#format_preference_value(key, value) ⇒ String
Formats a preference value for display Handles special cases like zone_ids, user_ids, country_id that should show names instead of IDs
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 |
# File 'app/helpers/spree/admin/base_helper.rb', line 354 def format_preference_value(key, value) return Spree.t(:unlimited) if value.blank? case key when :zone_ids zone_ids = Array(value).reject(&:blank?).map(&:to_i) return Spree.t(:none) if zone_ids.empty? Spree::Zone.where(id: zone_ids).pluck(:name).join(', ') when :market_ids market_ids = Array(value).reject(&:blank?).map(&:to_i) return Spree.t(:none) if market_ids.empty? Spree::Market.where(id: market_ids).pluck(:name).join(', ') when :channel_ids channel_ids = Array(value).reject(&:blank?).map(&:to_i) return Spree.t(:none) if channel_ids.empty? Spree::Channel.where(id: channel_ids).pluck(:name).join(', ') when :user_ids user_ids = Array(value).reject(&:blank?).map(&:to_i) return Spree.t(:none) if user_ids.empty? Spree.user_class.where(id: user_ids).map { |u| u.try(:email) || u.id }.join(', ') when :customer_group_ids customer_group_ids = Array(value).reject(&:blank?).map(&:to_i) return Spree.t(:none) if customer_group_ids.empty? Spree::CustomerGroup.where(id: customer_group_ids).pluck(:name).join(', ') when :country_id Spree::Country.find_by(id: value)&.name || value else value.is_a?(Array) ? value.reject(&:blank?).join(', ') : value end end |
#icon(icon_name, options = {}) ⇒ String
render an icon, using the tabler icons library
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'app/helpers/spree/admin/base_helper.rb', line 88 def icon(icon_name, = {}) if icon_name.ends_with?('.svg') icon_name = File.basename(icon_name, File.extname(icon_name)) end # translations for legacy icon names icon_name = 'device-floppy' if icon_name == 'save' icon_name = 'pencil' if icon_name == 'edit' icon_name = 'trash' if icon_name == 'delete' icon_name = 'plus' if icon_name == 'add' icon_name = 'x' if icon_name == 'cancel' [:style] ||= '' [:style] += ";font-size: #{[:height]}px !important;line-height:#{[:height]}px !important" if [:height] [:class] = "ti ti-#{icon_name} #{[:class]}" content_tag :i, nil, end |
#preference_field(object, form, key, i18n_scope: '') ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'app/helpers/spree/admin/base_helper.rb', line 208 def preference_field(object, form, key, i18n_scope: '') return unless object.has_preference?(key) case key when :currency content_tag(:div, form.label("preferred_#{key}", Spree.t(key, scope: i18n_scope)) + form.select("preferred_#{key}", current_store.supported_currencies_list, {}, { data: { controller: 'autocomplete-select' } }), class: 'form-group', id: [object.class.to_s.parameterize, 'preference', key].join('-')) else if object.preference_type(key).to_sym == :boolean content_tag(:div, class: 'form-group custom-control form-checkbox') do preference_field_for(form, "preferred_#{key}", type: object.preference_type(key)) + form.label( "preferred_#{key}", Spree.t(key, scope: i18n_scope), class: 'custom-control-label', id: [object.class.to_s.parameterize, 'preference', key].join('-') ) end else content_tag(:div, form.label("preferred_#{key}", Spree.t(key, scope: i18n_scope)) + preference_field_for(form, "preferred_#{key}", type: object.preference_type(key)), class: 'form-group', id: [object.class.to_s.parameterize, 'preference', key].join('-')) end end end |
#preference_field_for(form, field, options) ⇒ String
render a form field for a preference, according to the type of the preference (number, decimal, boolean, string, password, text) see https://spreecommerce.org/docs/developer/customization/model-preferences
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'app/helpers/spree/admin/base_helper.rb', line 121 def preference_field_for(form, field, ) case [:type] when :integer form.number_field(field, ()) when :decimal form.number_field(field, ()) when :boolean form.check_box(field, ()) when :string form.text_field(field, ()) when :password render 'spree/admin/preferences/password_field', form: form, field: field, options: when :text form.text_area(field, ()) when :datetime form.datetime_field(field, ()) else form.text_field(field, ()) end end |
#preference_field_options(options) ⇒ Hash
returns the options for a preference field, according to the type of the preference (number, decimal, boolean, string, password, text, datetime)
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'app/helpers/spree/admin/base_helper.rb', line 147 def () = case [:type] when :integer { size: 10, class: 'input_integer form-input' } when :decimal { size: 10, class: 'input_decimal form-input', step: [:step] || 0.01 } when :boolean { class: 'custom-control-input' } when :string { size: 10, class: 'input_string form-input' } when :password { size: 10, class: 'password_string form-input' } when :text { rows: 15, cols: 85, class: 'form-input' } when :datetime { class: 'form-input' } else { size: 10, class: 'input_string form-input' } end .merge!(readonly: [:readonly], disabled: [:disabled], size: [:size]) end |
#preference_fields(object, form, i18n_scope: '') ⇒ String
renders all the preference fields for an object
201 202 203 204 205 206 |
# File 'app/helpers/spree/admin/base_helper.rb', line 201 def preference_fields(object, form, i18n_scope: '') return unless object.respond_to?(:preferences) fields = object.preferences.keys.map { |key| preference_field(object, form, key, i18n_scope: i18n_scope) } safe_join(fields) end |
#progress_bar_component(value, options = {}) ⇒ String
renders a progress bar component
288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'app/helpers/spree/admin/base_helper.rb', line 288 def (value, = {}) min = [:min] || 0 max = [:max] || 100 percentage = (value.to_f / max * 100).round css_class = [:class] || 'progress' content_tag(:div, class: css_class) do content_tag(:div, { class: 'progress-bar', role: 'progressbar', style: "width: #{percentage}%", aria: { valuenow: value, valuemin: min, valuemax: max } }) do end end end |
#render_admin_partials(section, options = {}) ⇒ Object
4 5 6 7 8 |
# File 'app/helpers/spree/admin/base_helper.rb', line 4 def render_admin_partials(section, = {}) Spree.admin.partials.send(section.to_s.gsub('_partials', '').to_sym).map do |partial| render partial, end.join.html_safe end |
#required_span_tag ⇒ String
renders a red dot with a * to indicate that a field is required
237 238 239 |
# File 'app/helpers/spree/admin/base_helper.rb', line 237 def required_span_tag content_tag(:span, ' *', class: 'required font-weight-bold text-danger') end |
#settings_area? ⇒ Boolean
check if the current controller is a settings controller this is used to display different sidebar navigation for settings pages
35 36 37 |
# File 'app/helpers/spree/admin/base_helper.rb', line 35 def settings_area? @settings_area.present? end |
#show_spree_updater_notice? ⇒ Boolean
28 29 30 |
# File 'app/helpers/spree/admin/base_helper.rb', line 28 def show_spree_updater_notice? Spree::Admin::RuntimeConfig.admin_updater_enabled && can?(:manage, current_store) && spree_update_available? && !updater_notice_dismissed? end |
#spree_date(date, options = {}) ⇒ String
returns the local date for a given date
312 313 314 |
# File 'app/helpers/spree/admin/base_helper.rb', line 312 def spree_date(date, = {}) local_date(date, ) end |
#spree_time(time, options = {}) ⇒ String
returns the local time for a given time
319 320 321 |
# File 'app/helpers/spree/admin/base_helper.rb', line 319 def spree_time(time, = {}) local_time(time, ) end |
#spree_time_ago(time, options = {}) ⇒ String
returns the local time ago for a given time
326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'app/helpers/spree/admin/base_helper.rb', line 326 def spree_time_ago(time, = {}) return '' if time.blank? [:data] ||= {} [:data][:controller] = 'tooltip' # Generate the time ago element with tooltip content_tag(:span, ) do tooltip_text = spree_time(time) local_time_ago(time, class: '', title: nil) + tooltip(tooltip_text) end end |
#spree_update_available? ⇒ Boolean
19 20 21 |
# File 'app/helpers/spree/admin/base_helper.rb', line 19 def spree_update_available? @spree_update_available ||= !Rails.env.test? && spree_updater.update_available? end |
#spree_updater ⇒ Spree::Admin::Updater
Returns the spree updater.
15 16 17 |
# File 'app/helpers/spree/admin/base_helper.rb', line 15 def spree_updater @spree_updater ||= Spree::Admin::Updater end |
#tooltip(text = nil, &block) ⇒ Object
339 340 341 342 343 344 345 346 347 |
# File 'app/helpers/spree/admin/base_helper.rb', line 339 def tooltip(text = nil, &block) content_tag(:span, role: 'tooltip', data: { tooltip_target: 'tooltip' }, class: 'tooltip-container') do if block_given? capture(&block) else text end end end |
#updater_notice_dismissed? ⇒ Boolean
23 24 25 26 |
# File 'app/helpers/spree/admin/base_helper.rb', line 23 def updater_notice_dismissed? dismissal_data = session[:spree_updater_notice_dismissed] dismissal_data.is_a?(Hash) && dismissal_data['expires_at'].to_time > Time.current end |