Module: ContentfulModel::Associations::BelongsToMany::ClassMethods

Defined in:
lib/contentful_model/associations/belongs_to_many.rb

Instance Method Summary collapse

Instance Method Details

#belongs_to_many(association_names, opts = {}) ⇒ Object

Parameters:

  • association_names (Symbol)

    plural name of the class we need to search through, to find this class

  • options (true, Hash)

    options



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/contentful_model/associations/belongs_to_many.rb', line 22

def belongs_to_many(association_names, opts = {})
  default_options = {
    class_name: association_names.to_s.singularize.classify,
    inverse_of: self.to_s.underscore.to_sym
  }
  options = default_options.merge(opts)
  # Set up the association name for the instance which loaded this object
  # This is useful in situations where, primarily, it's a 1:* relationship (i.e. belongs_to)
  # even though this isn't actually supported by Contentful
  #
  # f = Foo.first
  # b = f.bars.first
  # b.foo #would return the instance of Foo which loaded it

  define_method association_names.to_s.singularize do
    instance_variable_get(:"@#{association_names.to_s.singularize}")
  end

  define_method "#{association_names.to_s.singularize}=" do |parent|
    instance_variable_set(:"@#{association_names.to_s.singularize}",parent)
    return self
  end

  # Set up the association name (plural)
  if self.respond_to?(association_names)
    self.send(association_names)
  else
    define_method "#{association_names}" do
      parents = instance_variable_get(:"@#{association_names}")
      if parents.nil?
        #get the parent class objects as an array
        parent_objects = options[:class_name].constantize.send(:all).send(:load)
        #iterate through parent objects and see if any of the children include the same ID as the method
        parents = parent_objects.select do |parent_object|
          #check to see if the parent object responds to the plural or singular.
          if parent_object.respond_to?(:"#{options[:inverse_of].to_s.pluralize}")
            collection_of_children_on_parent = parent_object.send(:"#{options[:inverse_of].to_s.pluralize}")
            #get the collection of children from the parent. This *might* be nil if the parent doesn't have
            # any children, in which case, just skip over this parent item and move on to the next.
            if collection_of_children_on_parent.nil?
              next
            else
              collection_of_children_on_parent.collect(&:id).include?(id)
            end
          else
            #if it doesn't respond to the plural, assume singular
            child_on_parent = parent_object.send(:"#{options[:inverse_of]}")
            # Do the same skipping routine on nil.
            if child_on_parent.nil?
              next
            else
              child_on_parent.send(:id) == id
            end

          end
        end
        instance_variable_set(:"@#{association_names}",parents)
      end
      parents
    end
  end
end