Class: Lolita::Adapter::Mongoid

Inherits:
Object
  • Object
show all
Includes:
AbstractAdapter, CommonHelper
Defined in:
lib/lolita/adapter/mongoid.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) ⇒ Mongoid

Returns a new instance of Mongoid.



8
9
10
11
# File 'lib/lolita/adapter/mongoid.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/mongoid.rb', line 7

def dbi
  @dbi
end

#klassObject (readonly)

Returns the value of attribute klass.



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

def klass
  @klass
end

Instance Method Details

#associationsObject

Return all class associations



53
54
55
56
57
58
59
60
61
62
# File 'lib/lolita/adapter/mongoid.rb', line 53

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

#collectionObject



215
216
217
# File 'lib/lolita/adapter/mongoid.rb', line 215

def collection
  self.klass.collection
end

#collection_nameObject



219
220
221
# File 'lib/lolita/adapter/mongoid.rb', line 219

def collection_name
  self.klass.collection_name
end

#collection_name=(value) ⇒ Object



223
224
225
# File 'lib/lolita/adapter/mongoid.rb', line 223

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

#collection_namesObject



231
232
233
# File 'lib/lolita/adapter/mongoid.rb', line 231

def collection_names
  db.collection_names
end

#collectionsObject



227
228
229
# File 'lib/lolita/adapter/mongoid.rb', line 227

def collections
  db.collections
end

#dbObject



207
208
209
# File 'lib/lolita/adapter/mongoid.rb', line 207

def db
  self.klass.db
end

#db_nameObject



211
212
213
# File 'lib/lolita/adapter/mongoid.rb', line 211

def db_name
  self.klass.db.name
end

#field_by_association(name) ⇒ Object



136
137
138
139
140
141
142
143
# File 'lib/lolita/adapter/mongoid.rb', line 136

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



130
131
132
133
134
# File 'lib/lolita/adapter/mongoid.rb', line 130

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

#fieldsObject



123
124
125
126
127
128
# File 'lib/lolita/adapter/mongoid.rb', line 123

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

#map_reduce_search(content_fields, query) ⇒ Object

FIXME



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/lolita/adapter/mongoid.rb', line 170

def map_reduce_search(content_fields,query)
  keys = "[" + @dbi.fields.map{|f|
    f.primary? || f.name.to_s.match(/^_/)  ? nil : "'#{f.name}'"
  }.compact.join(",").to_s + "]"
  content_keys = "[" + content_fields.map{|f| "'#{f.name}'"}.join(",").to_s + "]"
  result_obj = @dbi.fields.map{|f|
    f.name.to_s.match(/^_/) ? nil : "'#{f.name}': false"
  }.compact.join(",")
  result_obj = "{#{result_obj}}"
  map = %^
    function(){
      var doc = #{result_obj};
      var do_emit = false;
      for(var k in #{content_keys}){
        var c_val = this[k];
        if(c_val && c_val.match(/#{Regexp.escape(query)}/i)){
          do_emit = true;
          break;
        };
      };
      if(true){
        #{keys}.forEach(function(k){
          doc[k] = this.name;
        })
        emit(this.id,doc);
      };
    };
  ^
  reduce = %^
    function(key,values){

      return values[values.length-1];
    };
  ^
  self.collection.map_reduce(map,reduce, {:out => "custom_mr", :query => {}})
end

#nested_attributes_optionsObject



235
236
237
# File 'lib/lolita/adapter/mongoid.rb', line 235

def nested_attributes_options
  self.klass.relations
end

#order_methodObject



239
240
241
# File 'lib/lolita/adapter/mongoid.rb', line 239

def order_method
  :order_by
end

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



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/lolita/adapter/mongoid.rb', line 145

def search(query, options = {})
  unless query.blank?
    content_fields = @dbi.fields.map{|field|
      if field.type!="string" || field.name.match(/^_/)
        nil
      else
        field.name.to_sym
      end
    }.compact
    if options[:fields] && options[:fields].any?
      content_fields = content_fields & options[:fields]
    end
    content_fields = content_fields.slice(0..3)

    where_hash = {}
    content_fields.each do |field|
      where_hash[field] = /#{Regexp.escape(query.to_s)}/
    end
    klass.where(where_hash)
  else
    klass.where(nil)
  end
end