Class: Lolita::Adapter::ActiveRecord
Defined Under Namespace
Classes: Association, Field
Instance Attribute Summary collapse
Instance Method Summary
collapse
#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
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
Returns the value of attribute dbi.
7
8
9
|
# File 'lib/lolita/adapter/active_record.rb', line 7
def dbi
@dbi
end
|
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
#associations ⇒ Object
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
unless @associations
@associations = {}
klass.reflections.each{|name,association|
@associations[name] = Association.new(association,self)
}
end
@associations
end
|
#collection ⇒ Object
201
202
203
|
# File 'lib/lolita/adapter/active_record.rb', line 201
def collection
self.klass end
|
#collection_name ⇒ Object
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_names ⇒ Object
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
|
#collections ⇒ Object
213
214
215
|
# File 'lib/lolita/adapter/active_record.rb', line 213
def collections
self.klass end
|
193
194
195
|
# File 'lib/lolita/adapter/active_record.rb', line 193
def db
self.klass.connection
end
|
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
|
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_options ⇒ Object
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 = {})
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
|