Module: Mongoblazer::ActiveRecord::ClassMethods

Defined in:
lib/mongoblazer/active_record.rb

Instance Method Summary collapse

Instance Method Details

#belongs_to(name, options = {}) ⇒ Object



222
223
224
225
# File 'lib/mongoblazer/active_record.rb', line 222

def belongs_to(name, options={})
  mongoblazer_init embeds_one: {name => options[:class_name]}
  super
end

#find_blazed(id) ⇒ Object



207
208
209
210
211
212
213
# File 'lib/mongoblazer/active_record.rb', line 207

def find_blazed(id)
  ar_instance = select("#{self.table_name}.mongoblazer_id").find(id)

  recreate_mongoblazer_class!

  mongoblazer_class.find(ar_instance.mongoblazer_id)
end

#has_and_belongs_to_many(name, options = {}) ⇒ Object



237
238
239
240
# File 'lib/mongoblazer/active_record.rb', line 237

def has_and_belongs_to_many(name, options={})
  mongoblazer_init embeds_many: {name => options[:class_name]}
  super
end

#has_many(name, options = {}) ⇒ Object



232
233
234
235
# File 'lib/mongoblazer/active_record.rb', line 232

def has_many(name, options={})
  mongoblazer_init embeds_many: {name => options[:class_name]}
  super
end

#has_one(name, options = {}) ⇒ Object



227
228
229
230
# File 'lib/mongoblazer/active_record.rb', line 227

def has_one(name, options={})
  mongoblazer_init embeds_one: {name => options[:class_name]}
  super
end

#mongoblazable?Boolean

Is this model Mongoblazable?

Returns:

  • (Boolean)


218
219
220
# File 'lib/mongoblazer/active_record.rb', line 218

def mongoblazable?
  mongoblazer_options.present?
end

#mongoblazer_additional_attributes(options = {}) ⇒ Object

Defines additional attributes (e.g. not in db) to be merged in the mongodb document.



274
275
276
# File 'lib/mongoblazer/active_record.rb', line 274

def mongoblazer_additional_attributes(options={})
  mongoblazer_init additional_attributes: options
end

#mongoblazer_classObject



328
329
330
# File 'lib/mongoblazer/active_record.rb', line 328

def mongoblazer_class
  mongoblazer_class_name.constantize
end

#mongoblazer_class_nameObject



332
333
334
# File 'lib/mongoblazer/active_record.rb', line 332

def mongoblazer_class_name
  "#{self.name}Blazer"
end

#mongoblazer_default_scope(options = {}) ⇒ Object

Defines default scope to find the matching mongodb document.

Syntax same as where() in AR



259
260
261
# File 'lib/mongoblazer/active_record.rb', line 259

def mongoblazer_default_scope(options={})
  mongoblazer_init default_scope: options
end

#mongoblazer_includes(options = {}) ⇒ Object

Defines relation includes to be merged in the mongodb document.

Syntax same as eager loading syntax in AR:

http://guides.rubyonrails.org/active_record_querying.html#eager-loading-associations


249
250
251
# File 'lib/mongoblazer/active_record.rb', line 249

def mongoblazer_includes(options={})
  mongoblazer_init includes: options
end

#mongoblazer_index_fields(options = {}) ⇒ Object

Defines indexes on the blazer model.



266
267
268
# File 'lib/mongoblazer/active_record.rb', line 266

def mongoblazer_index_fields(options={})
  mongoblazer_init indexes: options
end

#mongoblazer_init(options) ⇒ Object

Initialize Mongoblazer wit some options:

includes: relations to include
default_scope: the default scope for the blazed model
indexes: fields to index in the blazed model
embeds_one: model to embed one of
embeds_many: model to embed many of


286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/mongoblazer/active_record.rb', line 286

def mongoblazer_init(options)
  unless @mongoblazer_options
    @mongoblazer_options = {}

    @mongoblazer_options[:indexes] =
      ::ActiveRecord::Base.connection.indexes(self.table_name).map do |ind|
      {ind => 1}
    end

    @mongoblazer_options[:uploaders] = []

    @mongoblazer_options[:embeds_one]  = {}
    @mongoblazer_options[:embeds_many] = {}
  end

  if one = options.delete(:embeds_one)
    @mongoblazer_options[:embeds_one][one.keys.first] = one.values.first
  end

  if many = options.delete(:embeds_many)
    @mongoblazer_options[:embeds_many][many.keys.first] = many.values.first
  end

  if uploader = options.delete(:uploaders)
    @mongoblazer_options[:uploaders] << uploader
  end

  @mongoblazer_options.merge! options

  create_mongoblazer_class!
end

#mongoblazer_optionsObject



318
319
320
321
322
323
324
325
326
# File 'lib/mongoblazer/active_record.rb', line 318

def mongoblazer_options
  if defined?(@mongoblazer_options)
    @mongoblazer_options
  elsif superclass.respond_to?(:mongoblazer_options)
    superclass.mongoblazer_options || { }
  else
    { }
  end
end

#recreate_mongoblazer_class!(called_from_parent = false) ⇒ Object



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/mongoblazer/active_record.rb', line 336

def recreate_mongoblazer_class!(called_from_parent=false)
  create_mongoblazer_class!

  unless called_from_parent
    begin
      configuration[:embeds_one].each do |rel|
        "#{rel.to_s.camelize}".constantize.recreate_mongoblazer_class!(true)
      end
    rescue NameError
    end
    begin
      configuration[:embeds_many].each do |rel|
        "#{rel.to_s.singularize.camelize}".constantize.recreate_mongoblazer_class!(true)
      end
    rescue NameError
    end
  end
end