Class: Storefront::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/storefront/models/attribute.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Attribute

Returns a new instance of Attribute.



5
6
7
8
9
10
11
# File 'lib/storefront/models/attribute.rb', line 5

def initialize(options = {})
  @name     = options[:name]
  @model    = options[:model]
  @required = options[:required].nil? ? model.required?(name) : options[:required]
  @disabled = options[:disabled]
  @top_level = options[:top_level]
end

Instance Attribute Details

#disabledObject (readonly)

Returns the value of attribute disabled.



3
4
5
# File 'lib/storefront/models/attribute.rb', line 3

def disabled
  @disabled
end

#modelObject (readonly)

Returns the value of attribute model.



3
4
5
# File 'lib/storefront/models/attribute.rb', line 3

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/storefront/models/attribute.rb', line 3

def name
  @name
end

#requiredObject (readonly)

Returns the value of attribute required.



3
4
5
# File 'lib/storefront/models/attribute.rb', line 3

def required
  @required
end

#top_levelObject (readonly)

Returns the value of attribute top_level.



3
4
5
# File 'lib/storefront/models/attribute.rb', line 3

def top_level
  @top_level
end

Instance Method Details

#access_keyObject



43
44
45
# File 'lib/storefront/models/attribute.rb', line 43

def access_key
  name.to_s[0, 1]
end

#configObject



13
14
15
# File 'lib/storefront/models/attribute.rb', line 13

def config
  Storefront.configuration
end

#disabled?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/storefront/models/attribute.rb', line 21

def disabled?
  @disabled
end

#errors?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/storefront/models/attribute.rb', line 29

def errors?
  top_level ? false : model.errors_on?(name, {})
end

#humanizeObject



25
26
27
# File 'lib/storefront/models/attribute.rb', line 25

def humanize
  model.humanized_attribute_name(name)
end

#input_type(options = {}) ⇒ Object



73
74
75
# File 'lib/storefront/models/attribute.rb', line 73

def input_type(options = {})
  model.default_input_type(name, options)
end

#param_for(*parts) ⇒ Object



47
48
49
50
# File 'lib/storefront/models/attribute.rb', line 47

def param_for(*parts)
  parts = parts.flatten.compact
  parts[0].to_s + parts[1..-1].map {|i| "[#{i}]"}.join("")
end

#required?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/storefront/models/attribute.rb', line 17

def required?
  @required
end

#to_id(options = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/storefront/models/attribute.rb', line 52

def to_id(options = {})
  parts = []
  parts << model.keys unless top_level
  parts << options[:parent_index].to_s if options[:parent_index].present?
  parts << name
  parts << options[:index].to_s if options[:index].present?
  parts << (options[:type] || "input").to_s
  
  parts.join(config.separator).underscore.strip.gsub(/[_\s]+/, config.separator)
end

#to_labelObject



77
78
79
# File 'lib/storefront/models/attribute.rb', line 77

def to_label
  humanize
end

#to_param(options = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/storefront/models/attribute.rb', line 63

def to_param(options = {})
  parts = []
  parts << model.keys unless top_level
  parts << options[:parent_index].to_s if options[:parent_index].present?
  parts << name
  parts << options[:index].to_s if options[:index].present?
  
  param_for(parts)
end

#validationsObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/storefront/models/attribute.rb', line 81

def validations
  return {} if top_level
  
  unless @validations.present?
    return {} unless model.validators?
    
    attr_name = humanize
    
    kinds = []
    
    @validations = model.validators_on(name).inject({}) do |hash, validator|
      key = case validator.kind
      when :presence
        :blank
      when :uniqueness
        :taken
      when :inclusion
        :inclusion
      when :length
        [:min, :max]
      when :format
        :invalid
      when :numericality
        :numericality
      else
        next
      end
      
      kinds << validator.kind
    
      value = case validator.kind
      when :presence
        true
      when :uniqueness
        true
      when :inclusion
        Array(validator.options[:in]).compact.join(", ")
      when :length
        {:min => validator.options[:minimum], :max => validator.options[:maximum]}
      when :format
        validator.options[:with].is_a?(::Regexp) ? validator.options[:with].source : validator.options[:with].to_s
      when :numericality
        true
      end
      
      unless validator.kind == :length
        message                                           = model.validation_message(name, key, :attribute => attr_name)
        
        hash[:"data-validates-#{validator.kind}"]         = value.to_s
        hash[:"data-validates-#{validator.kind}-message"] = message
      else
        key.each do |length_key|
          next unless value[length_key].present?
          message                                         = model.validation_message(name, length_key == :min ? :too_short : :too_long, :attribute => attr_name, :count => value[length_key])
          
          hash[:"data-validates-#{length_key}"]           = value[length_key].to_s
          hash[:"data-validates-#{length_key}-message"]   = message
        end
      end
      
      hash
    end
    
    @validations[:"data-validate"] = kinds.uniq.join(",") if kinds.present? && @validations.present?
  end
  
  @validations
end

#value(default = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/storefront/models/attribute.rb', line 33

def value(default = nil)
  return default if default.present?
  return nil if top_level
  if model.object.respond_to?(name)
    model.object.send(name)
  else
    nil
  end
end