Module: Warped::Controllers::Tabulatable

Extended by:
ActiveSupport::Concern
Includes:
Filterable, Pageable, Searchable, Sortable
Included in:
Ui
Defined in:
lib/warped/controllers/tabulatable.rb,
lib/warped/controllers/tabulatable/ui.rb

Overview

Provides functionality for filtering, sorting, searching, and paginating records from an ActiveRecord::Relation in a controller.

Example usage:

class UsersController < ApplicationController
  include Tabulatable

  tabulatable_by :email, :age, 'posts.created_at', 'posts.id' => { alias_name: 'post_id', kind: :integer }

  def index
    users = User.left_joins(:posts).group(:id)
    users = tabulate(users)
    render json: users, meta: tabulation
  end
end

There are cases where not all fields should be filterable or sortable. In such cases, you can use the ‘filterable_by` and `sortable_by` methods to override the tabulate fields.

Example:

class PostsController < ApplicationController
  include Tabulatable

  tabulatable_by :title, :content, :created_at, user: { alias_name: 'users.name' }
  filterable_by :created_at, user: { alias_name: 'users.name' }

  def index
    posts = Post.left_joins(:user).group(:id)
    posts = tabulate(posts)
    render json: posts, meta: tabulation
  end
end

Defined Under Namespace

Modules: Ui

Instance Method Summary collapse

Methods included from Pageable

#page, #paginate, #pagination, #per_page

Methods included from Searchable

#model_search_scope, #search, #search_param, #search_term

Methods included from Sortable

#current_action_sort_value, #sort

Methods included from Filterable

#current_action_filter_values, #current_action_filters, #filter, #parse_filter_params

Instance Method Details

#tabulate(scope) ⇒ ActiveRecord::Relation

Parameters:

  • scope (ActiveRecord::Relation)

Returns:

  • (ActiveRecord::Relation)

See Also:



69
70
71
72
73
74
# File 'lib/warped/controllers/tabulatable.rb', line 69

def tabulate(scope)
  scope = filter(scope)
  scope = search(scope)
  scope = sort(scope)
  paginate(scope)
end