releaf-content 0.2.1 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +19 -21
  3. data/app/assets/javascripts/{releaf/controllers → controllers}/releaf/content/nodes.js +0 -0
  4. data/app/assets/stylesheets/{releaf/controllers → controllers}/releaf/content/nodes.scss +0 -0
  5. data/app/builders/releaf/content/builders/action_dialog.rb +9 -1
  6. data/app/builders/releaf/content/builders/tree.rb +1 -1
  7. data/app/builders/releaf/content/content_type_dialog_builder.rb +2 -2
  8. data/app/builders/releaf/content/nodes/form_builder.rb +3 -3
  9. data/app/builders/releaf/content/nodes/index_builder.rb +1 -1
  10. data/app/builders/releaf/content/nodes/toolbox_builder.rb +0 -5
  11. data/app/controllers/releaf/content/nodes_controller.rb +109 -129
  12. data/app/middleware/releaf/content/routes_reloader.rb +12 -4
  13. data/app/services/releaf/content/node/copy.rb +90 -0
  14. data/app/services/releaf/content/node/move.rb +21 -0
  15. data/app/services/releaf/content/node/save_under_parent.rb +22 -0
  16. data/app/services/releaf/content/node/service.rb +17 -0
  17. data/app/validators/releaf/content/node/singleness_validator.rb +1 -24
  18. data/lib/releaf-content.rb +85 -6
  19. data/lib/releaf/content/acts_as_node.rb +0 -5
  20. data/lib/releaf/content/acts_as_node/active_record/acts/node.rb +2 -8
  21. data/lib/releaf/content/configuration.rb +61 -0
  22. data/lib/releaf/content/engine.rb +1 -34
  23. data/lib/releaf/content/node.rb +30 -101
  24. data/lib/releaf/content/node_mapper.rb +28 -2
  25. data/lib/releaf/content/route.rb +37 -25
  26. data/spec/builders/content/nodes/action_dialog_spec.rb +39 -0
  27. data/spec/builders/content/nodes/form_builder_spec.rb +47 -3
  28. data/spec/builders/content/nodes/toolbox_builder_spec.rb +1 -32
  29. data/spec/controllers/releaf/content/nodes_controller_spec.rb +42 -11
  30. data/spec/features/nodes_services_spec.rb +207 -0
  31. data/spec/features/nodes_spec.rb +328 -30
  32. data/spec/lib/releaf/content/acts_as_node_spec.rb +4 -32
  33. data/spec/lib/releaf/content/configuration_spec.rb +159 -0
  34. data/spec/lib/releaf/content/engine_spec.rb +149 -0
  35. data/spec/lib/releaf/content/node_spec.rb +66 -324
  36. data/spec/lib/releaf/content/route_spec.rb +223 -34
  37. data/spec/middleware/routes_reloader_spec.rb +62 -14
  38. data/spec/routing/node_mapper_spec.rb +223 -55
  39. data/spec/services/releaf/content/node/copy_spec.rb +115 -0
  40. data/spec/services/releaf/content/node/move_spec.rb +20 -0
  41. data/spec/services/releaf/content/node/save_under_parent_spec.rb +49 -0
  42. data/spec/services/releaf/content/node/service_spec.rb +19 -0
  43. metadata +38 -21
  44. data/app/builders/releaf/content/go_to_dialog_builder.rb +0 -9
  45. data/app/views/releaf/content/nodes/go_to_dialog.ruby +0 -1
  46. data/lib/releaf/content/builders_autoload.rb +0 -18
  47. data/releaf-content.gemspec +0 -20
@@ -1,26 +1,192 @@
1
1
  require "rails_helper"
2
2
 
3
3
  describe Releaf::Content::Route do
4
- let(:node_route) { FactoryGirl.build(:node_route, node_id: 12, locale: "en", path: "/en") }
4
+ let(:node_route) { FactoryGirl.build(:node_route, node_class: Node, node_id: 12, locale: "en", path: "/en") }
5
5
 
6
- describe ".node_class" do
7
- it "returns ::Node" do
8
- expect( described_class.node_class ).to eq ::Node
9
- end
10
- end
11
6
 
