coast 0.9.0 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/coast.rb +14 -36
  2. data/spec/coast_spec.rb +245 -0
  3. metadata +8 -5
@@ -80,23 +80,19 @@ module Coast
80
80
  # -----------------------------------------------------------------------------------------------
81
81
  # begin restful actions
82
82
 
83
-
84
83
  # begin UI actions
85
84
  def new
86
85
  invoke_callback(:before_new)
87
86
  @resourceful_item ||= resourceful_model.new
88
87
  send(self.class.authorize_method, :new, @resourceful_item, request)
89
88
  init_instance_variables
90
-
91
89
  invoke_callback(:respond_to_new)
92
90
  unless performed?
93
91
  respond_to do |format|
94
92
  format.html { render :new }
95
- format.xml { render :xml => { :message => "Format not supported! Use the html format." } }
96
- format.json { render :json => { :message => "Format not supported! Use the html format." } }
93
+ format_json_and_xml(format, :message => "Format not supported! Use the html format.")
97
94
  end
98
95
  end
99
-
100
96
  invoke_callback(:after_new)
101
97
  end
102
98
 
@@ -106,36 +102,29 @@ module Coast
106
102
  send(self.class.authorize_method, :edit, @resourceful_item, request)
107
103
  init_instance_variables
108
104
  invoke_callback(:respond_to_edit)
109
-
110
105
  unless performed?
111
106
  respond_to do |format|
112
107
  format.html { render :edit }
113
- format.xml { render :xml => { :message => "Format not supported! Use the html format." } }
114
- format.json { render :json => { :message => "Format not supported! Use the html format." } }
108
+ format_json_and_xml(format, :message => "Format not supported! Use the html format.")
115
109
  end
116
110
  end
117
-
118
111
  invoke_callback(:after_edit)
119
112
  end
120
113
  # end UI actions
121
114
 
122
-
123
115
  # begin READ actions
124
116
  def index
125
117
  invoke_callback(:before_index)
126
118
  @resourceful_list ||= resourceful_model.all
127
119
  send(self.class.authorize_method, :index, @resourceful_list, request)
128
120
  init_instance_variables
129
-
130
121
  invoke_callback(:respond_to_index)
131
122
  unless performed?
132
123
  respond_to do |format|
133
124
  format.html { render :index }
134
- format.xml { render :xml => @resourceful_list }
135
- format.json { render :json => @resourceful_list }
125
+ format_json_and_xml(format, @resourceful_list)
136
126
  end
137
127
  end
138
-
139
128
  invoke_callback(:after_index)
140
129
  end
141
130
 
@@ -144,21 +133,17 @@ module Coast
144
133
  @resourceful_item ||= resourceful_model.find(params[:id])
145
134
  send(self.class.authorize_method, :show, @resourceful_item, request)
146
135
  init_instance_variables
147
-
148
136
  invoke_callback(:respond_to_show)
149
137
  unless performed?
150
138
  respond_to do |format|
151
139
  format.html { render :show }
152
- format.xml { render :xml => @resourceful_item }
153
- format.json { render :json => @resourceful_item }
140
+ format_json_and_xml(format, @resourceful_item)
154
141
  end
155
142
  end
156
-
157
143
  invoke_callback(:after_show)
158
144
  end
159
145
  # end READ actions
160
146
 
161
-
162
147
  # begin MUTATING actions
163
148
  def create
164
149
  invoke_callback(:before_create)
@@ -166,23 +151,19 @@ module Coast
166
151
  send(self.class.authorize_method, :create, @resourceful_item, request)
167
152
  init_instance_variables
168
153
  success = @skip_db_create || @resourceful_item.save
169
-
170
154
  invoke_callback(:respond_to_create)
171
155
  unless performed?
172
156
  respond_to do |format|
173
157
  if success
174
158
  flash[:notice] = "#{resourceful_model.name} was successfully created."
175
159
  format.html { redirect_to(@resourceful_item) }
176
- format.xml { render :xml => @resourceful_item, :status => :created, :location => @resourceful_item }
177
- format.json { render :json => @resourceful_item, :status => :created, :location => @resourceful_item }
160
+ format_json_and_xml(format, @resourceful_item, :status => :created, :location => @resourceful_item)
178
161
  else
179
162
  format.html { render :action => "new" }
