Class: Lolita::Adapter::ActiveRecord

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

Defined Under Namespace

Classes: Association, Field

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CommonHelper

#association_by_klass, #associations_class_names, #by_id, #filter, #find_by_id, #paginate, #record, #reflect_on_association, #set_state_for, #switch_record_state

Constructor Details

#initialize(dbi) ⇒ ActiveRecord

Returns a new instance of ActiveRecord.



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

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

Instance Attribute Details

#dbiObject (readonly)

Returns the value of attribute dbi.



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

def dbi
  @dbi
end

#klassObject (readonly)

Returns the value of attribute klass.



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

def klass
  @klass
end

Instance Method Details

#associationsObject

Return all class associations



68
69
70
71
72
73
74
75
76
77
# File 'lib/lolita/adapter/active_record.rb', line 68

def associations
  # is caching ok?
  unless @associations
    @associations = {}
    klass.reflections.each{|name,association|
      @associations[name] = Association.new(association,self)
    }
  end
  @associations
end

#collectionObject



200
201
202
# File 'lib/lolita/adapter/active_record.rb', line 200

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

#collection_nameObject



204
205
206
# File 'lib/lolita/adapter/active_record.rb', line 204

def collection_name
  self.klass.table_name
end

#collection_name=(value) ⇒ Object



208
209
210
# File 'lib/lolita/adapter/active_record.rb', line 208

def collection_name=(value)
  self.klass.table_name = value
end

#collection_namesObject



216
217
218
# File 'lib/lolita/adapter/active_record.rb', line 216

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

#collectionsObject



212
213
214
# File 'lib/lolita/adapter/active_record.rb', line 212

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

#dbObject



192
193
194
# File 'lib/lolita/adapter/active_record.rb', line 192

def db
  self.klass.connection
end

#db_nameObject



196
197
198
# File 'lib/lolita/adapter/active_record.rb', line 196

def db_name
  db.current_database
end

#field_by_association(name) ⇒ Object



160
161
162
163
164
165
166
167
# File 'lib/lolita/adapter/active_record.rb', line 160

def field_by_association(name)
  possible_association = self.associations.detect{|assoc_name,association|
    name.to_s == assoc_name.to_s
  }
  if possible_association
    self.field_by_name(possible_association.last.key)
  end
end

#field_by_name(name) ⇒ Object



154
155
156
157
158
# File 'lib/lolita/adapter/active_record.rb', line 154

def field_by_name(name)
  self.fields.detect{|field|
    field.name.to_s == name.to_s
  }
end

#fieldsObject



147
148
149
150
151
152
# File 'lib/lolita/adapter/active_record.rb', line 147

def fields
  @fields||=self.klass.columns.collect{|column|
    Field.new(column,self)
  }
  @fields
end

#nested_attributes_optionsObject



220
221
222
# File 'lib/lolita/adapter/active_record.rb', line 220

def nested_attributes_options
  self.klass.nested_attributes_options
end

#order_methodObject



224
225
226
# File 'lib/lolita/adapter/active_record.rb', line 224

def order_method
  :order
end

#search(query, options = {}) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/lolita/adapter/active_record.rb', line 169

def search(query, options = {})
  #TODO raise error or warn when there are lot of records and no index on field
  unless query.blank?
    resources = self.klass.arel_table
    content_fields = @dbi.fields.map{|field| field.type!="string" ? nil : field.name.to_sym}.compact
    if options[:fields] && options[:fields].any?
      content_fields = content_fields & options[:fields]
    end
    scope = nil
    content_fields.each_with_index do |field,index|
      new_scope = resources[field].matches("%#{query}%")
      unless index == 0
        scope = scope.or(new_scope)
      else
        scope = new_scope
      end
    end
    self.klass.where(scope)
  else
    self.klass.where(nil)
  end
end