Module: Spree::Admin::BaseHelper

Defined in:
app/helpers/spree/admin/base_helper.rb

Instance Method Summary collapse

Instance Method Details

#allowed_file_types_for_uploadArray<String>

returns the allowed file types for upload, according to the active storage configuration

Returns:

  • (Array<String>)

    the allowed file types for upload, eg. [‘image/png’, ‘image/jpeg’, ‘image/gif’, ‘image/webp’]



300
301
302
# File 'app/helpers/spree/admin/base_helper.rb', line 300

def allowed_file_types_for_upload
  Rails.application.config.active_storage.web_image_content_types
end

#available_countries_isoArray<String>

Returns the available countries for checkout.

Returns:

  • (Array<String>)

    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

Parameters:

  • options (Hash) (defaults to: {})

    the options for the button

Options Hash (options):

  • :class (String)

    the CSS class(es) of the button

  • :data (Hash)

    the data attributes for the button

  • :title (String)

    the title of the button

Returns:

  • (String)

    the button



242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'app/helpers/spree/admin/base_helper.rb', line 242

def clipboard_button(options = {})
  options[:class] ||= 'btn btn-light btn-sm btn-clipboard'
  options[:icon_class] ||= 'mr-0 text-sm'
  options[:type] ||= 'button'
  options[:data] ||= {}
  options[:data][:action] = 'clipboard#copy'
  options[:data][:clipboard_target] = 'button'
  options[:data][:controller] = 'tooltip'
  options[:aria_label] ||= Spree.t('admin.copy_to_clipboard') # screen-reader label

  (:button, options) do
    icon('copy', class: options[:icon_class]) + tooltip(Spree.t('admin.copy_to_clipboard'))
  end
end

#clipboard_component(text, options = {}) ⇒ String

renders a clipboard component

Parameters:

  • text (String)

    the text to copy

  • options (Hash) (defaults to: {})

    the options for the component

Options Hash (options):

  • :class (String)

    the CSS class(es) of the component

  • :data (Hash)

    the data attributes for the component

  • :title (String)

    the title of the component

  • :button_class (String)

    the CSS class(es) of the button

  • :icon_class (String)

    the CSS class(es) of the icon

Returns:

  • (String)

    the component



266
267
268
269
270
271
272
273
274
275
# File 'app/helpers/spree/admin/base_helper.rb', line 266

def clipboard_component(text, options = {})
  options[:data] ||= {}
  options[:data][:controller] = 'clipboard'
  options[:data][:clipboard_success_content_value] ||= raw(icon('check', class: options[:icon_class]))

  (:span, data: options[:data], class: options[:class]) do
    hidden_field_tag(:clipboard_source, text, data: { clipboard_target: 'source' }) +
      clipboard_button(class: options[:button_class], icon_class: options[:icon_class])
  end
end

#display_on_options(model = nil) ⇒ Array<Array<String, String>>

returns the available display on options, eg backend, frontend, both

Returns:

  • (Array<Array<String, String>>)

    the available display on options



46
47
48
49
50
51
52
# File 'app/helpers/spree/admin/base_helper.rb', line 46