180
- format.xml { render :xml => @resourceful_item.errors, :status => :unprocessable_entity }
181
- format.json { render :json => @resourceful_item.errors, :status => :unprocessable_entity }
163
+ format_json_and_xml(format, @resourceful_item.errors, :status => :unprocessable_entity)
182
164
  end
183
165
  end
184
166
  end
185
-
186
167
  invoke_callback(:after_create)
187
168
  end
188
169
 
@@ -192,23 +173,19 @@ module Coast
192
173
  send(self.class.authorize_method, :update, @resourceful_item, request)
193
174
  init_instance_variables
194
175
  success = @skip_db_update || @resourceful_item.update_attributes(params[resourceful_model.name.underscore])
195
-
196
176
  invoke_callback(:respond_to_update)
197
177
  unless performed?
198
178
  respond_to do |format|
199
179
  if success
200
180
  flash[:notice] = "#{resourceful_model.name} was successfully updated."
201
181
  format.html { redirect_to(@resourceful_item) }
202
- format.xml { render :xml => @resourceful_item }
203
- format.json { render :json => @resourceful_item }
182
+ format_json_and_xml(format, @resourceful_item)
204
183
  else
205
184
  format.html { render :action => "edit" }
206
- format.xml { render :xml => @resourceful_item.errors, :status => :unprocessable_entity }
207
- format.json { render :json => @resourceful_item.errors, :status => :unprocessable_entity }
185
+ format_json_and_xml(format, @resourceful_item.errors, :status => :unprocessable_entity)
208
186
  end
209
187
  end
210
188
  end
211
-
212
189
  invoke_callback(:after_update)
213
190
  end
214
191
 
@@ -218,17 +195,14 @@ module Coast
218
195
  send(self.class.authorize_method, :destroy, @resourceful_item, request)
219
196
  init_instance_variables
220
197
  @resourceful_item.destroy unless @skip_db_destroy
221
-
222
198
  invoke_callback(:respond_to_destroy)
223
199
  unless performed?
224
200
  flash[:notice] = "#{resourceful_model.name} was successfully destroyed" if @resourceful_item.destroyed?
225
201
  respond_to do |format|
226
202
  format.html { redirect_to root_url }
227
- format.xml { render :xml => @resourceful_item }
228
- format.json { render :json => @resourceful_item }
203
+ format_json_and_xml(format, @resourceful_item)
229
204
  end
230
205
  end
231
-
232
206
  invoke_callback(:after_destroy)
233
207
  end
234
208
  # end MUTATING actions
@@ -250,7 +224,6 @@ module Coast
250
224
  def item_instance_var_name
251
225
  return @item_instance_var_name if @item_instance_var_name
252
226
  name = resourceful_model.name.underscore.gsub("/", "_")
253
- puts name
254
227
  @item_instance_var_name ||= "@#{name}"
255
228
  end
256
229
 
@@ -264,4 +237,9 @@ module Coast
264
237
  send(name) if respond_to?(name)
265
238
  end
266
239
 
240
+ def format_json_and_xml(format, data, options={})
241
+ format.xml { render({:xml => data}.merge(options)) }
242
+ format.json { render({:json => data}.merge(options)) }
243
+ end
244
+
267
245
  end
