Skip to content
Browse files

refactor to use before_filters

git-svn-id: http://jnewland.com/svn/public/ruby/rails/plugins/resource_this@21 9b6b69f6-dd27-0410-8144-a0f3c56a22ea
  • Loading branch information...
1 parent eaaaf74 commit 6668104fd3780a0aa8686b9bce2224bd8d10cf88 jnewland committed
Showing with 63 additions and 50 deletions.
  1. +0 −9 Rakefile
  2. +63 −41 lib/resource_this.rb
View
9 Rakefile
@@ -11,12 +11,3 @@ Rake::TestTask.new(:test) do |t|
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end
-
-desc 'Generate documentation for the resouce_this plugin.'
-Rake::RDocTask.new(:rdoc) do |rdoc|
- rdoc.rdoc_dir = 'rdoc'
- rdoc.title = 'resouce_this'
- rdoc.options << '--line-numbers' << '--inline-source'
- rdoc.rdoc_files.include('README')
- rdoc.rdoc_files.include('lib/**/*.rb')
-end
View
104 lib/resource_this.rb
@@ -13,42 +13,73 @@ def resource_this(options = {})
plural_name = singular_name.pluralize
will_paginate_index = options[:will_paginate] || false
+ module_eval <<-"end_eval", __FILE__, __LINE__
+ before_filter :load_#{singular_name}, :only => [ :create, :update, :destroy ]
+ before_filter :load_#{plural_name}, :only => [ :index ]
+ before_filter :new_#{singular_name}, :only => [ :new ]
+ before_filter :create_#{singular_name}, :only => [ :create ]
+ before_filter :update_#{singular_name}, :only => [ :update ]
+ before_filter :destroy_#{singular_name}, :only => [ :destoy ]
+
+ protected
+ def load_#{singular_name}
+ @#{singular_name} = #{class_name}.find(params[:id])
+ end
+
+ def new_#{singular_name}
+ @#{singular_name} = #{class_name}.new
+ end
+
+ def create_#{singular_name}
+ @#{singular_name} = #{class_name}.new(params[:#{singular_name}])
+ @#{singular_name} = @#{singular_name}.save
+ end
+
+ def update_#{singular_name}
+ @#{singular_name} = #{class_name}.update_attributes(params[:#{singular_name}])
+ end
+
+ def destroy_#{singular_name}
+ @#{singular_name} = @#{singular_name}.destroy
+ end
+ end_eval
+
+
if will_paginate_index
module_eval <<-"end_eval", __FILE__, __LINE__
- def index
+ def load_#{plural_name}
@#{plural_name} = #{class_name}.paginate(:page => params[:page])
#TODO: add sorting customizable by subclassed controllers
- respond_to do |format|
- format.html
- format.xml { render :xml => @#{plural_name} }
- end
end
end_eval
else
module_eval <<-"end_eval", __FILE__, __LINE__
- def index
+ def load_#{plural_name}
@#{plural_name} = #{class_name}.find(:all)
#TODO: add sorting customizable by subclassed controllers
- respond_to do |format|
- format.html
- format.xml { render :xml => @#{plural_name} }
- end
end
end_eval
end
module_eval <<-"end_eval", __FILE__, __LINE__
+ public
+ def index
+ @#{plural_name} = #{class_name}.paginate(:page => params[:page])
+ #TODO: add sorting customizable by subclassed controllers
+ respond_to do |format|
+ format.html
+ format.xml { render :xml => @#{plural_name} }
+ end
+ end
- def show
- @#{singular_name} = #{class_name}.find(params[:id])
+ def show
respond_to do |format|
format.html
format.xml { render :xml => @#{singular_name} }
end
end
- def new
- @#{singular_name} = #{class_name}.new
+ def new
respond_to do |format|
format.html { render :action => :edit }
format.xml { render :xml => @#{singular_name} }
@@ -56,49 +87,40 @@ def new
end
def create
- @#{singular_name} = #{class_name}.create!(params[:#{singular_name}])
- flash[:notice] = "#{class_name} was successfully created."
respond_to do |format|
- format.html { redirect_to :action => :index }
- format.xml { render :xml => @#{singular_name}, :status => :created, :location => @#{singular_name} }
- end
- rescue ActiveRecord::RecordInvalid
- flash[:error] = @#{singular_name}.errors
- respond_to do |format|
- format.html { render :action => :new }
- format.xml { render :xml => @#{singular_name}.errors, :status => :unprocessable_entity }
+ if @#{singular_name}
+ flash[:notice] = '#{class_name} was successfully created.'
+ format.html { redirect_to @#{singular_name} }
+ format.xml { render :xml => @#{singular_name}, :status => :created, :location => @#{singular_name} }
+ else
+ format.html { render :action => :new }
+ format.xml { render :xml => @#{singular_name}.errors, :status => :unprocessable_entity }
+ end
end
end
def edit
- @#{singular_name} = #{class_name}.find(params[:id])
end
def update
- @#{singular_name} = #{class_name}.find(params[:id])
- @#{singular_name}.update_attributes!(params[:#{singular_name}])
- flash[:notice] = "#{class_name} was successfully updated."
respond_to do |format|
- format.html { redirect_to @#{singular_name} }
- format.xml { head :ok }
- end
- rescue ActiveRecord::RecordInvalid
- flash[:error] = @#{singular_name}.errors
- respond_to do |format|
- format.html { render :action => :edit }
- format.xml { render :xml => @#{singular_name}.errors, :status => :unprocessable_entity }
+ if @#{singular_name}
+ flash[:notice] = '#{class_name} was successfully updated.'
+ format.html { redirect_to @#{singular_name} }
+ format.xml { head :ok }
+ else
+ format.html { render :action => :edit }
+ format.xml { render :xml => @#{singular_name.errors, :status => :unprocessable_entity }
+ end
end
end
- def destroy
- @#{singular_name} = #{class_name}.find(params[:id])
- @#{singular_name} = @#{singular_name}.destroy
+ def destroy
respond_to do |format|
- format.html { redirect_to :action => :index }
+ format.html { redirect_to :action => #{plural_name}_url }
format.xml { head :ok }
end
end
-
end_eval
end
end

0 comments on commit 6668104

Please sign in to comment.
Something went wrong with that request. Please try again.