Class: Lolita::Adapter::ActiveRecord

Inherits:
Object
  • Object
show all
Includes:
AbstractAdapter
Defined in:
lib/lolita/adapter/active_record.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dbi) ⇒ ActiveRecord

Returns a new instance of ActiveRecord.



7
8
9
10
# File 'lib/lolita/adapter/active_record.rb', line 7

def initialize(dbi)
  @dbi=dbi
  @klass=dbi.klass
end

Instance Attribute Details

#dbiObject (readonly)

Returns the value of attribute dbi.



6
7
8
# File 'lib/lolita/adapter/active_record.rb', line 6

def dbi
  @dbi
end

#klassObject (readonly)

Returns the value of attribute klass.



6
7
8
# File 'lib/lolita/adapter/active_record.rb', line 6

def klass
  @klass
end

Instance Method Details

#association_class_name(association) ⇒ Object



43
44
45
# File 'lib/lolita/adapter/active_record.rb', line 43

def association_class_name(association)
  association.class_name
end

#association_macro(association) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/lolita/adapter/active_record.rb', line 29

def association_macro(association)
  type=association.macro
  case type
  when :has_many
    :many
  when :has_one
    :one
  when :belongs_to
    :one
  when :has_and_belongs_to_many
    :many
  end
end

#associationsObject



12
13
14
# File 'lib/lolita/adapter/active_record.rb', line 12

def associations
  klass.reflections
end

#associations_klass_namesObject

Same as in mongoid



17
18
19
20
21
22
23
# File 'lib/lolita/adapter/active_record.rb', line 17

def associations_klass_names
  names=[]
  associations.each{|name,association|
    names << association.class_name
  }
  names
end

#collectionObject



81
82
83
# File 'lib/lolita/adapter/active_record.rb', line 81

def collection
  self.klass #FIXME not realy same as in mongoid
end

#collection_nameObject



85
86
87
# File 'lib/lolita/adapter/active_record.rb', line 85

def collection_name
  self.klass.table_name
end

#collection_namesObject



93
94
95
# File 'lib/lolita/adapter/active_record.rb', line 93

def collection_names
  self.klass.connection.select_all("show tables from #{db_name}").map{|r| r.values.first}
end

#collectionsObject



89
90
91
# File 'lib/lolita/adapter/active_record.rb', line 89

def collections
  self.klass #FIXME not  realy same as in mongoid
end

#dbObject



73
74
75
# File 'lib/lolita/adapter/active_record.rb', line 73

def db
  self.klass.connection
end

#db_nameObject



77
78
79
# File 'lib/lolita/adapter/active_record.rb', line 77

def db_name
  db.current_database
end

#fieldsObject



47
48
49
50
51
52
53
54
# File 'lib/lolita/adapter/active_record.rb', line 47

def fields
  @fields||=self.klass.columns.collect{|column|
    field_to_hash(column)
  }.reject{|column|
    column[:options][:primary]
  }
  @fields
end

#find(*args) ⇒ Object



60
61
62
# File 'lib/lolita/adapter/active_record.rb', line 60

def find(*args)
  self.klass.find(*args)
end

#find_by_id(id) ⇒ Object



56
57
58
# File 'lib/lolita/adapter/active_record.rb', line 56

def find_by_id(id)
  self.klass.where(:id=>id).first
end

#paginate(opt = {}) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/lolita/adapter/active_record.rb', line 64

def paginate(opt={})
  #FIXME depend on will_paginate
  if order=opt.delete(:sort)
    order=order.map{|c| c.join(" ")}.join(", ")
    opt[:order]=order
  end
  self.klass.paginate(opt)
end

#reflect_on_association(name) ⇒ Object



25
26
27
# File 'lib/lolita/adapter/active_record.rb', line 25

def reflect_on_association(name)
  klass.reflect_on_association(name)
end