Class: ImpliedPublicationCalculator

Inherits:
Object
  • Object
show all
Defined in:
backend/app/model/implied_publication_calculator.rb

Overview

All of the for_* methods in this class take an array of Sequel model objects and return a hash indicating whether each object can be considered to be published by implication.

For example, you give it:

[subject1, subject2, subject3]

and it returns:

{ subject1 => true, subject2 => false, subject3 => false }

meaning that subject1 was linked to at least one published record, while subjects 2 and 3 were not (maybe they’re not linked to anything, or maybe only to unpublished/suppressed records).

Instance Method Summary collapse

Instance Method Details

#for_agents(agents) ⇒ Object

An agent is published if it’s linked to at least one archival record that’s published.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'backend/app/model/implied_publication_calculator.rb', line 81

def for_agents(agents)
  return {} if agents.empty?
  assert_same_type!(agents)

  agent_model = agents[0].class
  result = Hash[agents.map {|node| [node, false]}]
  agents_by_id = Hash[agents.map {|agent| [agent.id, agent]}]

  agent_model.relationship_dependencies[:linked_agents].each do |model|

    # Things like events aren't publishable and shouldn't count for these calculations
    next unless model.included_modules.include?(Publishable)

    link_relationship = model.find_relationship(:linked_agents)

    # agent_person_rlshp
    link_table = link_relationship.table_name

    link_relationship.reference_columns_for(model).each do |model_link_column|
      link_relationship.reference_columns_for(agent_model).each do |agent_link_column|
        linked_records = model
                           .join(link_table,
                                 Sequel.qualify(link_table, model_link_column) => Sequel.qualify(model.table_name, :id))
                           .filter(Sequel.qualify(link_table, agent_link_column) => agents_by_id.keys)
                           .select(Sequel.qualify(model.table_name, :id),
                                   Sequel.qualify(model.table_name, :publish),
                                   Sequel.qualify(model.table_name, :suppressed),
                                   Sequel.as(Sequel.qualify(link_table, agent_link_column),
                                             :agent_id))

        if model.columns.include?(:repo_id)
          linked_records = linked_records
                             .select_append(Sequel.as(Sequel.qualify(model.table_name, :repo_id),
                                                      :repository_id))
        end

        published_status = if model.included_modules.include?(TreeNodes)
                             for_tree_nodes(linked_records
                                              .select_append(Sequel.qualify(model.table_name, :parent_id),
                                                             Sequel.qualify(model.table_name, :root_record_id))
                                              .all)
                           else
                             for_top_level_records(linked_records.all)
                           end

        published_status.each do |linked_record, published|
          if published
            result[agents_by_id.fetch(linked_record[:agent_id])] = true
          end
        end
      end
    end
  end

  result
end

#for_subjects(subjects) ⇒ Object

An subject is published if it’s linked to at least one archival record or agent record that’s published.



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'backend/app/model/implied_publication_calculator.rb', line 140

def for_subjects(subjects)
  result = Hash[subjects.map {|node| [node, false]}]
  subjects_by_id = Hash[subjects.map {|subject| [subject.id, subject]}]
  subject_rlshp_sources = Hash.new { |h, k| h[k] = [] }
  [:subject, :subject_agent_subrecord, :subject_agent_subrecord_place].each do |t|
    Subject.relationship_dependencies[t].each { |d| subject_rlshp_sources[d] << t }
  end

  subject_rlshp_sources.each do |model, sources|
    sources.each do |source|
      # i.e. one or more: subject_rlshp, subject_agent_subrecord_rlshp, subject_agent_subrecord_place_rlshp
      link_relationship = model.find_relationship(source)
      link_table = link_relationship.table_name

      link_relationship.reference_columns_for(model).each do |model_link_column|
        link_relationship.reference_columns_for(Subject).each do |subject_link_column|
          # Join subject_rlshp to (e.g.) accession
          linked_records = model
                            .join(link_table,
                                  Sequel.qualify(link_table, model_link_column) => Sequel.qualify(model.table_name, :id))
                            .filter(Sequel.qualify(link_table, subject_link_column) => subjects_by_id.keys)
                            .select(Sequel.qualify(model.table_name, :id),
                                    Sequel.qualify(model.table_name, :publish),
                                    Sequel.qualify(model.table_name, :suppressed),
                                    Sequel.as(Sequel.qualify(link_table, subject_link_column),
                                              :subject_id))

          if model.columns.include?(:repo_id)
            linked_records = linked_records
                              .select_append(Sequel.as(Sequel.qualify(model.table_name, :repo_id),
                                                        :repository_id))
          end

          published_status = if model.included_modules.include?(TreeNodes)
                               for_tree_nodes(linked_records
                                                           .select_append(Sequel.qualify(model.table_name, :parent_id),
                                                                         Sequel.qualify(model.table_name, :root_record_id))
                                                           .all)
                             elsif model.to_s =~ /^Agent/
                               for_agents_linked_to_subject(linked_records.all)
                             else
                               for_top_level_records(linked_records.all)
                             end

          published_status.each do |linked_record, published|
            if published
              result[subjects_by_id.fetch(linked_record[:subject_id])] = true
            end
          end
        end
      end
    end
  end

  result
end

#for_top_containers(top_containers) ⇒ Object

A top container is published if it’s linked (via an instance -> sub_container) to at least one published record



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
# File 'backend/app/model/implied_publication_calculator.rb', line 26

def for_top_containers(top_containers)
  top_containers_by_id = Hash[top_containers.map {|top_container| [top_container.id, top_container]}]
  result = Hash[top_containers.map {|top_container| [top_container, false]}]

  Instance.enclosing_associations.each do |association|
    model = association[:model]
    model_table = model.table_name

    top_container_link = SubContainer.find_relationship(:top_container_link)

    # Join our (e.g.) Accession to an Instance, then Instance to SubContainer,
    # then SubContainer to TopContainer.
    joined_ds = model
                  .join(:instance, association[:key] => Sequel.qualify(model_table, :id))
                  .join(:sub_container, :instance_id => Sequel.qualify(:instance, :id))
                  .join(top_container_link.table_name, :sub_container_id => Sequel.qualify(:sub_container, :id))
                  .join(:top_container, Sequel.qualify(:top_container, :id) => Sequel.qualify(top_container_link.table_name, :top_container_id))
                  .filter(Sequel.qualify(:top_container, :id) => top_containers_by_id.keys)

    linked_records = if model.included_modules.include?(TreeNodes)
                       for_tree_nodes(joined_ds
                                        .select(Sequel.qualify(model_table, :id),
                                                Sequel.qualify(model_table, :parent_id),
                                                Sequel.qualify(model_table, :root_record_id),
                                                Sequel.qualify(model_table, :publish),
                                                Sequel.qualify(model_table, :suppressed),
                                                Sequel.as(Sequel.qualify(model_table, :repo_id),
                                                          :repository_id),
                                                Sequel.as(Sequel.qualify(:top_container, :id),
                                                          :top_container_id))
                                        .all)
                     else
                       for_top_level_records(joined_ds
                                               .select(Sequel.qualify(model_table, :id),
                                                       Sequel.qualify(model_table, :publish),
                                                       Sequel.qualify(model_table, :suppressed),
                                                       Sequel.as(Sequel.qualify(model_table, :repo_id),
                                                                 :repository_id),
                                                       Sequel.as(Sequel.qualify(:top_container, :id),
                                                                 :top_container_id))
                                               .all)
                     end

    linked_records.each do |linked_record, published|
      if published
        result[top_containers_by_id.fetch(linked_record[:top_container_id])] = published
      end
    end
  end

  result
end