Module: CrudHelpers

Included in:
ArchivesSpaceService
Defined in:
backend/app/lib/crud_helpers.rb

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) scoped_dataset(model, where_clause)



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'backend/app/lib/crud_helpers.rb', line 25

def self.scoped_dataset(model, where_clause)
  dataset = (model.model_scope == :repository) ? model.this_repo : model

  if where_clause.is_a?(Hash) && where_clause.has_key?(:exclude)
    dataset = dataset.exclude(where_clause[:exclude])
    where_clause.delete(:exclude)
  end

  if !where_clause.is_a?(Hash) || !where_clause.empty?
    dataset = dataset.filter(where_clause)
  end

  dataset
end

+ (Object) with_record_conflict_reporting(model, json)



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'backend/app/lib/crud_helpers.rb', line 73

def self.with_record_conflict_reporting(model, json)
  begin
    yield
  rescue Sequel::ValidationFailed => e
    if e.errors && e.errors.any? {|key, errors| errors[0].end_with?("must be unique")}
      existing_record = model.find_matching(json)

      if existing_record
        e.errors[:conflicting_record] = [existing_record.uri]
      end
    end

    raise $!
  end
end

Instance Method Details

- (Object) handle_create(model, json, opts = {})



10
11
12
13
14
# File 'backend/app/lib/crud_helpers.rb', line 10

def handle_create(model, json, opts = {})
  obj = model.create_from_json(json, opts)

  created_response(obj, json)
end

- (Object) handle_delete(model, id)



17
18
19
20
21
22
# File 'backend/app/lib/crud_helpers.rb', line 17

def handle_delete(model, id)
  obj = model.get_or_die(id)
  obj.delete

  deleted_response(id)
end

- (Object) handle_listing(model, pagination_data, where = {}, order = nil)



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'backend/app/lib/crud_helpers.rb', line 48

def handle_listing(model, pagination_data, where = {}, order = nil)

  dataset = CrudHelpers.scoped_dataset(model, where)

  modified_since_time = Time.at(pagination_data[:modified_since])
  dataset = dataset.where { system_mtime >= modified_since_time }
  dataset = dataset.order(*order) if order

  if pagination_data[:page]
    # Classic pagination mode
    paginated = dataset.extension(:pagination).paginate(pagination_data[:page], pagination_data[:page_size])

    listing_response(paginated, model)

  elsif pagination_data[:all_ids]
    # Return a JSON array containing all IDs for the matching records
    json_response(dataset.select(:id).map {|rec| rec[:id]})

  elsif pagination_data[:id_set]
    # Return the requested set of IDs
    listing_response(dataset.filter(:id => pagination_data[:id_set]), model)
  end
end

- (Object) handle_unlimited_listing(model, where = {})



41
42
43
44
45
# File 'backend/app/lib/crud_helpers.rb', line 41

def handle_unlimited_listing(model, where = {})
  dataset = CrudHelpers.scoped_dataset(model, where)

  listing_response(dataset, model)
end

- (Object) handle_update(model, id, json, opts = {})



3
4
5
6
7
# File 'backend/app/lib/crud_helpers.rb', line 3

def handle_update(model, id, json, opts = {})
  obj = model.get_or_die(id)
  obj.update_from_json(json, opts)
  updated_response(obj, json)
end

- (Object) with_record_conflict_reporting(model, json)



90
91
92
93
94
# File 'backend/app/lib/crud_helpers.rb', line 90

def with_record_conflict_reporting(model, json)
  CrudHelpers::with_record_conflict_reporting(model, json) do
    yield
  end
end