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 :name, :email, :age, 'posts.created_at', 'posts.id' => 'post_id'

  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: 'users.name'
  filterable_by :created_at, user: '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

#sort

Methods included from Filterable

#filter, #filter_conditions, #filterable_by

Instance Method Details

#tabulate(scope) ⇒ ActiveRecord::Relation

Parameters:

  • scope (ActiveRecord::Relation)

Returns:

  • (ActiveRecord::Relation)

See Also:



72
73
74
75
76
77
# File 'lib/warped/controllers/tabulatable.rb', line 72

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