Module: Upmin::ActiveRecord::Model::ClassMethods

Defined in:
lib/upmin/active_record/model.rb

Instance Method Summary collapse

Instance Method Details

#associationsObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/upmin/active_record/model.rb', line 48

def associations
  return @associations if defined?(@associations)

  all = []
  ignored = []
  model_class.reflect_on_all_associations.each do |reflection|
    all << reflection.name.to_sym

    # We need to remove the ignored later because we don't know the order they come in.
    if reflection.is_a?(::ActiveRecord::Reflection::ThroughReflection)
      ignored << reflection.options[:through]
    end
  end

  return @associations = (all - ignored).uniq
end

#attribute_type(attribute) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/upmin/active_record/model.rb', line 28

def attribute_type(attribute)
  adapter = model_class.columns_hash[attribute.to_s]
  if adapter
    if attribute.in? enum_attributes
      return :enum
    else
      return adapter.type
    end
  end
  return :unknown
end

#default_attributesObject



24
25
26
# File 'lib/upmin/active_record/model.rb', line 24

def default_attributes
  return model_class.attribute_names.map(&:to_sym)
end

#enum_attributesObject



40
41
42
43
44
45
46
# File 'lib/upmin/active_record/model.rb', line 40

def enum_attributes
  if model_class.respond_to? :defined_enums
    model_class.defined_enums.keys.map(&:to_sym)
  else
    []
  end
end

#find(*args) ⇒ Object

NOTE - ANY method added here must be added to the bottom of Upmin::Model. This ensures that an instance of the class was created, which in turn ensures that the correct module was included in the class.



20
21
22
# File 'lib/upmin/active_record/model.rb', line 20

def find(*args)
  return model_class.find(*args)
end