12
- describe ".node_class_default_controller" do
7
+ describe ".default_controller" do
13
8
  context "when given node class inherits `ActionController::Base`" do
14
9
  it "returns undercored, stripped down controller class" do
15
- expect(described_class.node_class_default_controller(HomePagesController)).to eq("home_pages")
10
+ expect(described_class.default_controller(HomePagesController)).to eq("home_pages")
16
11
  end
17
12
  end
18
13
 
19
14
  context "when given node class does not inherit `ActionController::Base`" do
20
15
  it "returns pluralized, underscorized class" do
21
- expect(described_class.node_class_default_controller(TextPage)).to eq("text_pages")
16
+ expect(described_class.default_controller(TextPage)).to eq("text_pages")
17
+ end
18
+ end
19
+ end
20
+
21
+
22
+ describe '#params' do
23
+ let(:options_argument) { { foo: :bar } }
24
+
25
+ it "returns params for router method" do
26
+ expect(node_route.params("home#index")).to eq ['/en', {to: "home#index", node_class: "Node", node_id: "12", locale: "en", as: nil }]
27
+ end
28
+
29
+ it "uses #path_for to calculate path" do
30
+ allow(node_route).to receive(:path_for).with("home#index", options_argument).and_return( "foo_path" )
31
+ expect(node_route.params("home#index", options_argument)[0]).to eq "foo_path"
32
+ end
33
+
34
+ it "uses #options_for to calculate options" do
35
+ allow(node_route).to receive(:options_for).with("home#index", options_argument).and_return( some: :options )
36
+ expect(node_route.params("home#index", options_argument)[1]).to eq( some: :options )
37
+ end
38
+
39
+ it "converts method_or_path argument to string" do
40
+ expect(node_route).to receive(:path_for).with("foo", options_argument)
41
+ expect(node_route).to receive(:options_for).with("foo", options_argument)
42
+ node_route.params(:foo, options_argument)
43
+ end
44
+
45
+ it "does not require options argument" do
46
+ expect(node_route).to receive(:path_for).with("home#index", {})
47
+ expect(node_route).to receive(:options_for).with("home#index", {})
48
+ node_route.params("home#index")
49
+ end
50
+
51
+ end
52
+
53
+ describe "#path_for" do
54
+
55
+ it "returns route path" do
56
+ expect( node_route.path_for( "foo", {} ) ).to eq "/en"
57
+ end
58
+
59
+ context "when options contain a :to key" do
60
+ it "returns route path combined with given method_or_path" do
61
+ expect( node_route.path_for( "foo", { to: "bar" } ) ).to eq "/en/foo"
62
+ end
63
+ end
64
+
65
+ context "when first argument contains a controller#action or #action string" do
66
+ it "returns the route path" do
67
+ expect( node_route.path_for( "home#index", { to: "bar" } ) ).to eq "/en"
68
+ end
69
+ end
70
+
71
+ end
72
+
73
+ describe "#options_for" do
74
+
75
+ it "returns route options hash" do
76
+ expect( node_route.options_for( "home#index", {} ) ).to be_a Hash
77
+ end
78
+
79
+ it "sets :node_class to route node class name string" do
80
+ expect( node_route.options_for( "home#index", {} ) ).to include( node_class: "Node" )
81
+ end
82
+
83
+ it "sets :node_id to route node id string" do
84
+ expect( node_route.options_for( "home#index", {} ) ).to include( node_id: "12" )
85
+ end
86
+
87
+ it "sets :locale to route locale" do
88
+ expect( node_route.options_for( "home#index", {} ) ).to include( locale: "en" )
89
+ end
90
+
91
+ it "sets :to to the result of #controller_and_action_for" do
92
+ allow(node_route).to receive(:controller_and_action_for).with( "home#index", foo: :bar).and_return("ok")
93
+ expect( node_route.options_for( "home#index", foo: :bar ) ).to include( to: "ok" )
94
+ end
95
+
96
+ it "sets :as to the result of #name by passing all other calculated options" do
97
+ expected_name_options = {
98
+ foo: :bar,
99
+ to: "home#index",
100
+ node_class: "Node",
101
+ node_id: "12",
102
+ locale: "en"
103
+ }
104
+
105
+ allow(node_route).to receive(:name).with( expected_name_options ).and_return("ok")
106
+ expect( node_route.options_for( "home#index", foo: :bar ) ).to include( as: "ok" )
107
+ end
108
+
109
+ it "preserves unrecognized option keys" do
110
+ expect( node_route.options_for( "home#index", foo: :bar ) ).to include( foo: :bar )
111
+ end
112
+
113
+ it "uses calculated keys in case conflicting option keys given" do
114
+ in_options = { to: "invalid", node_class: "invalid", node_id: "invalid", locale: "invalid" }
115
+
116
+ expect( node_route.options_for( "home#index", in_options ) ).to include({
117
+ to: "home#index",
118
+ node_class: "Node",
119
+ node_id: "12",
120
+ locale: "en"
121
+ })
122
+ end
123
+
124
+ context "when node route has site" do
125
+
126
+ it "sets :site to route site" do
127
+ node_route.site = "ok"
128
+ expect( node_route.options_for( "home#index", {} ) ).to include( site: "ok" )
129
+ end
130
+
131
+ it "overrides site given in options" do
132
+ node_route.site = "ok"
133
+ expect( node_route.options_for( "home#index", { site: "foo" } ) ).to include( site: "ok" )
134
+ end
135
+
136
+ end
137
+
138
+ context "when node route does not have site" do
139
+
140
+ it "does not set :site" do
141
+ expect( node_route.options_for( "home#index", {} ) ).to_not include( :site )
142
+ end
143
+
144
+ it "allows site from options" do
145
+ expect( node_route.options_for( "home#index", { site: "ok" } ) ).to include( site: "ok" )
146
+ end
147
+
148
+ end
149
+
150
+
151
+ end
152
+
153
+ describe "#name" do
154
+
155
+ context "when route options contain :as option" do
156
+
157
+ context "when neither :site nor :locale are set or are blank in route options" do
158
+ it "returns the value intact" do
159
+ expect(node_route.name( { as: "foo" } )).to eq "foo"
160
+ expect(node_route.name( { as: "foo", site: nil, locale: nil } )).to eq "foo"
161
+ end
162
+ end
163
+
164
+ context "when :site is set in route options" do
165
+ it "returns the value with site prepended" do
166
+ expect(node_route.name( { as: "foo", site: "sss" } )).to eq "sss_foo"
167
+ end
168
+ end
169
+
170
+ context "when :locale is set in route options" do
171
+ it "returns the value with locale prepended" do
172
+ expect(node_route.name( { as: "foo", locale: "lll" } )).to eq "lll_foo"
173
+ end
174
+ end
175
+
176
+ context "when both :site and :locale are set in route options" do
177
+ it "returns the value with site and locale prepended" do
178
+ expect(node_route.name( { as: "foo", site: "sss", locale: "lll" } )).to eq "sss_lll_foo"
179
+ end
180
+ end
181
+
182
+ end
183
+
184
+ context "when route options do not contain :as option" do
185
+ it "returns nil" do
186
+ expect(node_route.name( {} )).to be_nil
22
187
  end
