module Mongoblazer::ActiveRecord::ClassMethods

Public Instance Methods

belongs_to(name, options={}) click to toggle source
Calls superclass method
# 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) click to toggle source
# 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={}) click to toggle source
Calls superclass method
# 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={}) click to toggle source
Calls superclass method
# 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={}) click to toggle source
Calls superclass method
# File lib/mongoblazer/active_record.rb, line 227
def has_one(name, options={})
  mongoblazer_init embeds_one: {name => options[:class_name]}
  super
end
mongoblazable?() click to toggle source

Is this model Mongoblazable?

# File lib/mongoblazer/active_record.rb, line 218
def mongoblazable?
  mongoblazer_options.present?
end
mongoblazer_additional_attributes(options={}) click to toggle source

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

# File lib/mongoblazer/active_record.rb, line 274
def mongoblazer_additional_attributes(options={})
  mongoblazer_init additional_attributes: options
end
mongoblazer_class() click to toggle source
# File lib/mongoblazer/active_record.rb, line 328
def mongoblazer_class
  mongoblazer_class_name.constantize
end
mongoblazer_class_name() click to toggle source
# File lib/mongoblazer/active_record.rb, line 332
def mongoblazer_class_name
  "#{self.name}Blazer"
end
mongoblazer_default_scope(options={}) click to toggle source

Defines default scope to find the matching mongodb document.

Syntax same as where() in AR

# File lib/mongoblazer/active_record.rb, line 259
def mongoblazer_default_scope(options={})
  mongoblazer_init default_scope: options
end
mongoblazer_includes(options={}) click to toggle source

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
# File lib/mongoblazer/active_record.rb, line 249
def mongoblazer_includes(options={})
  mongoblazer_init includes: options
end
mongoblazer_index_fields(options={}) click to toggle source

Defines indexes on the blazer model.

# File lib/mongoblazer/active_record.rb, line 266
def mongoblazer_index_fields(options={})
  mongoblazer_init indexes: options
end
mongoblazer_init(options) click to toggle source

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
# 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_options() click to toggle source
# 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) click to toggle source
# 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