genki-merb_component 0.2.0 → 0.2.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/README +0 -12
- data/Rakefile +1 -1
- data/lib/merb_component/controller_ext.rb +30 -7
- data/spec/admin_spec.rb +20 -10
- data/spec/fixture/app/views/comments/index.html.erb +2 -0
- data/spec/fixture/app/views/posts/show.html.erb +1 -1
- data/spec/merb_component_spec.rb +8 -7
- data/spec/posts_spec.rb +23 -0
- metadata +2 -2
data/README
CHANGED
@@ -25,20 +25,8 @@ Example of use:
|
|
25
25
|
In views:
|
26
26
|
|
27
27
|
Content of the user (id is 2) goes here
|
28
|
-
<%= component Users, :show, :id => 2 %>
|
29
|
-
|
30
|
-
Or, you can use symbol to specify subsidiary controller
|
31
28
|
<%= component :users, :show, :id => 2 %>
|
32
29
|
|
33
|
-
Index of posts related with the @user go here
|
34
|
-
<% Post.related_with @user do %>
|
35
|
-
<%= component Posts, :index %>
|
36
|
-
<% end %>
|
37
|
-
|
38
|
-
In app/views/posts/index.html.erb,
|
39
|
-
You can access to the corresponding relation like this
|
40
|
-
<%= Post.relation #=> @user %>
|
41
|
-
|
42
30
|
For detail, you can see spec/fixture as an example.
|
43
31
|
|
44
32
|
Enjoy!
|
data/Rakefile
CHANGED
@@ -58,8 +58,12 @@ class Merb::Controller
|
|
58
58
|
cc.class.layout(layout)
|
59
59
|
end
|
60
60
|
object = cc.instance_variable_get(var)
|
61
|
+
throw(:halt, proc{
|
62
|
+
target = controller_name.singular.intern
|
63
|
+
target = object.send(target) if object.respond_to?(target)
|
64
|
+
redirect resource(target)
|
65
|
+
}) if object.errors.empty?
|
61
66
|
c.instance_variable_set(var, object)
|
62
|
-
object = model.new if object.errors.empty?
|
63
67
|
end
|
64
68
|
elsif params[:id]
|
65
69
|
# GET with component id
|
@@ -77,7 +81,7 @@ class Merb::Controller
|
|
77
81
|
end
|
78
82
|
|
79
83
|
class Aggregator
|
80
|
-
attr_reader :controller, :object, :result
|
84
|
+
attr_reader :controller, :object, :result, :context
|
81
85
|
|
82
86
|
def initialize(context, controller, &block)
|
83
87
|
@context = context
|
@@ -123,8 +127,22 @@ class Merb::Controller
|
|
123
127
|
(aggregators[self.class] ||= []).last
|
124
128
|
end
|
125
129
|
|
130
|
+
def url_with_scope(*args)
|
131
|
+
result = url_without_scope(*args)
|
132
|
+
if (agg = aggregator) && (key = agg.key)
|
133
|
+
resource_without_scope(key) + result
|
134
|
+
else
|
135
|
+
result
|
136
|
+
end
|
137
|
+
end
|
138
|
+
alias_method :url_without_scope, :url
|
139
|
+
alias_method :url, :url_with_scope
|
140
|
+
|
126
141
|
private
|
127
142
|
def component(controller, action, params = {})
|
143
|
+
params = self.params.merge(
|
144
|
+
:controller => controller, :action => action
|
145
|
+
).merge(params)
|
128
146
|
var = "@#{controller.to_s.singular}"
|
129
147
|
object = instance_variable_get("#{var}_component")
|
130
148
|
controller = Object.full_const_get(controller.to_s.camel_case)
|
@@ -156,8 +174,9 @@ private
|
|
156
174
|
end
|
157
175
|
end
|
158
176
|
|
159
|
-
def
|
160
|
-
|
177
|
+
def resource_with_scope(first, *args)
|
178
|
+
agg = aggregator
|
179
|
+
return resource_without_scope(first, *args) unless agg
|
161
180
|
|
162
181
|
controller = case first
|
163
182
|
when Symbol, String
|
@@ -166,8 +185,12 @@ private
|
|
166
185
|
Object.full_const_get(first.class.to_s.pluralize.camel_case)
|
167
186
|
end
|
168
187
|
|
169
|
-
|
170
|
-
|
171
|
-
|
188
|
+
if controller <=> agg.controller && agg.key
|
189
|
+
resource_without_scope(agg.key, first, *args)
|
190
|
+
else
|
191
|
+
resource_without_scope(first, *args)
|
192
|
+
end
|
172
193
|
end
|
194
|
+
alias_method :resource_without_scope, :resource
|
195
|
+
alias_method :resource, :resource_with_scope
|
173
196
|
end
|
data/spec/admin_spec.rb
CHANGED
@@ -25,13 +25,24 @@ describe "Admin controller" do
|
|
25
25
|
Comment.count.should > 0
|
26
26
|
end
|
27
27
|
|
28
|
-
it "should index html" do
|
28
|
+
it "should show index html" do
|
29
29
|
res = request(resource(:admin))
|
30
30
|
res.should be_successful
|
31
31
|
res.should have_xpath("//h1")
|
32
32
|
res.should have_xpath("//h2")
|
33
33
|
res.should have_xpath("//ul/li")
|
34
34
|
res.should_not have_xpath("//form")
|
35
|
+
res.should have_xpath("//a[@href='/admin/comments?page=1']")
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should show index html for pagination params" do
|
39
|
+
res = request("/admin/comments?page=2")
|
40
|
+
res.should be_successful
|
41
|
+
res.should have_xpath("//h1")
|
42
|
+
res.should have_xpath("//h2")
|
43
|
+
res.should have_xpath("//ul")
|
44
|
+
res.should_not have_xpath("//form")
|
45
|
+
res.should have_xpath("//a[@href='/admin/comments?page=3']")
|
35
46
|
end
|
36
47
|
|
37
48
|
it "should show html after update a comment" do
|
@@ -39,14 +50,14 @@ describe "Admin controller" do
|
|
39
50
|
comment.should be_kind_of(Comment)
|
40
51
|
res = request(resource(:admin, comment),
|
41
52
|
:method => 'PUT', :params => {:comment => {:body => "bar"}})
|
53
|
+
res.should redirect_to("/admin")
|
54
|
+
|
55
|
+
res = request(res.headers["Location"])
|
42
56
|
res.should be_successful
|
43
57
|
res.should have_xpath("//h1")
|
44
58
|
res.should have_xpath("//h2")
|
45
59
|
res.should have_xpath("//ul/li[1]")
|
46
|
-
res.
|
47
|
-
res.should have_xpath("//form[@method='post']")
|
48
|
-
res.should_not have_xpath("//input[@value='put']")
|
49
|
-
res.should contain("bar")
|
60
|
+
res.should_not have_xpath("//form")
|
50
61
|
end
|
51
62
|
|
52
63
|
it "should show html after failed to update a comment" do
|
@@ -70,14 +81,13 @@ describe "Admin controller" do
|
|
70
81
|
comment = @post.comments.last
|
71
82
|
comment.should be_kind_of(Comment)
|
72
83
|
res = request(resource(:admin, comment), :method => 'DELETE')
|
84
|
+
res.should redirect_to("/admin")
|
85
|
+
|
86
|
+
res = request(res.headers["Location"])
|
73
87
|
res.should be_successful
|
74
88
|
res.should have_xpath("//h1")
|
75
89
|
res.should have_xpath("//h2")
|
76
|
-
res.
|
77
|
-
res.should have_xpath("//form[@action='/admin/comments']")
|
78
|
-
res.should have_xpath("//form[@method='post']")
|
79
|
-
res.should_not have_xpath("//input[@value='put']")
|
80
|
-
res.should_not have_xpath("//body/meta")
|
90
|
+
res.should_not have_xpath("//form")
|
81
91
|
@post.comments.count.should == count - 1
|
82
92
|
end
|
83
93
|
|
data/spec/merb_component_spec.rb
CHANGED
@@ -5,21 +5,22 @@ describe "merb_component" do
|
|
5
5
|
Posts.private_instance_methods.should be_include("component")
|
6
6
|
Posts.private_instance_methods.should be_include("form_for_component")
|
7
7
|
Posts.instance_methods.should be_include("aggregator")
|
8
|
+
Posts.private_instance_methods.should be_include("resource")
|
9
|
+
Posts.public_instance_methods.should be_include("url")
|
8
10
|
end
|
9
11
|
|
10
12
|
it "should not extend model" do
|
11
13
|
Post.public_methods.should_not be_include("related_with")
|
12
14
|
end
|
13
15
|
|
14
|
-
it "should accept controller class as the first param of compoent" do
|
15
|
-
req = Merb::Request.new({})
|
16
|
-
result = Posts.new(req).send(:component, Comments, :index)
|
17
|
-
result.should be_kind_of(String)
|
18
|
-
end
|
19
|
-
|
20
16
|
it "should accept symbol as the first param of compoent" do
|
21
17
|
req = Merb::Request.new({})
|
22
|
-
|
18
|
+
c = Posts.new(req)
|
19
|
+
proc do
|
20
|
+
c.send(:component, :comments, :index)
|
21
|
+
end.should raise_error(Merb::Router::GenerationError)
|
22
|
+
c.instance_variable_set(:@post, Post.create)
|
23
|
+
result = c.send(:component, :comments, :index)
|
23
24
|
result.should be_kind_of(String)
|
24
25
|
end
|
25
26
|
end
|
data/spec/posts_spec.rb
CHANGED
@@ -35,12 +35,29 @@ describe "Posts controller" do
|
|
35
35
|
res.should have_xpath("//form[@action='/posts/#{@post.id}/comments']")
|
36
36
|
res.should_not have_xpath("//input[@value='put']")
|
37
37
|
res.should have_xpath("//input[@value='new']")
|
38
|
+
res.should have_xpath("//a[@href='/posts/#{@post.id}/comments?page=1']")
|
38
39
|
end
|
39
40
|
|
41
|
+
it "should show html for pagination params" do
|
42
|
+
res = request("/posts/#{@post.id}/comments?page=2")
|
43
|
+
res.should be_successful
|
44
|
+
res.should have_xpath("//h1")
|
45
|
+
res.should have_xpath("//h2")
|
46
|
+
res.should have_xpath("//ul/li")
|
47
|
+
res.should have_xpath("//form[@method='post']")
|
48
|
+
res.should have_xpath("//form[@action='/posts/#{@post.id}/comments']")
|
49
|
+
res.should_not have_xpath("//input[@value='put']")
|
50
|
+
res.should have_xpath("//input[@value='new']")
|
51
|
+
res.should have_xpath("//a[@href='/posts/#{@post.id}/comments?page=3']")
|
52
|
+
end
|
53
|
+
|
40
54
|
it "should show html after post a comment" do
|
41
55
|
count = @post.comments.count
|
42
56
|
res = request(resource(@post, :comments),
|
43
57
|
:method => 'POST', :params => {:comment => {:body => "foo"}})
|
58
|
+
res.should redirect_to("/posts/#{@post.id}")
|
59
|
+
|
60
|
+
res = request(res.headers["Location"])
|
44
61
|
res.should be_successful
|
45
62
|
res.should have_xpath("//h1")
|
46
63
|
res.should have_xpath("//h2")
|
@@ -75,6 +92,9 @@ describe "Posts controller" do
|
|
75
92
|
comment.should be_kind_of(Comment)
|
76
93
|
res = request(resource(@post, comment),
|
77
94
|
:method => 'PUT', :params => {:comment => {:body => "bar"}})
|
95
|
+
res.should redirect_to("/posts/#{@post.id}")
|
96
|
+
|
97
|
+
res = request(res.headers["Location"])
|
78
98
|
res.should be_successful
|
79
99
|
res.should have_xpath("//h1")
|
80
100
|
res.should have_xpath("//h2")
|
@@ -109,6 +129,9 @@ describe "Posts controller" do
|
|
109
129
|
comment = @post.comments.last
|
110
130
|
comment.should be_kind_of(Comment)
|
111
131
|
res = request(resource(@post, comment), :method => 'DELETE')
|
132
|
+
res.should redirect_to("/posts/#{@post.id}")
|
133
|
+
|
134
|
+
res = request(res.headers["Location"])
|
112
135
|
res.should be_successful
|
113
136
|
res.should have_xpath("//h1")
|
114
137
|
res.should have_xpath("//h2")
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: genki-merb_component
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.0
|
4
|
+
version: 0.2.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Genki Takiuchi
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-01-
|
12
|
+
date: 2009-01-27 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|