Class: StimulusGridRails::RowsController
- Inherits:
-
BaseController
- Object
- BaseController
- StimulusGridRails::RowsController
- Defined in:
- app/controllers/stimulus_grid_rails/rows_controller.rb
Overview
Row search / create / destroy — RAILS.md §14/§15/§21.
GET /grids/:resource/rows?q=&filters= → index (server-side search/filter, JSON)
POST /grids/:resource/rows → create
DELETE /grids/:resource/rows/bulk → destroy_bulk (ids[])
DELETE /grids/:resource/rows/:row_id → destroy
Create/destroy broadcast AUTOMATICALLY via the model’s commit callbacks (Broadcastable) — these actions just persist and return an empty 200; the originating tab applies the change when the broadcast lands.
Constant Summary collapse
- MAX_ROWS =
5_000
Instance Method Summary collapse
Instance Method Details
#create ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'app/controllers/stimulus_grid_rails/rows_controller.rb', line 38 def create grid = grid_for(params[:resource]) row = grid.build_new_row(create_attributes) # Stamp the tenant if the host uses ActsAsTenant and the model is scoped — # otherwise a created row could escape the tenant. ActsAsTenant normally # sets this automatically when current_tenant is present. if row.save head :ok # after_create_commit broadcasts row-insert-sorted to everyone else render json: { errors: row.errors. }, status: :unprocessable_entity end end |
#destroy ⇒ Object
51 52 53 54 55 |
# File 'app/controllers/stimulus_grid_rails/rows_controller.rb', line 51 def destroy grid = grid_for(params[:resource]) find_row!(grid, params[:row_id]).destroy # after_destroy_commit broadcasts row-remove head :ok end |
#destroy_bulk ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'app/controllers/stimulus_grid_rails/rows_controller.rb', line 57 def destroy_bulk grid = grid_for(params[:resource]) params.require(:ids) # Scoped where (not find) so ids outside the user's scope are simply # ignored rather than raising — each destroy fires its own broadcast. grid.scope(current_grid_user).where(id: params[:ids]).find_each(&:destroy) head :ok end |
#index ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'app/controllers/stimulus_grid_rails/rows_controller.rb', line 17 def index grid = grid_for(params[:resource]) relation = grid.search_and_filter(grid.scope(current_grid_user), q: params[:q], filters: parse_filters) relation = grid.apply_sort(relation, parse_sort) total = relation.count if params[:page].present? # Server-side row model: return just the requested window. page = params[:page].to_i page_size = (params[:page_size].presence || 25).to_i.clamp(1, 1000) window = relation.offset(page * page_size).limit(page_size) rows = window.map { |r| grid.row_to_h(r) } render json: { rows: rows, total: total, page: page, page_size: page_size, limited: false } else # Client-side model: the capped full (filtered) set. rows = relation.limit(MAX_ROWS).map { |r| grid.row_to_h(r) } render json: { rows: rows, total: total, limited: total > MAX_ROWS } end end |