public
Description: Not scaffolding. Resourcing. Creates extremely customizable resource controllers with one line of code.
Clone URL: git://github.com/jnewland/resource_this.git
Search Repo:
refactor to use before_filters

git-svn-id: 
http://jnewland.com/svn/public/ruby/rails/plugins/resource_this@21 
9b6b69f6-dd27-0410-8144-a0f3c56a22ea
jnewland (author)
Sun Sep 16 12:03:44 -0700 2007
commit  6668104fd3780a0aa8686b9bce2224bd8d10cf88
tree    a82564a08017f058372af104f4e6604438bd97f7
parent  eaaaf7499745419dace8337c642a147efc74622e
...
11
12
13
14
15
16
17
18
19
20
21
22
...
11
12
13
 
 
 
 
 
 
 
 
 
0
@@ -11,13 +11,4 @@
0
   t.pattern = 'test/**/*_test.rb'
0
   t.verbose = true
0
 end
0
-
0
-desc 'Generate documentation for the resouce_this plugin.'
0
-Rake::RDocTask.new(:rdoc) do |rdoc|
0
- rdoc.rdoc_dir = 'rdoc'
0
- rdoc.title = 'resouce_this'
0
- rdoc.options << '--line-numbers' << '--inline-source'
0
- rdoc.rdoc_files.include('README')
0
- rdoc.rdoc_files.include('lib/**/*.rb')
0
-end
...
13
14
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
17
18
 
19
20
21
22
23
24
25
26
27
28
29
 
30
31
32
33
34
35
36
37
38
39
40
 
 
 
 
 
 
 
 
 
41
42
43
 
44
45
46
47
48
49
50
51
 
52
53
54
55
56
57
58
59
60
61
62
63
...
56
57
58
59
60
61
62
63
 
 
 
 
 
 
 
 
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
 
 
 
 
 
 
 
 
84
85
86
87
88
89
90
91
92
93
94
95
 
96
97
 
98
99
100
101
102
103
104
...
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 
50
51
52
 
 
 
 
53
54
55
56
 
57
58
59
 
 
 
 
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
 
 
75
76
77
78
79
80
81
 
 
82
83
84
85
86
87
88
89
90
91
92
93
94
...
87
88
89
 
 
90
 
 
91
92
93
94
95
96
97
98
99
 
 
 
 
 
 
100
101
102
 
103
104
105
 
 
 
106
 
 
107
108
109
110
111
112
113
114
115
 
 
 
 
 
 
116
117
 
 
 
118
119
 
120
121
122
123
 
124
125
126
0
@@ -13,42 +13,73 @@
0
       plural_name = singular_name.pluralize
0
       will_paginate_index = options[:will_paginate] || false
0
       
0
+ module_eval <<-"end_eval", __FILE__, __LINE__
0
+ before_filter :load_#{singular_name}, :only => [ :create, :update, :destroy ]
0
+ before_filter :load_#{plural_name}, :only => [ :index ]
0
+ before_filter :new_#{singular_name}, :only => [ :new ]
0
+ before_filter :create_#{singular_name}, :only => [ :create ]
0
+ before_filter :update_#{singular_name}, :only => [ :update ]
0
+ before_filter :destroy_#{singular_name}, :only => [ :destoy ]
0
+
0
+ protected
0
+ def load_#{singular_name}
0
+ @#{singular_name} = #{class_name}.find(params[:id])
0
+ end
0
+
0
+ def new_#{singular_name}
0
+ @#{singular_name} = #{class_name}.new
0
+ end
0
+
0
+ def create_#{singular_name}
0
+ @#{singular_name} = #{class_name}.new(params[:#{singular_name}])
0
+ @#{singular_name} = @#{singular_name}.save
0
+ end
0
+
0
+ def update_#{singular_name}
0
+ @#{singular_name} = #{class_name}.update_attributes(params[:#{singular_name}])
0
+ end
0
+
0
+ def destroy_#{singular_name}
0
+ @#{singular_name} = @#{singular_name}.destroy
0
+ end
0
+ end_eval
0
+
0
+
0
       if will_paginate_index
0
         module_eval <<-"end_eval", __FILE__, __LINE__
0
- def index
0
+ def load_#{plural_name}
0
             @#{plural_name} = #{class_name}.paginate(:page => params[:page])
