Class: Storefront::Attribute
- Inherits:
-
Object
- Object
- Storefront::Attribute
- Defined in:
- lib/storefront/models/attribute.rb
Instance Attribute Summary collapse
-
#disabled ⇒ Object
readonly
Returns the value of attribute disabled.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
-
#top_level ⇒ Object
readonly
Returns the value of attribute top_level.
Instance Method Summary collapse
- #access_key ⇒ Object
- #config ⇒ Object
- #disabled? ⇒ Boolean
- #errors? ⇒ Boolean
- #humanize ⇒ Object
-
#initialize(options = {}) ⇒ Attribute
constructor
A new instance of Attribute.
- #input_type(options = {}) ⇒ Object
- #param_for(*parts) ⇒ Object
- #required? ⇒ Boolean
- #to_id(options = {}) ⇒ Object
- #to_label ⇒ Object
- #to_param(options = {}) ⇒ Object
- #validations ⇒ Object
- #value(default = nil) ⇒ Object
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( = {}) @name = [:name] @model = [:model] @required = [:required].nil? ? model.required?(name) : [:required] @disabled = [:disabled] @top_level = [:top_level] end |
Instance Attribute Details
#disabled ⇒ Object (readonly)
Returns the value of attribute disabled.
3 4 5 |
# File 'lib/storefront/models/attribute.rb', line 3 def disabled @disabled end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
3 4 5 |
# File 'lib/storefront/models/attribute.rb', line 3 def model @model end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/storefront/models/attribute.rb', line 3 def name @name end |
#required ⇒ Object (readonly)
Returns the value of attribute required.
3 4 5 |
# File 'lib/storefront/models/attribute.rb', line 3 def required @required end |
#top_level ⇒ Object (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_key ⇒ Object
43 44 45 |
# File 'lib/storefront/models/attribute.rb', line 43 def access_key name.to_s[0, 1] end |
#config ⇒ Object
13 14 15 |
# File 'lib/storefront/models/attribute.rb', line 13 def config Storefront.configuration end |
#disabled? ⇒ Boolean
21 22 23 |
# File 'lib/storefront/models/attribute.rb', line 21 def disabled? @disabled end |
#errors? ⇒ Boolean
29 30 31 |
# File 'lib/storefront/models/attribute.rb', line 29 def errors? top_level ? false : model.errors_on?(name, {}) end |
#humanize ⇒ Object
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( = {}) model.default_input_type(name, ) 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
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( = {}) parts = [] parts << model.keys unless top_level parts << [:parent_index].to_s if [:parent_index].present? parts << name parts << [:index].to_s if [:index].present? parts << ([:type] || "input").to_s parts.join(config.separator).underscore.strip.gsub(/[_\s]+/, config.separator) end |
#to_label ⇒ Object
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( = {}) parts = [] parts << model.keys unless top_level parts << [:parent_index].to_s if [:parent_index].present? parts << name parts << [:index].to_s if [:index].present? param_for(parts) end |
#validations ⇒ Object
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.[:in]).compact.join(", ") when :length {:min => validator.[:minimum], :max => validator.[:maximum]} when :format validator.[:with].is_a?(::Regexp) ? validator.[:with].source : validator.[:with].to_s when :numericality true end unless validator.kind == :length = model.(name, key, :attribute => attr_name) hash[:"data-validates-#{validator.kind}"] = value.to_s hash[:"data-validates-#{validator.kind}-message"] = else key.each do |length_key| next unless value[length_key].present? = model.(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"] = 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 |