class IscController < ApplicationController protected def isc_index(model) build_singular_class_name_hash(model) @start_row = Integer(params[:_startRow]) @end_row = Integer(params[:_endRow]) scoped_model = build_scoped_model(model) @total_rows = scoped_model.count @end_row = @total_rows - 1 if @end_row >= @total_rows case params[:_sortBy] when /^-(.+)/ then order = "#{$1} DESC" when /(.+)/ then order = "#{$1}" else order = "id" end instances = scoped_model.scoped(:offset => @start_row, :limit => (@end_row - @start_row + 1), :order => order) respond_to do |format| format.xml { render :xml => (instances.any? ? instances : empty_xml(model.table_name)) } format.json { render :json => isc_response(instances), :callback => params[:_callback] } end end def isc_create(model) build_singular_class_name_hash(model) case params[:_operationType] when 'add' then # Create! when 'update' then isc_update(model); return when 'remove' then isc_destroy(model); return else raise "Operazione sconosciuta." end instance = model.new(params[:singular_class_name]) if instance.save respond_to do |format| format.xml { render :xml => instance, :status => :created, :location => instance } format.json { render :json => isc_response(instance), :status => :created, :location => instance, :callback => params[:_callback] } end else respond_to do |format| format.xml { render :xml => instance, :status => :bad_request } format.json { render :json => isc_response(instance, -4), :status => :bad_request, :callback => params[:_callback] } end end end def isc_update(model) instance = model.find(params[:id]) instance.attributes = params[:"#{model.name.underscore}"] if instance.save respond_to do |format| format.xml { render :xml => instance } format.json { render :json => isc_response(instance), :callback => params[:_callback] } end else respond_to do |format| format.xml { render :xml => instance, :status => :bad_request } format.json { render :json => isc_response(instance, -4), :status => :bad_request, :callback => params[:_callback] } end end end def isc_destroy(model) instance = model.find(params[:id]) instance.destroy respond_to do |format| format.xml { render :xml => instance } format.json { render :json => isc_response(instance), :callback => params[:_callback] } end end def isc_response(data, status = 0) meta_data = { :status => status, :data => data } meta_data[:totalRows] = @total_rows if @total_rows meta_data[:startRow] = @start_row if @start_row meta_data[:endRow] = @end_row if @end_row logger.debug meta_data.except(:data).inspect { :response => meta_data } end def empty_xml(root) "\n<#{root}/>" end def build_singular_class_name_hash(model) params[:singular_class_name] = params.reject {|k, v| !model.column_names.include?(k) } end def build_scoped_model(model) scoped_model = model.scoped({}) params[:singular_class_name].each do |k, v| scoped_model = if [:string, :text].include?(model.columns_hash[k].type) && params[:_textMatchStyle] == 'substring' scoped_model.scoped(:conditions => [ "#{k} LIKE ?", "%#{v}%" ]) else scoped_model.scoped(:conditions => [ "#{k} = ?", v ]) end end scoped_model end end