Class: AssessmentAttributeDefinitions

Inherits:
JSONModel
  • Object
show all
Extended by:
JSONModel
Defined in:
backend/app/model/assessment_attribute_definitions.rb,
frontend/app/models/assessment_attribute_definitions.rb

Constant Summary

Constants included from JSONModel

JSONModel::REFERENCE_KEY_REGEX

Class Method Summary collapse

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

Class Method Details

.apply_definitions(repo_id, json) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
# File 'backend/app/model/assessment_attribute_definitions.rb', line 5

def self.apply_definitions(repo_id, json)
  # Global definitions are immutable, so drop them out here.
  definitions = json.definitions.reject {|d| d['global']}
  definitions.each do |d| d.delete('global'); end

  DB.open do |db|
    # Deleted things are deleted!  That is, IDs that aren't in our new list
    begin
      db[:assessment_attribute_definition]
        .filter(:repo_id => repo_id)
        .where { Sequel.~(:id => definitions.map {|d| d['id']}.compact) }
        .delete
    rescue Sequel::ForeignKeyConstraintViolation
      raise ConflictException.new("RECORD_IN_USE")
    end


    # Don't allow a label to be set if it would conflict with a label used by one of the global attributes
    definitions.each do |d|
      conflicting_labels = db[:assessment_attribute_definition]
                             .filter(:repo_id => 1, :type => d['type'], :label => d['label'])
                             .select(:label)
                             .all

      unless conflicting_labels.empty?
        raise ConflictException.new(conflicting_labels.map {|row| row[:label]})
      end

      conflicting_repo_labels = db[:assessment_attribute_definition]
                                  .filter(:repo_id => repo_id,
                                          :type => d['type'],
                                          :label => d['label'])
                                  .where { Sequel.~(:id => d['id']) }
                                  .select(:label)
                                  .all

      unless conflicting_repo_labels.empty?
        raise ConflictException.new(conflicting_repo_labels.map {|row| row[:label]})
      end
    end

    # Existing definitions get updated
    definitions.each_with_index do |definition, position|
      next unless definition['id']

      db[:assessment_attribute_definition]
        .filter(:repo_id => repo_id, :id => definition['id'])
        .update(definition.merge('position' => position,
                                 'readonly' => (definition['readonly'] ? 1 : 0)))
    end

    # New definitions are inserted
    seen_labels = {}

    definitions.each_with_index do |definition, position|
      next if definition['id'] || seen_labels[definition['label']]

      seen_labels[definition['label']] = true
      db[:assessment_attribute_definition].insert(definition.merge('repo_id' => repo_id,
                                                                   'position' => position,
                                                                   'readonly' => (definition['readonly'] ? 1 : 0)))
    end
  end
end

.get(repo_id) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'backend/app/model/assessment_attribute_definitions.rb', line 70

def self.get(repo_id)
  result = JSONModel(:assessment_attribute_definitions).new

  logical_position_by_repo_and_type = {}

  DB.open do |db|
    db[:assessment_attribute_definition]
      .filter(:repo_id => [repo_id, Repository.global_repo_id])
      .order(:repo_id, :position, :id).each do |definition|

      logical_position_by_repo_and_type[definition[:repo_id]] ||= {}
      logical_position_by_repo_and_type[definition[:repo_id]][definition[:type]] ||= 0

      result.definitions << {
        :id => definition[:id],
        :label => definition[:label],
        :type => definition[:type],
        :global => (definition[:repo_id] == Repository.global_repo_id),
        :readonly => (definition[:readonly] == 1),
        :position => logical_position_by_repo_and_type[definition[:repo_id]][definition[:type]],
      }

      logical_position_by_repo_and_type[definition[:repo_id]][definition[:type]] += 1
    end
  end

  result
end

Instance Method Details

#global_conservation_issuesObject



23
24
25
# File 'frontend/app/models/assessment_attribute_definitions.rb', line 23

def global_conservation_issues
  attributes_for_type('conservation_issue', true)
end

#global_formatsObject



15
16
17
# File 'frontend/app/models/assessment_attribute_definitions.rb', line 15

def global_formats
  attributes_for_type('format', true)
end

#global_ratingsObject



19
20
21
# File 'frontend/app/models/assessment_attribute_definitions.rb', line 19

def global_ratings
  attributes_for_type('rating', true)
end

#label_for_id(id) ⇒ Object



39
40
41
42
43
44
# File 'frontend/app/models/assessment_attribute_definitions.rb', line 39

def label_for_id(id)
  attribute = definitions.find {|d| d['id'] == id}
  return "UKNOWN" if attribute.nil?

  attribute.fetch('label')
end

#repo_conservation_issuesObject



11
12
13
# File 'frontend/app/models/assessment_attribute_definitions.rb', line 11

def repo_conservation_issues
  attributes_for_type('conservation_issue', false)
end

#repo_conservation_issues=(conservation_issues) ⇒ Object



35
36
37
# File 'frontend/app/models/assessment_attribute_definitions.rb', line 35

def repo_conservation_issues=(conservation_issues)
  set_repo_attributes_for_type('conservation_issue', conservation_issues)
end

#repo_formatsObject



3
4
5
# File 'frontend/app/models/assessment_attribute_definitions.rb', line 3

def repo_formats
  attributes_for_type('format', false)
end

#repo_formats=(formats) ⇒ Object



27
28
29
# File 'frontend/app/models/assessment_attribute_definitions.rb', line 27

def repo_formats=(formats)
  set_repo_attributes_for_type('format', formats)
end

#repo_ratingsObject



7
8
9
# File 'frontend/app/models/assessment_attribute_definitions.rb', line 7

def repo_ratings
  attributes_for_type('rating', false)
end

#repo_ratings=(ratings) ⇒ Object



31
32
33
# File 'frontend/app/models/assessment_attribute_definitions.rb', line 31

def repo_ratings=(ratings)
  set_repo_attributes_for_type('rating', ratings)
end