class Para::Stall::Inputs::VariantSelectInput

Public Instance Methods

input(wrapper_options = nil) click to toggle source
# File lib/para/stall/inputs/variant_select_input.rb, line 7
def input(wrapper_options = nil)
  ensure_target_relation_present!

  template.render partial: partial_path, locals: {
    form: @builder,
    model: model,
    attribute_name: attribute_name,
    foreign_key: foreign_key,
    properties: properties,
    variants_data: variants_data,
    price_selector: price_selector,
    variant_label_class: variant_label_class,
    variant_property_class: variant_property_class
  }
end

Private Instance Methods

foreign_key() click to toggle source
# File lib/para/stall/inputs/variant_select_input.rb, line 64
def foreign_key
  @foreign_key ||= model.reflect_on_association(attribute_name).foreign_key
end
partial_path() click to toggle source
# File lib/para/stall/inputs/variant_select_input.rb, line 60
def partial_path
  options.fetch(:partial_path, 'para/stall/inputs/variant_select')
end
price_selector() click to toggle source
# File lib/para/stall/inputs/variant_select_input.rb, line 25
def price_selector
  @price_selector ||= options[:price_selector] || '[data-sellable-price]'
end
product() click to toggle source
# File lib/para/stall/inputs/variant_select_input.rb, line 29
def product
  @product ||= options[:product]
end
properties() click to toggle source

Iterate on every variant to fetch available properties and values and build a VariantsPropertyConfig for each available one

# File lib/para/stall/inputs/variant_select_input.rb, line 40
def properties
  @properties ||= variants.each_with_object({}) do |variant, hash|
    variant.variant_property_values.each do |variant_property_value|
      property = variant_property_value.property_value.property

      unless hash[property]
        hash[property] = VariantsPropertyConfig.new(
          product, property, variants
        )
      end
    end
  end.values
end
relation() click to toggle source
# File lib/para/stall/inputs/variant_select_input.rb, line 33
def relation
  @relation ||= options.fetch(:relation, :variants)
end
variant_label_class() click to toggle source
# File lib/para/stall/inputs/variant_select_input.rb, line 79
def variant_label_class
  options.fetch(:variant_label_class, 'col-md-3')
end
variant_property_class() click to toggle source
# File lib/para/stall/inputs/variant_select_input.rb, line 83
def variant_property_class
  options.fetch(:variant_property_class, 'col-md-9')
end
variants() click to toggle source
# File lib/para/stall/inputs/variant_select_input.rb, line 54
def variants
  @variants ||= options.fetch(:variants) do
    product.variants.select(&:published?)
  end
end
variants_data() click to toggle source
# File lib/para/stall/inputs/variant_select_input.rb, line 68
def variants_data
  @variants_data ||= variants.map do |variant|
    { id: variant.id, price: template.number_to_currency(variant.price) }.tap do |data|
      properties.each do |property_config|
        data[property_config.property.id] =
          property_config.property_value_for(variant).id
      end
    end
  end
end