From fbba36005e83fc4ce4a5d7289c359c677f69010f Mon Sep 17 00:00:00 2001 From: Matchu Date: Tue, 22 Jun 2010 13:00:55 -0400 Subject: [PATCH] distribute items as jsonp --- app/controllers/items_controller.rb | 17 +++++++++++++++-- app/models/item.rb | 10 ++++++++++ config/routes.rb | 1 + 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb index e391c5a8..5b532b63 100644 --- a/app/controllers/items_controller.rb +++ b/app/controllers/items_controller.rb @@ -4,9 +4,22 @@ class ItemsController < ApplicationController def index if params.has_key?(:q) begin - @results = Item.search(@query).alphabetize.paginate :page => params[:page] + if params[:per_page] + per_page = params[:per_page].to_i + per_page = 50 if per_page && per_page > 50 + else + per_page = nil + end + @results = Item.search(@query).alphabetize.paginate :page => params[:page], :per_page => per_page + respond_to do |format| + format.html { render } + format.js { render :json => {:items => @results, :total_pages => @results.total_pages}, :callback => params[:callback] } + end rescue - flash.now[:alert] = $!.message + respond_to do |format| + format.html { flash.now[:error] = $!.message } + format.js { render :json => {:error => $!.message}, :status => :bad_request, :callback => params[:callback] } + end end end end diff --git a/app/models/item.rb b/app/models/item.rb index de342d43..21283df7 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -55,6 +55,16 @@ class Item < ActiveRecord::Base end end + def as_json(options = {}) + { + :description => description, + :id => id, + :name => name, + :thumbnail_url => thumbnail_url, + :zones_restrict => zones_restrict + } + end + private SearchFilterScopes = [] diff --git a/config/routes.rb b/config/routes.rb index 158b2969..3fd984fc 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,6 @@ OpenneoImpressItems::Application.routes.draw do |map| match '/' => 'items#index', :as => :items + match '/index.js' => 'items#index', :format => :js match '/:id' => 'items#show', :as => :item match '/items/:item_id/swf_assets.json' => 'swf_assets#index', :as => :item_swf_assets