Module: ActiveRepository::Associations::Methods

Defined in:
lib/active_repository/associations.rb

Instance Method Summary collapse

Instance Method Details

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

Defines “belongs to” type relation between ActiveRepository objects



40
41
42
43
44
45
46
47
48
49
# File 'lib/active_repository/associations.rb', line 40

def belongs_to(association_id, options = {})
  options = {
    class_name:  association_id.to_s.classify,
    foreign_key: association_id.to_s.foreign_key
  }.merge(options)

  field options[:foreign_key].to_sym

  belongs_to_methods(association_id, options)
end

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

Defines “has many” type relation between ActiveRepository objects



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/active_repository/associations.rb', line 15

def has_many(association_id, options = {})
  define_method(association_id) do
    options = {
      class_name:  association_id.to_s.classify,
      foreign_key: self.class.to_s.foreign_key
    }.merge(options)

    foreign_key = options[:foreign_key]
    klass = options[:class_name].constantize

    klass.where(foreign_key => id)
  end
end

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

Defines “has one” type relation between ActiveRepository objects



30
31
32
33
34
35
36
37
# File 'lib/active_repository/associations.rb', line 30

def has_one(association_id, options = {})
  options = {
    class_name:  association_id.to_s.classify,
    foreign_key: self.to_s.foreign_key
  }.merge(options)

  has_one_methods(association_id, options)
end