Created
November 19, 2009 05:57
-
-
Save danielbeardsley/238577 to your computer and use it in GitHub Desktop.
A Generalized Json Controller for use with extjs-mvc and Restful CRUD
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 characters
| 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment