Class: DefaultValues

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json) ⇒ DefaultValues

Returns a new instance of DefaultValues.



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

def initialize(json)
  @json = json
end

Class Method Details

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



42
43
44
# File 'backend/app/model/default_values.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/default_values.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



14
15
16
# File 'frontend/app/models/default_values.rb', line 14

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

.get(record_type) ⇒ Object



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

def self.get(record_type)
  uri = "/repositories/#{JSONModel.repository}/default_values/#{record_type}"
  begin
    self.new(JSONModel(:default_values).from_hash(JSONModel::HTTP.get_json(uri)))
  rescue RecordNotFound
    nil
  end
end

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



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

def self.sequel_to_jsonmodel(objs, opts = {})
  jsons = objs.map {|obj|
    json = JSONModel(:default_values).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/default_values.rb', line 20

def self.to_jsonmodel(obj, opts = {})
  if obj.is_a? String
    obj = DefaultValues[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 default_values object



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

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

#saveObject



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

def save
  uri = "/repositories/#{JSONModel.repository}/default_values/#{@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/default_values.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/default_values.rb', line 53

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

#valuesObject



32
33
34
# File 'frontend/app/models/default_values.rb', line 32

def values
  @json.defaults || {}
end