0
             #TODO: add sorting customizable by subclassed controllers
0
- respond_to do |format|
0
- format.html
0
- format.xml { render :xml => @#{plural_name} }
0
- end
0
           end
0
         end_eval
0
       else
0
         module_eval <<-"end_eval", __FILE__, __LINE__
0
- def index
0
+ def load_#{plural_name}
0
             @#{plural_name} = #{class_name}.find(:all)
0
             #TODO: add sorting customizable by subclassed controllers
0
- respond_to do |format|
0
- format.html
0
- format.xml { render :xml => @#{plural_name} }
0
- end
0
           end
0
         end_eval
0
       end
0
 
0
       module_eval <<-"end_eval", __FILE__, __LINE__
0
+ public
0
+ def index
0
+ @#{plural_name} = #{class_name}.paginate(:page => params[:page])
0
+ #TODO: add sorting customizable by subclassed controllers
0
+ respond_to do |format|
0
+ format.html
0
+ format.xml { render :xml => @#{plural_name} }
0
+ end
0
+ end
0
 
0
- def show
0
- @#{singular_name} = #{class_name}.find(params[:id])
0
+ def show
0
           respond_to do |format|
0
             format.html
0
             format.xml { render :xml => @#{singular_name} }
0
           end
0
         end
0
 
0
- def new
0
- @#{singular_name} = #{class_name}.new
0
+ def new
0
           respond_to do |format|
0
             format.html { render :action => :edit }
0
             format.xml { render :xml => @#{singular_name} }
0
0
0
0
0
0
0
0
0
0
@@ -56,49 +87,40 @@
0
         end
0
 
0
         def create
0
- @#{singular_name} = #{class_name}.create!(params[:#{singular_name}])
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 => @#{singular_name}, :status => :created, :location => @#{singular_name} }
0
+ if @#{singular_name}
0
+ flash[:notice] = '#{class_name} was successfully created.'
0
+ format.html { redirect_to @#{singular_name} }
0
+ format.xml { render :xml => @#{singular_name}, :status => :created, :location => @#{singular_name} }
0
+ else
0
+ format.html { render :action => :new }
0
+ format.xml { render :xml => @#{singular_name}.errors, :status => :unprocessable_entity }
0
+ end
0
           end
0
- rescue ActiveRecord::RecordInvalid
0
- flash[:error] = @#{singular_name}.errors
0
- respond_to do |format|
0
- format.html { render :action => :new }
0
- format.xml { render :xml => @#{singular_name}.errors, :status => :unprocessable_entity }
0
- end
0
         end
0
 
0
         def edit
0
- @#{singular_name} = #{class_name}.find(params[:id])
0
         end
0
 
0
         def update
0
- @#{singular_name} = #{class_name}.find(params[:id])
0
- @#{singular_name}.update_attributes!(params[:#{singular_name}])
0
- flash[:notice] = "#{class_name} was successfully updated."
0
           respond_to do |format|
0
- format.html { redirect_to @#{singular_name} }
0
- format.xml { head :ok }
0
+ if @#{singular_name}
0
+ flash[:notice] = '#{class_name} was successfully updated.'
0
+ format.html { redirect_to @#{singular_name} }
0
+ format.xml { head :ok }
0
+ else
0
+ format.html { render :action => :edit }
0
+ format.xml { render :xml => @#{singular_name.errors, :status => :unprocessable_entity }
0
+ end
0
           end
0
- rescue ActiveRecord::RecordInvalid
0
- flash[:error] = @#{singular_name}.errors
0
- respond_to do |format|
0
- format.html { render :action => :edit }
0
- format.xml { render :xml => @#{singular_name}.errors, :status => :unprocessable_entity }
0
- end
0
         end
0
 
0
- def destroy
0
- @#{singular_name} = #{class_name}.find(params[:id])
0
- @#{singular_name} = @#{singular_name}.destroy
0
+ def destroy
0
           respond_to do |format|
0
- format.html { redirect_to :action => :index }
0
+ format.html { redirect_to :action => #{plural_name}_url }
0
             format.xml { head :ok }
0
           end
0
         end
0
-
0
       end_eval
0
     end
0
   end

Comments

    No one has commented yet.