Module: ActiveRecordEx::ManyToMany::ClassMethods

Defined in:
lib/active-record-ex/many_to_many.rb

Instance Method Summary collapse

Instance Method Details

#belongs_to(name, scope = nil, options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/active-record-ex/many_to_many.rb', line 37

def belongs_to(name, scope = nil, options = {})
  scope, options = set_scope_and_options(scope, options)
  subtypes = options.delete(:subtypes)
  if ActiveRecord::VERSION::MAJOR < 4
    super(name, options)
  else
    super
  end
  define_belongs_assoc(name, options.merge(subtypes: subtypes))
end

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



48
49
50
51
52
53
54
55
56
# File 'lib/active-record-ex/many_to_many.rb', line 48

def has_many(name, scope = nil, options = {}, &extension)
  scope, options = set_scope_and_options(scope, options)
  if ActiveRecord::VERSION::MAJOR < 4
    super(name, options, &extension)
  else
    super
  end
  define_has_assoc(name.to_s.singularize, options)
end

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



58
59
60
61
62
63
64
65
66
# File 'lib/active-record-ex/many_to_many.rb', line 58

def has_one(name, scope = nil, options = {}, &extension)
  scope, options = set_scope_and_options(scope, options)
  if ActiveRecord::VERSION::MAJOR < 4
    super(name, options, &extension)
  else
    super
  end
  define_has_assoc(name.to_s, options)
end

#set_scope_and_options(scope, options) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/active-record-ex/many_to_many.rb', line 29

def set_scope_and_options(scope, options)
  if scope.is_a?(Hash)
    options = scope
    scope   = nil
  end
  [scope, options]
end

#singularize(method_name) ⇒ Object



68
69
70
71
72
# File 'lib/active-record-ex/many_to_many.rb', line 68

def singularize(method_name)
  define_method(method_name) do |*params|
    ModelArel.new(self).send(method_name, *params)
  end
end