Module: Boosted::Controllers::Tabulatable
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/boosted/controllers/tabulatable.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: tabulate_info
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: tabulate_info
end
end
Instance Method Summary collapse
Instance Method Details
#tabulate(scope) ⇒ ActiveRecord::Relation
72 73 74 75 76 77 |
# File 'lib/boosted/controllers/tabulatable.rb', line 72 def tabulate(scope) scope = filter(scope) scope = search(scope) scope = sort(scope) paginate(scope) end |
#tabulate_info ⇒ Hash
80 81 82 83 84 85 86 87 88 |
# File 'lib/boosted/controllers/tabulatable.rb', line 80 def tabulate_info { filters: filter_conditions(*filter_fields, *mapped_filter_fields), sorts: sort_conditions(*sort_fields, *mapped_sort_fields), search_term:, search_param:, page_info: } end |