23
188
  end
189
+
24
190
  end
25
191
 
26
192
  describe ".for" do
@@ -29,26 +195,27 @@ describe Releaf::Content::Route do
29
195
  end
30
196
 
31
197
  it "returns an array" do
32
- expect(described_class.for(HomePage, 'foo').class).to eq(Array)
198
+ expect(described_class.for(Node, HomePage, 'foo')).to be_a Array
33
199
  end
34
200
 
35
201
  context "when databse doesn't exists" do
36
202
  it "returns an empty array" do
37
- allow(described_class.node_class).to receive(:where).and_raise(ActiveRecord::NoDatabaseError.new("xxx"))
38
- expect(described_class.for(HomePage, 'foo')).to eq([])
203
+ allow(Node).to receive(:where).and_raise(ActiveRecord::NoDatabaseError.new("xxx"))
204
+ expect(described_class.for(Node, HomePage, 'foo')).to eq([])
39
205
  end
40
206
  end
41
207
 
42
- context "when releaf_nodes table doesn't exists" do
208
+ context "when node table doesn't exist" do
43
209
  it "returns an empty array" do
44
- allow(described_class.node_class).to receive(:where).and_raise(ActiveRecord::StatementInvalid.new("xxx"))
45
- expect(described_class.for(HomePage, 'foo')).to eq([])
210
+ allow(Node).to receive(:where).and_raise(ActiveRecord::StatementInvalid.new("xxx"))
211
+ expect(described_class.for(Node, HomePage, 'foo')).to eq([])
46
212
  end
