class Para::Stall::VariantsPropertyConfig

Attributes

property[R]
resource[R]
variants[R]

Public Class Methods

new(resource, property, variants) click to toggle source
# File lib/para/stall/variants_property_config.rb, line 6
def initialize(resource, property, variants)
  @resource = resource
  @property = property
  @variants = variants
end

Public Instance Methods

active?() click to toggle source
# File lib/para/stall/variants_property_config.rb, line 23
def active?
  variants.any? do |variant|
    variant.variant_property_values.any? do |variant_property_value|
      variant_property_value.property_value.try(:property) == property
    end
  end
end
available_options() click to toggle source
# File lib/para/stall/variants_property_config.rb, line 37
def available_options
  @available_options ||= property.property_values.each_with_object([]) do |property_value, ary|
    if property_value_used?(property_value)
      ary << [property_value.value, property_value.id]
    end
  end
end
current_value() click to toggle source
# File lib/para/stall/variants_property_config.rb, line 31
def current_value
  @current_value ||= property.property_values.find do |property_value|
    property_value_used?(property_value)
  end
end
options() click to toggle source
# File lib/para/stall/variants_property_config.rb, line 12
def options
  property.property_values.map do |property_value|
    [
      property_value.value,
      property_value.id,
      selected: property_value_used?(property_value),
      data: { name: property_value.value }
    ]
  end
end
property_value_for(variant) click to toggle source
# File lib/para/stall/variants_property_config.rb, line 45
def property_value_for(variant)
  variant_property_value_for(variant).try(:property_value)
end
variant_property_value_for(variant) click to toggle source
# File lib/para/stall/variants_property_config.rb, line 49
def variant_property_value_for(variant)
  variant_property_value = variant.variant_property_values.find do |variant_property_value|
    variant_property_value.property_value.try(:property) == property
  end
end
variant_property_value_or_build_for(variant) click to toggle source
# File lib/para/stall/variants_property_config.rb, line 55
def variant_property_value_or_build_for(variant)
  variant_property_value_for(variant) || variant.variant_property_values.build
end

Private Instance Methods

property_value_used?(property_value) click to toggle source
# File lib/para/stall/variants_property_config.rb, line 69
def property_value_used?(property_value)
  variants.any? do |variant|
    variant.variant_property_values.any? do |variant_property_value|
      variant_property_value.property_value == property_value
    end
  end
end
reflection() click to toggle source
# File lib/para/stall/variants_property_config.rb, line 65
def reflection
  @reflection ||= variant_model.reflect_on_association(property.name)
end
variant_model() click to toggle source
# File lib/para/stall/variants_property_config.rb, line 61
def variant_model
  @variant_model ||= resource.class.reflect_on_association(relation).klass
end