Module: Shared::Containable
- Extended by:
- ActiveSupport::Concern
- Included in:
- AnatomicalPart, CollectionObject, Container, Extract
- Defined in:
- app/models/concerns/shared/containable.rb
Overview
Shared code for objects that maybe be placed in Container(s).
Class Method Summary collapse
-
.containable_types ⇒ Object
(Here instead of in an initializer so that it works with spec models too.).
Instance Method Summary collapse
-
#contain ⇒ Object
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.
-
#containable? ⇒ True
This instance is containable.
-
#contained? ⇒ Boolean
return [Boolean] whether this item is contained in something else.
-
#contained_by?(kontainer) ⇒ Boolean
return [Boolean] whether this object is contained by the passed container.
-
#contained_siblings ⇒ Object
Array.
-
#enclosing_containers ⇒ Array
Return all Containers containing this container.
-
#put_in_container(kontainer) ⇒ Boolean
True if item is placed in container, false if not.
Class Method Details
.containable_types ⇒ Object
(Here instead of in an initializer so that it works with spec models too.)
7 8 9 10 11 12 13 |
# File 'app/models/concerns/shared/containable.rb', line 7 def self.containable_types ApplicationRecord .descendants .select { |m| m < Shared::Containable } .map { |m| m.base_class.name } .uniq end |
Instance Method Details
#contain ⇒ Object
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
30 31 32 33 34 35 36 37 38 39 |
# File 'app/models/concerns/shared/containable.rb', line 30 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 |
#containable? ⇒ True
Returns this instance is containable.
62 63 64 |
# File 'app/models/concerns/shared/containable.rb', line 62 def containable? true end |
#contained? ⇒ Boolean
return [Boolean]
whether this item is contained in something else
68 69 70 |
# File 'app/models/concerns/shared/containable.rb', line 68 def contained? !container.nil? end |
#contained_by?(kontainer) ⇒ Boolean
return [Boolean]
whether this object is contained by the passed container
74 75 76 |
# File 'app/models/concerns/shared/containable.rb', line 74 def contained_by?(kontainer) enclosing_containers.include?(kontainer) end |
#contained_siblings ⇒ Object
Returns Array.
42 43 44 |
# File 'app/models/concerns/shared/containable.rb', line 42 def contained_siblings reload_container_item&.siblings&.map(&:contained_object) || [] end |
#enclosing_containers ⇒ Array
Return all Containers containing this container
55 56 57 58 |
# File 'app/models/concerns/shared/containable.rb', line 55 def enclosing_containers return [] if !contained? container_item.ancestors.map(&:contained_object) end |
#put_in_container(kontainer) ⇒ Boolean
Returns true if item is placed in container, false if not.
48 49 50 51 |
# File 'app/models/concerns/shared/containable.rb', line 48 def put_in_container(kontainer) return false if self.new_record? || kontainer.new_record? kontainer.add_container_items([self]) end |