47
213
  end
48
214
 
49
- context "when releaf_nodes table exists" do
50
- it "returns an array of Node::Route objects" do
51
- result = described_class.for(HomePage, 'foo')
215
+ context "when node table exists" do
216
+ it "returns an array of Node::Route objects returned by build_route_object" do
217
+ expect(described_class).to receive(:build_route_object).and_call_original
218
+ result = described_class.for(Node, HomePage, 'foo')
52
219
  expect(result.count).to eq(1)
53
220
  expect(result.first.class).to eq(described_class)
54
221
  end
@@ -56,30 +223,52 @@ describe Releaf::Content::Route do
56
223
  context "when node is not available" do
57
224
  it "does not include it in return" do
58
225
  allow_any_instance_of(Node).to receive(:available?).and_return(false)
59
- expect(described_class.for(HomePage, 'foo')).to eq([])
226
+ expect(described_class.for(Node, HomePage, 'foo')).to eq([])
60
227
  end
61
228
  end
229
+
230
+ it "accepts node_class as string also" do
231
+ result = described_class.for('Node', HomePage, 'foo')
232
+ expect(result.count).to eq(1)
233
+ end
234
+
62
235
  end
63
236
  end
64
237
 
65
- describe '#params' do
66
- it "returns params for router method" do
67
- expect(node_route.params("home#index")).to eq ['/en', {:node_id=>"12", :locale=>"en", :to=>"home#index"}]
238
+
239
+ describe ".build_route_object" do
240
+
241
+ let(:node) { create(:home_page_node, id: 23, locale: "lv", slug: "llvv") }
242
+ let(:controller) { described_class.default_controller( node.class ) }
243
+ let(:route) { described_class.build_route_object(node, controller) }
244
+
245
+ it "returns a route instance" do
246
+ expect(route).to be_a Releaf::Content::Route
68
247
  end
69
248
 
70
- context "when :as given in args" do
71
- context "when node has a locale" do
72
- it "prepends locale to :as" do
73
- expect(node_route.params("home#index", as: "home").last).to match hash_including(as: "en_home")
74
- end
75
- end
249
+ it "assigns node_class from given node" do
250
+ expect(route.node_class).to be Node
251
+ end
76
252
 
77
- context "when node does not have a locale" do
78
- it "doesn't modify :as option" do
79
- node_route.locale = nil
80
- expect(node_route.params("home#index", as: "home").last).to match hash_including(as: "home")
81
- end
82
- end
253
+ it "assigns node_id from given node" do
254
+ expect(route.node_id).to eq "23"
255
+ end
256
+
257
+ it "assigns path from given node" do
258
+ expect(route.path).to eq "/llvv"
259
+ end
260
+
261
+ it "assigns locale from given node" do
262
+ expect(route.locale).to eq "lv"
263
+ end
264
+
265
+ it "assigns default_controller from given argument" do
266
+ expect(route.default_controller).to be controller
267
+ end
268
+
269
+ it "assigns site from content routing configuration" do
270
+ allow( Releaf::Content).to receive(:routing).and_return('Node' => {site: 'some_site'})
271
+ expect(route.site).to eq 'some_site'
83
272
  end
84
273
  end
85
274
  end
@@ -15,34 +15,82 @@ describe Releaf::Content::RoutesReloader do
15
15
 
16
16
  describe "on each request" do
17
17
  it "compares latest updates" do
18
- expect(described_class).to receive(:reload_if_expired)
18
+ expect(described_class).to receive(:reload_if_needed)
19
19
  request
20
20
  end
21
21
  end
22
22
 
