Module: Mensa::Scope

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
app/tables/mensa/scope.rb

Defined Under Namespace

Modules: Helper

Instance Method Summary collapse

Instance Method Details

#filtered_scopeObject

Returns the scope, but filtered



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/tables/mensa/scope.rb', line 17

def filtered_scope
  return @filtered_scope if @filtered_scope

  @filtered_scope = scope
  # See https://github.com/textacular/textacular
  # This has problems - not all table fields are searched
  @filtered_scope = @filtered_scope.basic_search(params[:query]) if params[:query]

  if params[:filters]
    params[:filters].each do |column_name, value|
      next unless (column = column(column_name))

      @filtered_scope = if column.filter&.scope
                          @filtered_scope.instance_exec(Helper.normalize(value), &column.filter.scope)
                        else
                          @filtered_scope.where(column.attribute_for_condition => Helper.normalize(value))
                        end
    end
  end

  @filtered_scope
end

#ordered_scopeObject

Returns the filtered_columns, but ordered, we always reorder, scope shouldn’t include ordering



41
42
43
44
45
46
47
48
# File 'app/tables/mensa/scope.rb', line 41

def ordered_scope
  return @ordered_scope if @ordered_scope

  @ordered_scope = filtered_scope
  @ordered_scope = @ordered_scope.reorder(order_hash)

  @ordered_scope
end

#paged_scopeObject



60
61
62
63
# File 'app/tables/mensa/scope.rb', line 60

def paged_scope
  pagy_object
  @records
end

#pagy_detailsObject



65
66
67
68
# File 'app/tables/mensa/scope.rb', line 65

def pagy_details
  pagy_object
  @pagy_details
end

#scopeObject

Returns the records we want to display, using the Active Record Query Interface By default it returns all records



12
13
14
# File 'app/tables/mensa/scope.rb', line 12

def scope
  model.all
end

#selected_scopeObject

Return the ordered_scope, but with only the columns selected



51
52
53
54
55
56
57
58
# File 'app/tables/mensa/scope.rb', line 51

def selected_scope
  return @selected_scope if @selected_scope

  @selected_scope = ordered_scope
  @selected_scope = @selected_scope.select([:id] + columns.map(&:attribute).compact)

  @selected_scope
end