Module: Consort::Mongoid::ActiveRecord::ClassMethods

Defined in:
lib/consort/mongoid/active_record.rb

Instance Method Summary collapse

Instance Method Details

#belongs_to_active_record(klass) ⇒ Object

Defines a belongs_to relationship with an ActiveRecord object. An appropriate foreign key field must exist on your model.

Examples:

class Narwhal
  include Mongoid::Document

  belongs_to_active_record :pod
  field :pod_id, type: Integer
end

Parameters:

  • klass (Symbol)


48
49
50
51
52
53
54
# File 'lib/consort/mongoid/active_record.rb', line 48

def belongs_to_active_record(klass)
  class_eval "    def \#{klass}\n      \#{klass.to_s.classify}.where(id: \#{klass.to_s.foreign_key})\n    end\n  CODE\nend\n"

#has_many_active_record(klass) ⇒ Object

Defines a has_many relationship with an ActiveRecord object.

Examples:

has_many_active_record :unicorns

Parameters:

  • klass (Symbol)

Since:

  • 0.0.2



24
25
26
27
28
29
30
# File 'lib/consort/mongoid/active_record.rb', line 24

def has_many_active_record(klass)
  class_eval "    def \#{klass}\n      \#{klass.to_s.classify}.where(\#{name.foreign_key}: id)\n    end\n  CODE\nend\n"

#has_many_active_records(klass) ⇒ Object

Deprecated.

Use #has_many_active_record instead. Will be removed in 1.0.0.



33
34
35
36
# File 'lib/consort/mongoid/active_record.rb', line 33

def has_many_active_records(klass)
  ActiveSupport::Deprecation.warn 'Please use the singular has_many_active_record instead.'
  has_many_active_record(klass)
end

#has_one_active_record(klass) ⇒ Object

Defines a has_one relationship with an ActiveRecord object.

Examples:

has_one_active_record :dolphin

Parameters:

  • klass (Symbol)


11
12
13
14
15
16
17
# File 'lib/consort/mongoid/active_record.rb', line 11

def has_one_active_record(klass)
  class_eval "    def \#{klass}\n      \#{klass.to_s.classify}.where(\#{name.foreign_key}: id)\n    end\n  CODE\nend\n"

#mongoid_consorts_with_active_record?Boolean

Allows easy validation of whether Mongoid to ActiveRecord bridge is loaded.

Returns:

  • (Boolean)

    true if bridge is loaded



58
59
60
# File 'lib/consort/mongoid/active_record.rb', line 58

def mongoid_consorts_with_active_record?
  true
end