0
+module ResourceThis # :nodoc:
0
+ def self.included(base)
0
+ base.extend(ClassMethods)
0
+ def resource_this(options = {})
0
+ options.assert_valid_keys(:class_name, :will_paginate, :sort_method)
0
+ singular_name = options[:class_name].downcase.singularize || controller_name.singularize
0
+ class_name = options[:class_name] || singular_name.camelize
0
+ plural_name = singular_name.pluralize
0
+ will_paginate_index = options[:class_name] || false
0
+ module_eval <<-"end_eval", __FILE__, __LINE__
0
+ if will_paginate_index
0
+ instance_variable_set("@#{plural_name}", class_name.paginate(:page => params[:page]))
0
+ instance_variable_set("@#{plural_name}", class_name.find(:all))
0
+ #TODO: add sorting customizable by subclassed controllers
0
+ respond_to do |format|
0
+ format.xml { render :xml => instance_variable_get("@#{plural_name}") }
0
+ instance_variable_set("@#{singular_name}", class_name.find(params[:id]))
0
+ respond_to do |format|
0
+ format.xml { render :xml => instance_variable_get("@#{singular_name}") }
0
+ instance_variable_set("@#{singular_name}", class_name.new)
0
+ respond_to do |format|
0
+ format.html { render :action => :edit }
0
+ format.xml { render :xml => instance_variable_get("@#{singular_name}") }
0
+ instance_variable_set("@#{singular_name}", class_name.create!(params_hash))
0
+ flash[:notice] = "#{class_name} was successfully created."
0
+ respond_to do |format|
0
+ format.html { redirect_to :action => :index }
0
+ format.xml { render :xml => instance_variable_get("@#{singular_name}"), :status => :created, :location => instance_variable_get("@#{singular_name}") }
0
+ rescue ActiveRecord::RecordInvalid
0
+ flash[:error] = instance_variable_get("@#{singular_name}").errors
0
+ respond_to do |format|
0
+ format.html { render :action => :new }
0
+ format.xml { render :xml => instance_variable_get("@#{singular_name}").errors, :status => :unprocessable_entity }
0
+ instance_variable_set("@#{singular_name}", class_name.find(params[:id]))
0
+ instance_variable_set("@#{singular_name}", class_name.find(params[:id]))
0
+ eval("@#{singular_name}").update_attributes!(params_hash)
0
+ flash[:notice] = "#{class_name} was successfully updated."
0
+ respond_to do |format|
0
+ format.html { redirect_to(instance_variable_get("@#{singular_name}")) }
0
+ format.xml { head :ok }
0
+ rescue ActiveRecord::RecordInvalid
0
+ flash[:error] = instance_variable_get("@#{singular_name}").errors
0
+ respond_to do |format|
0
+ format.html { render :action => :edit }
0
+ format.xml { render :xml => instance_variable_get("@#{singular_name}").errors, :status => :unprocessable_entity }
0
+ class_name.find(params[:id]).destroy
0
+ respond_to do |format|
0
+ format.html { redirect_to :action => :index }
0
+ format.xml { head :ok }
Comments
No one has commented yet.