Class: ActiveRecord::Associations::CollectionProxy

Inherits:
Object
  • Object
show all
Includes:
Hobo::Model::Scopes::ApplyScopes
Defined in:
lib/hobo/extensions/active_record/associations/proxy.rb,
lib/hobo/extensions/active_record/relation_with_origin.rb,
lib/hobo/extensions/active_record/associations/collection.rb

Overview

:nodoc:

Instance Method Summary collapse

Methods included from Hobo::Model::Scopes::ApplyScopes

#apply_scopes

Instance Method Details

#hobo_association_collection?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/hobo/extensions/active_record/associations/collection.rb', line 8

def hobo_association_collection?
  Hobo::Model.in?(proxy_association.owner.class.included_modules)
end

#is_a?(klass) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
# File 'lib/hobo/extensions/active_record/associations/collection.rb', line 28

def is_a?(klass)
  if has_one_collection?
    load_target
    proxy_association.target.is_a?(klass)
  else
    [].is_a?(klass)
  end
end

#member_classObject



37
38
39
# File 'lib/hobo/extensions/active_record/associations/collection.rb', line 37

def member_class
  proxy_association.reflection.klass
end

#method_missing_with_origin(method, *args, &block) ⇒ Object

FIXME Ralis4: really hoping that we can replace this with something based on github.com/rails/rails/issues/5717 def scoped_with_origin

relation = scoped_without_origin.clone
relation.origin = proxy_association.owner
relation.origin_attribute = proxy_association.reflection.name
relation

end alias_method_chain :scoped, :origin



37
38
39
40
41
42
# File 'lib/hobo/extensions/active_record/relation_with_origin.rb', line 37

def method_missing_with_origin(method, *args, &block)
  res = method_missing_without_origin(method, *args, &block)
  res.origin = proxy_association.owner if res.respond_to?(:origin)
  res.origin_attribute = proxy_association.reflection.name if res.respond_to?(:origin_attribute)
  res
end

#new_candidate(attributes = {}) ⇒ Object



13
14
15
16
17
18
# File 'lib/hobo/extensions/active_record/associations/collection.rb', line 13

def new_candidate(attributes = {})
  record = new
  proxy_association.target.delete record
  set_reverse_association(record) if hobo_association_collection?
  record
end

#originObject



4
5
6
# File 'lib/hobo/extensions/active_record/associations/proxy.rb', line 4

def origin
  proxy_association.owner
end

#origin_attributeObject



8
9
10
# File 'lib/hobo/extensions/active_record/associations/proxy.rb', line 8

def origin_attribute
  proxy_association.reflection.name
end

#user_new_candidate(user, attributes = {}) ⇒ Object



21
22
23
24
25
26
# File 'lib/hobo/extensions/active_record/associations/collection.rb', line 21

def user_new_candidate(user, attributes = {})
  record = user_new(user, attributes)
  proxy_association.target.delete record
  set_reverse_association(record) if hobo_association_collection?
  record
end