Class: Para::Stall::VariantsPropertyConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/para/stall/variants_property_config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, property, variants) ⇒ VariantsPropertyConfig

Returns a new instance of VariantsPropertyConfig.



6
7
8
9
10
# File 'lib/para/stall/variants_property_config.rb', line 6

def initialize(resource, property, variants)
  @resource = resource
  @property = property
  @variants = variants
end

Instance Attribute Details

#propertyObject (readonly)

Returns the value of attribute property.



4
5
6
# File 'lib/para/stall/variants_property_config.rb', line 4

def property
  @property
end

#resourceObject (readonly)

Returns the value of attribute resource.



4
5
6
# File 'lib/para/stall/variants_property_config.rb', line 4

def resource
  @resource
end

#variantsObject (readonly)

Returns the value of attribute variants.



4
5
6
# File 'lib/para/stall/variants_property_config.rb', line 4

def variants
  @variants
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
# 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_optionsObject



37
38
39
40
41
42
43
# 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_valueObject



31
32
33
34
35
# 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

#optionsObject



12
13
14
15
16
17
18
19
20
21
# 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) ⇒ Object



45
46
47
# 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) ⇒ Object



49
50
51
52
53
# 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) ⇒ Object



55
56
57
# 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