Class: Upmin::Model
- Inherits:
-
Object
- Object
- Upmin::Model
- Defined in:
- lib/upmin/model.rb
Instance Attribute Summary collapse
-
#instance ⇒ Object
Returns the value of attribute instance.
-
#klass ⇒ Object
Returns the value of attribute klass.
Instance Method Summary collapse
- #action_parameters(action) ⇒ Object
- #association(assoc_name, options = {}) ⇒ Object
-
#association_type(assoc_name) ⇒ Object
Returns the type of an association.
-
#attribute(attr_name) ⇒ Object
Returns the value of the attr_name method.
-
#attribute_editable?(attr_name) ⇒ Boolean
Returns whether or not the attr_name is an attribute that can be edited.
- #attribute_form_id(attr_name) ⇒ Object
- #attribute_label_name(attr_name) ⇒ Object
-
#attribute_type(attr_name) ⇒ Object
Returns the type of an attribute.
- #color ⇒ Object
-
#initialize(instance, options = {}) ⇒ Model
constructor
A new instance of Model.
- #path_hash ⇒ Object
- #perform_action(action, arguments) ⇒ Object
-
#title ⇒ Object
Methods for rendering in views.
Constructor Details
Instance Attribute Details
#instance ⇒ Object
Returns the value of attribute instance.
4 5 6 |
# File 'lib/upmin/model.rb', line 4 def instance @instance end |
#klass ⇒ Object
Returns the value of attribute klass.
5 6 7 |
# File 'lib/upmin/model.rb', line 5 def klass @klass end |
Instance Method Details
#action_parameters(action) ⇒ Object
107 108 109 |
# File 'lib/upmin/model.rb', line 107 def action_parameters(action) instance.method(action).parameters end |
#association(assoc_name, options = {}) ⇒ Object
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/upmin/model.rb', line 96 def association(assoc_name, = {}) association = instance.send(assoc_name) if association.respond_to?(:each) # We have a collection, at least we hope we do. if [:limit] && association.respond_to?(:limit) association = association.limit(5) end end return association end |
#association_type(assoc_name) ⇒ Object
Returns the type of an association. If we can’t figure it out we fall back to :unknown
88 89 90 91 92 93 94 |
# File 'lib/upmin/model.rb', line 88 def association_type(assoc_name) type = klass.association_type(assoc_name) if type == :unknown && data = association(assoc_name).first type = data.class.name.underscore end return type end |
#attribute(attr_name) ⇒ Object
Returns the value of the attr_name method
71 72 73 74 75 |
# File 'lib/upmin/model.rb', line 71 def attribute(attr_name) attr_name = attr_name.to_sym # TODO(jon): Add some way to handle exceptions. Probably a custom error that we display. return instance.send(attr_name) end |
#attribute_editable?(attr_name) ⇒ Boolean
Returns whether or not the attr_name is an attribute that can be edited.
61 62 63 64 65 66 67 68 |
# File 'lib/upmin/model.rb', line 61 def attribute_editable?(attr_name) attr_name = attr_name.to_sym return false if attr_name == :id return false if attr_name == :created_at return false if attr_name == :updated_at # TODO(jon): Add a way to declare which attributes are editable and which are not later. return instance.respond_to?("#{attr_name}=") end |
#attribute_form_id(attr_name) ⇒ Object
77 78 79 |
# File 'lib/upmin/model.rb', line 77 def attribute_form_id(attr_name) return "#{klass.name.underscore}_#{attr_name}" end |
#attribute_label_name(attr_name) ⇒ Object
81 82 83 |
# File 'lib/upmin/model.rb', line 81 def attribute_label_name(attr_name) return attr_name.to_s.gsub(/_/, " ").capitalize end |
#attribute_type(attr_name) ⇒ Object
Returns the type of an attribute. If it is nil and we can’t figure it out from the db columns we just fall back to :unknown
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 |
# File 'lib/upmin/model.rb', line 34 def attribute_type(attr_name) type = klass.attribute_type(attr_name) if type == :unknown # See if we can deduce it by looking at the data data = attribute(attr_name) class_sym = data.class.to_s.underscore.to_sym if class_sym == :false_class || class_sym == :true_class type = :boolean elsif class_sym == :nil_class type = :unknown elsif class_sym == :fixnum type = :integer elsif class_sym == :big_decimal type = :decimal elsif class_sym == :"active_support/time_with_zone" type = :datetime else # This should prevent any classes from being skipped, but we may not have an exhaustive list yet. type = class_sym end end return type end |
#color ⇒ Object
17 18 19 |
# File 'lib/upmin/model.rb', line 17 def color return klass.color end |
#path_hash ⇒ Object
21 22 23 24 25 26 |
# File 'lib/upmin/model.rb', line 21 def path_hash return { klass: klass.name, id: instance.id } end |
#perform_action(action, arguments) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/upmin/model.rb', line 111 def perform_action(action, arguments) unless klass.actions.include?(action.to_sym) raise "Invalid action: #{action}" end params = action_parameters(action) params_array = [] params.each do |param_type, param_name| if param_type == :req raise "Missing argument: #{param_name}" unless arguments[param_name] params_array << arguments[param_name] elsif param_type == :opt params_array << arguments[param_name] if arguments[param_name] else # :block or ?? next end end return instance.send(action, *params_array) end |
#title ⇒ Object
Methods for rendering in views
13 14 15 |
# File 'lib/upmin/model.rb', line 13 def title return "#{klass.humanized_name(:singular)} # #{instance.id}" end |