class RailsAiAdmin::Navigation::SavedViews
Named list settings that belong to one actor in one tenant. Saving one grants nothing: each view is read back through ListState, so a revoked search, sort, tab or filter grant drops out of the link.
Constants
- MAX_NAME
- MAX_PER_RESOURCE
- View
Public Class Methods
Source
# File lib/rails_ai_admin/navigation/saved_views.rb, line 22 def self.available? SavedView.table_exists? rescue ActiveRecord::ActiveRecordError false end
Source
# File lib/rails_ai_admin/navigation/saved_views.rb, line 28 def initialize(access) @access = access @context = access.context @resource = access.resource end
Public Instance Methods
Source
# File lib/rails_ai_admin/navigation/saved_views.rb, line 67 def delete(id) @access.authorize!(:index) raise AccessDenied unless id.is_a?(String) && id.match?(/\A[1-9]\d{0,18}\z/) owned.where(id: id).delete_all.positive? || raise(AccessDenied) end
Source
# File lib/rails_ai_admin/navigation/saved_views.rb, line 36 def list @access.authorize!(:index) owned.order(:name).limit(MAX_PER_RESOURCE).map do |row| state = ListState.new(@access, parse(row.state)).state.except("page") View.new(row.id, row.name, state) end end
The actorβs views for this resource, by name, each with its current canonical state (without a page).
Source
# File lib/rails_ai_admin/navigation/saved_views.rb, line 45 def save(name, state) @access.authorize!(:index) name = normalize_name(name) state = ListState.new(@access, state).state.except("page") SavedView.transaction(requires_new: true) do row = owned.lock.find_by(name: name) if row row.update!(state: JSON.generate(state)) else raise Rejected, :limit if owned.count >= MAX_PER_RESOURCE row = SavedView.create!(actor_ref: @context.actor_ref, tenant_ref: @context.tenant_ref, resource_key: @resource.key.to_s, name: name, state: JSON.generate(state)) end View.new(row.id, row.name, state) end rescue ActiveRecord::RecordNotUnique # Two saves of a new name raced; the other one won. retry_save ||= 0 retry if (retry_save += 1) < 2 raise end
Saves state under name, replacing a view of the same name.