Skip to content

Instantly share code, notes, and snippets.

@danielbeardsley
Created November 19, 2009 05:57
Show Gist options
  • Select an option

  • Save danielbeardsley/238577 to your computer and use it in GitHub Desktop.

Select an option

Save danielbeardsley/238577 to your computer and use it in GitHub Desktop.

Revisions

  1. danielbeardsley revised this gist Apr 30, 2010. 1 changed file with 30 additions and 39 deletions.
    69 changes: 30 additions & 39 deletions json_controller.rb
    Original file line number Diff line number Diff line change
    @@ -1,51 +1,36 @@
    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 ]
    before_filter :find_record, :only => [ :get, :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
    class << self
    attr_accessor :model_class
    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
    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 # index.html.erb (no data required)
    format.json { render :json => {:success => true, :total => @records.length, :data => (@records.collect {|rec|rec.to_record}) } }
    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 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])
    @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 = 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
    @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}
    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
    end
  2. danielbeardsley renamed this gist Nov 19, 2009. 1 changed file with 0 additions and 0 deletions.
  3. danielbeardsley created this gist Nov 19, 2009.
    96 changes: 96 additions & 0 deletions JsonController for use with extjs-mvc
    Original 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