23
- describe ".reload_if_expired" do
24
- context "when no nodes exists" do
23
+ describe ".reset!" do
24
+
25
+ it "changes class @updated_at instance to nil" do
26
+ described_class.instance_variable_set(:@updated_at, "x")
27
+ expect{ described_class.reset! }.to change{ described_class.instance_variable_get(:@updated_at) }.to(nil)
28
+ end
29
+
30
+ end
31
+
32
+ describe ".reload_if_needed" do
33
+
34
+ context "when reload is needed" do
35
+ it "reloads routes" do
36
+ allow(described_class).to receive(:needs_reload?).and_return true
37
+ expect(Rails.application).to receive(:reload_routes!)
38
+ described_class.reload_if_needed
39
+ end
40
+ end
41
+
42
+ context "when reload is not needed" do
25
43
  it "does not reload routes" do
26
- allow(Node).to receive(:updated_at).and_return(nil)
44
+ allow(described_class).to receive(:needs_reload?).and_return false
27
45
  expect(Rails.application).to_not receive(:reload_routes!)
28
- described_class.reload_if_expired
46
+ described_class.reload_if_needed
29
47
  end
30
48
  end
31
49
 
32
- context "when routes is up to date" do
33
- it "does not reload routes" do
50
+ end
51
+
52
+ describe ".needs_reload?" do
53
+
54
+ context "when no nodes exist" do
55
+ it "returns false" do
56
+ allow(Node).to receive(:updated_at).and_return(nil)
57
+ expect(described_class.needs_reload?).to be false
58
+ end
59
+ end
60
+
61
+ context "when node routes has not been loaded" do
62
+ it "returns true" do
63
+ described_class.instance_variable_set(:@updated_at, nil)
34
64
  allow(Node).to receive(:updated_at).and_return(Time.parse("1991-01-01"))
35
- expect(Rails.application).to_not receive(:reload_routes!)
36
- described_class.reload_if_expired
65
+ expect(described_class.needs_reload?).to be true
37
66
  end
38
67
  end
39
68
 
40
- context "when routes is outdated" do
41
- it "reloads routes" do
42
- allow(Node).to receive(:updated_at).and_return(Time.now)
43
- expect(Rails.application).to receive(:reload_routes!)
44
- described_class.reload_if_expired
69
+ context "when node routes are up to date" do
70
+ it "returns false" do
71
+ described_class.instance_variable_set(:@updated_at, Time.parse("1991-01-01"))
72
+ allow(Node).to receive(:updated_at).and_return(Time.parse("1991-01-01"))
73
+ expect(described_class.needs_reload?).to be false
45
74
  end
46
75
  end
76
+
77
+ context "when node routes are outdated" do
78
+ it "returns true" do
79
+ allow(Node).to receive(:updated_at).and_return(Time.now + 1.minute)
80
+ expect(described_class.needs_reload?).to be true
81
+ end
82
+ end
83
+
84
+ it "checks all content model classes" do
85
+ class RouteReloaderDummyNode
86
+ def self.updated_at
87
+ Time.now + 1.minute
88
+ end
89
+ end
90
+ allow(Releaf::Content).to receive(:models).and_return [ Node, RouteReloaderDummyNode ]
91
+ allow(Node).to receive(:updated_at).and_return(nil)
92
+ expect(described_class.needs_reload?).to be true
93
+ end
94
+
47
95
  end
48
96
  end
@@ -1,142 +1,310 @@
1
1
  require "rails_helper"
2
2
 
3
3
  describe Releaf::Content::NodeMapper do
4
- after(:all) do
5
- # reset dummy app routes
4
+
5
+ let(:multiple_node_resources) {{
6
+ 'Node' => {
7
+ controller: 'Releaf::Content::NodesController',
8
+ routing: { site: "main_site", constraints: { host: /^releaf\.local$/ } }
9
+ },
10
+ 'OtherSite::OtherNode' => {
11
+ controller: 'Admin::OtherSite::OtherNodesController',
12
+ routing: { site: "other_site", constraints: { host: /^other\.releaf\.local$/ } }
13
+ }
14
+ }}
15
+
16
+ before do
17
+ @text_page = create(:text_page)
18
+ @lv_root_node = create(:home_page_node, name: "lv", locale: "lv", slug: 'lv')
19
+ @node = create(:node, slug: 'test-page', content: @text_page, parent: @lv_root_node)
20
+ end
21
+
22
+ before with_multiple_node_classes: true do
23
+ allow( Releaf.application.config ).to receive(:content).and_return(
24
+ Releaf::Content::Configuration.new(resources: multiple_node_resources)
25
+ )
26
+ @other_text_page = create(:text_page)
27
+ @other_lv_root_node = create(:other_home_page_node, name: "lv", locale: "lv", slug: 'lv')
28
+ @other_node = create(:other_node, slug: 'test-page', content: @other_text_page, parent: @other_lv_root_node)
29
+ end
30
+
31
+ after do
6
32
  Dummy::Application.reload_routes!
