Skip to content

Instantly share code, notes, and snippets.

View alexvbush's full-sized avatar

Alex Bush alexvbush

View GitHub Profile
@alexvbush
alexvbush / articles_controller.rb
Last active August 29, 2015 14:03
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
@alexvbush
alexvbush / articles_search.rb
Created July 14, 2014 01:22
Articles Search mixin.
module ArticlesSearch
def articles_search(search_criteria, state = nil)
@articles = Article.includes(:categories).where('title LIKE ? OR categories.name LIKE ?', "%#{search_criteria}%", "%#{search_criteria}%")
if state
@articles = @articles.where(state: state)
end
@articles
@alexvbush
alexvbush / articles_controller.rb
Last active August 29, 2015 14:03
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')
@alexvbush
alexvbush / article.rb
Last active August 29, 2015 14:03
First naive implementation of Article model.
class Article < ActiveRecord::Base
include Categorizable
APPROVED = 'approved'
PENDING = 'pending'
REJECTED = 'rejected'
attr_accessible :approved_at, :url, :body, :image, :rating, :state, :title
validates :url, :uniqueness => true