class CustomAttributes::List

Public Instance Methods

edit_tag(view, tag_id, tag_name, custom_value, options = {}) click to toggle source
# File lib/custom_attributes/field_types/list.rb, line 5
def edit_tag(view, tag_id, tag_name, custom_value, options = {})
  if custom_value.custom_field.edit_tag_style == 'check_box'
    check_box_edit_tag(view, tag_id, tag_name, custom_value, options)
  else
    select_edit_tag(view, tag_id, tag_name, custom_value, options)
  end
end

Protected Instance Methods

check_box_edit_tag(view, _tag_id, tag_name, custom_value, options = {}) click to toggle source

Renders the edit tag as check box or radio tags

# File lib/custom_attributes/field_types/list.rb, line 36
def check_box_edit_tag(view, _tag_id, tag_name, custom_value, options = {})
  opts = []
  unless custom_value.custom_field.multiple? || custom_value.custom_field.is_required?
    opts << ["(#{::I18n.t('label_none')})", '']
  end
  opts += possible_custom_value_options(custom_value)
  s = ''.html_safe
  tag_method = custom_value.custom_field.multiple? ? :check_box_tag : :radio_button_tag
  opts.each do |label, value|
    value ||= label
    checked = (custom_value.value.is_a?(Array) && custom_value.value.include?(value)) || custom_value.value.to_s == value
    tag = view.send(tag_method, tag_name, value, checked, id: nil)
    s << view.content_tag('label', tag + ' ' + label)
  end
  if custom_value.custom_field.multiple?
    s << view.hidden_field_tag(tag_name, '', id: nil)
  end
  css = "#{options[:class]} check_box_group"
  view.content_tag('span', s, options.merge(class: css))
end
select_edit_tag(view, tag_id, tag_name, custom_value, options = {}) click to toggle source

Renders the edit tag as a select tag

# File lib/custom_attributes/field_types/list.rb, line 16
def select_edit_tag(view, tag_id, tag_name, custom_value, options = {})
  blank_option = ''.html_safe
  unless custom_value.custom_field.multiple?
    if custom_value.custom_field.is_required?
      unless custom_value.custom_field.default_value.present?
        blank_option = view.content_tag('option', "--- #{::I18n.t('actionview_instancetag_blank_option')} ---", value: '')
      end
    else
      blank_option = view.content_tag('option', '&nbsp;'.html_safe, value: '')
    end
  end
  options_tags = blank_option + view.options_for_select(possible_custom_value_options(custom_value), custom_value.value)
  s = view.select_tag(tag_name, options_tags, options.merge(id: tag_id, multiple: custom_value.custom_field.multiple?))
  if custom_value.custom_field.multiple?
    s << view.hidden_field_tag(tag_name, '')
  end
  s
end