exposure 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest.txt +0 -2
- data/README.mdown +61 -8
- data/Rakefile +1 -4
- data/lib/exposure/common.rb +12 -5
- data/lib/exposure/configuration.rb +3 -2
- data/lib/exposure.rb +4 -3
- data/spec/exposure_spec.rb +21 -1
- data/spec/spec_helper.rb +10 -1
- metadata +5 -7
- data/lib/exposure/patterns/resource.rb +0 -226
- data/spec/resource_spec.rb +0 -192
data/Manifest.txt
CHANGED
@@ -5,7 +5,6 @@ exposure.gemspec
|
|
5
5
|
lib/exposure.rb
|
6
6
|
lib/exposure/common.rb
|
7
7
|
lib/exposure/configuration.rb
|
8
|
-
lib/exposure/patterns/resource.rb
|
9
8
|
lib/exposure/patterns/resources.rb
|
10
9
|
script/console
|
11
10
|
script/destroy
|
@@ -28,7 +27,6 @@ spec/flashers/flash_with_block_spec.rb
|
|
28
27
|
spec/flashers/flash_with_method_spec.rb
|
29
28
|
spec/flashers/flash_with_proc_spec.rb
|
30
29
|
spec/flashers/flash_with_string_spec.rb
|
31
|
-
spec/resource_spec.rb
|
32
30
|
spec/resources_spec.rb
|
33
31
|
spec/responders/respond_to_mime_types_spec.rb
|
34
32
|
spec/responders/respond_with_block_spec.rb
|
data/README.mdown
CHANGED
@@ -1,19 +1,72 @@
|
|
1
|
-
exposure
|
2
|
-
|
1
|
+
# exposure
|
2
|
+
Exposure is a Rails is a controller abstraction layer like [`resource_this`](http://github.com/jnewland/resource_this), [`make_resourceful`](http://github.com/hcatlin/make_resourceful) and [`resource_controller`](resource_controller) with the same goal: exposing resources at the controller level with fewer headaches.
|
3
3
|
|
4
|
-
|
5
|
-
|
4
|
+
class PostsController < ApplicationController
|
5
|
+
expose :posts
|
6
|
+
end
|
6
7
|
|
8
|
+
## Customization:
|
9
|
+
Exposure has a number of customization points:
|
10
|
+
### Callbacks
|
11
|
+
Exposure uses `ActiveSupport`'s callback system and has the following callbacks:
|
12
|
+
|
13
|
+
* `before_find`
|
14
|
+
* `after_find`
|
15
|
+
* `after_find_on_failure`
|
16
|
+
* `after_find_on_success`
|
17
|
+
* `before_assign`
|
18
|
+
* `after_assign`
|
19
|
+
* `before_save`
|
20
|
+
* `after_save`
|
21
|
+
* `after_save_on_failure`
|
22
|
+
* `after_save_on_success`
|
23
|
+
* `before_create`
|
24
|
+
* `after_create_on_failure`
|
25
|
+
* `after_create_on_success`
|
26
|
+
* `before_update`
|
27
|
+
* `after_update_on_failure`
|
28
|
+
* `after_update_on_success`
|
29
|
+
* `before_destroy`
|
30
|
+
* `after_destroy_on_success`
|
31
|
+
* `before_response`
|
32
|
+
* `before_response_on_success`
|
33
|
+
* `before_response_on_failure`
|
34
|
+
|
35
|
+
You can access these directly
|
36
|
+
|
37
|
+
class PostsController
|
38
|
+
before_find :do_somethng_special, :if => {|c| c.action_name == 'show' }
|
39
|
+
private
|
40
|
+
def do_somethng_special
|
41
|
+
...
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
or through a specialty syntax
|
46
|
+
|
47
|
+
class PostsController
|
48
|
+
before :find, :do_somethng_special, :only => [:create]
|
49
|
+
private
|
50
|
+
def do_somethng_special
|
51
|
+
...
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
### Finders
|
56
|
+
### Flashers
|
57
|
+
### Responders
|
7
58
|
|
8
59
|
INSTALL:
|
9
|
-
|
60
|
+
------
|
61
|
+
|
62
|
+
$ sudo gem install exposure
|
10
63
|
|
11
|
-
|
12
|
-
|
64
|
+
# environment.rb
|
65
|
+
gem.exposure
|
13
66
|
|
14
67
|
|
15
68
|
LICENSE:
|
16
|
-
|
69
|
+
------
|
17
70
|
|
18
71
|
(The MIT License)
|
19
72
|
|
data/Rakefile
CHANGED
@@ -12,10 +12,7 @@ Hoe.plugin :newgem
|
|
12
12
|
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
13
13
|
$hoe = Hoe.spec 'exposure' do
|
14
14
|
self.developer 'Trek Glowacki', 'trek.glowacki@gmail.com'
|
15
|
-
|
16
|
-
# self.rubyforge_name = self.name # TODO this is default value
|
17
|
-
# self.extra_deps = [['activesupport','>= 2.0.2']]
|
18
|
-
|
15
|
+
self.summary = 'exposed resources'
|
19
16
|
end
|
20
17
|
|
21
18
|
require 'newgem/tasks'
|
data/lib/exposure/common.rb
CHANGED
@@ -26,15 +26,16 @@ module Exposure
|
|
26
26
|
# defaults to [:html]
|
27
27
|
def response_for(action_name, options = {}, &block)
|
28
28
|
options[:is] ||= block
|
29
|
-
options[:formats]
|
29
|
+
formats = options[:formats] || [:html]
|
30
|
+
|
30
31
|
case options[:on]
|
31
32
|
when NilClass, :any
|
32
|
-
|
33
|
-
|
33
|
+
build_custom_response(action_name, :success, formats, options[:is])
|
34
|
+
build_custom_response(action_name, :failure, formats, options[:is])
|
34
35
|
when :success
|
35
|
-
|
36
|
+
build_custom_response(action_name, :success, formats, options[:is])
|
36
37
|
when :failure
|
37
|
-
|
38
|
+
build_custom_response(action_name, :failure, formats, options[:is])
|
38
39
|
end
|
39
40
|
end
|
40
41
|
|
@@ -100,6 +101,12 @@ module Exposure
|
|
100
101
|
end
|
101
102
|
end
|
102
103
|
|
104
|
+
def build_custom_response(action_name, success_status, formats, response)
|
105
|
+
formats.each do |format|
|
106
|
+
self.const_get(:Responses)["#{action_name}.#{success_status}.#{format}"] = response
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
103
110
|
# builds callbacks that adhere to the ActiveSupport::Callbacks interface
|
104
111
|
def build_callback(prefix, trigger, action, options) #:nodoc:
|
105
112
|
callback_name = "#{prefix}_#{trigger}"
|
@@ -40,7 +40,7 @@ module Exposure
|
|
40
40
|
|
41
41
|
if nesting = options[:nested]
|
42
42
|
|
43
|
-
self.parent_model = nesting.shift.to_s.singularize.
|
43
|
+
self.parent_model = nesting.shift.to_s.singularize.camelize.constantize
|
44
44
|
|
45
45
|
build_default_finders(self.resources_name, nesting)
|
46
46
|
|
@@ -48,7 +48,7 @@ module Exposure
|
|
48
48
|
self.member_nesting = nesting + [ [self.resource_name.to_sym] ]
|
49
49
|
self.collection_nesting = nesting + [ [self.resources_name.to_sym] ]
|
50
50
|
else
|
51
|
-
self.parent_model = self.resource_name.
|
51
|
+
self.parent_model = self.resource_name.camelize.constantize
|
52
52
|
build_default_finders(self.resource_name, [])
|
53
53
|
self.member_nesting = [ [self.resource_name.to_sym] ]
|
54
54
|
self.collection_nesting = [ [self.resources_name.to_sym] ]
|
@@ -59,5 +59,6 @@ module Exposure
|
|
59
59
|
|
60
60
|
define_callbacks(*Patterns::Resources::Callbacks)
|
61
61
|
end
|
62
|
+
alias :expose :expose_many
|
62
63
|
end
|
63
64
|
end
|
data/lib/exposure.rb
CHANGED
@@ -1,14 +1,15 @@
|
|
1
1
|
$:.unshift(File.dirname(__FILE__)) unless
|
2
2
|
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
|
-
|
3
|
+
require 'action_controller'
|
4
4
|
require 'exposure/configuration'
|
5
5
|
require 'exposure/common'
|
6
6
|
require 'exposure/patterns/resources'
|
7
7
|
require 'exposure/patterns/resource'
|
8
8
|
|
9
9
|
module Exposure
|
10
|
-
VERSION = '0.0.
|
10
|
+
VERSION = '0.0.5'
|
11
11
|
def self.included(base)
|
12
12
|
base.extend Configuration
|
13
13
|
end
|
14
|
-
end
|
14
|
+
end
|
15
|
+
ActionController::Base.send :include, Exposure
|
data/spec/exposure_spec.rb
CHANGED
@@ -1,4 +1,24 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/spec_helper.rb'
|
2
2
|
|
3
|
-
describe "exposure" do
|
3
|
+
describe "exposure", :type => :controller do
|
4
|
+
class ShipTypesController < ActionController::Base
|
5
|
+
expose_many(:ship_types)
|
6
|
+
end
|
7
|
+
controller_name :ship_types
|
8
|
+
|
9
|
+
before(:each) do
|
10
|
+
@controller = ShipTypesController.new
|
11
|
+
@request = ActionController::TestRequest.new
|
12
|
+
@response = ActionController::TestResponse.new
|
13
|
+
ActionController::Routing::Routes.draw do |map|
|
14
|
+
map.resources :ship_types
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "parent models" do
|
19
|
+
it "should constantize resource name" do
|
20
|
+
ShipTypesController.resources_name.should == "ship_types"
|
21
|
+
ShipTypesController.parent_model.should == ShipType
|
22
|
+
end
|
23
|
+
end
|
4
24
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -29,7 +29,7 @@ require 'exposure'
|
|
29
29
|
|
30
30
|
ActiveRecord::Base.logger = Logger.new(STDOUT) if ENV['DEBUG']
|
31
31
|
ActionController::Base.logger = Logger.new(STDOUT) if ENV['DEBUG']
|
32
|
-
ActionController::Base.send :include, Exposure
|
32
|
+
# ActionController::Base.send :include, Exposure
|
33
33
|
ActionController::Base.view_paths=(File.dirname(__FILE__) + '/fixtures')
|
34
34
|
|
35
35
|
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
@@ -54,6 +54,12 @@ ActiveRecord::Schema.define(:version => 1) do
|
|
54
54
|
t.column :created_at, :datetime
|
55
55
|
t.column :updated_at, :datetime
|
56
56
|
end
|
57
|
+
create_table :ship_types do |t|
|
58
|
+
t.column :title, :string
|
59
|
+
t.column :body, :text
|
60
|
+
t.column :created_at, :datetime
|
61
|
+
t.column :updated_at, :datetime
|
62
|
+
end
|
57
63
|
end
|
58
64
|
|
59
65
|
class Pirate < ActiveRecord::Base
|
@@ -68,4 +74,7 @@ class Ship < ActiveRecord::Base
|
|
68
74
|
def validate
|
69
75
|
end
|
70
76
|
validates_associated :pirate
|
77
|
+
end
|
78
|
+
|
79
|
+
class ShipType < ActiveRecord::Base
|
71
80
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: exposure
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Trek Glowacki
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-10-
|
12
|
+
date: 2009-10-08 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -22,7 +22,7 @@ dependencies:
|
|
22
22
|
- !ruby/object:Gem::Version
|
23
23
|
version: 2.3.3
|
24
24
|
version:
|
25
|
-
description:
|
25
|
+
description: ""
|
26
26
|
email:
|
27
27
|
- trek.glowacki@gmail.com
|
28
28
|
executables: []
|
@@ -39,7 +39,6 @@ files:
|
|
39
39
|
- lib/exposure.rb
|
40
40
|
- lib/exposure/common.rb
|
41
41
|
- lib/exposure/configuration.rb
|
42
|
-
- lib/exposure/patterns/resource.rb
|
43
42
|
- lib/exposure/patterns/resources.rb
|
44
43
|
- script/console
|
45
44
|
- script/destroy
|
@@ -62,7 +61,6 @@ files:
|
|
62
61
|
- spec/flashers/flash_with_method_spec.rb
|
63
62
|
- spec/flashers/flash_with_proc_spec.rb
|
64
63
|
- spec/flashers/flash_with_string_spec.rb
|
65
|
-
- spec/resource_spec.rb
|
66
64
|
- spec/resources_spec.rb
|
67
65
|
- spec/responders/respond_to_mime_types_spec.rb
|
68
66
|
- spec/responders/respond_with_block_spec.rb
|
@@ -97,10 +95,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
97
95
|
version:
|
98
96
|
requirements: []
|
99
97
|
|
100
|
-
rubyforge_project:
|
98
|
+
rubyforge_project: exposure
|
101
99
|
rubygems_version: 1.3.4
|
102
100
|
signing_key:
|
103
101
|
specification_version: 3
|
104
|
-
summary:
|
102
|
+
summary: exposed resources
|
105
103
|
test_files: []
|
106
104
|
|
@@ -1,226 +0,0 @@
|
|
1
|
-
module Exposure
|
2
|
-
module Patterns
|
3
|
-
module Resource
|
4
|
-
def self.extended(base)
|
5
|
-
base::const_set(:DefaultResponses, DefaultResponses)
|
6
|
-
base::const_set(:DefaultFlashMessages, DefaultFlashMessages)
|
7
|
-
base::const_set(:DefaultFinders, {
|
8
|
-
base.resource_name => Proc.new {|parent,controller| parent.find(controller.params[:id])},
|
9
|
-
})
|
10
|
-
end
|
11
|
-
|
12
|
-
Callbacks = %w(
|
13
|
-
before_find after_find after_find_on_failure after_find_on_success
|
14
|
-
before_assign after_assign
|
15
|
-
before_save after_save after_save_on_failure after_save_on_success
|
16
|
-
before_create after_create_on_failure after_create_on_success
|
17
|
-
before_update after_update_on_failure after_update_on_success
|
18
|
-
before_destroy after_destroy_on_success
|
19
|
-
before_response before_response_on_success before_response_on_failure
|
20
|
-
)
|
21
|
-
|
22
|
-
DefaultFlashMessages = {
|
23
|
-
true => {
|
24
|
-
:create => Proc.new {|c| "#{c.send(:resource_name).capitalize} successfully created" },
|
25
|
-
:update => Proc.new {|c| "#{c.send(:resource_name).capitalize} successfully updated" },
|
26
|
-
:destroy => Proc.new {|c| "#{c.send(:resource_name).capitalize} successfully removed" }
|
27
|
-
},
|
28
|
-
false => {}
|
29
|
-
}
|
30
|
-
|
31
|
-
DefaultResponses = {
|
32
|
-
true => {
|
33
|
-
:show => Proc.new {|c| c.send(:render, 'show') },
|
34
|
-
:new => Proc.new {|c| c.send(:render, 'new') },
|
35
|
-
:create => Proc.new {|c| c.send(:redirect_to, {:action => 'show'}) },
|
36
|
-
:edit => Proc.new {|c| c.send(:render, 'edit') },
|
37
|
-
:update => Proc.new {|c| c.send(:redirect_to, {:action => 'show' }) },
|
38
|
-
:destroy => Proc.new {|c| c.send(:redirect_to, {:action => 'new'}) }
|
39
|
-
},
|
40
|
-
false => {
|
41
|
-
:create => Proc.new {|c| c.send(:render, 'new') },
|
42
|
-
:update => Proc.new {|c| c.send(:render, 'edit') }
|
43
|
-
}
|
44
|
-
}
|
45
|
-
|
46
|
-
module Actions
|
47
|
-
# no index on single resources
|
48
|
-
|
49
|
-
def show
|
50
|
-
run_callbacks(:before_find)
|
51
|
-
if find_record
|
52
|
-
run_callbacks(:after_find_on_success)
|
53
|
-
run_callbacks(:after_find)
|
54
|
-
run_callbacks(:before_response)
|
55
|
-
run_callbacks(:before_response_on_success)
|
56
|
-
response_for(:show, true)
|
57
|
-
else
|
58
|
-
run_callbacks(:after_find_on_failure)
|
59
|
-
run_callbacks(:after_find)
|
60
|
-
run_callbacks(:before_response)
|
61
|
-
run_callbacks(:before_response_on_failure)
|
62
|
-
response_for(:show, false)
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
def new
|
67
|
-
run_callbacks(:before_assign)
|
68
|
-
new_record
|
69
|
-
run_callbacks(:after_assign)
|
70
|
-
run_callbacks(:before_response)
|
71
|
-
run_callbacks(:before_response_on_success)
|
72
|
-
response_for(:new, true)
|
73
|
-
end
|
74
|
-
|
75
|
-
def create
|
76
|
-
run_callbacks(:before_assign)
|
77
|
-
new_record
|
78
|
-
run_callbacks(:after_assign)
|
79
|
-
|
80
|
-
run_callbacks(:before_create)
|
81
|
-
run_callbacks(:before_save)
|
82
|
-
|
83
|
-
if save_record
|
84
|
-
run_callbacks(:after_save_on_success)
|
85
|
-
run_callbacks(:after_create_on_success)
|
86
|
-
run_callbacks(:before_response)
|
87
|
-
run_callbacks(:before_response_on_success)
|
88
|
-
flash_for(:create, true)
|
89
|
-
response_for(:create, true)
|
90
|
-
else
|
91
|
-
run_callbacks(:after_save_on_failure)
|
92
|
-
run_callbacks(:after_create_on_failure)
|
93
|
-
run_callbacks(:before_response)
|
94
|
-
run_callbacks(:before_response_on_failure)
|
95
|
-
flash_for(:create, false)
|
96
|
-
response_for(:create, false)
|
97
|
-
end
|
98
|
-
|
99
|
-
end
|
100
|
-
|
101
|
-
def edit
|
102
|
-
run_callbacks(:before_find)
|
103
|
-
if find_record
|
104
|
-
run_callbacks(:after_find_on_success)
|
105
|
-
run_callbacks(:after_find)
|
106
|
-
run_callbacks(:before_response)
|
107
|
-
run_callbacks(:before_response_on_success)
|
108
|
-
response_for(:edit, true)
|
109
|
-
else
|
110
|
-
run_callbacks(:after_find_on_failure)
|
111
|
-
run_callbacks(:after_find)
|
112
|
-
run_callbacks(:before_response)
|
113
|
-
run_callbacks(:before_response_on_failure)
|
114
|
-
response_for(:edit, false)
|
115
|
-
end
|
116
|
-
end
|
117
|
-
|
118
|
-
def update
|
119
|
-
run_callbacks(:before_find)
|
120
|
-
if find_record
|
121
|
-
run_callbacks(:after_find_on_success)
|
122
|
-
run_callbacks(:after_find)
|
123
|
-
if update_record
|
124
|
-
run_callbacks(:after_save_on_success)
|
125
|
-
run_callbacks(:after_update_on_success)
|
126
|
-
run_callbacks(:before_response)
|
127
|
-
run_callbacks(:before_response_on_success)
|
128
|
-
flash_for(:update, true)
|
129
|
-
response_for(:update, true)
|
130
|
-
else
|
131
|
-
run_callbacks(:after_save_on_failure)
|
132
|
-
run_callbacks(:after_create_on_failure)
|
133
|
-
run_callbacks(:before_response)
|
134
|
-
run_callbacks(:before_response_on_failure)
|
135
|
-
flash_for(:update, false)
|
136
|
-
response_for(:update, false)
|
137
|
-
end
|
138
|
-
else
|
139
|
-
run_callbacks(:after_find_on_failure)
|
140
|
-
run_callbacks(:after_find)
|
141
|
-
run_callbacks(:before_response)
|
142
|
-
run_callbacks(:before_response_on_failure)
|
143
|
-
response_for(:edit, false)
|
144
|
-
end
|
145
|
-
end
|
146
|
-
|
147
|
-
def destroy
|
148
|
-
run_callbacks(:before_find)
|
149
|
-
if find_record
|
150
|
-
run_callbacks(:after_find_on_success)
|
151
|
-
run_callbacks(:after_find)
|
152
|
-
run_callbacks(:before_destroy)
|
153
|
-
|
154
|
-
delete_record
|
155
|
-
|
156
|
-
run_callbacks(:after_destroy_on_success)
|
157
|
-
run_callbacks(:before_response)
|
158
|
-
run_callbacks(:before_response_on_success)
|
159
|
-
flash_for(:destroy, true)
|
160
|
-
response_for(:destroy, true)
|
161
|
-
|
162
|
-
else
|
163
|
-
run_callbacks(:after_find_on_failure)
|
164
|
-
run_callbacks(:after_find)
|
165
|
-
response_for(:destroy, false)
|
166
|
-
end
|
167
|
-
end
|
168
|
-
|
169
|
-
private
|
170
|
-
def custom_response_for(action_name, action_successful)
|
171
|
-
false
|
172
|
-
end
|
173
|
-
|
174
|
-
def default_response_for(action_name, action_successful)
|
175
|
-
self.class::DefaultResponses[action_successful][action_name].call(self)
|
176
|
-
end
|
177
|
-
|
178
|
-
def response_for(action_name, action_successful)
|
179
|
-
custom_response_for(action_name, action_successful) || default_response_for(action_name, action_successful)
|
180
|
-
end
|
181
|
-
|
182
|
-
def custom_flash_for(action_name, action_successful)
|
183
|
-
false
|
184
|
-
end
|
185
|
-
|
186
|
-
def default_flash_for(action_name, action_successful)
|
187
|
-
if message_proc = self.class::DefaultFlashMessages[action_successful][action_name]
|
188
|
-
flash[:message] = message_proc.call(self)
|
189
|
-
end
|
190
|
-
end
|
191
|
-
|
192
|
-
def flash_for(action_name, action_successful)
|
193
|
-
custom_flash_for(action_name, action_successful) || default_flash_for(action_name, action_successful)
|
194
|
-
end
|
195
|
-
|
196
|
-
def resource_name
|
197
|
-
self.class.resource_name
|
198
|
-
end
|
199
|
-
|
200
|
-
def save_record
|
201
|
-
@resource.save
|
202
|
-
end
|
203
|
-
|
204
|
-
def update_record
|
205
|
-
@resource.update_attributes(params[resource_name])
|
206
|
-
end
|
207
|
-
|
208
|
-
def new_record
|
209
|
-
@resource = instance_variable_set("@#{resource_name}", resource_name.camelize.constantize.new(params[resource_name]))
|
210
|
-
end
|
211
|
-
|
212
|
-
def find_record
|
213
|
-
@resource = instance_variable_set("@#{resource_name}", resource_name.camelize.constantize.find(params[:id]))
|
214
|
-
end
|
215
|
-
|
216
|
-
def find_records
|
217
|
-
@resources = instance_variable_set("@#{resource_name.pluralize}", resource_name.camelize.constantize.all)
|
218
|
-
end
|
219
|
-
|
220
|
-
def delete_record
|
221
|
-
@resource.destroy
|
222
|
-
end
|
223
|
-
end
|
224
|
-
end
|
225
|
-
end
|
226
|
-
end
|
data/spec/resource_spec.rb
DELETED
@@ -1,192 +0,0 @@
|
|
1
|
-
# require File.dirname(__FILE__) + '/spec_helper'
|
2
|
-
#
|
3
|
-
# describe "a REST patterned resource of 'post'", :type => :controller do
|
4
|
-
# class ShipController < ActionController::Base
|
5
|
-
# expose_one :ship
|
6
|
-
# end
|
7
|
-
# controller_name :ship
|
8
|
-
#
|
9
|
-
# before(:each) do
|
10
|
-
# @controller = ShipController.new
|
11
|
-
# @request = ActionController::TestRequest.new
|
12
|
-
# @response = ActionController::TestResponse.new
|
13
|
-
# ActionController::Routing::Routes.draw do |map|
|
14
|
-
# map.resource :ship
|
15
|
-
# end
|
16
|
-
# end
|
17
|
-
#
|
18
|
-
# describe 'configuration' do
|
19
|
-
# it "has a resource name of 'post'" do
|
20
|
-
# @controller.send(:resource_name).should == 'ship'
|
21
|
-
# end
|
22
|
-
#
|
23
|
-
# %w(show new create edit update destroy).each do |action|
|
24
|
-
# it "has the REST action #{action}" do
|
25
|
-
# @controller.should respond_to(action)
|
26
|
-
# end
|
27
|
-
# end
|
28
|
-
# end
|
29
|
-
#
|
30
|
-
# describe 'show' do
|
31
|
-
# describe 'with found resource' do
|
32
|
-
# before(:each) do
|
33
|
-
# Ship.should_receive(:find).with('1').and_return(Factory.stub(:ship))
|
34
|
-
# get :show, {:id => 1}
|
35
|
-
# end
|
36
|
-
#
|
37
|
-
# it { should assign_to(:ship) }
|
38
|
-
# it { should assign_to(:resource) }
|
39
|
-
# it { should respond_with(:success) }
|
40
|
-
# it { should render_template(:show) }
|
41
|
-
# it { should_not set_the_flash }
|
42
|
-
# end
|
43
|
-
#
|
44
|
-
# describe 'with missing resource' do
|
45
|
-
# before(:each) do
|
46
|
-
# rescue_action_in_public!
|
47
|
-
# Ship.should_receive(:find).with('1').and_raise(ActiveRecord::RecordNotFound)
|
48
|
-
# get :show, {:id => 1}
|
49
|
-
# end
|
50
|
-
#
|
51
|
-
# it { should_not assign_to(:ship) }
|
52
|
-
# it { should_not assign_to(:resource) }
|
53
|
-
# it { should respond_with(:not_found) }
|
54
|
-
# it { should_not render_template(:show) }
|
55
|
-
# it { should_not set_the_flash }
|
56
|
-
# end
|
57
|
-
# end
|
58
|
-
#
|
59
|
-
# describe 'new' do
|
60
|
-
# before(:each) do
|
61
|
-
# get :new
|
62
|
-
# end
|
63
|
-
#
|
64
|
-
# it { should assign_to(:ship) }
|
65
|
-
# it { should assign_to(:resource) }
|
66
|
-
# it { should respond_with(:success) }
|
67
|
-
# it { should render_template(:new) }
|
68
|
-
# it { should_not set_the_flash }
|
69
|
-
#
|
70
|
-
# end
|
71
|
-
#
|
72
|
-
# describe 'create' do
|
73
|
-
# describe 'with valid data' do
|
74
|
-
# before(:each) do
|
75
|
-
# params = {
|
76
|
-
# :ship => {}
|
77
|
-
# }
|
78
|
-
# post_before_saving = Factory.build(:ship)
|
79
|
-
# Ship.should_receive(:new).with(params[:ship]).and_return(post_before_saving)
|
80
|
-
# post_before_saving.should_receive(:save).and_return(true)
|
81
|
-
# post(:create, params)
|
82
|
-
# end
|
83
|
-
#
|
84
|
-
# it { should assign_to(:ship) }
|
85
|
-
# it { should assign_to(:resource) }
|
86
|
-
# it { should respond_with(:redirect).to(:show) }
|
87
|
-
# it { should set_the_flash.to('Ship successfully created') }
|
88
|
-
# end
|
89
|
-
#
|
90
|
-
# describe 'with invalid data' do
|
91
|
-
# before(:each) do
|
92
|
-
# params = {
|
93
|
-
# :ship => {}
|
94
|
-
# }
|
95
|
-
# post_before_saving = Factory.build(:ship)
|
96
|
-
# Ship.should_receive(:new).with(params[:ship]).and_return(post_before_saving)
|
97
|
-
# post_before_saving.should_receive(:save).and_return(false)
|
98
|
-
# post(:create, params)
|
99
|
-
# end
|
100
|
-
#
|
101
|
-
# it { should assign_to(:ship) }
|
102
|
-
# it { should assign_to(:resource) }
|
103
|
-
# it { should respond_with(:success) }
|
104
|
-
# it { should render_template(:new) }
|
105
|
-
# it { should_not set_the_flash }
|
106
|
-
# end
|
107
|
-
# end
|
108
|
-
#
|
109
|
-
# describe 'edit' do
|
110
|
-
# describe 'with found resource' do
|
111
|
-
# before(:each) do
|
112
|
-
# Ship.should_receive(:find).with('1').and_return(Factory.stub(:ship))
|
113
|
-
# get :edit, {:id => 1}
|
114
|
-
# end
|
115
|
-
#
|
116
|
-
# it { should assign_to(:ship) }
|
117
|
-
# it { should assign_to(:resource) }
|
118
|
-
# it { should respond_with(:success) }
|
119
|
-
# it { should render_template(:edit) }
|
120
|
-
# it { should_not set_the_flash }
|
121
|
-
# end
|
122
|
-
#
|
123
|
-
# describe 'with missing resource' do
|
124
|
-
# before(:each) do
|
125
|
-
# rescue_action_in_public!
|
126
|
-
# Ship.should_receive(:find).with('1').and_raise(ActiveRecord::RecordNotFound)
|
127
|
-
# get :edit, {:id => 1}
|
128
|
-
# end
|
129
|
-
#
|
130
|
-
# it { should_not assign_to(:ship) }
|
131
|
-
# it { should_not assign_to(:resource) }
|
132
|
-
# it { should respond_with(:not_found) }
|
133
|
-
# it { should_not render_template(:edit) }
|
134
|
-
# it { should_not set_the_flash }
|
135
|
-
# end
|
136
|
-
# end
|
137
|
-
#
|
138
|
-
# describe 'update' do
|
139
|
-
# describe 'with valid data' do
|
140
|
-
# before(:each) do
|
141
|
-
# params = {
|
142
|
-
# :id => 1,
|
143
|
-
# :ship => {}
|
144
|
-
# }
|
145
|
-
# post_before_saving = Factory.build(:ship)
|
146
|
-
# Ship.should_receive(:find).with("1").and_return(post_before_saving)
|
147
|
-
# post_before_saving.should_receive(:update_attributes).with(params[:ship]).and_return(true)
|
148
|
-
# put(:update, params)
|
149
|
-
# end
|
150
|
-
#
|
151
|
-
# it { should assign_to(:ship) }
|
152
|
-
# it { should assign_to(:resource) }
|
153
|
-
# it { should respond_with(:redirect).to(:show) }
|
154
|
-
# it { should set_the_flash.to('Ship successfully updated') }
|
155
|
-
#
|
156
|
-
# end
|
157
|
-
#
|
158
|
-
# describe 'with invalid data' do
|
159
|
-
# before(:each) do
|
160
|
-
# params = {
|
161
|
-
# :id => 1,
|
162
|
-
# :ship => {}
|
163
|
-
# }
|
164
|
-
# post_before_saving = Factory.build(:ship)
|
165
|
-
# Ship.should_receive(:find).with("1").and_return(post_before_saving)
|
166
|
-
# post_before_saving.should_receive(:update_attributes).with(params[:ship]).and_return(false)
|
167
|
-
# put(:update, params)
|
168
|
-
# end
|
169
|
-
#
|
170
|
-
# it { should assign_to(:ship) }
|
171
|
-
# it { should assign_to(:resource) }
|
172
|
-
# it { should respond_with(:success) }
|
173
|
-
# it { should render_template(:edit) }
|
174
|
-
# it { should_not set_the_flash }
|
175
|
-
# end
|
176
|
-
# end
|
177
|
-
#
|
178
|
-
# describe 'destroy' do
|
179
|
-
# before(:each) do
|
180
|
-
# params = {
|
181
|
-
# :id => 1,
|
182
|
-
# }
|
183
|
-
# post_before_saving = Factory.build(:ship)
|
184
|
-
# Ship.should_receive(:find).with("1").and_return(post_before_saving)
|
185
|
-
# post_before_saving.should_receive(:destroy)
|
186
|
-
# delete(:destroy, params)
|
187
|
-
# end
|
188
|
-
#
|
189
|
-
# it { should respond_with(:redirect).to(:new) }
|
190
|
-
# it { should set_the_flash.to('Ship successfully removed') }
|
191
|
-
# end
|
192
|
-
# end
|