Module: GarlandRails::Extend

Defined in:
lib/garland_rails/extend.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
# File 'lib/garland_rails/extend.rb', line 3

def self.included(base)
  base.extend(self)
end

Instance Method Details

#has_many(name, scope = nil, options = {}, &extension) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/garland_rails/extend.rb', line 7

def has_many(name, scope = nil, options = {}, &extension)
  name_superclass = name.to_s.classify.constantize.superclass
  if name_superclass == GarlandRails::Base
    # if there are no scope and there are some options,
    # scope will contain options, and we need to swap them
    if scope.class == Hash
      options = scope
      scope = nil
    end

    belongs_to_type = self.name
    scope = -> { where(belongs_to_type: belongs_to_type) }
    options = options.merge(foreign_key: "belongs_to_id")
  end

  super(name, scope, options, &extension)
end