Created
November 19, 2009 05:57
-
-
Save danielbeardsley/238577 to your computer and use it in GitHub Desktop.
Revisions
-
danielbeardsley revised this gist
Apr 30, 2010 . 1 changed file with 30 additions and 39 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,51 +1,36 @@ class JsonController < ApplicationController rescue_from ActiveRecord::RecordNotFound do |exception| render :json => { :success => false }, :status => :not_found end before_filter :find_record, :only => [ :get, :update, :destroy ] layout 'base' class << self attr_accessor :model_class end def self.active_record_model_class (in_model) #if a model is passed in use that, if anything else lookup it up self.model_class = in_model.is_a?(ActiveRecord::Base) ? in_model : in_model.to_s.camelcase.constantize end def index respond_to do |format| format.html format.json { @record_hashes ||= scoped_model.all.map(&:to_record) render :json => {:success => true, :total => @record_hashes.length, :data => @record_hashes } } end end def create reject_non_attributes @record = scoped_model.new(params[:data]) success = @record.save error_messages = success ? nil : @record.errors.full_messages.join(', ') @@ -75,22 +60,28 @@ def destroy end protected def find_record @record = scoped_model.find(params[:id]) if params[:id] end def reject_non_attributes() #remove incoming attributes that aren't really columns names = model.column_names params["data"].reject!{|key, value| !names.include? key} if params["data"] end def scoped_model model end def model self.class.model_class ||= model_from_controller_name end def model_from_controller_name #try to infer the model name from the controller if it hasn't been set controller = controller_class_name.sub(/Controller$/, '') controller.singularize.constantize end end -
danielbeardsley renamed this gist
Nov 19, 2009 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
danielbeardsley created this gist
Nov 19, 2009 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,96 @@ class JsonController < ApplicationController include ExtJS::Controller helper ExtJS::Helpers::Store helper ExtJS::Helpers::Component rescue_from ActiveRecord::RecordNotFound do |exception| render :json => { :success => false }, :status => :not_found end before_filter :find_records, :only => [ :index, :get ] before_filter :find_record, :only => [ :update, :destroy ] layout 'base' cattr_accessor :model def self.model_class (in_model) #if a model is passed in use that, if anything else lookup it up self.model = in_model.is_a?(ActiveRecord::Base) ? in_model : in_model.to_s.camelcase.constantize end def model #try to infer the model name from the controller if it hasn't been set if @@model.nil? controller = controller_class_name.sub(/Controller$/, '') @@model = controller.singularize.constantize end @@model end def index respond_to do |format| format.html # index.html.erb (no data required) format.json { render :json => {:success => true, :total => @records.length, :data => (@records.collect {|rec|rec.to_record}) } } end end def get render :json => { :success => @records, :data => (@records.collect {|rec|rec.to_record}), :total => @records.length } end def create reject_non_attributes @record = model.new(params[:data]) success = @record.save error_messages = success ? nil : @record.errors.full_messages.join(', ') render :json => { :success => success, :message => error_messages || "Created the record", :data => @record.to_record} end def update reject_non_attributes success = @record.update_attributes(params["data"]) error_messages = success ? nil : @record.errors.full_messages.join(', ') render :json => { :success => success, :message => error_messages || "Updated the record", :data => @record.to_record} end def destroy success = @record.destroy render :json => { :success => success, :message => "Deleted the record"} end protected def find_record @record = model.find(params[:id]) end def find_records if params[:id] @records = [find_record] else @records = model.find(:all, :include => model.extjs_used_associations) end end def reject_non_attributes() #remove incoming attributes that aren't really columns names = model.column_names params["data"].reject!{|key, value| !names.include? key} end end