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.
264
265
266
267
268
269
|
# File 'lib/upmin/model.rb', line 264
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
276
277
278
279
280
281
282
283
|
# File 'lib/upmin/model.rb', line 276
def Model.actions(*actions)
if actions.any?
@actions = actions.map{|a| a.to_sym}
end
@actions ||= []
return @actions
end
|
.active_record? ⇒ Boolean
216
217
218
219
220
221
222
|
# File 'lib/upmin/model.rb', line 216
def Model.active_record?
if defined?(ActiveRecord)
return (model_class < ::ActiveRecord::Base) == true
else
return false
end
end
|
.all ⇒ Object
Returns all upmin models.
133
134
135
136
137
138
139
|
# File 'lib/upmin/model.rb', line 133
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
312
313
314
315
|
# File 'lib/upmin/model.rb', line 312
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.
241
242
243
244
|
# File 'lib/upmin/model.rb', line 241
def Model.attribute(attribute = nil)
= [] unless defined?()
<< attribute.to_sym if attribute
end
|
.attribute_type(attribute) ⇒ Object
307
308
309
310
|
# File 'lib/upmin/model.rb', line 307
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.
250
251
252
253
254
255
256
257
258
259
|
# File 'lib/upmin/model.rb', line 250
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
192
193
194
195
196
|
# File 'lib/upmin/model.rb', line 192
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.
210
211
212
213
214
|
# File 'lib/upmin/model.rb', line 210
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
198
199
200
|
# File 'lib/upmin/model.rb', line 198
def Model.colors
return Upmin.configuration.colors
end
|
.count(*args) ⇒ Object
103
104
105
|
# File 'lib/upmin/model.rb', line 103
def Model.count(*args)
return model_class.count(*args)
end
|
.data_mapper? ⇒ Boolean
224
225
226
227
228
229
230
|
# File 'lib/upmin/model.rb', line 224
def Model.data_mapper?
if defined?(DataMapper)
return model_class.is_a?(::DataMapper::Model)
else
return false
end
end
|
.default_attributes ⇒ Object
302
303
304
305
|
# File 'lib/upmin/model.rb', line 302
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.
297
298
299
300
|
# File 'lib/upmin/model.rb', line 297
def Model.find(*args)
new
return find(*args)
end
|
.find_class(model) ⇒ Object
107
108
109
|
# File 'lib/upmin/model.rb', line 107
def Model.find_class(model)
return find_or_create_class(model.to_s)
end
|
.find_or_create_class(model_name) ⇒ Object
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/upmin/model.rb', line 111
def Model.find_or_create_class(model_name)
::Rails.application.eager_load!
create_name = model_name.gsub(":", "")
return "Admin#{create_name}".constantize
rescue NameError
if model_name.match(/::/)
class_str = " class ::Admin\#{create_name} < Upmin::Model\n def self.model_class\n return \#{model_name}\n end\n end\n class_string\n eval(class_str)\n else\n eval(\"class ::Admin\#{create_name} < Upmin::Model; end\")\n end\n return \"Admin\#{create_name}\".constantize\nend\n"
|
.humanized_name(type = :plural) ⇒ Object
172
173
174
175
176
177
178
|
# File 'lib/upmin/model.rb', line 172
def Model.humanized_name(type = :plural)
names = model_class_name.split(/(?=[A-Z])/).map{|n| n.gsub(":", "")}
if type == :plural
names[names.length-1] = names.last.pluralize
end
return names.join(" ")
end
|
.inferred_model_class ⇒ Object
151
152
153
154
155
156
157
|
# File 'lib/upmin/model.rb', line 151
def Model.inferred_model_class
name = inferred_model_class_name
return name.constantize
rescue NameError => error
raise if name && !error.missing_name?(name)
raise Upmin::UninferrableSourceError.new(self)
end
|
.inferred_model_class_name ⇒ Object
159
160
161
162
|
# File 'lib/upmin/model.rb', line 159
def Model.inferred_model_class_name
raise NameError if name.nil? || name.demodulize !~ /Admin.+$/
return name.demodulize[5..-1]
end
|
.items_per_page(items = Upmin.configuration.items_per_page) ⇒ Object
285
286
287
|
# File 'lib/upmin/model.rb', line 285
def Model.items_per_page(items = Upmin.configuration.items_per_page)
return @items_per_page ||= items
end
|
.model_class ⇒ Object
147
148
149
|
# File 'lib/upmin/model.rb', line 147
def Model.model_class
return @model_class ||= inferred_model_class
end
|
.model_class? ⇒ Boolean
141
142
143
144
145
|
# File 'lib/upmin/model.rb', line 141
def Model.model_class?
return model_class
rescue Upmin::UninferrableSourceError
return false
end
|
.model_class_name ⇒ Object
164
165
166
|
# File 'lib/upmin/model.rb', line 164
def Model.model_class_name
return model_class.name
end
|
.model_name ⇒ Object
168
169
170
|
# File 'lib/upmin/model.rb', line 168
def Model.model_name
return ActiveModel::Name.new(model_class)
end
|
.next_color ⇒ Object
202
203
204
205
206
207
|
# File 'lib/upmin/model.rb', line 202
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
188
189
190
|
# File 'lib/upmin/model.rb', line 188
def Model.search_path
return Upmin::Engine.routes.url_helpers.upmin_search_path(klass: model_class_name)
end
|
.underscore_name(type = :singular) ⇒ Object
180
181
182
183
184
185
186
|
# File 'lib/upmin/model.rb', line 180
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
|