Module: RestrictionCalculator

Defined in:
backend/app/model/mixins/restriction_calculator.rb

Defined Under Namespace

Modules: ClassMethods, Implementation

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
# File 'backend/app/model/mixins/restriction_calculator.rb', line 3

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#active_restrictions(clock = Date) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'backend/app/model/mixins/restriction_calculator.rb', line 30

def active_restrictions(clock = Date)
  now = clock.today

  restrictions.select {|restriction|
    if restriction.begin && now < restriction.begin
      false
    elsif restriction.end && now > restriction.end
      false
    elsif restriction.rights_restriction_type.empty? && restriction.begin.nil? && restriction.end.nil?
      false
    else
      true
    end
  }
end

#restrictionsObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'backend/app/model/mixins/restriction_calculator.rb', line 8

def restrictions
  models_supporting_rights_restrictions = RightsRestriction.applicable_models.values

  models_supporting_rights_restrictions.map {|model|
    instance_link_column = model.association_reflection(:instance)[:key]

    id_set = TopContainer.linked_instance_ds.
             filter(:top_container__id => self.id).
             where { Sequel.~(instance_link_column => nil) }.
             select(instance_link_column).
             map {|row| row[instance_link_column]}

    model_to_record_ids = Implementation.expand_to_tree(model, id_set)

    model_to_record_ids.map {|restriction_model, restriction_ids|
      restriction_link_column = restriction_model.association_reflection(:rights_restriction)[:key]
      RightsRestriction.filter(restriction_link_column => restriction_ids).all
    }
  }.flatten.uniq(&:id)
end