cells 3.6.6 → 3.6.7
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/.travis.yml +3 -0
- data/CHANGES.textile +5 -0
- data/README.rdoc +2 -0
- data/lib/cell.rb +2 -2
- data/lib/cell/test_case.rb +34 -11
- data/lib/cells/version.rb +1 -1
- data/test/test_case_test.rb +25 -1
- metadata +4 -3
data/.gitignore
CHANGED
data/.travis.yml
ADDED
data/CHANGES.textile
CHANGED
data/README.rdoc
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
<em>View Components for Rails.</em>
|
4
4
|
|
5
|
+
{<img src="https://secure.travis-ci.org/apotonick/cells.png" />}[http://travis-ci.org/apotonick/cells]
|
6
|
+
|
5
7
|
== Overview
|
6
8
|
|
7
9
|
Say you're writing a Rails online shop - the shopping cart is reappearing again and again in every view. You're thinking about a clean solution for that part. A mixture of controller code, before-filters, partials and helpers?
|
data/lib/cell.rb
CHANGED
@@ -14,8 +14,8 @@ module Cell
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def render_cell_for(controller, name, state, *args)
|
17
|
-
cell = create_cell_for(controller, name, *args)
|
18
|
-
yield cell if block_given?
|
17
|
+
cell = create_cell_for(controller, name, *args) # DISCUSS: pass args here?
|
18
|
+
yield cell if block_given? # DISCUSS: call block before or after render?
|
19
19
|
|
20
20
|
cell.render_state_with_args(state, *args)
|
21
21
|
end
|
data/lib/cell/test_case.rb
CHANGED
@@ -58,8 +58,8 @@ module Cell
|
|
58
58
|
super(HTML::Document.new(last_invoke).root, *args, &block)
|
59
59
|
end
|
60
60
|
end
|
61
|
-
|
62
|
-
module
|
61
|
+
|
62
|
+
module CommonTestMethods
|
63
63
|
def setup
|
64
64
|
@controller = Class.new(ActionController::Base).new
|
65
65
|
@request = ::ActionController::TestRequest.new
|
@@ -68,15 +68,40 @@ module Cell
|
|
68
68
|
@controller.response = @response
|
69
69
|
@controller.params = {}
|
70
70
|
end
|
71
|
+
|
72
|
+
# Runs the block while computing the instance variables diff from before and after.
|
73
|
+
def extract_state_ivars_for(cell)
|
74
|
+
before = cell.instance_variables
|
75
|
+
yield
|
76
|
+
after = cell.instance_variables
|
77
|
+
|
78
|
+
Hash[(after - before).collect do |var|
|
79
|
+
next if var =~ /^@_/
|
80
|
+
[var[1, var.length].to_sym, cell.instance_variable_get(var)]
|
81
|
+
end]
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
71
85
|
|
86
|
+
module TestMethods
|
87
|
+
include CommonTestMethods
|
88
|
+
|
89
|
+
attr_reader :last_invoke, :subject_cell, :view_assigns
|
90
|
+
|
72
91
|
# Use this for functional tests of your application cells.
|
73
92
|
#
|
74
93
|
# Example:
|
75
94
|
# should "spit out a h1 title" do
|
76
95
|
# html = render_cell(:news, :latest)
|
77
|
-
#
|
78
|
-
def render_cell(*args)
|
79
|
-
|
96
|
+
# assert_select html, "h1", "The latest and greatest!"
|
97
|
+
def render_cell(name, state, *args)
|
98
|
+
# DISCUSS: should we allow passing a block here, just as in controllers?
|
99
|
+
@subject_cell = ::Cell::Base.create_cell_for(@controller, name, *args)
|
100
|
+
@view_assigns = extract_state_ivars_for(@subject_cell) do
|
101
|
+
@last_invoke = @subject_cell.render_state_with_args(state, *args)
|
102
|
+
end
|
103
|
+
|
104
|
+
@last_invoke
|
80
105
|
end
|
81
106
|
|
82
107
|
# Builds an instance of <tt>name</tt>Cell for unit testing.
|
@@ -89,8 +114,8 @@ module Cell
|
|
89
114
|
cell.instance_eval &block if block_given?
|
90
115
|
cell
|
91
116
|
end
|
92
|
-
|
93
|
-
# Execute the passed +block+ in a real view context of +cell_class+.
|
117
|
+
|
118
|
+
# Execute the passed +block+ in a real view context of +cell_class+.
|
94
119
|
# Usually you'd test helpers here.
|
95
120
|
#
|
96
121
|
# Example:
|
@@ -101,7 +126,7 @@ module Cell
|
|
101
126
|
setup_test_states_in(subject) # add #in_view to subject cell.
|
102
127
|
subject.render_state(:in_view)
|
103
128
|
end
|
104
|
-
|
129
|
+
|
105
130
|
protected
|
106
131
|
def setup_test_states_in(cell)
|
107
132
|
cell.instance_eval do
|
@@ -119,9 +144,7 @@ module Cell
|
|
119
144
|
|
120
145
|
extend ActionController::TestCase::Behavior::ClassMethods
|
121
146
|
class_attribute :_controller_class
|
122
|
-
|
123
|
-
|
124
|
-
attr_reader :last_invoke
|
147
|
+
|
125
148
|
|
126
149
|
def invoke(state, *args)
|
127
150
|
@last_invoke = self.class.controller_class.new(@controller, *args).render_state_with_args(state, *args)
|
data/lib/cells/version.rb
CHANGED
data/test/test_case_test.rb
CHANGED
@@ -11,7 +11,6 @@ class TestCaseTest < Cell::TestCase
|
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
|
-
|
15
14
|
should "respond to #render_cell" do
|
16
15
|
assert_equal "Doo", render_cell(:bassist, :play)
|
17
16
|
end
|
@@ -32,6 +31,31 @@ class TestCaseTest < Cell::TestCase
|
|
32
31
|
assert_equal({:topic => :peace}, cell(:bassist, :topic => :peace).options)
|
33
32
|
end
|
34
33
|
|
34
|
+
context "#subject_cell" do
|
35
|
+
should "return the last rendered cell" do
|
36
|
+
render_cell(:bassist, :play)
|
37
|
+
assert_kind_of BassistCell, subject_cell
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
context "#view_assigns" do
|
43
|
+
should "be emtpy when nothing was set" do
|
44
|
+
render_cell(:bassist, :play)
|
45
|
+
assert_equal({}, view_assigns)
|
46
|
+
end
|
47
|
+
|
48
|
+
should "return the instance variables from the last #render_cell" do
|
49
|
+
BassistCell.class_eval do
|
50
|
+
def sleep
|
51
|
+
@duration = "8h"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
render_cell(:bassist, :sleep)
|
55
|
+
assert_equal({:duration => "8h"}, view_assigns)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
35
59
|
context "in declarative tests" do
|
36
60
|
should "respond to TestCase.tests" do
|
37
61
|
self.class.tests BassistCell
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 3
|
7
7
|
- 6
|
8
|
-
-
|
9
|
-
version: 3.6.
|
8
|
+
- 7
|
9
|
+
version: 3.6.7
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Nick Sutterer
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-09-
|
17
|
+
date: 2011-09-28 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -121,6 +121,7 @@ extra_rdoc_files: []
|
|
121
121
|
|
122
122
|
files:
|
123
123
|
- .gitignore
|
124
|
+
- .travis.yml
|
124
125
|
- CHANGES.textile
|
125
126
|
- Gemfile
|
126
127
|
- README.rdoc
|