Module: CrudHelpers
- Defined in:
- backend/app/lib/crud_helpers.rb,
backend/app/lib/bulk_import/crud_helpers.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#handle_create(model, json, opts = {}) ⇒ Object
-
#handle_delete(model, id) ⇒ Object
-
#handle_listing(model, pagination_data, where = {}, order = nil) ⇒ Object
-
#handle_raw_listing(model, where = {}, current_user) ⇒ Object
-
#handle_unlimited_listing(model, where = {}) ⇒ Object
-
#handle_update(model, id, json, opts = {}) ⇒ Object
-
#with_record_conflict_reporting(model, json) ⇒ Object
Class Method Details
.scoped_dataset(model, where_clause) ⇒ Object
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 |
.with_record_conflict_reporting(model, json) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'backend/app/lib/crud_helpers.rb', line 71 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 = e.errors.any? {|key, errors| errors[0].include?("Authority ID")} ? model.find_matching_id(json) : model.find_matching(json) if existing_record e.errors[:conflicting_record] = [existing_record.uri] end end raise $! end end |
Instance Method Details
#handle_create(model, json, opts = {}) ⇒ Object
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 |
#handle_delete(model, id) ⇒ Object
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 |
#handle_listing(model, pagination_data, where = {}, order = nil) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'backend/app/lib/crud_helpers.rb', line 48 def handle_listing(model, pagination_data, where = {}, order = nil) dataset = CrudHelpers.scoped_dataset(model, where) order = Sequel.send(pagination_data['sort_direction'], pagination_data['sort_field']) unless order modified_since_time = Time.at(pagination_data[:modified_since]) dataset = dataset.where { system_mtime >= modified_since_time }.order(*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 |
#handle_raw_listing(model, where = {}, current_user) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'backend/app/lib/bulk_import/crud_helpers.rb', line 4 def handle_raw_listing(model, where = {}, current_user) dataset = CrudHelpers.scoped_dataset(model, where) objs = dataset.respond_to?(:all) ? dataset.all : dataset opts = { :calculate_linked_repositories => current_user.can?(:index_system) } jsons = model.sequel_to_jsonmodel(objs, opts).map { |json| if json.is_a?(JSONModelType) json.to_hash(:trusted) else json end } # results = resolve_references(jsons, true) jsons end |
#handle_unlimited_listing(model, where = {}) ⇒ Object
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 |
#handle_update(model, id, json, opts = {}) ⇒ Object
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 |
#with_record_conflict_reporting(model, json) ⇒ Object
88 89 90 91 92 |
# File 'backend/app/lib/crud_helpers.rb', line 88 def with_record_conflict_reporting(model, json) CrudHelpers::with_record_conflict_reporting(model, json) do yield end end |