Class: Upmin::Model

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AutomaticDelegation

#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

#modelObject (readonly) 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.



292
293
294
295
296
297
# File 'lib/upmin/model.rb', line 292

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



304
305
306
307
308
309
310
311
# File 'lib/upmin/model.rb', line 304

def Model.actions(*actions)
  if actions.any?
    # set the actions
    @actions = actions.map(&:to_sym)
  end
  @actions ||= []
  return @actions
end

.active_record?Boolean

Returns:

  • (Boolean)


227
228
229
230
231
232
233
# File 'lib/upmin/model.rb', line 227

def Model.active_record?
  if defined?(ActiveRecord)
    return (model_class < ::ActiveRecord::Base) == true
  else
    return false
  end
end

.allObject

Returns all upmin models.



142
143
144
145
146
147
148
# File 'lib/upmin/model.rb', line 142

def Model.all
  all = []
  Upmin.configuration.models.each do |m|
    all << find_or_create_class(m.to_s.camelize)
  end
  return all
end

.associationsObject



344
345
346
347
# File 'lib/upmin/model.rb', line 344

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.



252
253
254
255
# File 'lib/upmin/model.rb', line 252

def Model.attribute(attribute = nil)
  @extra_attrs = [] unless defined?(@extra_attrs)
  @extra_attrs << attribute.to_sym if attribute
end

.attribute_type(attribute) ⇒ Object



339
340
341
342
# File 'lib/upmin/model.rb', line 339

def Model.attribute_type(attribute)
  new
  return attribute_type(attribute)
end

.attributes(*attrs) ⇒ 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.



266
267
268
269
270
271
272
273
274
275
# File 'lib/upmin/model.rb', line 266

def Model.attributes(*attrs)
  @extra_attrs = [] unless defined?(@extra_attrs)

  if attrs.any?
    @attributes = attrs.map{|a| a.to_sym}
  end
  @attributes ||= default_attributes

  return (@attributes + @extra_attrs).uniq
end

.colorObject



203
204
205
206
207
# File 'lib/upmin/model.rb', line 203

def Model.color
  return @color if defined?(@color)
  @color = Model.next_color
  return @color
end

.color_indexObject

This is not currently used, but could be used to ensure colors are always the same.



221
222
223
224
225
# File 'lib/upmin/model.rb', line 221

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

.colorsObject



209
210
211
# File 'lib/upmin/model.rb', line 209

def Model.colors
  return Upmin.configuration.colors
end

.count(*args) ⇒ Object

Class methods



112
113
114
# File 'lib/upmin/model.rb', line 112

def Model.count(*args)
  return model_class.count(*args)
end

.data_mapper?Boolean

Returns:

  • (Boolean)


235
236
237
238
239
240
241
# File 'lib/upmin/model.rb', line 235

def Model.data_mapper?
  if defined?(DataMapper)
    return model_class.is_a?(::DataMapper::Model)
  else
    return false
  end
end

.default_attributesObject



334
335
336
337
# File 'lib/upmin/model.rb', line 334

def Model.default_attributes
  new
  return default_attributes
end

.display_name(name) ⇒ Object



317
318
319
# File 'lib/upmin/model.rb', line 317

def Model.display_name (name)
  return @display_name ||= name
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.



329
330
331
332
# File 'lib/upmin/model.rb', line 329

def Model.find(*args)
  new
  return find(*args)
end

.find_class(model) ⇒ Object



116
117
118
# File 'lib/upmin/model.rb', line 116

def Model.find_class(model)
  return find_or_create_class(model.to_s)
end

.find_or_create_class(model_name) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/upmin/model.rb', line 120

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_string
      class ::Admin#{create_name} < Upmin::Model
        def self.model_class
          return #{model_name}
        end
      end
    class_string
    eval(class_str)
  else
    eval("class ::Admin#{create_name} < Upmin::Model; end")
  end
  return "Admin#{create_name}".constantize
end

.form_attribute(attribute = nil) ⇒ Object



257
258
259
260
# File 'lib/upmin/model.rb', line 257

def Model.form_attribute(attribute = nil)
  @extra_form_attrs = [] unless defined?(@extra_form_attrs)
  @extra_form_attrs << attribute.to_sym if attribute
end

.form_attributes(*attrs) ⇒ Object

Edit/Create form specific attributes



