Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Second iteration of Articles controller.
class Api::Private::ArticlesController < Api::Private::BaseController
include ArticlesSearch
before_filter :find_or_create_article, except: [:index]
PAGE_SIZE = 20
def index
@articles = Article.scoped
total_pages = 0
page = params[:page] ? params[:page] : 1
if params[:state] == Article::PENDING
@articles = Article.pending
elsif params[:state] == Article::APPROVED
@articles = Article.approved
else
@articles = @articles.order('articles.created_at DESC')
end
if params[:query]
@articles = articles_search(params[:query], params[:state])
end
@articles = @articles.page(page).per_page(PAGE_SIZE)
total_pages = @articles.total_pages
respond_with @articles, each_serializer: article_serializer, root: 'articles', meta: { total_pages: total_pages }
end
#####
# ...
# The rest of the methods ommited
# ...
#####
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment