Class: LargeTree

Inherits:
Object
  • Object
show all
Includes:
JSONModel
Defined in:
backend/app/model/large_tree.rb

Constant Summary collapse

WAYPOINT_SIZE =
200

Constants included from JSONModel

JSONModel::REFERENCE_KEY_REGEX

Instance Method Summary collapse

Methods included from JSONModel

JSONModel, #JSONModel, add_error_handler, all, allow_unmapped_enum_value, backend_url, check_valid_refs, client_mode?, custom_validations, destroy_model, enum_default_value, enum_values, handle_error, init, load_schema, #models, models, parse_jsonmodel_ref, parse_reference, repository, repository_for, schema_src, set_publish_flags!, set_repository, strict_mode, strict_mode?, validate_schema, with_repository

Constructor Details

#initialize(root_record, opts = {}) ⇒ LargeTree

Returns a new instance of LargeTree.



59
60
61
62
63
64
65
66
67
68
# File 'backend/app/model/large_tree.rb', line 59

def initialize(root_record, opts = {})
  @decorators = []

  @root_record = root_record

  @root_table = @root_type = root_record.class.root_type.intern
  @node_table = @node_type = root_record.class.node_type.intern

  @published_only = opts.fetch(:published_only, false)
end

Instance Method Details

#add_decorator(decorator) ⇒ Object



70
71
72
# File 'backend/app/model/large_tree.rb', line 70

def add_decorator(decorator)
  @decorators << decorator
end

#node(node_record) ⇒ Object



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
137
138
# File 'backend/app/model/large_tree.rb', line 108

def node(node_record)
  DB.open do |db|
    child_count = db[@node_table]
                  .filter(:root_record_id => @root_record.id,
                          :parent_id => node_record.id)
                  .filter(published_filter)
                  .count

    my_position = node_record.position

    node_position = db[@node_table]
                    .filter(:root_record_id => @root_record.id,
                            :parent_id => node_record.parent_id)
                    .filter(published_filter)
                    .where { position < my_position }
                    .count + 1

    response = waypoint_response(child_count).merge("title" => node_record.display_string,
                                                    "uri" => node_record.uri,
                                                    "position" => node_position,
                                                    "jsonmodel_type" => @node_table.to_s)

    @decorators.each do |decorator|
      response = decorator.node(response, node_record)
    end

    precalculate_waypoints(response, node_record.id)

    response
  end
end

#node_from_root(node_ids, repo_id) ⇒ Object



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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'backend/app/model/large_tree.rb', line 140

def node_from_root(node_ids, repo_id)
  child_to_parent_map = {}
  node_to_position_map = {}
  node_to_root_record_map = {}
  node_to_title_map = {}

  result = {}

  DB.open do |db|
    ## Fetch our mappings of nodes to parents and nodes to positions
    nodes_to_expand = node_ids

    while !nodes_to_expand.empty?
      # Get the set of parents of the current level of nodes
      next_nodes_to_expand = []

      db[@node_table]
        .filter(:id => nodes_to_expand)
        .filter(published_filter)
        .select(:id, :parent_id, :root_record_id, :position, :display_string).each do |row|
        child_to_parent_map[row[:id]] = row[:parent_id]
        node_to_position_map[row[:id]] = row[:position]
        node_to_title_map[row[:id]] = row[:display_string]
        node_to_root_record_map[row[:id]] = row[:root_record_id]
        next_nodes_to_expand << row[:parent_id]
      end

      nodes_to_expand = next_nodes_to_expand.compact.uniq
    end

    ## Calculate the waypoint that each node will fall into
    node_to_waypoint_map = {}

    (child_to_parent_map.keys + child_to_parent_map.values).compact.uniq.each do |node_id|
      this_position = db[@node_type]
                      .filter(:parent_id => child_to_parent_map[node_id])
                      .filter(:root_record_id => node_to_root_record_map[node_id])
                      .filter(published_filter)
                      .where { position <= node_to_position_map[node_id] }
                      .count

      node_to_waypoint_map[node_id] = (this_position / WAYPOINT_SIZE)
    end

    root_record_titles = {}
    db[@root_table]
      .join(@node_table, :root_record_id => :id)
      .filter(Sequel.qualify(@node_table, :id) => node_ids)
      .select(Sequel.qualify(@root_table, :id),
              Sequel.qualify(@root_table, :title))
      .distinct
      .each do |row|
      root_record_titles[row[:id]] = row[:title]
    end

    ## Build up the path of waypoints for each node
    node_ids.each do |node_id|
      root_record_id = node_to_root_record_map.fetch(node_id)
      root_record_uri = JSONModel(@root_type).uri_for(root_record_id, :repo_id => repo_id)

      path = []

      current_node = node_id
      while child_to_parent_map[current_node]
        parent_node = child_to_parent_map[current_node]

        path << {"node" => JSONModel(@node_type).uri_for(parent_node, :repo_id => repo_id),
                 "root_record_uri" => root_record_uri,
                 "jsonmodel_type" => @node_type,
                 "title" => node_to_title_map.fetch(parent_node),
                 "offset" => node_to_waypoint_map.fetch(current_node),
                 "parsed_title" => MixedContentParser.parse(node_to_title_map.fetch(parent_node), '/')}

        current_node = parent_node
      end

      path << {"node" => nil,
               "root_record_uri" => root_record_uri,
               "offset" => node_to_waypoint_map.fetch(current_node),
               "jsonmodel_type" => @root_type,
               "title" => root_record_titles[root_record_id],
               "parsed_title" => MixedContentParser.parse(root_record_titles[root_record_id], '/')}

      result[node_id] = path.reverse
    end
  end

  result