278
279
280
281
282
283
284
285
286
287
# File 'lib/upmin/model.rb', line 278

def Model.form_attributes(*attrs)
  @extra_form_attrs = [] unless defined?(@extra_form_attrs)

  if attrs.any?
    @form_attributes = attrs.map{|a| a.to_sym}
  end
  @form_attributes ||= default_attributes

  return (@form_attributes + @extra_form_attrs).uniq
end

.humanized_name(type = :plural) ⇒ Object



181
182
183
184
185
186
187
188
189
# File 'lib/upmin/model.rb', line 181

def Model.humanized_name(type = :plural)
  names = @display_name ? [@display_name] : 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_classObject



160
161
162
163
164
165
166
# File 'lib/upmin/model.rb', line 160

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_nameObject

Raises:

  • (NameError)


168
169
170
171
# File 'lib/upmin/model.rb', line 168

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



313
314
315
# File 'lib/upmin/model.rb', line 313

def Model.items_per_page(items = Upmin.configuration.items_per_page)
  return @items_per_page ||= items
end

.model_classObject



156
157
158
# File 'lib/upmin/model.rb', line 156

def Model.model_class
  return @model_class ||= inferred_model_class
end

.model_class?Boolean

Returns:

  • (Boolean)


150
151
152
153
154
# File 'lib/upmin/model.rb', line 150

def Model.model_class?
  return model_class
rescue Upmin::UninferrableSourceError
  return false
end

.model_class_nameObject



173
174
175
# File 'lib/upmin/model.rb', line 173

def Model.model_class_name
  return model_class.name
end

.model_nameObject



177
178
179
# File 'lib/upmin/model.rb', line 177

def Model.model_name
  return ActiveModel::Name.new(model_class)
end

.next_colorObject



213
214
215
216
217
218
# File 'lib/upmin/model.rb', line 213

def Model.next_color
  @color_index ||= 0
  next_color = colors[@color_index]
  @color_index = (@color_index + 1) % colors.length
  return next_color
end

.search_pathObject



199
200
201
# File 'lib/upmin/model.rb', line 199

def Model.search_path
  return Upmin::Engine.routes.url_helpers.upmin_search_path(klass: model_class_name)
end

.underscore_name(type = :singular) ⇒ Object



191
192
193
194
195
196
197
# File 'lib/upmin/model.rb', line 191

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

#actionsObject



71
72
73
74
75
76
77
78
# File 'lib/upmin/model.rb', line 71

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

#associationsObject



62
63
64
65
66
67
68
69
# File 'lib/upmin/model.rb', line 62

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

#attributesObject



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

#colorObject

TODO(jon): Delegations here weren’t working in 3.2 so this is done with normal old methods. delegate(:color, to: :class)



87
88
89
# File 'lib/upmin/model.rb', line 87

def color
  return self.class.color
end

#create_pathObject



36
37
38
# File 'lib/upmin/model.rb', line 36

def create_path
  return upmin_create_model_path(klass: model_class_name)
end

#form_attributesObject



53
54
55
56
57
58
59
60
# File 'lib/upmin/model.rb', line 53

def form_attributes
  return @form_attributes if defined?(@form_attributes)
  @form_attributes = []
  self.class.form_attributes.each do |attr_name|
    @form_attributes << Upmin::Attribute.new(self, attr_name)
  end
  return @form_attributes
end

#humanized_name(type = :plural) ⇒ Object

delegate(:humanized_name, to: :class)



91
92
93
# File 'lib/upmin/model.rb', line 91

def humanized_name(type = :plural)
  return self.class.humanized_name(type)
end

#model_classObject

delegate(:model_class, to: :class)



99
100
101
# File 'lib/upmin/model.rb', line 99

def model_class
  return self.class.model_class
end

#model_class_nameObject

delegate(:model_class_name, to: :class)



103
104
105
# File 'lib/upmin/model.rb', line 103

def model_class_name
  return self.class.model_class_name
end

#pathObject



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

#titleObject



40
41
42
# File 'lib/upmin/model.rb', line 40

def title
  return "#{humanized_name(:singular)} # #{id}"
end

#underscore_nameObject

delegate(:underscore_name, to: :class)



95
96
97
# File 'lib/upmin/model.rb', line 95

def underscore_name
  return self.class.underscore_name
end