class CustomAttributes::FieldType

Public Class Methods

available_types() click to toggle source
# File lib/custom_attributes/field_type.rb, line 20
def self.available_types
  descendants
end
find(name) click to toggle source
# File lib/custom_attributes/field_type.rb, line 24
def self.find(name)
  "CustomAttributes::#{name}FieldType".constantize.instance
rescue NameError
  nil
end

Public Instance Methods

after_save_custom_value(custom_field, custom_value) click to toggle source
# File lib/custom_attributes/field_type.rb, line 53
def after_save_custom_value(custom_field, custom_value); end
before_custom_field_save(custom_field) click to toggle source
# File lib/custom_attributes/field_type.rb, line 55
def before_custom_field_save(custom_field); end
cast_custom_value(custom_value) click to toggle source

Cast the value of an existing CustomValue

# File lib/custom_attributes/field_type.rb, line 70
def cast_custom_value(custom_value)
  cast_value(custom_value.custom_field, custom_value.value, custom_value.customizable)
end
cast_single_value(_custom_field, value, _customizable = nil) click to toggle source
# File lib/custom_attributes/field_type.rb, line 88
def cast_single_value(_custom_field, value, _customizable = nil)
  value.to_s
end
cast_value(custom_field, value, customizable = nil) click to toggle source

Cast the value according to field type rules

# File lib/custom_attributes/field_type.rb, line 75
def cast_value(custom_field, value, customizable = nil)
  if value.blank?
    nil
  elsif value.is_a?(Array)
    casted = value.map do |v|
      cast_single_value(custom_field, v, customizable)
    end
    casted.compact.sort
  else
    cast_single_value(custom_field, value, customizable)
  end
end
edit_tag(view, tag_id, tag_name, custom_value, options = {}) click to toggle source

Returns the HTML Tag to edit this field format.

# File lib/custom_attributes/field_type.rb, line 148
def edit_tag(view, tag_id, tag_name, custom_value, options = {})
  view.text_field_tag(tag_name, custom_value.value, options.merge(id: tag_id))
end
label() click to toggle source
# File lib/custom_attributes/field_type.rb, line 16
def label
  "label_#{name}"
end
name() click to toggle source
# File lib/custom_attributes/field_type.rb, line 12
def name
  self.class.type_name
end
possible_custom_value_options(custom_value) click to toggle source

Use to get the possible values of a CustomValue

# File lib/custom_attributes/field_type.rb, line 138
def possible_custom_value_options(custom_value)
  possible_values_options(custom_value.custom_field, custom_value.customizable)
end
possible_values_options(_custom_field, _object = nil) click to toggle source

Override this in subclass to specify the possible values for the field with this type

# File lib/custom_attributes/field_type.rb, line 143
def possible_values_options(_custom_field, _object = nil)
  []
end
set_custom_field_value(_custom_field, _custom_field_value, value) click to toggle source

Prepares and sets CustomFieldValues value.

# File lib/custom_attributes/field_type.rb, line 58
def set_custom_field_value(_custom_field, _custom_field_value, value)
  if value.is_a?(Array)
    value = value.map(&:to_s).reject { |v| v == '' }.uniq
    value << '' if value.empty?
  else
    value = value.to_s
  end

  value
end
validate_custom_field(_custom_field) click to toggle source

Overide this in subclass to validate custom fields

# File lib/custom_attributes/field_type.rb, line 49
def validate_custom_field(_custom_field)
  []
end
validate_custom_value(custom_value) click to toggle source

Validate a CustomValue according to the type roles. This method can be overridden by field types if the way bulk handling should happen changes (e.g. List type)

# File lib/custom_attributes/field_type.rb, line 33
def validate_custom_value(custom_value)
  # 1. Wrap in Array so we can pass Array of values and single values, reject empty values
  values = Array.wrap(custom_value.value).reject { |value| value.to_s == '' }
  # 2. Validate each value
  errors = values.map do |value|
    validate_single_value(custom_value.custom_field, value, custom_value.customizable)
  end
  errors.flatten.uniq
end
validate_single_value(_custom_field, _value, _customizable = nil) click to toggle source

Override this in subclass to validate custom values

# File lib/custom_attributes/field_type.rb, line 44
def validate_single_value(_custom_field, _value, _customizable = nil)
  []
end
value_from_keyword(custom_field, keyword, object) click to toggle source

Checks if a keyword is in the possible values and returns the possible value if found If there are no possible values, returns the keyword

# File lib/custom_attributes/field_type.rb, line 94
def value_from_keyword(custom_field, keyword, object)
  possible_values_options = possible_values_options(custom_field, object)
  if possible_values_options.present?
    parse_keyword(custom_field, keyword) do |k|
      if v = possible_values_options.detect { |text, _id| k.casecmp(text) == 0 }
        if v.is_a?(Array)
          v.last
        else
          v
        end
      end
    end
  else
    keyword
  end
end

Protected Instance Methods

parse_keyword(custom_field, keyword) { |strip| ... } click to toggle source

Parses keyword, allows comma delimited values

# File lib/custom_attributes/field_type.rb, line 112
def parse_keyword(custom_field, keyword)
  separator = Regexp.escape ','
  keyword = keyword.to_s

  if custom_field.multiple?
    values = []
    until keyword.empty?
      k = keyword.dup
      loop do
        if value = yield(k.strip)
          values << value
          break
        elsif k.slice!(/#{separator}([^#{separator}]*)\Z/).nil?
          break
        end
      end
      keyword.slice!(/\A#{Regexp.escape k}#{separator}?/)
    end
    values
  else
    yield keyword.strip
  end
end