Last active
May 16, 2016 17:55
-
-
Save richmolj/e6e8b5e4f2ef645b1f9e3abe5827d54d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Basic AR | |
| class PeopleController < ApplicationController | |
| jsonapi do | |
| allow_filter :name, aliases: [:full_name] # ?filter[full_name] == ?filter[name] | |
| allow_filter :title, if: :admin? # calls #admin? on the controller | |
| includes whitelist: [:company, { pets: :breeds}] | |
| end | |
| # now supports pagination, sorting, filters, includes | |
| def index | |
| scope = Person.all | |
| render_ams(scope) | |
| end | |
| end | |
| # Customized with Trample | |
| class PeopleController < ApplicationController | |
| jsonapi do | |
| allow_filter :name_prefix do |scope, value| | |
| scope.condition(:name).starts_with(value) | |
| end | |
| paginate do |scope, page, per_page| | |
| scope.metadata.pagination.current_page = page | |
| scope.metadata.pagination.per_page = per_page | |
| scope | |
| end | |
| sort do |scope, att, dir| | |
| scope.metadata.sorts = [{att: att, dir: dir}] | |
| end | |
| includes whitelist: [:company, { pets: :breeds}] do |scope, includes| | |
| scope.metadata.includes = includes | |
| end | |
| def index | |
| search = Search::Person.new | |
| search = jsonapi_scope(search) | |
| search.query! | |
| render_ams(search.records.to_a) # would query elastic but return ActiveRecord | |
| render_ams(search.results) # would render Hash::Mashes of elastic results | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment