Class: RequiredFields

Inherits:
Sequel::Model
  • Object
show all
Includes:
ASModel
Defined in:
backend/app/model/required_fields.rb,
frontend/app/models/required_fields.rb

Constant Summary

Constants included from JSONModel

JSONModel::REFERENCE_KEY_REGEX

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ASModel

all_models, included, update_publish_flag, update_suppressed_flag

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(json) ⇒ RequiredFields

Returns a new instance of RequiredFields.



20
21
22
# File 'frontend/app/models/required_fields.rb', line 20

def initialize(json)
  @json = json
end

Class Method Details

.create_from_json(json, opts = {}) ⇒ Object



42
43
44
# File 'backend/app/model/required_fields.rb', line 42

def self.create_from_json(json, opts = {})
  super(json, opts.merge('blob' => json.to_json))
end

.create_or_update(json, repo_id, record_type) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'backend/app/model/required_fields.rb', line 7

def self.create_or_update(json, repo_id, record_type)
  id = "#{repo_id}_#{record_type}"

  if self[id]
    self[id].update_from_json(json)
  else
    self.create_from_json(json, {:id => "#{repo_id}_#{record_type}"})
  end

  self[id]
end

.from_hash(hash) ⇒ Object



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

def self.from_hash(hash)
  self.new(JSONModel(:required_fields).from_hash(hash))
end

.get(record_type) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'frontend/app/models/required_fields.rb', line 4

def self.get(record_type)
  uri = "/repositories/#{JSONModel.repository}/required_fields/#{record_type}"
  result = JSONModel::HTTP.get_json(uri)
  if result
    self.new(JSONModel(:required_fields).from_hash(result))
  else
    nil
  end
end

.sequel_to_jsonmodel(objs, opts = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'backend/app/model/required_fields.rb', line 30

def self.sequel_to_jsonmodel(objs, opts = {})
  jsons = objs.map {|obj|
    json = JSONModel(:required_fields).new(ASUtils.json_parse(obj[:blob]))
    json.uri = obj.uri
    json.lock_version = obj.lock_version
    json
  }

  jsons
end

.to_jsonmodel(obj, opts = {}) ⇒ Object



20
21
22
23
24
25
26
27
# File 'backend/app/model/required_fields.rb', line 20

def self.to_jsonmodel(obj, opts = {})
  if obj.is_a? String
    obj = RequiredFields[obj]
    raise NotFoundException.new("#{self} not found") unless obj
  end

  self.sequel_to_jsonmodel([obj], opts)[0]
end

Instance Method Details

#form_valuesObject

We kind of cheat here: the form thinks ‘lock_version’ applies to the archival record, but it’s really for the required_fields object



28
29
30
# File 'frontend/app/models/required_fields.rb', line 28

def form_values
  values.merge({:lock_version => @json.lock_version})
end

#saveObject



38
39
40
41
42
43
44
45
46
47
48
49
# File 'frontend/app/models/required_fields.rb', line 38

def save
  uri = "/repositories/#{JSONModel.repository}/required_fields/#{@json.record_type}"
  url = URI("#{JSONModel::HTTP.backend_url}#{uri}")

  response = JSONModel::HTTP.post_json(url, ASUtils.to_json((@json.to_hash)))

  if response.code != '200'
    raise response.body
  end

  response
end

#update_from_json(json, opts = {}, apply_nested_records = false) ⇒ Object



47
48
49
50
# File 'backend/app/model/required_fields.rb', line 47

def update_from_json(json, opts = {}, apply_nested_records = false)
  json['lock_version'] ||= 0
  super(json, opts.merge('blob' => json.to_json))
end

#uriObject



53
54
55
# File 'backend/app/model/required_fields.rb', line 53

def uri
  "/repositories/#{self.repo_id}/required_fields/#{self.record_type}"
end

#valuesObject



33
34
35
# File 'frontend/app/models/required_fields.rb', line 33

def values
  @json.required || {}
end