7
33
  end
8
34
 
9
- before do
35
+ after(:all) do
36
+ # without this the test environent remains polluted with test node class config.
37
+ # the routing configuration gets already reset after each test in the after block above
38
+ # but that seems to not be enough
10
39
  Dummy::Application.reload_routes!
11
- @text_page = FactoryGirl.create(:text_page)
12
- @lv_root = create(:home_page_node, name: "lv", locale: "lv", slug: 'lv')
13
- @node = FactoryGirl.create(:node, slug: 'test-page', content: @text_page, parent: @lv_root)
14
40
  end
15
41
 
16
- describe "#releaf_routes_for" do
42
+ describe "#node_routes_for", create_nodes: true do
43
+
44
+ it "draws public website routes for default node class" do
45
+
46
+ routes.draw do
47
+ node_routes_for(TextPage) do |route|
48
+ get 'show'
49
+ delete :destroy
50
+ end
51
+ end
17
52
 
18
- describe "using current node path" do
19
- example "using default controller " do
53
+ expect(get: '/lv/test-page').to route_to(
54
+ "controller" => "text_pages",
55
+ "action" => "show",
56
+ "node_class" => "Node",
57
+ "node_id" => @node.id.to_s,
58
+ "locale" => 'lv'
59
+ )
60
+
61
+ expect(delete: '/lv/test-page').to route_to(
62
+ "controller" => "text_pages",
63
+ "action" => "destroy",
64
+ "node_class" => "Node",
65
+ "node_id" => @node.id.to_s,
66
+ "locale" => 'lv'
67
+ )
68
+ end
69
+
70
+ context "when a controller is given in arguments" do
71
+
72
+ it "draws the public website route to given controller" do
20
73
  routes.draw do
21
- releaf_routes_for(TextPage) do |route|
74
+ node_routes_for(TextPage, controller: 'home_pages') do |route|
22
75
  get 'show'
23
- delete :destroy
24
76
  end
25
77
  end
26
78
 
27
79
  expect(get: '/lv/test-page').to route_to(
28
- "controller" => "text_pages",
80
+ "controller" => "home_pages",
29
81
  "action" => "show",
30
- "node_id" => @node.id.to_s,
31
- "locale" => 'lv'
32
- )
33
-
34
- expect(delete: '/lv/test-page').to route_to(
35
- "controller" => "text_pages",
36
- "action" => "destroy",
82
+ "node_class" => "Node",
37
83
  "node_id" => @node.id.to_s,
38
84
  "locale" => 'lv'
39
85
  )
40
86
  end
41
87
 
42
- example "overriding default controller" do
88
+ end
89
+
90
+ context "when a controller is passed in route options" do
91
+ it "draws the public website route using the controller from options" do
43
92
  routes.draw do
44
- releaf_routes_for(TextPage, controller: 'home_pages') do |route|
45
- get 'show'
46
- delete :destroy
93
+ node_routes_for(TextPage, controller: 'doesnt_matter') do |route|
94
+ get 'home_pages#hide'
47
95
  end
48
96
  end
49
97
 
50
98
  expect(get: '/lv/test-page').to route_to(
51
99
  "controller" => "home_pages",
52
- "action" => "show",
100
+ "action" => "hide",
101
+ "node_class" => "Node",
53
102
  "node_id" => @node.id.to_s,
54
103
  "locale" => 'lv'
55
104
  )
56
105
  end
106
+ end
57
107
 
58
- example "specifying different than default controller for route" do
108
+ context "when custom action is given in :to" do
109
+ it "draws the public website route using the action from :to" do
59
110
  routes.draw do
