Class: RequiredFields
- Inherits:
-
Sequel::Model
- Object
- Sequel::Model
- RequiredFields
show all
- Defined in:
- backend/app/model/required_fields.rb,
frontend/app/models/required_fields.rb
Class Method Summary
collapse
Instance Method Summary
collapse
-
#add_errors(obj) ⇒ Object
this is limited to checking for subrecord presence and checking presence of fields of subrecords that are immediately attached to a property of the top-level record as an array, but could be expanded to cover other parts of the record.
-
#each_required_subrecord ⇒ Object
-
#initialize(json) ⇒ RequiredFields
constructor
A new instance of RequiredFields.
-
#method_missing(meth, *args) ⇒ Object
-
#required?(property, type, field = nil) ⇒ Boolean
Ideally a subrecord field requirement should probably involve: 1) a property of the parent schema; 2) the type of subrecord; 3) the field name But since form data carried through an invalid create attempt is missing #2, and javascript templates lack #1, and furthermore migrated requirements don’t have record types; and since in practice there is generally only one model type that will satisfy a property (e.g., “metadata_rights_declarations / metadata_rights_declaration”), we will accept nil for either property or type (on either side for type), and assume it’s a match.
-
#save ⇒ Object
-
#update_from_json(json, opts = {}, apply_nested_records = false) ⇒ Object
-
#uri ⇒ Object
Constructor Details
Returns a new instance of RequiredFields.
17
18
19
|
# File 'frontend/app/models/required_fields.rb', line 17
def initialize(json)
@json = json
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args) ⇒ Object
21
22
23
|
# File 'frontend/app/models/required_fields.rb', line 21
def method_missing(meth, *args)
@json.send(meth, *args)
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
13
14
15
|
# File 'frontend/app/models/required_fields.rb', line 13
def self.from_hash(hash)
self.new(JSONModel(:required_fields).from_hash(hash))
end
|
.get(record_type) ⇒ Object
3
4
5
6
7
8
9
10
11
|
# File 'frontend/app/models/required_fields.rb', line 3
def self.get(record_type)
uri = "/repositories/#{JSONModel.repository}/required_fields/#{record_type}"
begin
result = JSONModel::HTTP.get_json(uri)
self.new(JSONModel(:required_fields).from_hash(result))
rescue RecordNotFound => e
self.new(JSONModel(:required_fields).from_hash(record_type: record_type))
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
#add_errors(obj) ⇒ Object
this is limited to checking for subrecord presence and checking presence of fields
of subrecords that are immediately attached to a property of the top-level record
as an array, but could be expanded to cover other parts of the record
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'frontend/app/models/required_fields.rb', line 41
def add_errors(obj)
unless obj.jsonmodel_type == @json.record_type
raise "Cannot validate a #{obj.jsonmodel_type} with these #{@json.record_type} requirements!"
end
@json.subrecord_requirements.each do |requirements|
property = requirements["property"]
type = requirements["record_type"]
if obj[property].present?
obj[property].each_with_index do |subrecord, i|
next unless subrecord.is_a?(Hash)
next if subrecord['jsonmodel_type'] && subrecord['jsonmodel_type'].to_s != requirements['record_type']
requirements['required_fields'].each do |required_field|
unless subrecord[required_field].present?
obj.add_error("#{property}/#{i}/#{required_field}", :missing_required_property)
end
end
end
elsif requirements["required"]
obj.add_error(property, :missing_required_subrecord)
end
end
end
|
#each_required_subrecord ⇒ Object
87
88
89
90
91
92
|
# File 'frontend/app/models/required_fields.rb', line 87
def each_required_subrecord
@json.subrecord_requirements.each do |requirements|
next unless requirements['required']
yield requirements['property'], { 'jsonmodel_type' => requirements['record_type'] }
end
end
|
#required?(property, type, field = nil) ⇒ Boolean
Ideally a subrecord field requirement should probably involve:
1) a property of the parent schema;
2) the type of subrecord;
3) the field name
But since form data carried through an invalid create attempt is missing #2,
and javascript templates lack #1, and furthermore migrated requirements don’t
have record types; and since in practice there is generally
only one model type that will satisfy a property (e.g.,
“metadata_rights_declarations / metadata_rights_declaration”), we
will accept nil for either property or type (on either side for type),
and assume it’s a match. caveat validator.
77
78
79
80
81
82
83
84
85
|
# File 'frontend/app/models/required_fields.rb', line 77
def required?(property, type, field = nil)
@json.subrecord_requirements.each do |requirements|
next unless type.nil? || requirements['record_type'].nil? || requirements['record_type'] == type.to_s
next unless property.nil? || requirements['property'] == property.to_s
return true if property && type && field.nil? && requirements['required']
return true if requirements['required_fields'].include?(field.to_s) && field.present?
end
false
end
|
#save ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'frontend/app/models/required_fields.rb', line 25
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
|
#uri ⇒ Object
53
54
55
|
# File 'backend/app/model/required_fields.rb', line 53
def uri
"/repositories/#{self.repo_id}/required_fields/#{self.record_type}"
end
|