Created
October 21, 2012 23:48
-
-
Save Yuffster/3928976 to your computer and use it in GitHub Desktop.
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 CrudController < ApplicationController | |
| # GET /<models> | |
| # GET /<models>.json | |
| def index | |
| respond_with call_on_model(:scoped) | |
| end | |
| # GET /<models>/1 | |
| # GET /<models>/1.json | |
| def show | |
| respond_with find_on_model params[:id] | |
| end | |
| # GET /<models>/new | |
| # GET /<models>/new.json | |
| def new | |
| respond_with call_on_model(:new) | |
| end | |
| # GET /<models>/1/edit | |
| def edit | |
| respond_with find_on_model params[:id] | |
| end | |
| # POST /<models> | |
| # POST /<models>.json | |
| def create | |
| @obj = call_on_model(:new, params[model_name.to_sym]) | |
| respond_to do |format| | |
| if @obj.save | |
| respond_with @obj | |
| else | |
| format.html { render :action => "new" } | |
| respond_with @obj.errors | |
| end | |
| end | |
| end | |
| # PUT /<models>/1 | |
| # PUT /<models>/1 | |
| # PUT /<models>/1.js | |
| def update | |
| @obj = find_on_model params[:id] | |
| respond_to do |format| | |
| if @obj.update_attributes(params[model_name.to_sym]) | |
| respond_with @obj | |
| else | |
| format.html { render :action => "edit" } | |
| format.json respond_with @obj.errors | |
| end | |
| end | |
| end | |
| # DELETE /<models>/1 | |
| # DELETE /<models>/1.json | |
| def destroy | |
| (find_on_model params[:id]).destroy | |
| respond_to do |format| | |
| format.html { redirect_to(model_name.pluralize+'_url') } | |
| format.json { head :ok } | |
| end | |
| end | |
| private | |
| def get_model_name | |
| nil | |
| end | |
| def model_name | |
| get_model_name || self.class.to_s.gsub(/Controller$/, '').singularize | |
| end | |
| def find_on_model *args | |
| call_on_model(:find, *args) | |
| end | |
| def call_on_model meth, *args | |
| User.send(meth, *args) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment