class Upmin::Model

Attributes

model[R]
object[R]

Public Class Methods

action(action) click to toggle source

Add a single action to upmin actions. If this is called before upmin_actions the actions will not include any defaults actions.

# 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) click to toggle source

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

# 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?() click to toggle source
# 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
all() click to toggle source

Returns all upmin models.

# 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
associations() click to toggle source
# File lib/upmin/model.rb, line 344
def Model.associations
  new
  return associations
end
attribute(attribute = nil) click to toggle source

Add a single attribute to upmin attributes. If this is called before upmin_attributes the attributes will not include any defaults attributes.

# 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) click to toggle source
# File lib/upmin/model.rb, line 339
def Model.attribute_type(attribute)
  new
  return attribute_type(attribute)
end
attributes(*attrs) click to toggle source

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.

# 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
color() click to toggle source
# File lib/upmin/model.rb, line 203
def Model.color
  return @color if defined?(@color)
  @color = Model.next_color
  return @color
end
color_index() click to toggle source

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

# 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
colors() click to toggle source
# File lib/upmin/model.rb, line 209
def Model.colors
  return Upmin.configuration.colors
end
count(*args) click to toggle source

Class methods

# File lib/upmin/model.rb, line 112
def Model.count(*args)
  return model_class.count(*args)
end
data_mapper?() click to toggle source
# 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_attributes() click to toggle source
# File lib/upmin/model.rb, line 334
def Model.default_attributes
  new
  return default_attributes
end
display_name(name) click to toggle source
# File lib/upmin/model.rb, line 317
def Model.display_name (name)
  return @display_name ||= name
end
find(*args) click to toggle source

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.

# File lib/upmin/model.rb, line 329
def Model.find(*args)
  new
  return find(*args)
end
find_class(model) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

Edit/Create form specific attributes

# 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) click to toggle source
# 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_class() click to toggle source
# 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_name() click to toggle source
# 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) click to toggle source
# 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_class() click to toggle source
# File lib/upmin/model.rb, line 156
def Model.model_class
  return @model_class ||= inferred_model_class
end
model_class?() click to toggle source
# File lib/upmin/model.rb, line 150
def Model.model_class?
  return model_class
rescue Upmin::UninferrableSourceError
  return false
end
model_class_name() click to toggle source
# File lib/upmin/model.rb, line 173
def Model.model_class_name
  return model_class.name
end
model_name() click to toggle source
# File lib/upmin/model.rb, line 177
def Model.model_name
  return ActiveModel::Name.new(model_class)
end
new(model = nil, options = {}) click to toggle source
# 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
next_color() click to toggle source
# 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_path() click to toggle source
# 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) click to toggle source
# 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

Public Instance Methods

actions() click to toggle source
# 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
associations() click to toggle source
# 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
attributes() click to toggle source
# 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() click to toggle source

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

# File lib/upmin/model.rb, line 87
def color
  return self.class.color
end
create_path() click to toggle source
# File lib/upmin/model.rb, line 36
def create_path
  return upmin_create_model_path(klass: model_class_name)
end
form_attributes() click to toggle source
# 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) click to toggle source

delegate(:humanized_name, to: :class)

# File lib/upmin/model.rb, line 91
def humanized_name(type = :plural)
  return self.class.humanized_name(type)
end
model_class() click to toggle source

delegate(:model_class, to: :class)

# File lib/upmin/model.rb, line 99
def model_class
  return self.class.model_class
end
model_class_name() click to toggle source

delegate(:model_class_name, to: :class)

# File lib/upmin/model.rb, line 103
def model_class_name
  return self.class.model_class_name
end
path() click to toggle source
# 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() click to toggle source
# File lib/upmin/model.rb, line 40
def title
  return "#{humanized_name(:singular)} # #{id}"
end
underscore_name() click to toggle source

delegate(:underscore_name, to: :class)

# File lib/upmin/model.rb, line 95
def underscore_name
  return self.class.underscore_name
end