testing this

Ruby code posted by foobar
created at 18 Oct 20:36, updated at 21 Oct 07:13

Edit | Back
1
2
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
ProductsHelper.module_eval do

  require 'json'

  # help w/ formatting the validation string
  # this method will likely be removed as we move everything to the client
  def custom_html_options(option)
    h = {:style=>"float: left;", :class=>"customization #{validation_classes(option)}"}
    va = validation_attributes(option)

    h.merge! va if va
    h
  end

  private

  def validation_attributes(option)

    return unless option.data_validation

    validation_hash=Hash.new

    data_validation = JSON.parse option.data_validation

    # Note that when you have a data_validation hash such as:
    # :data_validation=>{:type => :decimal, :min => :min_width, :max => :max_width, :required => true})
    # some the key syms match up with the jquery validation names e.g. min, max.  That's why we are able
    # to use these as-is as keys in the validation_hash below

    # min, max represent the current list of available jquery validation rules that can be specified inline
    [:min, :max].each do |m|

      next unless data_validation[m.to_s]

      # need to pull the value from the calculator via the preference name provided

      val = option.calculator.send("preferred_#{data_validation[m.to_s]}")
      validation_hash[m.to_s] = val.to_s if val

    end
    validation_hash
  end

  def validation_classes(option)
    return unless option.data_validation

    validation_str=[]

    data_validation = JSON.parse option.data_validation


    # handle data type first
    validation_str << case data_validation["type"]
                      when "string" then ""
                      when "integer" then "digits" # that's what jquery.validate uses
                      when "decimal" then "number" # that's what jquery.validate uses
                      else ""
                      end if data_validation["type"]

    validation_str << "required" if data_validation["required"]
    validation_str.join(" ")
  end

  def ad_hoc_option_value_options(ad_hoc_option_values)
    options = ad_hoc_option_values.map do |ah_ov|

      plus_or_minus=""

      if ah_ov.price_modifier>0
        plus_or_minus = t("add")
      elsif ah_ov.price_modifier<0
        plus_or_minus =t("subtract")
      end

      price_change_text = ah_ov.price_modifier == 0 ? "" : " (#{plus_or_minus} #{format_price ah_ov.price_modifier.abs})"
      [(ah_ov.price_modifier.nil? ? ah_ov.option_value.presentation : "#{ah_ov.option_value.presentation}#{price_change_text}"), ah_ov.id.to_s]
    end
  end

  def calculator_name(product_customization_type)
    product_customization_type.calculator.class.name.demodulize.underscore rescue ""
  end
end
2.66 KB in 5 ms with coderay