Class: Upmin::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/upmin/model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance, options = {}) ⇒ Model

Returns a new instance of Model.



7
8
9
10
# File 'lib/upmin/model.rb', line 7

def initialize(instance, options = {})
  self.instance = instance
  self.klass = Upmin::Klass.find(instance.class.name)
end

Instance Attribute Details

#instanceObject

Returns the value of attribute instance.



4
5
6
# File 'lib/upmin/model.rb', line 4

def instance
  @instance
end

#klassObject

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, options = {})
  association = instance.send(assoc_name)
  if association.respond_to?(:each)
    # We have a collection, at least we hope we do.
    if options[: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.

Returns:

  • (Boolean)


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

#colorObject



17
18
19
# File 'lib/upmin/model.rb', line 17

def color
  return klass.color
end

#path_hashObject



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

#titleObject

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