Skip to content

Instantly share code, notes, and snippets.

@hcatlin
Created Mar 17, 2009
Embed
What would you like to do?
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