@@ -0,0 +1,245 @@
1
+ require "rubygems"
2
+
3
+ # fix MiniTest issue with 1.9
4
+ unless defined? Gem::Deprecate
5
+ module Gem
6
+ Deprecate = Module.new do
7
+ include Deprecate
8
+ end
9
+ end
10
+ end
11
+
12
+ require "bundler/setup"
13
+ require "minitest/autorun"
14
+ require "active_support/all"
15
+ require "turn"
16
+ require "pry"
17
+
18
+ dirname = File.dirname(__FILE__)
19
+ require File.join(dirname, "mock")
20
+ Dir.glob(File.join(dirname, "..", "lib", "*.rb")) { |f| require f }
21
+ MODEL_PATH = File.join(dirname, "testable_model.rb")
22
+ CONTROLLER_PATH = File.join(dirname, "testable_controller.rb")
23
+ load MODEL_PATH
24
+ load CONTROLLER_PATH
25
+
26
+
27
+
28
+
29
+ describe Coast do
30
+ RESTFUL_METHODS = %w(index show new edit create update destroy)
31
+ ITEM_METHODS = %w(show new edit create update destroy)
32
+ UI_METHODS = %w(new edit)
33
+ READ_METHODS = %w(index show)
34
+ MUTATE_METHODS = %w(create update destroy)
35
+
36
+ # Resets the mock class defs.
37
+ def reset
38
+ Coast.send(:remove_const, :TestableController)
39
+ Coast.send(:remove_const, :TestableModel)
40
+ load MODEL_PATH
41
+ load CONTROLLER_PATH
42
+ Coast::TestableController.set_resourceful_model Coast::TestableModel
43
+ end
44
+
45
+ # class method tests ============================================================================
46
+
47
+ describe :set_authorize_method do
48
+ it "sets the method used to perform authorization checks" do
49
+ reset
50
+ Coast::TestableController.set_authorize_method :authorize!
51
+ assert Coast::TestableController.authorize_method == :authorize!
52
+ end
53
+ end
54
+
55
+ describe :authorize_method= do
56
+ it "sets the method used to perform authorization checks" do
57
+ reset
58
+ Coast::TestableController.authorize_method = :authorize!
59
+ assert Coast::TestableController.authorize_method == :authorize!
60
+ end
61
+ end
62
+
63
+ describe :set_resourceful_model do
64
+ it "sets the model that the controller manages" do
65
+ reset
66
+ model = Object.new
67
+ Coast::TestableController.set_resourceful_model model
68
+ assert Coast::TestableController.resourceful_model == model
69
+ end
70
+ end
71
+
72
+ describe :resourceful_model= do
73
+ it "sets the model that the controller manages" do
74
+ reset
75
+ model = Object.new
76
+ Coast::TestableController.resourceful_model = model
77
+ assert Coast::TestableController.resourceful_model == model
78
+ end
79
+ end
80
+
81
+ def verify_callback_setter(event, action)
82
+ reset
83
+ Coast::TestableController.send(event, action, &(lambda {}))
84
+ assert Coast::TestableController.new.respond_to?("#{event}_#{action}")
85
+ end
86
+
87
+ describe :before do
88
+ it "stores a before callback for all RESTful actions" do
89
+ RESTFUL_METHODS.each { |m| verify_callback_setter :before, m }
90
+ end
91
+ end
92
+
93
+ describe :respond_to do
94
+ it "stores a respond_to callback for all RESTful actions" do
95
+ RESTFUL_METHODS.each { |m| verify_callback_setter :respond_to, m }
96
+ end
97
+ end
98
+
99
+ describe :after do
100
+ it "stores an after callback for all RESTful actions" do
101
+ RESTFUL_METHODS.each { |m| verify_callback_setter :after, m }
102
+ end
103
+ end
104
+
105
+
106
+
107
+
108
+
109
+
110
+
111
+
112
+
113
+
114
+
115
+ # instance method tests =========================================================================
116
+
117
+
118
+ it "supports all RESTful methods" do
119
+ reset
120
+ controller = Coast::TestableController.new
121
+ RESTFUL_METHODS.each { |m| controller.must_respond_to m }
122
+ end
123
+
124
+ # %w(index show new).each do |method|
125
+ RESTFUL_METHODS.each do |method|
126
+ describe "##{method}" do
127
+ before do
128
+ reset
129
+ end
130
+
131
+ it "responds and renders" do
132
+ controller = Coast::TestableController.new
133
+ controller.send(method)
134
+ assert controller.responded?, "Did not respond"
135
+ assert controller.rendered?, "Did not render"
136
+ end
137
+
138
+ it "renders for the formats html, xml, json" do
139
+ controller = Coast::TestableController.new
140
+ controller.send(method)
141
+ assert controller.html, "Did not respond to the html format"
142
+ assert controller.xml, "Did not respond to the xml format"
143
+ assert controller.json, "Did not respond to the json format"
144
+ end
145
+
146
+ it "invokes the callbacks before, respond_to, after" do
147
+ callbacks = []
148
+
149
+ Coast::TestableController.class_eval do
150
+ before(method) { callbacks << :before }
151
+ respond_to(method) { callbacks << :respond_to }
152
+ after(method) { callbacks << :after }
153
+ end
154
+
155
+ controller = Coast::TestableController.new
156
+ controller.send(method)
157
+ assert callbacks.include?(:before), "Did not invoke the before callback"
158
+ assert callbacks.include?(:respond_to), "Did not invoke the respond_to callback"
159
+ assert callbacks.include?(:after), "Did not invoke the after callback"
160
+ end
161
+
162
+ it "allows :respond_to callback to perform the render" do
163
+ Coast::TestableController.respond_to(method) { @performed = true }
164
+ controller = Coast::TestableController.new
165
+ controller.send(method)
166
+ assert controller.responded? == false, "Did not allow :respond_to callback to perform the render"
167
+ end
168
+
169
+ it "invokes the authorize_method when set" do
170
+ Coast::TestableController.authorize_method = :authorize!
171
+ controller = Coast::TestableController.new
172
+ controller.send(:index)
173
+ assert controller.authorize_invoked?, "Did not invoke the authorize_method"
174
+ end
175
+
176
+ end
177
+ end
178
+
179
+ ITEM_METHODS.each do |method|
180
+ describe "##{method}" do
181
+ before do
182
+ reset
183
+ end
184
+
185
+ it "allows :before callback to set the item" do
186
+ item = Object.new
187
+ Coast::TestableController.before(:index) { @resourceful_item = item }
188
+ controller = Coast::TestableController.new
189
+ controller.index
190
+ assert controller.instance_eval { @resourceful_item } == item, "Did not allow :before callback to set the resourceful_item"
191
+ end
192
+
193
+ it "sets a custom named instance variable for the item" do
194
+ item = Object.new
195
+ Coast::TestableController.before(:index) { @resourceful_item = item }
196
+ controller = Coast::TestableController.new
197
+ controller.index
198
+ variable = controller.instance_eval { instance_variable_get(item_instance_var_name) }
199
+ assert variable != nil, "Did not set a custom instance variable for the item"
200
+ assert variable == item, "Did not set a custom instance variable for the item to the correct value"
201
+ end
202
+
203
+ end
204
+ end
205
+
206
+ MUTATE_METHODS.each do |method|
207
+ describe "##{method}" do
208
+ before do
209
+ reset
210
+ end
211
+
212
+ it "redirects" do
213
+ controller = Coast::TestableController.new
214
+ controller.send(method)
215
+ assert controller.redirected?, "Did not redirect"
216
+ end
217
+ end
218
+ end
219
+
220
+
221
+ describe "#index" do
222
+ before do
223
+ reset
224
+ end
225
+
226
+ it "allows :before callback to set the list" do
227
+ list = [ Object.new ]
228
+ Coast::TestableController.before(:index) { @resourceful_list = list }
229
+ controller = Coast::TestableController.new
230
+ controller.index
231
+ assert controller.instance_eval { @resourceful_list } == list, "Did not allow :before callback to set the resourceful_list"
232
+ end
233
+
234
+ it "sets a custom named instance variable for the item" do
235
+ list = [ Object.new ]
236
+ Coast::TestableController.before(:index) { @resourceful_list = list }
237
+ controller = Coast::TestableController.new
238
+ controller.index
239
+ variable = controller.instance_eval { instance_variable_get(list_instance_var_name) }
240
+ assert variable != nil, "Did not set a custom instance variable for the item"
241
+ assert variable == list, "Did not set a custom instance variable for the item to the correct value"
242
+ end
243
+ end
244
+
245
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: coast
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-04-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &2164573380 !ruby/object:Gem::Requirement
16
+ requirement: &2160361220 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,14 +21,16 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2164573380
24
+ version_requirements: *2160361220
25
25
  description: Resourceful behavior for Rails controllers with a Sinatra like DSL.
26
- email: nate.hop@gmail.com
26
+ email:
27
+ - nate.hop@gmail.com
27
28
  executables: []
28
29
  extensions: []
29
30
  extra_rdoc_files: []
30
31
  files:
31
32
  - lib/coast.rb
33
+ - spec/coast_spec.rb
32
34
  homepage: https://github.com/hopsoft/coast
33
35
  licenses:
34
36
  - MIT
@@ -54,4 +56,5 @@ rubygems_version: 1.8.10
54
56
  signing_key:
55
57
  specification_version: 3
56
58
  summary: Coast
57
- test_files: []
59
+ test_files:
60
+ - spec/coast_spec.rb