Class: Upmin::Attribute
- Inherits:
-
Object
- Object
- Upmin::Attribute
- Defined in:
- lib/upmin/attribute.rb
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #editable? ⇒ Boolean
- #enum_options ⇒ Object
- #errors? ⇒ Boolean
- #form_id ⇒ Object
-
#initialize(model, attr_name, options = {}) ⇒ Attribute
constructor
A new instance of Attribute.
- #label_name ⇒ Object
- #nilable_id ⇒ Object
- #type ⇒ Object
- #value ⇒ Object
Constructor Details
#initialize(model, attr_name, options = {}) ⇒ Attribute
Returns a new instance of Attribute.
6 7 8 9 |
# File 'lib/upmin/attribute.rb', line 6 def initialize(model, attr_name, = {}) @model = model @name = attr_name.to_sym end |
Instance Attribute Details
#model ⇒ Object (readonly)
Returns the value of attribute model.
3 4 5 |
# File 'lib/upmin/attribute.rb', line 3 def model @model end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/upmin/attribute.rb', line 4 def name @name end |
Instance Method Details
#editable? ⇒ Boolean
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/upmin/attribute.rb', line 31 def editable? case name.to_sym when :id return false when :created_at return false when :created_on return false when :updated_at return false when :updated_on return false else # TODO(jon): Add a way to declare which attributes are editable and which are not later. return model.respond_to?("#{name}=") end end |
#enum_options ⇒ Object
65 66 67 |
# File 'lib/upmin/attribute.rb', line 65 def model.class.model_class.defined_enums[name.to_s].keys end |
#errors? ⇒ Boolean
49 50 51 |
# File 'lib/upmin/attribute.rb', line 49 def errors? return model.errors[name].any? end |
#form_id ⇒ Object
57 58 59 |
# File 'lib/upmin/attribute.rb', line 57 def form_id return "#{model.underscore_name}_#{name}" end |
#label_name ⇒ Object
53 54 55 |
# File 'lib/upmin/attribute.rb', line 53 def label_name return name.to_s.gsub(/_/, " ").capitalize end |
#nilable_id ⇒ Object
61 62 63 |
# File 'lib/upmin/attribute.rb', line 61 def nilable_id return "#{form_id}_is_nil" end |
#type ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/upmin/attribute.rb', line 16 def type # TODO(jon): Add a way to override with widgets? return @type if defined?(@type) # Try to get it from the model_class @type = model.class.attribute_type(name) # If we still don't know the type, try to infer it from the value if @type == :unknown @type = infer_type_from_value end return @type end |
#value ⇒ Object
11 12 13 14 |
# File 'lib/upmin/attribute.rb', line 11 def value # TODO(jon): Add some way to handle exceptions. return model.model.send(name) end |