Class: ActiveRecord::Base::VeneerInterface::ClassWrapper

Inherits:
Veneer::Base::ClassWrapper show all
Defined in:
lib/veneer/adapters/activerecord/class_wrapper.rb,
lib/veneer/adapters/activerecord/pre_3.0_class_wrapper.rb

Instance Attribute Summary

Attributes inherited from Veneer::Base::ClassWrapper

#klass, #opts

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Veneer::Base::ClassWrapper

#all, #create, #create!, #first, inherited, #initialize, subclasses, #validators_on

Constructor Details

This class inherits a constructor from Veneer::Base::ClassWrapper

Class Method Details

.except_classesObject



7
8
9
10
11
12
# File 'lib/veneer/adapters/activerecord/class_wrapper.rb', line 7

def self.except_classes
  @@except_classes ||= [
    "CGI::Session::ActiveRecordStore::Session",
    "ActiveRecord::SessionStore::Session"
  ]
end

.model_classesObject



14
15
16
17
18
19
# File 'lib/veneer/adapters/activerecord/class_wrapper.rb', line 14

def self.model_classes
  klasses = ::ActiveRecord::Base.descendants
  klasses.select do |klass|
    !klass.abstract_class? && !except_classes.include?(klass.name)
  end
end

Instance Method Details

#collection_associationsObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/veneer/adapters/activerecord/class_wrapper.rb', line 25

def collection_associations
  @collection_associations ||= begin
    associations = []
    [:has_many, :has_and_belongs_to_many].each do |macro|
      associations += klass.reflect_on_all_associations(macro)
    end
    associations.inject([]) do |ary, reflection|
      ary << {
        :name  => reflection.name,
        :class => reflection.class_name.constantize
      }
      ary
    end
  end
end

#count(opts = {}) ⇒ Object



95
96
97
98
# File 'lib/veneer/adapters/activerecord/class_wrapper.rb', line 95

def count(opts ={})
  opts = ::Hashie::Mash.new(opts)
  build_query(opts).count
end

#destroy_allObject



83
84
85
# File 'lib/veneer/adapters/activerecord/class_wrapper.rb', line 83

def destroy_all
  klass.destroy_all
end

#find_first(opts) ⇒ Object



87
88
89
# File 'lib/veneer/adapters/activerecord/class_wrapper.rb', line 87

def find_first(opts)
  build_query(opts).first
end

#find_many(opts) ⇒ Object



91
92
93
# File 'lib/veneer/adapters/activerecord/class_wrapper.rb', line 91

def find_many(opts)
  build_query(opts).all
end

#max(field, opts = {}) ⇒ Object



105
106
107
108
# File 'lib/veneer/adapters/activerecord/class_wrapper.rb', line 105

def max(field, opts={})
  opts = ::Hashie::Mash.new(opts)
  build_query(opts).maximum(field)
end

#member_associationsObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/veneer/adapters/activerecord/class_wrapper.rb', line 41

def member_associations
  @member_associations ||= begin
    associations = []
    [:belongs_to, :has_one].each do |macro|
      associations += klass.reflect_on_all_associations(macro)
    end
    associations.inject([]) do |ary, reflection|
      ary << {
        :name  => reflection.name,
        :class => reflection.class_name.constantize
      }
      ary
    end
  end
end

#min(field, opts = {}) ⇒ Object



110
111
112
113
# File 'lib/veneer/adapters/activerecord/class_wrapper.rb', line 110

def min(field, opts={})
  opts = ::Hashie::Mash.new(opts)
  build_query(opts).minimum(field)
end

#new(opts = {}) ⇒ Object



21
22
23
# File 'lib/veneer/adapters/activerecord/class_wrapper.rb', line 21

def new(opts = {})
  ::Kernel::Veneer(klass.new(opts))
end

#primary_keysObject



79
80
81
# File 'lib/veneer/adapters/activerecord/class_wrapper.rb', line 79

def primary_keys
  @primary_keys ||= [klass.primary_key.to_sym]
end

#propertiesObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/veneer/adapters/activerecord/class_wrapper.rb', line 57

def properties
  @properties ||= begin
    klass.columns.map do |property|
      name = property.name.to_sym
     ::ActiveRecord::Base::VeneerInterface::Property.new(
        self,
        {
          :name => name,
          :type => property.type,
          :constraints => {
            :length => property.limit,
            :nullable? => property.null,
            :precision => property.precision,
            :scale => property.scale,
          },
          :primary => primary_keys.include?(name),
        }
      )
    end
  end
end

#sum(field, opts = {}) ⇒ Object



100
101
102
103
# File 'lib/veneer/adapters/activerecord/class_wrapper.rb', line 100

def sum(field, opts={})
  opts = ::Hashie::Mash.new(opts)
  build_query(opts).sum(field)
end