Module: SpreeCmCommissioner::OptionValueDecorator

Defined in:
app/models/spree_cm_commissioner/option_value_decorator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/models/spree_cm_commissioner/option_value_decorator.rb', line 3

def self.prepended(base)
  base.include SpreeCmCommissioner::OptionValueAttrType
  base.has_many :option_value_vehicle_types,
                class_name: 'SpreeCmCommissioner::OptionValueVehicleType',
                foreign_key: :option_value_id,
                dependent: :destroy

  base.validates :name, presence: true, uniqueness: { scope: :option_type_id, case_sensitive: -> { name_case_sensitive? } }

  # Ticket types: case-SENSITIVE matching (preserve external API casing)
  # Other types: case-INSENSITIVE storage (prevent duplicates from varying API casing; presentation keeps original)
  def base.find_or_create_by_name!(option_type, name)
    name = name.strip
    if option_type.ticket_type?
      where(option_type_id: option_type.id, name: name).first_or_create! do |ov|
        ov.option_type = option_type
        ov.name = name
        ov.presentation = name
      end
    else
      where(option_type_id: option_type.id).where('LOWER(name) = ?', name.downcase).first_or_create! do |ov|
        ov.option_type = option_type
        ov.name = name.downcase
        ov.presentation = name
      end
    end
  end
end

Instance Method Details

#display_iconObject



41
42
43
44
45
# File 'app/models/spree_cm_commissioner/option_value_decorator.rb', line 41

def display_icon
  return 'cm-default-icon.svg' unless icon&.end_with?(*SpreeCmCommissioner::VectorIcon::ACCEPTED_EXTENSIONS)

  icon
end

#name_case_sensitive?Boolean

Override Spree’s name validation to make it case-sensitive only for ticket-type option values. Spree’s default is case-insensitive (false), but we enforce case-sensitivity (true) for ticket-types. Which means ticket type “STANDARD” and “Standard” are different option values.

Returns:

  • (Boolean)


35
36
37
38
39
# File 'app/models/spree_cm_commissioner/option_value_decorator.rb', line 35

def name_case_sensitive?
  return true if ticket_type?

  false
end