60
- releaf_routes_for(TextPage, controller: 'doesnt_matter') do |route|
61
- get 'home_pages#hide'
111
+ node_routes_for(TextPage) do |route|
112
+ get '' , to: '#list'
62
113
  end
63
114
  end
64
115
 
65
116
  expect(get: '/lv/test-page').to route_to(
66
- "controller" => "home_pages",
67
- "action" => "hide",
117
+ "controller" => "text_pages",
118
+ "action" => "list",
119
+ "node_class" => "Node",
68
120
  "node_id" => @node.id.to_s,
69
121
  "locale" => 'lv'
70
122
  )
123
+
71
124
  end
72
125
  end
73
126
 
74
- describe "adding uri parts to node path" do
75
- example "speciffying custom controller and action" do
127
+ context "when custom controller and action are given in :to" do
128
+ it "draws the public website route using the given controller and action from :to" do
76
129
  routes.draw do
77
- releaf_routes_for(TextPage) do |route|
78
- get ':my_id/list', to: 'home_pages#list'
130
+ node_routes_for(TextPage) do |route|
131
+ get '' , to: 'home_pages#list'
79
132
  end
80
133
  end
81
134
 
82
- expect(get: '/lv/test-page/8888/list').to route_to(
135
+ expect(get: '/lv/test-page').to route_to(
83
136
  "controller" => "home_pages",
84
137
  "action" => "list",
138
+ "node_class" => "Node",
85
139
  "node_id" => @node.id.to_s,
86
- "locale" => 'lv',
87
- "my_id" => '8888'
140
+ "locale" => 'lv'
88
141
  )
89
142
  end
143
+ end
90
144
 
91
- example "speciffying action" do
145
+ context "when additional params are given in route path" do
146
+ it "passes the params to the public website route" do
92
147
  routes.draw do
93
- releaf_routes_for(TextPage) do |route|
94
- get ':my_id/list', to: '#list'
148
+ node_routes_for(TextPage) do |route|
149
+ get ':some_id', to: "#view"
95
150
  end
96
151
  end
97
152
 
98
- expect(get: '/lv/test-page/8888/list').to route_to(
153
+ expect(get: '/lv/test-page/8888').to route_to(
99
154
  "controller" => "text_pages",
100
- "action" => "list",
155
+ "action" => "view",
156
+ "node_class" => "Node",
101
157
  "node_id" => @node.id.to_s,
102
158
  "locale" => 'lv',
103
- "my_id" => '8888'
159
+ "some_id" => '8888'
104
160
  )
105
161
  end
162
+ end
106
163
 
107
- example "speciffying action and overriding default controller" do
164
+ context "when custom path is given for route" do
165
+ it "uses the custom path for public website route" do
108
166
  routes.draw do
109
- releaf_routes_for(TextPage, controller: 'home_pages') do |route|
110
- get ':my_id/list', to: '#list'
167
+ node_routes_for(TextPage) do |route|
168
+ get 'home_pages#show', path: "#{route.path}/abc/:my_id"
111
169
  end
112
170
  end
113
171
 
114
- expect(get: '/lv/test-page/8888/list').to route_to(
172
+ expect(get: '/lv/test-page/abc/333').to route_to(
115
173
  "controller" => "home_pages",
116
- "action" => "list",
174
+ "action" => "show",
175
+ "node_class" => "Node",
117
176
  "node_id" => @node.id.to_s,
118
177
  "locale" => 'lv',
119
- "my_id" => '8888'
178
+ "my_id" => '333'
120
179
  )
121
180
  end
181
+ end
122
182
 
123
- example "speciffying custom path" do
183
+ context "when custom node class is given as an argument", with_multiple_node_classes: true do
184
+
185
+ it "uses that node class for the public website route" do
124
186
  routes.draw do
125
- releaf_routes_for(TextPage) do |route|
126
- get 'home_pages#show', path: "#{route.path}/abc/:my_id"
187
+ node_routes_for(TextPage, node_class: "OtherSite::OtherNode") do |route|
188
+ get 'show'
127
189
  end
128
190
  end
129
191
 
130
- expect(get: '/lv/test-page/abc/333').to route_to(
131
- "controller" => "home_pages",
192
+ expect(get: '/lv/test-page').to route_to(
193
+ "controller" => "text_pages",
132
194
  "action" => "show",
133
- "node_id" => @node.id.to_s,
195
+ "node_class" => "OtherSite::OtherNode",
196
+ "node_id" => @other_node.id.to_s,
134
197
  "locale" => 'lv',
135
- "my_id" => '333'
198
+ "site" => 'other_site'
136
199
  )
137
200
  end
138
201
 
139
202
  end
140
203
 
141
204
  end
205
+
206
+ describe "#for_node_class", with_multiple_node_classes: true do
207
+
208
+ it "uses given node class as a default when drawing public website routes in the given block" do
209
+ routes.draw do
210
+ for_node_class "OtherSite::OtherNode" do
211
+ node_routes_for(TextPage) do |route|
212
+ get 'show'
213
+ end
214
+ end
215
+ end
216
+
217
+ expect(get: '/lv/test-page').to route_to(
218
+ "controller" => "text_pages",
219
+ "action" => "show",
220
+ "node_class" => "OtherSite::OtherNode",
221
+ "node_id" => @other_node.id.to_s,
222
+ "locale" => 'lv',
223
+ "site" => 'other_site'
224
+ )
225
+ end
226
+
227
+ it "restores default node class after block has been executed" do
228
+ routes.draw do
229
+ for_node_class "OtherSite::OtherNode" do
230
+ node_routes_for(TextPage) do |route|
231
+ get 'show'
232
+ end
233
+ end
234
+ node_routes_for(HomePage) do |route|
235
+ get 'show'
236
+ end
237
+ end
238
+
239
+ expect(get: '/lv').to route_to(
240
+ "controller" => "home_pages",
241
+ "action" => "show",
242
+ "node_class" => "Node",
243
+ "node_id" => @lv_root_node.id.to_s,
244
+ "locale" => 'lv',
245
+ "site" => 'main_site'
246
+ )
247
+ end
248
+
249
+ end
250
+
251
+ describe "#node_routing", with_multiple_node_classes: true do
252
+
253
+ it "draws public website routes for all node classes in the given routing hash using respective route constraints" do
254
+
255
+ routes.draw do
256
+ node_routing( Releaf::Content.routing ) do
257
+ node_routes_for(TextPage) do |route|
258
+ get 'show'
259
+ end
260
+ end
261
+ end
262
+
263
+ expect(get: 'http://releaf.local/lv/test-page').to route_to(
264
+ "controller" => "text_pages",
265
+ "action" => "show",
266
+ "node_class" => "Node",
267
+ "node_id" => @node.id.to_s,
268
+ "locale" => 'lv',
269
+ "site" => 'main_site'
270
+ )
271
+
272
+ expect(get: 'http://other.releaf.local/lv/test-page').to route_to(
273
+ "controller" => "text_pages",
274
+ "action" => "show",
275
+ "node_class" => "OtherSite::OtherNode",
276
+ "node_id" => @other_node.id.to_s,
277
+ "locale" => 'lv',
278
+ "site" => 'other_site'
279
+ )
280
+
281
+ end
282
+
283
+ context "when passed a routing hash for only a subset of available node classes" do
284
+ it "draws constrained public website routes only for the given node classes" do
285
+ routes.draw do
286
+ routing_hash = Releaf::Content.routing.except('Node')
287
+ node_routing( routing_hash ) do
288
+ node_routes_for(TextPage) do |route|
289
+ get 'show'
290
+ end
291
+ end
292
+ end
293
+
294
+ expect(get: 'http://other.releaf.local/lv/test-page').to route_to(
295
+ "controller" => "text_pages",
296
+ "action" => "show",
297
+ "node_class" => "OtherSite::OtherNode",
298
+ "node_id" => @other_node.id.to_s,
299
+ "locale" => 'lv',
300
+ "site" => 'other_site'
301
+ )
302
+
303
+ expect(get: 'http://releaf.local/lv/test-page').to_not be_routable
304
+
305
+ end
306
+ end
307
+
308
+ end
309
+
142
310
  end