Skip to content

Instantly share code, notes, and snippets.

@HamptonMakes
Created March 17, 2009 17:38
Show Gist options
  • Save HamptonMakes/80671 to your computer and use it in GitHub Desktop.
Save HamptonMakes/80671 to your computer and use it in GitHub Desktop.
class ArticlesController < ApplicationController
make_resourceful do
actions :show, :index
before :index do
@lead_news_item = NewsItem.last
end
response_for :index do |format|
format.html
format.json do
data = current_objects.collect do |article|
"#{article.autocomplete_title}\n"
end
render :text => data.join
end
end
end
private
def current_objects
sql = "state = ?"
data = ["live"]
if @search_term = params[:search]
sql << " AND published_content LIKE ? "
data << "%#{@search_term}%"
end
if params[:q]
sql << " AND title LIKE ? "
data << "%#{params[:q]}%"
end
conditions = [sql] + data
@search_count = Article.count(:conditions => conditions )
Article.find(:all,
:conditions => conditions,
:order => "updated_at DESC",
:limit => 20,
:include => {:author => :user}
)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment