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



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

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

#collectionObject



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

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

#collection_nameObject



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

def collection_name
  self.klass.table_name
end

#collection_name=(value) ⇒ Object



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

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

#collection_namesObject



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

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

#collectionsObject



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

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

#dbObject



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

def db
  self.klass.connection
end

#db_nameObject



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

def db_name
  db.current_database
end

#field_by_association(name) ⇒ Object



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

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



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

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

#fieldsObject



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

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

#nested_attributes_optionsObject



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

def nested_attributes_options
  self.klass.nested_attributes_options
end

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



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

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