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



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

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
# 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
    }.merge(options)

    foreign_key = self.class.to_s.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



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

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

  has_one_methods(association_id, options)
end