Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module RestfulScaffold
- def self.included(controller)
- controller.extend ClassMethods
- end
- module ClassMethods
- def rest_methods_for(table_name, *args)
- module_eval do
- after_filter :standard_respond_to, :only => [ :index, :show, :new ]
- after_filter :set_friendly_instance_variable
- # if table_name == :people; return People
- define_method :model_class do
- table_name.to_s.classify.constantize
- end
- # if table_name == :people; return "person"
- define_method :friendly_model_var_name do
- table_name.to_s.singularize
- end
- # if table_name == :people; return "people"
- define_method :friendly_model_var_name_plural do
- table_name.to_s
- end
- define_method :set_friendly_instance_variable do
- if defined?(@model)
- instance_variable_set("@#{friendly_model_var_name}", @model)
- elsif defined?(@models)
- instance_variable_set("@#{friendly_model_var_name_plural}", @models)
- end
- end
- # if table_name == :people, return result of people_path
- define_method :friendly_model_index_path do
- eval "#{table_name}_path"
- end
- include RestMethods
- end
- end
- end
- module RestMethods
- def index
- @models = model_class.find(:all)
- end
- def show
- @model = model_class.find(params[:id])
- end
- def new
- @model = model_class.new
- end
- def edit
- @model = model_class.find(params[:id])
- end
- def create
- @model = model_class.new(params[friendly_model_var_name])
- respond_to do |format|
- if @model.save
- flash[:notice] = "#{friendly_model_var_name.humanize} was successfully updated."
- format.html { redirect_to(self.instance_variable_get("@#{singular}")) }
- format.xml { head :ok }
- format.json { render :json => @model.to_json }
- else
- format.html { render :action => "edit" }
- format.xml { render :xml => @model.errors, :status => :unprocessable_entity }
- format.json { render :json => @model, :status => :unprocessable_entity }
- end
- end
- end
- def update
- @model = model_class.find(params[:id])
- respond_to do |format|
- if @model.update_attributes(params[friendly_model_var_name])
- flash[:notice] = "#{friendly_model_var_name.humanize} was successfully updated."
- format.html { redirect_to(@model) }
- format.xml { head :ok }
- format.json { render :json => @model.to_json }
- else
- format.html { render :action => "edit" }
- format.xml { render :xml => @model.errors, :status => :unprocessable_entity }
- format.json { render :json => @model, :status => :unprocessable_entity }
- end
- end
- end
- def destroy
- @model = model_class.find(params[:id])
- @model.destroy
- respond_to do |format|
- format.html { redirect_to( friendly_model_index_path ) }
- format.xml { head :ok }
- format.json { head :ok }
- end
- end
- private
- def standard_respond_to
- respond_to do |format|
- format.html
- format.xml { render :xml => @model }
- format.json { render :json => @model }
- end
- end
- end
- end
- # Automatically include it so we only have to use the
- # macro-style declaration to get the restful goodness
- class ActionController::Base
- include RestfulScaffold
- end
Add Comment
Please, Sign In to add comment