Class: CurrentScope::SubjectsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/current_scope/subjects_controller.rb

Constant Summary collapse

PER_PAGE =
50
SEARCH_COLUMNS =

Identity columns searched by ?q=, in the same preference order as current_scope_subject_label's default chain. Intersected with the subject table's real column_names before use, so a query never spans a page.

%w[email email_address name first_name last_name].freeze

Instance Method Summary collapse

Instance Method Details

#indexObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/controllers/current_scope/subjects_controller.rb', line 10

def index
  klass = CurrentScope.config.subject_class.constantize
  @query = params[:q].to_s.strip
  scope = filter_subjects(klass.order(:id), @query)

  @page = [ params[:page].to_i, 1 ].max
  @subjects = scope.limit(PER_PAGE).offset((@page - 1) * PER_PAGE)
  @has_next_page = scope.offset(@page * PER_PAGE).exists?

  @roles = Role.order(:name)
  @assignments = RoleAssignment.where(subject: @subjects)
                               .index_by { |a| [ a.subject_type, a.subject_id ] }
  # Safe polymorphic resource preload (resolvable types only) — full
  # includes(:resource) NameErrors on a stale resource_type and 500s the
  # page; skip-unresolvable + label as inert instead (#90 / PR #104).
  scoped_rows = ScopedRoleAssignment.where(subject: @subjects).includes(:role).to_a
  ScopedRoleAssignment.preload_resolvable_resources!(scoped_rows)
  @scoped = scoped_rows.group_by { |a| [ a.subject_type, a.subject_id ] }
end