Module: Shared::Containable

Extended by:
ActiveSupport::Concern
Included in:
CollectionObject, Container
Defined in:
app/models/concerns/shared/containable.rb

Overview

Shared code for objects that maybe be placed in Container(s).

Instance Method Summary (collapse)

Instance Method Details

- (Object) contain

what has been put in contained_in might be a container, or the id of a container: convert an id to a container, and put self into that container



20
21
22
23
24
25
26
27
28
29
# File 'app/models/concerns/shared/containable.rb', line 20

def contain
  c = nil
  if contained_in.is_a?(Container)
    c = contained_in
  else
    c = Container.find(contained_in)
  end

  put_in_container(c)
end

- (True) containable?

Returns this instance is containable

Returns:

  • (True)

    this instance is containable



47
48
49
# File 'app/models/concerns/shared/containable.rb', line 47

def containable?
  true
end

- (Boolean) contained?

return [Boolean]

whether this item is contained in something else

Returns:

  • (Boolean)


53
54
55
# File 'app/models/concerns/shared/containable.rb', line 53

def contained?
  !container.nil?
end

- (Boolean) contained_by?(kontainer)

return [Boolean]

whether this object is contained by the passed container

Returns:

  • (Boolean)


59
60
61
# File 'app/models/concerns/shared/containable.rb', line 59

def contained_by?(kontainer)
  enclosing_containers.include?(kontainer)
end

- (Array) enclosing_containers

Return all Containers containing this container

Returns:

  • (Array)

    return all Containers containing this container



40
41
42
43
# File 'app/models/concerns/shared/containable.rb', line 40

def enclosing_containers
  return [] if !contained?
  container_item.ancestors.map(&:contained_object)
end

- (Boolean) put_in_container(kontainer)

Returns true if item is placed in container, false if not

Returns:

  • (Boolean)

    true if item is placed in container, false if not



33
34
35
36
# File 'app/models/concerns/shared/containable.rb', line 33

def put_in_container(kontainer)
  return false if self.new_record? || kontainer.new_record?
  kontainer.add_container_items([self])
end