Module: Netzke::Grid::Services
- Included in:
- Base, Tree::Base
- Defined in:
- lib/netzke/grid/services.rb
Overview
Implements Grid server-side operations. Used by the Endpoints module.
Instance Method Summary collapse
- #count_records(params) ⇒ Object
-
#create(data) ⇒ Object
Returns hash with results per client (temporary) id, e.g.:.
-
#destroy(ids) ⇒ Object
Destroys records by ids Returns hash with results per id, e.g.:.
-
#get_records(params) ⇒ Object
Returns an array of records.
-
#on_data_changed ⇒ Object
Override this method to react on each operation that caused changing of data.
-
#read(params = {}) ⇒ Object
Implementation for the "server_read" endpoint.
-
#update(data) ⇒ Object
Receives an array of attribute hashes, e.g.:.
Instance Method Details
#count_records(params) ⇒ Object
79 80 81 82 83 |
# File 'lib/netzke/grid/services.rb', line 79 def count_records(params) params[:scope] = config[:scope] # note, params[:scope] becomes ActiveSupport::HashWithIndifferentAccess model_adapter.count_records(params, final_columns) end |
#create(data) ⇒ Object
Returns hash with results per client (temporary) id, e.g.:
{
1: {errors: [], record: {title: 'New title'}},
2: {errors: ["Title must be present"], record: nil}
}
20 21 22 23 24 25 26 |
# File 'lib/netzke/grid/services.rb', line 20 def create(data) data.inject({}) do |out, attrs| id = attrs.delete('internal_id') record = model_adapter.new_record out.merge(id => update_record(record, attrs)) end end |
#destroy(ids) ⇒ Object
Destroys records by ids Returns hash with results per id, e.g.:
{
1: "ok",
2: "error: "This record could not be destroyed"
}
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/netzke/grid/services.rb', line 53 def destroy(ids) out = {} ids.each {|id| record = model_adapter.find_record(id, scope: config[:scope]) next if record.nil? if record.destroy out[id] = "ok" else out[id] = {error: record.errors.to_a.first} end } out end |
#get_records(params) ⇒ Object
Returns an array of records.
71 72 73 74 75 76 77 |
# File 'lib/netzke/grid/services.rb', line 71 def get_records(params) params[:filters] = normalize_filters(params[:filters]) if params[:filters] params[:query] = normalize_query(params[:query]) if params[:query].present? params[:scope] = config[:scope] # note, params[:scope] becomes ActiveSupport::HashWithIndifferentAccess model_adapter.get_records(params, final_columns) end |
#on_data_changed ⇒ Object
Override this method to react on each operation that caused changing of data
86 87 |
# File 'lib/netzke/grid/services.rb', line 86 def on_data_changed end |
#read(params = {}) ⇒ Object
Implementation for the "server_read" endpoint
6 7 8 9 10 11 12 |
# File 'lib/netzke/grid/services.rb', line 6 def read(params = {}) {}.tap do |res| records = get_records(params) res[:data] = records.map{|r| model_adapter.record_to_array(r, final_columns)} res[:total] = count_records(params) end end |
#update(data) ⇒ Object
Receives an array of attribute hashes, e.g.:
[{"id": 1, title: "New title"}, {"id": 2, title: ""}]
Returns hash with results per id, e.g.:
{
1: {errors: [], record: {title: 'New title'}},
2: {errors: ["Title must be present"], record: nil}
}
38 39 40 41 42 43 44 |
# File 'lib/netzke/grid/services.rb', line 38 def update(data) data.inject({}) do |out, attrs| id = attrs.delete(model_adapter.primary_key) record = model_adapter.find_record(id, scope: config.scope) record.nil? ? out.merge(id => {error: "Not allowed to edit record #{id}"}) : out.merge(id => update_record(record, attrs)) end end |