acts_as_controller_for 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Jay Hayes
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,3 @@
1
+ ActsAsControllerFor
2
+ ===================
3
+ As a README, I want to be descriptive so that I can help others become acquainted with this repository.
@@ -0,0 +1,178 @@
1
+ module ActsAsControllerFor
2
+ module Base
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ def acts_as_controller_for(model, options = {})
9
+ # Store the model and options for this controller in class variables for usage in actions
10
+ class_variable_set :@@model, model
11
+ class_variable_set :@@options, options
12
+
13
+ # Load and authorize (via +cancan+ gem) if set to
14
+ load_and_authorize_resource if options[:load_and_authorize]
15
+
16
+ # Define actions...
17
+ include InstanceMethods
18
+ end
19
+ end
20
+
21
+ module InstanceMethods
22
+
23
+ # <tt>block</tt> - the optional block defines the value of the models instance variable set for this action
24
+ # GET /models
25
+ # GET /models.xml
26
+ # GET /models.json
27
+ def index(&block)
28
+ model = self.class.send :class_variable_get, :@@model
29
+ options = self.class.send :class_variable_get, :@@options
30
+
31
+ inst_var_ref = "@#{model.name.underscore.pluralize}".to_sym
32
+ inst_var_p_ref = "@paginated_#{model.name.underscore.pluralize}".to_sym
33
+
34
+ if block
35
+ instance_variable_set options[:paginate] ? inst_var_p_ref : inst_var_ref, yield
36
+ else
37
+ if !options[:load_and_authorize]
38
+ instance_variable_set inst_var_ref, model.all
39
+ end
40
+
41
+ if options[:paginate]
42
+ instance_variable_set inst_var_p_ref, instance_variable_get(inst_var_ref).paginate(:page => params[:page], :per_page => model.per_page)
43
+ end
44
+ end
45
+
46
+ respond_to do |format|
47
+ format.html # index.html.(erb|haml)
48
+ format.xml { render :xml => instance_variable_get(inst_var_ref) }
49
+ format.json { render :json => instance_variable_get(inst_var_ref) }
50
+ end
51
+ end
52
+
53
+ # GET /models/:id
54
+ # GET /models/:id.xml
55
+ # GET /models/:id.json
56
+ def show
57
+ model = self.class.send :class_variable_get, :@@model
58
+ options = self.class.send :class_variable_get, :@@options
59
+
60
+ inst_var_ref = "@#{model.name.underscore}".to_sym
61
+
62
+ if !options[:load_and_authorize]
63
+ instance_variable_set inst_var_ref, model.find(params[:id])
64
+ end
65
+
66
+ respond_to do |format|
67
+ format.html # show.html.(erb|haml)
68
+ format.xml { render :xml => instance_variable_get(inst_var_ref) }
69
+ format.json { render :json => instance_variable_get(inst_var_ref) }
70
+ end
71
+ end
72
+
73
+ # GET /models/new
74
+ def new
75
+ model = self.class.send :class_variable_get, :@@model
76
+ options = self.class.send :class_variable_get, :@@options
77
+
78
+ inst_var_ref = "@#{model.name.underscore}".to_sym
79
+
80
+ if !options[:load_and_authorize]
81
+ instance_variable_set inst_var_ref, model.new
82
+ end
83
+ end
84
+
85
+ # GET /models/:id/edit
86
+ def edit
87
+ model = self.class.send :class_variable_get, :@@model
88
+ options = self.class.send :class_variable_get, :@@options
89
+
90
+ inst_var_ref = "@#{model.name.underscore}".to_sym
91
+
92
+ if !options[:load_and_authorize]
93
+ instance_variable_set inst_var_ref, model.find(params[:id])
94
+ end
95
+ end
96
+
97
+ # <tt>block</tt> - the optional block defines the value of the model instance variable set for this action
98
+ # POST /models
99
+ # POST /models.xml
100
+ # POST /models.json
101
+ def create(&block)
102
+ model = self.class.send :class_variable_get, :@@model
103
+ options = self.class.send :class_variable_get, :@@options
104
+
105
+ inst_var_ref = "@#{model.name.underscore}".to_sym
106
+
107
+ if block
108
+ instance_variable_set inst_var_ref, yield
109
+ else
110
+ if !options[:load_and_authorize]
111
+ instance_variable_set inst_var_ref, model.new(params[model.name.to_sym])
112
+ end
113
+ end
114
+
115
+ respond_to do |format|
116
+ if instance_variable_get(inst_var_ref).save
117
+ format.html { redirect_to(send("admin_#{model.name.underscore.pluralize}_path"), :notice => "#{model.name} was successfully created.") }
118
+ format.xml { render :xml => instance_variable_get(inst_var_ref), :status => :created, :location => [:admin, instance_variable_get(inst_var_ref)] }
119
+ format.json { render :json => instance_variable_get(inst_var_ref), :status => :created, :location => [:admin, instance_variable_get(inst_var_ref)] }
120
+ else
121
+ format.html { render :action => "new" }
122
+ format.xml { render :xml => instance_variable_get(inst_var_ref).errors, :status => :unprocessable_entity }
123
+ format.json { render :json => instance_variable_get(inst_var_ref).errors, :status => :unprocessable_entity }
124
+ end
125
+ end
126
+ end
127
+
128
+ # <tt>block</tt> - the optional block defines the hash of attributes to update the edited model with. Defaults to params[:model]
129
+ # PUT /models/:id
130
+ # PUT /models/:id.xml
131
+ # PUT /models/:id.json
132
+ def update(&block)
133
+ model = self.class.send :class_variable_get, :@@model
134
+ options = self.class.send :class_variable_get, :@@options
135
+
136
+ inst_var_ref = "@#{model.name.underscore}".to_sym
137
+
138
+ if !options[:load_and_authorize]
139
+ instance_variable_set inst_var_ref, model.find(params[:id])
140
+ end
141
+
142
+ respond_to do |format|
143
+ if instance_variable_get(inst_var_ref).update_attributes(block ? yield : params[model.name.underscore])
144
+ format.html { redirect_to(send("admin_#{model.name.underscore.pluralize}_path"), :notice => "#{model.name} was successfully updated.") }
145
+ format.xml { head :ok }
146
+ format.json { head :ok }
147
+ else
148
+ format.html { render :action => "edit" }
149
+ format.xml { render :xml => instance_variable_get(inst_var_ref).errors, :status => :unprocessable_entity }
150
+ format.json { render :json => instance_variable_get(inst_var_ref).errors, :status => :unprocessable_entity }
151
+ end
152
+ end
153
+ end
154
+
155
+ # DELETE /models/1
156
+ # DELETE /models/1.xml
157
+ # DELETE /models/1.json
158
+ def destroy
159
+ model = self.class.send :class_variable_get, :@@model
160
+ options = self.class.send :class_variable_get, :@@options
161
+
162
+ inst_var_ref = "@#{model.name.underscore}".to_sym
163
+
164
+ if !options[:load_and_authorize]
165
+ instance_variable_set inst_var_ref, model.new(params[model.name.to_sym])
166
+ end
167
+
168
+ instance_variable_get(inst_var_ref).destroy
169
+
170
+ respond_to do |format|
171
+ format.html { redirect_to(send("admin_#{model.name.underscore.pluralize}_path"), :notice => "#{model.name} was destroyed.") }
172
+ format.xml { head :ok }
173
+ format.json { head :ok }
174
+ end
175
+ end
176
+ end
177
+ end
178
+ end
@@ -0,0 +1,10 @@
1
+ module ActsAsControllerFor
2
+ module Version
3
+ MAJOR = 0
4
+ MINOR = 0
5
+ TINY = 1
6
+ PRE = nil
7
+
8
+ STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.')
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ require 'action_controller'
2
+ require 'acts_as_controller_for/base'
3
+ require 'acts_as_controller_for/version'
4
+
5
+ ActionController::Base.class_eval { include ActsAsControllerFor::Base }
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_controller_for
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Jay Hayes
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-07-05 00:00:00 Z
14
+ dependencies: []
15
+
16
+ description: This gem is an attempt to identify a pattern of simple rails controllers for common implementations
17
+ email: ur@iamvery.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README.md
26
+ - LICENSE
27
+ - lib/acts_as_controller_for/base.rb
28
+ - lib/acts_as_controller_for/version.rb
29
+ - lib/acts_as_controller_for.rb
30
+ homepage: https://github.com/iamvery/acts_as_controller_for
31
+ licenses: []
32
+
33
+ post_install_message:
34
+ rdoc_options: []
35
+
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ requirements: []
51
+
52
+ rubyforge_project:
53
+ rubygems_version: 1.8.5
54
+ signing_key:
55
+ specification_version: 3
56
+ summary: Common rails controller implementation
57
+ test_files: []
58
+