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/9198f78d95b7911239fd to your computer and use it in GitHub Desktop.
Save alexvbush/9198f78d95b7911239fd to your computer and use it in GitHub Desktop.
First naive implementation of Articles controller.
class Api::Private::ArticlesController < Api::Private::BaseController
before_filter :find_or_create_article, except: [:index]
def index
@articles = Article.scoped
if params[:state] == Article::PENDING
@articles = Article.pending.order(:published_at)
elsif params[:state] == Article::APPROVED
@articles = Article.approved.order('approved_at DESC')
elsif params[:query]
@articles = articles_search(params[:query]).order('articles.created_at DESC')
end
respond_with @articles, each_serializer: article_serializer, root: 'articles'
end
private
def articles_search(search_criteria)
Article.includes(:categories).where('title LIKE ? OR categories.name LIKE ?', "%#{search_criteria}%", "%#{search_criteria}%")
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