Class: Upmin::Model
Instance Attribute Summary collapse
-
#model ⇒ Object
(also: #object)
readonly
Returns the value of attribute model.
Class Method Summary
collapse
Instance Method Summary
collapse
#delegatable?, #delegated?, #method, #method_missing, #respond_to?
Constructor Details
#initialize(model = nil, options = {}) ⇒ Model
Returns a new instance of Model.
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/upmin/model.rb', line 9
def initialize(model = nil, options = {})
if self.class.active_record?
self.class.send(:include, Upmin::ActiveRecord::Model)
elsif self.class.data_mapper?
self.class.send(:include, Upmin::DataMapper::Model)
end
if model.is_a?(Hash)
unless model.has_key?(:id)
raise ":id or model instance is required."
end
@model = self.class.find(model[:id])
elsif model.nil?
@model = self.model_class.new
else
@model = model
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
in the class Upmin::AutomaticDelegation
Instance Attribute Details
#model ⇒ Object
Also known as:
object
Returns the value of attribute model.
6
7
8
|
# File 'lib/upmin/model.rb', line 6
def model
@model
end
|
Class Method Details
.action(action) ⇒ Object
Add a single action to upmin actions. If this is called before upmin_actions the actions will not include any defaults actions.
255
256
257
258
259
260
|
# File 'lib/upmin/model.rb', line 255
def Model.action(action)
@actions ||= []
action = action.to_sym
@actions << action unless @actions.include?(action)
end
|
.actions(*actions) ⇒ Object
Sets the upmin_actions to the provided actions if any are provided. If no actions are provided, and upmin_actions hasn’t been defined, then the upmin_actions are set to the default actions. Returns the upmin_actions
267
268
269
270
271
272
273
274
|
# File 'lib/upmin/model.rb', line 267
def Model.actions(*actions)
if actions.any?
@actions = actions.map{|a| a.to_sym}
end
@actions ||= []
return @actions
end
|
.active_record? ⇒ Boolean
206
207
208
209
210
211
212
|
# File 'lib/upmin/model.rb', line 206
def Model.active_record?
if defined?(ActiveRecord)
return model_class.superclass == ::ActiveRecord::Base
else
return false
end
end
|
.all ⇒ Object
Returns all upmin models.
122
123
124
125
126
127
128
|
# File 'lib/upmin/model.rb', line 122
def Model.all
all = []
Upmin.configuration.models.each do |m|
all << find_or_create_class(m.to_s.camelize)
end
return all
end
|
.associations ⇒ Object
300
301
302
303
|
# File 'lib/upmin/model.rb', line 300
def Model.associations
new
return associations
end
|
.attribute(attribute = nil) ⇒ Object
Add a single attribute to upmin attributes. If this is called before upmin_attributes the attributes will not include any defaults attributes.
232
233
234
235
|
# File 'lib/upmin/model.rb', line 232
def Model.attribute(attribute = nil)
= [] unless defined?()
<< attribute.to_sym if attribute
end
|
.attribute_type(attribute) ⇒ Object
295
296
297
298
|
# File 'lib/upmin/model.rb', line 295
def Model.attribute_type(attribute)
new
return attribute_type(attribute)
end
|
.attributes(*attributes) ⇒ Object
Sets the attributes to the provided attributes # if any are any provided. If no attributes are provided then the attributes are set to the default attributes of the model class.
241
242
243
244
245
246
247
248
249
250
|
# File 'lib/upmin/model.rb', line 241
def Model.attributes(*attributes)
= [] unless defined?()
if attributes.any?
@attributes = attributes.map{|a| a.to_sym}
end
@attributes ||= default_attributes
return (@attributes + ).uniq
end
|
.color ⇒ Object
181
182
183
184
185
|
# File 'lib/upmin/model.rb', line 181
def Model.color
return @color if defined?(@color)
@color = Model.next_color
return @color
end
|
.color_index ⇒ Object
This is not currently used, but could be used to ensure colors are always the same.
199
200
201
202
203
|
# File 'lib/upmin/model.rb', line 199
def Model.color_index
return @color_index if defined?(@color_index)
@color_index = model_class_name.split("").map(&:ord).inject(:+) % colors.length
return @color_index
end
|
.colors ⇒ Object
187
188
189
|
# File 'lib/upmin/model.rb', line 187
def Model.colors
return Upmin.configuration.colors
end
|
.count(*args) ⇒ Object
105
106
107
|
# File 'lib/upmin/model.rb', line 105
def Model.count(*args)
return model_class.count(*args)
end
|
.data_mapper? ⇒ Boolean
214
215
216
217
218
219
220
|
# File 'lib/upmin/model.rb', line 214
def Model.data_mapper?
if defined?(DataMapper)
return model_class.is_a?(::DataMapper::Model)
else
return false
end
end
|
.default_attributes ⇒ Object
290
291
292
293
|
# File 'lib/upmin/model.rb', line 290
def Model.default_attributes
new
return default_attributes
end
|
.find(*args) ⇒ Object
Methods that need to be to be overridden. If the Model.method_name version of these are ever called it means that it wasn’t overridden, or an instance of the class hasn’t been created yet.
285
286
287
288
|
# File 'lib/upmin/model.rb', line 285
def Model.find(*args)
new
return find(*args)
end
|
.find_class(model) ⇒ Object
109
110
111
|
# File 'lib/upmin/model.rb', line 109
def Model.find_class(model)
return find_or_create_class(model.to_s)
end
|
.find_or_create_class(model_name) ⇒ Object
113
114
115
116
117
118
119
|
# File 'lib/upmin/model.rb', line 113
def Model.find_or_create_class(model_name)
::Rails.application.eager_load!
return "Admin#{model_name}".constantize
rescue NameError
eval("class ::Admin#{model_name} < Upmin::Model; end")
return "Admin#{model_name}".constantize
end
|
.humanized_name(type = :plural) ⇒ Object
161
162
163
164
165
166
167
|
# File 'lib/upmin/model.rb', line 161
def Model.humanized_name(type = :plural)
names = model_class_name.split(/(?=[A-Z])/)
if type == :plural
names[names.length-1] = names.last.pluralize
end
return names.join(" ")
end
|
.inferred_model_class ⇒ Object
144
145
146
147
148
149
150
|
# File 'lib/upmin/model.rb', line 144
def Model.inferred_model_class
name = model_class_name
return name.constantize
rescue NameError => error
raise if name && !error.missing_name?(name)
raise Upmin::UninferrableSourceError.new(self)
end
|
.model_class ⇒ Object
130
131
132
|
# File 'lib/upmin/model.rb', line 130
def Model.model_class
@model_class ||= inferred_model_class
end
|
.model_class? ⇒ Boolean
134
135
136
137
138
|
# File 'lib/upmin/model.rb', line 134
def Model.model_class?
return model_class
rescue Upmin::UninferrableSourceError
return false
end
|
.model_class_name ⇒ Object
152
153
154
155
|
# File 'lib/upmin/model.rb', line 152
def Model.model_class_name
raise NameError if name.nil? || name.demodulize !~ /Admin.+$/
return name.demodulize[5..-1]
end
|
.model_name ⇒ Object
157
158
159
|
# File 'lib/upmin/model.rb', line 157
def Model.model_name
return ActiveModel::Name.new(model_class)
end
|
.next_color ⇒ Object
191
192
193
194
195
196
|
# File 'lib/upmin/model.rb', line 191
def Model.next_color
@color_index ||= 0
next_color = colors[@color_index]
@color_index = (@color_index + 1) % colors.length
return next_color
end
|
.search_path ⇒ Object
177
178
179
|
# File 'lib/upmin/model.rb', line 177
def Model.search_path
return Upmin::Engine.routes.url_helpers.upmin_search_path(klass: model_class_name)
end
|
.underscore_name(type = :singular) ⇒ Object
169
170
171
172
173
174
175
|
# File 'lib/upmin/model.rb', line 169
def Model.underscore_name(type = :singular)
if type == :singular
return model_class_name.underscore
else
return model_class_name.pluralize.underscore
end
end
|
Instance Method Details
#actions ⇒ Object
62
63
64
65
66
67
68
69
|
# File 'lib/upmin/model.rb', line 62
def actions
return @actions if defined?(@actions)
@actions = []
self.class.actions.each do |action_name|
@actions << Upmin::Action.new(self, action_name)
end
return @actions
end
|
#associations ⇒ Object
53
54
55
56
57
58
59
60
|
# File 'lib/upmin/model.rb', line 53
def associations
return @associations if defined?(@associations)
@associations = []
self.class.associations.each do |assoc_name|
@associations << Upmin::Association.new(self, assoc_name)
end
return @associations
end
|
#attributes ⇒ Object
44
45
46
47
48
49
50
51
|
# File 'lib/upmin/model.rb', line 44
def attributes
return @attributes if defined?(@attributes)
@attributes = []
self.class.attributes.each do |attr_name|
@attributes << Upmin::Attribute.new(self, attr_name)
end
return @attributes
end
|
#color ⇒ Object
TODO(jon): Delegations here weren’t working in 3.2 so this is done with normal old methods. delegate(:color, to: :class)
78
79
80
|
# File 'lib/upmin/model.rb', line 78
def color
return self.class.color
end
|
#create_path ⇒ Object
36
37
38
|
# File 'lib/upmin/model.rb', line 36
def create_path
return upmin_create_model_path(klass: model_class_name)
end
|
#humanized_name(type = :plural) ⇒ Object
delegate(:humanized_name, to: :class)
82
83
84
|
# File 'lib/upmin/model.rb', line 82
def humanized_name(type = :plural)
return self.class.humanized_name(type)
end
|
#model_class ⇒ Object
delegate(:model_class, to: :class)
90
91
92
|
# File 'lib/upmin/model.rb', line 90
def model_class
return self.class.model_class
end
|
#model_class_name ⇒ Object
delegate(:model_class_name, to: :class)
94
95
96
|
# File 'lib/upmin/model.rb', line 94
def model_class_name
return self.class.model_class_name
end
|
#path ⇒ Object
28
29
30
31
32
33
34
|
# File 'lib/upmin/model.rb', line 28
def path
if new_record?
return upmin_new_model_path(klass: model_class_name)
else
return upmin_model_path(klass: model_class_name, id: id)
end
end
|
#title ⇒ Object
40
41
42
|
# File 'lib/upmin/model.rb', line 40
def title
return "#{humanized_name(:singular)} # #{id}"
end
|
#underscore_name ⇒ Object
delegate(:underscore_name, to: :class)
86
87
88
|
# File 'lib/upmin/model.rb', line 86
def underscore_name
return self.class.underscore_name
end
|