Class: ThroughHierarchy::Hierarchy
- Inherits:
- BasicObject
- Defined in:
- lib/through_hierarchy/hierarchy.rb
Instance Method Summary collapse
- #has_many(name, scope = nil, **options) ⇒ Object
- #has_one(name, scope = nil, **options) ⇒ Object
-
#initialize(klass, members) ⇒ Hierarchy
constructor
A new instance of Hierarchy.
Constructor Details
#initialize(klass, members) ⇒ Hierarchy
Returns a new instance of Hierarchy.
3 4 5 6 7 8 |
# File 'lib/through_hierarchy/hierarchy.rb', line 3 def initialize(klass, members) @klass = klass @members = members validate_hierarchy end |
Instance Method Details
#has_many(name, scope = nil, **options) ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/through_hierarchy/hierarchy.rb', line 16 def has_many(name, scope = nil, **) .merge!(scope: scope) if scope.present? if .key?(:uniq) assoc = ::ThroughHierarchy::Associations::HasUniq.new(name, @klass, @members, ) else assoc = ::ThroughHierarchy::Associations::HasMany.new(name, @klass, @members, ) end ::ThroughHierarchy::Builder.new(@klass).add_association(name, assoc) end |
#has_one(name, scope = nil, **options) ⇒ Object
10 11 12 13 14 |
# File 'lib/through_hierarchy/hierarchy.rb', line 10 def has_one(name, scope = nil, **) .merge!(scope: scope) if scope.present? assoc = ::ThroughHierarchy::Associations::HasOne.new(name, @klass, @members, ) ::ThroughHierarchy::Builder.new(@klass).add_association(name, assoc) end |