end

#published_filterObject



74
75
76
77
78
79
80
81
# File 'backend/app/model/large_tree.rb', line 74

def published_filter
  filter = {}

  filter[:publish] = @published_only ? [1] : [0, 1]
  filter[:suppressed] = @published_only ? [0] : [0, 1]

  filter
end

#rootObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'backend/app/model/large_tree.rb', line 83

def root
  DB.open do |db|
    child_count = db[@node_table]
                  .filter(:root_record_id => @root_record.id,
                          :parent_id => nil)
                  .filter(published_filter)
                  .count


    # ANW-617: generate a slugged URL for inclusion in the JSON for the root node that's being returned to the LargeTree JS so it can be used in place of the URIs if needed.
    response = waypoint_response(child_count).merge("title" => @root_record.title,
                                                    "uri" => @root_record.uri,
                                                    "slugged_url" => SlugHelpers.get_slugged_url_for_largetree(@root_record.class.to_s, @root_record.repo_id, @root_record.slug),
                                                    "jsonmodel_type" => @root_table.to_s,
                                                    "parsed_title" => MixedContentParser.parse(@root_record.title, '/'))
    @decorators.each do |decorator|
      response = decorator.root(response, @root_record)
    end

    precalculate_waypoints(response, nil)

    response
  end
end

#waypoint(parent_id, offset) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'backend/app/model/large_tree.rb', line 231

def waypoint(parent_id, offset)
  record_ids = []
  records = {}

  DB.open do |db|

    # ANW-617: We need to grab the slug field from the database so we can use it to generate a slugged URL later.
    db[@node_table]
      .filter(:root_record_id => @root_record.id,
              :parent_id => parent_id)
      .filter(published_filter)
      .order(:position)
      .select(:id, :repo_id, :title, :position, :slug)
      .offset(offset * WAYPOINT_SIZE)
      .limit(WAYPOINT_SIZE)
      .each do |row|
        record_ids << row[:id]
        records[row[:id]] = row
      end

    # Count up their children
    child_counts = Hash[db[@node_table]
                         .filter(:root_record_id => @root_record.id,
                                 :parent_id => records.keys)
                         .filter(published_filter)
                         .group_and_count(:parent_id)
                         .map {|row| [row[:parent_id], row[:count]]}]

    response = record_ids.each_with_index.map do |id, idx|
      row = records[id]
      child_count = child_counts.fetch(id, 0)

      # ANW-617: generate a slugged URL for inclusion in the JSON for the standard node that's being returned to the LargeTree JS so it can be used in place of the URIs if needed.
      waypoint_response(child_count).merge("title" => row[:title],
                                           "slugged_url" => SlugHelpers.get_slugged_url_for_largetree(@node_type.to_s, row[:repo_id], row[:slug]),
                                           "parsed_title" => MixedContentParser.parse(row[:title], '/'),
                                           "uri" => JSONModel(@node_type).uri_for(row[:id], :repo_id => row[:repo_id]),
                                           "position" => (offset * WAYPOINT_SIZE) + idx,
                                           "parent_id" => parent_id,
                                           "jsonmodel_type" => @node_type.to_s)

    end

    @decorators.each do |decorator|
      response = decorator.waypoint(response, record_ids)
    end

    response
  end
end