Class: Spree::SizeChart

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/spree/size_chart.rb

Constant Summary collapse

UNITS =
['cm', 'inch']

Instance Method Summary collapse

Instance Method Details

#find_or_initialize_size_value(opt_value_id, size_type_id) ⇒ Object



50
51
52
# File 'app/models/spree/size_chart.rb', line 50

def find_or_initialize_size_value(opt_value_id, size_type_id)
  (hash_size_values[opt_value_id] and hash_size_values[opt_value_id][size_type_id]) || size_values.build(:size_type_id => size_type_id, :option_value_id => opt_value_id)
end

#find_or_initialize_size_valuesObject



39
40
41
42
43
44
45
46
47
48
# File 'app/models/spree/size_chart.rb', line 39

def find_or_initialize_size_values
  @size_values = []
  option_value_ids.each do |opt_value_id|
    size_type_ids.each do |size_type_id|
      @size_values << find_or_initialize_size_value(opt_value_id, size_type_id)
    end
  end

  @size_values
end

#find_size_valuesObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/models/spree/size_chart.rb', line 25

def find_size_values
  @size_values = []
  option_value_ids.each do |opt_value_id|
    size_type_ids.each do |size_type_id|
      if _size_value = find_or_initialize_size_value(opt_value_id, size_type_id) and !_size_value.new_record?
        @size_values << _size_value
      end
    end
  end

  @size_values
end

#hash_size_valuesObject



54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/models/spree/size_chart.rb', line 54

def hash_size_values
  return @hsize_values if @hsize_values
  @hsize_values = {}
  size_values(true).each do |size_val|
    if @hsize_values[size_val.option_value_id]
      @hsize_values[size_val.option_value_id].merge!(size_val.size_type_id => size_val)
    else
      @hsize_values[size_val.option_value_id] = { size_val.size_type_id => size_val }
    end
  end
  @hsize_values
end

#option_type_with_unitObject



67
68
69
# File 'app/models/spree/size_chart.rb', line 67

def option_type_with_unit
  "#{option_type.try(:presentation)} (#{unit})"
end

#size_values_attributes_with_sanity_check=(attributes) ⇒ Object



14
15
16
17
18
19
20
21
# File 'app/models/spree/size_chart.rb', line 14

def size_values_attributes_with_sanity_check=(attributes)
  attributes.each_value do |attrs|
    if attrs['value'].blank?
      attrs.merge!('_destroy' => true)
    end
  end
  self.size_values_attributes_without_sanity_check = attributes
end