Module: MongoidArAssociation::ClassMethods

Defined in:
lib/mongoid_ar_association.rb

Instance Method Summary collapse

Instance Method Details

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

Connection methods from mysql to mongoid



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/mongoid_ar_association.rb', line 77

def belongs_to_document(name, options = {})
  object_class = options[:class_name].constantize || name.to_s.titleize.delete(' ').constantize
  primary_key = options[:primary_key] || self.class.primary_key
  foreign_key = options[:foreign_key] || "#{name}_id"
  self.instance_eval do
    define_method(name) do |reload = false|
      if reload
        self.instance_variable_set("@#{name}", nil)
      end

      if self.instance_variable_get("@#{name}").blank?
        self.instance_variable_set("@#{name}", object_class.where(primary_key => self.send(foreign_key)).first)
      end

      self.instance_variable_get("@#{name}")
    end

    define_method("#{name}=(new_instance)") do
      self.send("#{name}_id=", new_instance.id)
      self.instance_variable_set("@#{name}", nil)
    end
  end
end

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

Connection methods from mongoid to mysql



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mongoid_ar_association.rb', line 12

def belongs_to_record(name, options = {})
  field "#{name}_id", type: Integer
  object_class = options[:class_name].constantize || name.to_s.titleize.delete(' ').constantize
  primary_key = options[:primary_key] || object_class.primary_key
  foreign_key = options[:foreign_key] || "#{name}_id"
  self.instance_eval do
    define_method(name) do |reload = false|
      if reload
        self.instance_variable_set("@#{name}", nil)
      end

      if self.instance_variable_get("@#{name}").blank?
        self.instance_variable_set("@#{name}", object_class.where(primary_key => self.send(foreign_key)).first)
      end

      self.instance_variable_get("@#{name}")
    end

    define_method("#{name}=(new_instance)") do
      self.send("#{name}_id=", new_instance.id)
      self.instance_variable_set("@#{name}", nil)
    end
  end
end

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



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/mongoid_ar_association.rb', line 101

def has_many_documents(name, options = {})
  plural_name = name.to_s.pluralize
  foreign_key = options[:foreign_key] || "#{self.name.underscore}_id"
  object_class = options[:class_name].constantize || name.to_s.singularize.titleize.delete(' ').constantize
  primary_key = options[:primary_key] || self.class.primary_key
  self.instance_eval do
    define_method(plural_name) do |reload = false|
      if reload
        self.instance_variable_set("@#{name}", nil)
      end

      if self.instance_variable_get("@#{name}").blank?
        self.instance_variable_set("@#{name}", object_class.where(foreign_key => self.send(primary_key)))
      end

      self.instance_variable_get("@#{name}")
    end
  end
end

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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/mongoid_ar_association.rb', line 37

def has_many_records(name, options = {})
  plural_name = name.to_s.pluralize
  foreign_key = options[:foreign_key] || "#{self.name.underscore}_id"
  object_class = options[:class_name].constantize || name.to_s.singularize.titleize.delete(' ').constantize
  primary_key = options[:primary_key] || 'id'
  self.instance_eval do
    define_method(plural_name) do |reload = false|
      if reload
        self.instance_variable_set("@#{name}", nil)
      end

      if self.instance_variable_get("@#{name}").blank?
        self.instance_variable_set("@#{name}", object_class.where(foreign_key => self.send(primary_key).to_s))
      end

      self.instance_variable_get("@#{name}")
    end
  end
end

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



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/mongoid_ar_association.rb', line 121

def has_one_document(name, options = {})
  foreign_key = options[:foreign_key] || "#{self.name.underscore}_id"
  object_class = options[:class_name].constantize || name.to_s.titleize.delete(' ').constantize
  primary_key = options[:primary_key] || self.class.primary_key
  self.instance_eval do
    define_method(name) do |reload = false|
      if reload
        self.instance_variable_set("@#{name}", nil)
      end

      if self.instance_variable_get("@#{name}").blank?
        self.instance_variable_set("@#{name}", object_class.where(foreign_key => self.send(primary_key)).first)
      end

      self.instance_variable_get("@#{name}")
    end
  end
end

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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/mongoid_ar_association.rb', line 57

def has_one_record(name, options = {})
  foreign_key = options[:foreign_key] || "#{self.name.underscore}_id"
  object_class = options[:class_name].constantize || name.to_s.titleize.delete(' ').constantize
  primary_key = options[:primary_key] || 'id'
  self.instance_eval do
    define_method(name) do |reload = false|
      if reload
        self.instance_variable_set("@#{name}", nil)
      end

      if self.instance_variable_get("@#{name}").blank?
        self.instance_variable_set("@#{name}", object_class.where(foreign_key => self.send(primary_key).to_s).first)
      end

      self.instance_variable_get("@#{name}")
    end
  end
end