def display_on_options(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

Returns:

  • (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

Parameters:

  • object (Spree::Model)

    the object to render the error message for

  • method (String)

    the method to render the error message for

  • options (Hash)

    the options for the error message

Returns:

  • (String)

    the error message



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/helpers/spree/admin/base_helper.rb', line 59

def error_message_on(object, method, _options = {})
  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)
    (:span, errors, class: 'formError')
  else
    ''
  end
end

#flag_emoji(iso) ⇒ String

returns the flag emoji for a country

Parameters:

  • iso (String)

    the ISO code of the country

Returns:

  • (String)

    the flag emoji



106
107
108
# File 'app/helpers/spree/admin/base_helper.rb', line 106

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

Parameters:

  • key (Symbol)

    the preference key

  • value (Object)

    the preference value

Returns:

  • (String)

    the formatted value



349
350
351
352
353
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
# File 'app/helpers/spree/admin/base_helper.rb', line 349

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 :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

Parameters:

  • icon_name (String)

    the name of the icon, eg: ‘pencil’, see: tabler.io/icons

  • options (Hash) (defaults to: {})

    the options for the icon

Returns:

  • (String)

    the icon



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/helpers/spree/admin/base_helper.rb', line 83

def icon(icon_name, options = {})
  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'

  options[:style] ||= ''
  options[:style] += ";font-size: #{options[:height]}px !important;line-height:#{options[:height]}px !important" if options[:height]

  options[:class] = "ti ti-#{icon_name} #{options[:class]}"

   :i, nil, options
end

#preference_field(object, form, key, i18n_scope: '') ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'app/helpers/spree/admin/base_helper.rb', line 203

def preference_field(object, form, key, i18n_scope: '')
  return unless object.has_preference?(key)

  case key
  when :currency
    (: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
      (: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
      (: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 spreecommerce.org/docs/developer/customization/model-preferences

Parameters:

  • form (ActionView::Helpers::FormBuilder)

    the form builder

  • field (String)

    the name of the field

  • options (Hash)

    the options for the field

Returns:

  • (String)

    the preference field



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/helpers/spree/admin/base_helper.rb', line 116

def preference_field_for(form, field, options)
  case options[:type]
  when :integer
    form.number_field(field, preference_field_options(options))
  when :decimal
    form.number_field(field, preference_field_options(options))
  when :boolean
    form.check_box(field, preference_field_options(options))
  when :string
    form.text_field(field, preference_field_options(options))
  when :password
    render 'spree/admin/preferences/password_field', form: form, field: field, options: options
  when :text
    form.text_area(field, preference_field_options(options))
  when :datetime
    form.datetime_field(field, preference_field_options(options))
  else
    form.text_field(field, preference_field_options(options))
  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)

Parameters:

  • options (Hash)

    the options for the field

Options Hash (options):

  • :type (Symbol)

    the type of the preference, eg. :integer, :decimal, :boolean, :string, :password, :text, :datetime

  • :disabled (Boolean)

    whether the field is disabled

Returns:

  • (Hash)

    the options for the field



142
143
144
145
146
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
# File 'app/helpers/spree/admin/base_helper.rb', line 142

def preference_field_options(options)
  field_options = case options[:type]
                  when :integer
                    {
                      size: 10,
                      class: 'input_integer form-input'
                    }
                  when :decimal
                    {
                      size: 10,
                      class: 'input_decimal form-input',
                      step: options[: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

  field_options.merge!(readonly: options[:readonly],
                       disabled: options[:disabled],
                       size: options[:size])
end

#preference_fields(object, form, i18n_scope: '') ⇒ String

renders all the preference fields for an object

Parameters:

  • object (Spree::TaxRate, Spree::Calculator, Spree::PaymentMethod, Spree::ShippingMethod, Spree::Store)

    the object to render the preference fields for

  • form (ActionView::Helpers::FormBuilder)

    the form builder

  • i18n_scope (String) (defaults to: '')

    the i18n scope for the preference fields

Returns:

  • (String)

    the preference fields



196
197
198
199
200
201
# File 'app/helpers/spree/admin/base_helper.rb', line 196

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

Parameters:

  • options (Hash) (defaults to: {})

    the options for the component

  • value (Integer)

    the value of the progress bar

Options Hash (options):

  • :min (Integer)

    the minimum value of the progress bar

  • :max (Integer)

    the maximum value of the progress bar

Returns:

  • (String)

    the component



283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'app/helpers/spree/admin/base_helper.rb', line 283

def progress_bar_component(value, options = {})
  min = options[:min] || 0
  max = options[:max] || 100
  percentage = (value.to_f / max * 100).round

  css_class = options[:class] || 'progress'

  (:div, class: css_class) do
    (: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, options = {})
  Spree.admin.partials.send(section.to_s.gsub('_partials', '').to_sym).map do |partial|
    render partial, options
  end.join.html_safe
end

#required_span_tagString

renders a red dot with a * to indicate that a field is required

Returns:

  • (String)

    the required span tag



232
233
234
# File 'app/helpers/spree/admin/base_helper.rb', line 232

def required_span_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

Returns:

  • (Boolean)


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

Returns:

  • (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

Parameters:

  • date (Date)

    the date to format

Returns:

  • (String)

    the local date



307
308
309
# File 'app/helpers/spree/admin/base_helper.rb', line 307

def spree_date(date, options = {})
  local_date(date, options)
end

#spree_time(time, options = {}) ⇒ String

returns the local time for a given time

Parameters:

  • time (Time)

    the time to format

Returns:

  • (String)

    the local time



314
315
316
# File 'app/helpers/spree/admin/base_helper.rb', line 314

def spree_time(time, options = {})
  local_time(time, options)
end

#spree_time_ago(time, options = {}) ⇒ String

returns the local time ago for a given time

Parameters:

  • time (Time)

    the time to format

Returns:

  • (String)

    the local time ago



321
322
323
324
325
326
327
328
329
330
331
332
# File 'app/helpers/spree/admin/base_helper.rb', line 321

def spree_time_ago(time, options = {})
  return '' if time.blank?

  options[:data] ||= {}
  options[:data][:controller] = 'tooltip'

  # Generate the time ago element with tooltip
  (:span, options) do
    tooltip_text = spree_time(time)
    local_time_ago(time, class: '', title: nil) + tooltip(tooltip_text)
  end
end

#spree_update_available?Boolean

Returns:

  • (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_updaterSpree::Admin::Updater

Returns the spree updater.

Returns:



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



334
335
336
337
338
339
340
341
342
# File 'app/helpers/spree/admin/base_helper.rb', line 334

def tooltip(text = nil, &block)
  (: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

Returns:

  • (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