Skip to content

Instantly share code, notes, and snippets.

@alexvbush
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexvbush/86970eb4f4aad191ca1d to your computer and use it in GitHub Desktop.
Save alexvbush/86970eb4f4aad191ca1d to your computer and use it in GitHub Desktop.
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