releaf-content 1.1.20 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (31) hide show
  1. checksums.yaml +5 -5
  2. data/app/assets/config/releaf_content_manifest.js +2 -0
  3. data/app/assets/javascripts/controllers/releaf/content/nodes.js +6 -9
  4. data/app/controllers/releaf/content/nodes_controller.rb +1 -1
  5. data/lib/releaf/content/engine.rb +2 -2
  6. data/lib/releaf/content/node.rb +3 -3
  7. data/lib/releaf/content/route.rb +0 -1
  8. metadata +12 -58
  9. data/spec/builders/content/nodes/action_dialog_spec.rb +0 -39
  10. data/spec/builders/content/nodes/content_form_builder_spec.rb +0 -24
  11. data/spec/builders/content/nodes/form_builder_spec.rb +0 -262
  12. data/spec/builders/content/nodes/toolbox_builder_spec.rb +0 -77
  13. data/spec/controllers/releaf/content/nodes_controller_spec.rb +0 -52
  14. data/spec/features/nodes_services_spec.rb +0 -437
  15. data/spec/features/nodes_spec.rb +0 -572
  16. data/spec/fixtures/dummy.png +0 -0
  17. data/spec/lib/releaf/content/acts_as_node_spec.rb +0 -90
  18. data/spec/lib/releaf/content/configuration_spec.rb +0 -159
  19. data/spec/lib/releaf/content/engine_spec.rb +0 -149
  20. data/spec/lib/releaf/content/node_spec.rb +0 -591
  21. data/spec/lib/releaf/content/route_spec.rb +0 -229
  22. data/spec/middleware/routes_reloader_spec.rb +0 -96
  23. data/spec/routing/node_mapper_spec.rb +0 -310
  24. data/spec/services/releaf/content/build_route_objects_spec.rb +0 -72
  25. data/spec/services/releaf/content/node/copy_spec.rb +0 -338
  26. data/spec/services/releaf/content/node/move_spec.rb +0 -20
  27. data/spec/services/releaf/content/node/save_under_parent_spec.rb +0 -49
  28. data/spec/services/releaf/content/node/service_spec.rb +0 -19
  29. data/spec/validators/content/node/parent_validator_spec.rb +0 -56
  30. data/spec/validators/content/node/root_validator_spec.rb +0 -69
  31. data/spec/validators/content/node/singleness_validator_spec.rb +0 -145
Binary file
@@ -1,90 +0,0 @@
1
- require "rails_helper"
2
-
3
- class ContactFormController < ActionController::Base
4
- acts_as_node
5
- end
6
-
7
- describe ActsAsNode do
8
- before do
9
- Book.acts_as_node
10
- end
11
-
12
- describe ".classes" do
13
- it "returns all registerd classes" do
14
- expect(ActsAsNode.classes).to include("ContactFormController", "Book")
15
- end
16
- end
17
-
18
- describe ".acts_as_node" do
19
- it "have configuration options for params and fields available through acts_as_node_configuration class method" do
20
- expect(Book.acts_as_node_configuration).to eq(params: nil, fields: nil)
21
-
22
- Book.acts_as_node params: ["x"], fields: ["a"]
23
- expect(Book.acts_as_node_configuration).to eq(params: ["x"], fields: ["a"])
24
- end
25
-
26
- it "has hard typed configuration options" do
27
- expect{ Book.acts_as_node xxxx: ["x"] }.to raise_error(ArgumentError, "unknown keyword: xxxx")
28
- end
29
- end
30
-
31
- describe ActiveRecord::Acts::Node do
32
- context "when model acts as node" do
33
- it "has name included within ActsAsNode.classes" do
34
- expect(ActsAsNode.classes.include?(Book.to_s)).to be true
35
- end
36
- end
37
-
38
- context ".acts_as_node_params" do
39
- before do
40
- allow_any_instance_of(Releaf::ResourceParams).to receive(:values).and_return(["a", "b"])
41
- end
42
-
43
- context "when `params` configuration is nil" do
44
- it "returns model params with `id` param" do
45
- allow(Book).to receive(:acts_as_node_configuration).and_return(params: nil)
46
- expect(Releaf::ResourceParams).to receive(:new).with(Book).and_call_original
47
- expect(Book.acts_as_node_params).to eq(["a", "b", :id])
48
- end
49
- end
50
-
51
- context "when `params` configuration is not nil" do
52
- it "returns configuration values with `id` param" do
53
- allow(Book).to receive(:acts_as_node_configuration).and_return(params: ["c", "d"])
54
- expect(Book.acts_as_node_params).to eq(["c", "d", :id])
55
- end
56
- end
57
- end
58
-
59
- context ".acts_as_node_fields" do
60
- before do
61
- allow_any_instance_of(Releaf::ResourceFields).to receive(:values).and_return(["a", "b"])
62
- end
63
-
64
- context "when `fields` configuration is nil" do
65
- it "returns model fields" do
66
- allow(Book).to receive(:acts_as_node_configuration).and_return(fields: nil)
67
- expect(Releaf::ResourceFields).to receive(:new).with(Book).and_call_original
68
- expect(Book.acts_as_node_fields).to eq(["a", "b"])
69
- end
70
- end
71
-
72
- context "when `fields` configuration is not nil" do
73
- it "returns configuration values" do
74
- allow(Book).to receive(:acts_as_node_configuration).and_return(fields: ["c", "d"])
75
- expect(Book.acts_as_node_fields).to eq(["c", "d"])
76
- end
77
- end
78
- end
79
-
80
- end
81
-
82
- describe ActionController::Acts::Node do
83
- context "when controller acts as node" do
84
- it "has name included within ActsAsNode.classes" do
85
- expect(ActsAsNode.classes.include?(ContactFormController.to_s)).to be true
86
- end
87
- end
88
-
89
- end
90
- end
@@ -1,159 +0,0 @@
1
- require "rails_helper"
2
-
3
- describe Releaf::Content::Configuration do
4
- class ContentConfigurationDummyNode; end;
5
- class ContentConfigurationDummyNodesController; end;
6
- subject{ described_class.new(resources: {}) }
7
-
8
- describe "#verify_resources_config" do
9
- context "when valid config" do
10
- it "does no raise an error" do
11
- config = {'Node' => { controller: 'Releaf::Content::NodesController'}}
12
- expect{ subject.verify_resources_config(config) }.to_not raise_error
13
- end
14
- end
15
-
16
- context "when config is not a hash" do
17
- it "raises an error" do
18
- config = :foo
19
- expect{ subject.verify_resources_config(config) }.to raise_error Releaf::Error, "Releaf.application.config.content.resources must be a Hash"
20
- end
21
- end
22
-
23
- context "when any of the hash keys are not strings" do
24
- it "raises an error" do
25
- config = {Node => { controller: 'Releaf::Content::NodesController', foo: "wat"}}
26
- expect{ subject.verify_resources_config(config) }.to raise_error Releaf::Error, "Releaf.application.config.content.resources must have string keys"
27
- end
28
- end
29
-
30
- context "when any of the entries does not have a hash as a value" do
31
- it "raises an error" do
32
- config = {'Node' => :foo}
33
- expect{ subject.verify_resources_config(config) }.to raise_error Releaf::Error, "Node in Releaf.application.config.content.resources must have a hash value"
34
- end
35
- end
36
-
37
- context "when any of the entries does not have controller class name set" do
38
- it "raises an error" do
39
- config = {'Node' => { foo: "wat" }}
40
- expect{ subject.verify_resources_config(config) }.to raise_error Releaf::Error, "Node in Releaf.application.config.content.resources must have controller class specified as a string"
41
- end
42
- end
43
-
44
- context "when any of the entries does not have a string for the controller class name" do
45
- it "raises an error" do
46
- config = {'Node' => { controller: Releaf::Content::NodesController, foo: "wat" }}
47
- expect{ subject.verify_resources_config(config) }.to raise_error Releaf::Error, "Node in Releaf.application.config.content.resources must have controller class specified as a string"
48
- end
49
- end
50
- end
51
-
52
- describe "#models" do
53
- it "returns an array of node model classes" do
54
- expect(subject).to receive(:model_names).and_return(['ContentConfigurationDummyNode', 'Object'])
55
- expect(subject.models).to eq [ContentConfigurationDummyNode, Object]
56
- end
57
- end
58
-
59
- describe "#model_names" do
60
- it "returns an array of defined node class names" do
61
- expect(subject).to receive(:resources).and_return(
62
- 'ContentConfigurationDummyNode' => { controller: 'Releaf::Content::NodesController' }
63
- )
64
- expect(subject.model_names).to eq [ 'ContentConfigurationDummyNode' ]
65
- end
66
-
67
- it "caches the result" do
68
- expect(subject).to receive(:resources).once.and_call_original
69
- subject.model_names
70
- subject.model_names
71
- end
72
- end
73
-
74
- describe "#default_model" do
75
- it "returns the first model from #models" do
76
- expect(subject).to receive(:models).and_return [ :foo, :bar ]
77
- expect(subject.default_model).to eq :foo
78
- end
79
- end
80
-
81
- describe "#controllers" do
82
- it "returns an array of node controller classes" do
83
- expect(subject).to receive(:controller_names).and_return([
84
- 'Releaf::Content::NodesController', 'Admin::OtherSite::OtherNodesController'
85
- ])
86
- expect(subject.controllers).to eq [Releaf::Content::NodesController, Admin::OtherSite::OtherNodesController]
87
- end
88
- end
89
-
90
- describe "#controller_names" do
91
- it "returns an array of defined node controller class names" do
92
- allow(subject).to receive(:resources).and_return(
93
- 'Node' => { controller: 'Releaf::Content::NodesController' },
94
- 'ContentConfigurationDummyNode' => { controller: 'ContentConfigurationDummyNodesController' }
95
- )
96
- expect(subject.controller_names).to eq [ 'Releaf::Content::NodesController', 'ContentConfigurationDummyNodesController' ]
97
- end
98
-
99
- it "caches the result" do
100
- expect(subject).to receive(:resources).once.and_call_original
101
- subject.controller_names
102
- subject.controller_names
103
- end
104
- end
105
-
106
- describe ".routing" do
107
- it "returns a hash with all node class names as string keys" do
108
- allow(subject).to receive(:resources).and_return(
109
- 'Node' => { controller: 'Releaf::Content::NodesController' },
110
- 'ContentConfigurationDummyNode' => { controller: 'ContentConfigurationDummyNodesController' }
111
- )
112
- result = subject.routing
113
- expect( result ).to be_a Hash
114
- expect( result.keys ).to eq ['Node', 'ContentConfigurationDummyNode' ]
115
- end
116
-
117
- context "when node has no routing defined" do
118
- it "returns routing hash with site and constraints set to nil" do
119
- allow(subject).to receive(:resources).and_return(
120
- 'Node' => { controller: 'Releaf::Content::NodesController' }
121
- )
122
- expect(subject.routing).to eq 'Node' => { site: nil, constraints: nil }
123
- end
124
- end
125
-
126
- context "when node has nil values for site and constraints in routing config" do
127
- it "returns routing hash with site and constraints set to nil" do
128
- allow(subject).to receive(:resources).and_return(
129
- 'Node' => {controller: 'Releaf::Content::NodesController', routing: { site: nil, constraints: nil }}
130
- )
131
- expect(subject.routing).to eq 'Node' => { site: nil, constraints: nil }
132
- end
133
- end
134
-
135
- context "when node has site defined in routing config" do
136
- it "returns the defined value in routing hash" do
137
- allow(subject).to receive(:resources).and_return(
138
- 'Node' => {controller: 'Releaf::Content::NodesController', routing: { site: "foo" }}
139
- )
140
- expect(subject.routing).to eq 'Node' => { site: "foo", constraints: nil }
141
- end
142
- end
143
-
144
- context "when node has constraints defined in routing config" do
145
- it "returns the defined value in routing hash" do
146
- allow(subject).to receive(:resources).and_return(
147
- 'Node' => {controller: 'Releaf::Content::NodesController', routing: { constraints: { host: /foo/ }}}
148
- )
149
- expect(subject.routing).to eq 'Node' => { site: nil, constraints: { host: /foo/ } }
150
- end
151
- end
152
-
153
- it "caches the result" do
154
- expect(subject).to receive(:resources).once.and_call_original
155
- subject.routing
156
- subject.routing
157
- end
158
- end
159
- end
@@ -1,149 +0,0 @@
1
- require "rails_helper"
2
-
3
- describe Releaf::Content do
4
- let(:configuration){ Releaf::Content::Configuration.new(resources: {}) }
5
-
6
- describe ".configure_component" do
7
- it "adds new `Releaf::Content::Configuration` configuration with default resources" do
8
- allow(Releaf::Content::Configuration).to receive(:new)
9
- .with(resources: { 'Node' => { controller: 'Releaf::Content::NodesController' } }).and_return("_new")
10
- expect(Releaf.application.config).to receive(:add_configuration).with("_new")
11
- described_class.configure_component
12
- end
13
- end
14
-
15
- describe ".configuration" do
16
- it "returns a configuration instance" do
17
- expect(Releaf.application.config).to receive(:content).and_return(:ok)
18
- expect(described_class.configuration).to eq :ok
19
- end
20
- end
21
-
22
- [ :resources, :models, :default_model, :controllers, :routing ].each do |method|
23
- describe ".#{method}" do
24
- it "returns the method result from the configuration instance" do
25
- allow(described_class).to receive(:configuration).and_return(configuration)
26
- allow(configuration).to receive(method).and_return(:ok)
27
- expect(described_class.send(method)).to eq :ok
28
- end
29
- end
30
- end
31
-
32
- describe ".draw_component_routes", type: :routing do
33
- before do
34
- allow(described_class).to receive(:configuration).and_return(configuration)
35
- allow(configuration).to receive(:resources).and_return(
36
- 'Node' => { controller: 'Releaf::Content::NodesController' },
37
- 'OtherSite::OtherNode' => { controller: 'Admin::OtherSite::OtherNodesController' }
38
- )
39
- Dummy::Application.reload_routes!
40
- end
41
-
42
- after(:all) do
43
- Dummy::Application.reload_routes!
44
- end
45
-
46
- context "draws named admin routes for all defined content node controllers" do
47
-
48
- it "draws #index route" do
49
- expect( releaf_content_nodes_path ).to eq "/admin/nodes"
50
- expect( admin_other_site_other_nodes_path ).to eq "/admin/other_nodes"
51
- expect(get: "/admin/nodes").to route_to("releaf/content/nodes#index")
52
- expect(get: "/admin/other_nodes").to route_to("admin/other_site/other_nodes#index")
53
- end
54
-
55
- it "draws #new route" do
56
- expect( new_releaf_content_node_path ).to eq "/admin/nodes/new"
57
- expect( new_admin_other_site_other_node_path ).to eq "/admin/other_nodes/new"
58
- expect(get: "/admin/nodes/new").to route_to("releaf/content/nodes#new")
59
- expect(get: "/admin/other_nodes/new").to route_to("admin/other_site/other_nodes#new")
60
- end
61
-
62
- it "draws #create route" do
63
- expect(post: "/admin/nodes").to route_to("releaf/content/nodes#create")
64
- expect(post: "/admin/other_nodes").to route_to("admin/other_site/other_nodes#create")
65
- end
66
-
67
- it "draws #content_type_dialog route" do
68
- expect( content_type_dialog_releaf_content_nodes_path ).to eq "/admin/nodes/content_type_dialog"
69
- expect( content_type_dialog_admin_other_site_other_nodes_path ).to eq "/admin/other_nodes/content_type_dialog"
70
- expect(get: "/admin/nodes/content_type_dialog").to route_to("releaf/content/nodes#content_type_dialog")
71
- expect(get: "/admin/other_nodes/content_type_dialog").to route_to("admin/other_site/other_nodes#content_type_dialog")
72
- end
73
-
74
- it "draws #generate_url route" do
75
- expect( generate_url_releaf_content_nodes_path ).to eq "/admin/nodes/generate_url"
76
- expect( generate_url_admin_other_site_other_nodes_path ).to eq "/admin/other_nodes/generate_url"
77
- expect(get: "/admin/nodes/generate_url").to route_to("releaf/content/nodes#generate_url")
78
- expect(get: "/admin/other_nodes/generate_url").to route_to("admin/other_site/other_nodes#generate_url")
79
- end
80
-
81
- it "draws #edit route" do
82
- expect( edit_releaf_content_node_path(1) ).to eq "/admin/nodes/1/edit"
83
- expect( edit_admin_other_site_other_node_path(1) ).to eq "/admin/other_nodes/1/edit"
84
- expect(get: "/admin/nodes/1/edit").to route_to("releaf/content/nodes#edit", "id" => "1")
85
- expect(get: "/admin/other_nodes/1/edit").to route_to("admin/other_site/other_nodes#edit", "id" => "1")
86
- end
87
-
88
- it "draws #update route" do
89
- expect(patch: "/admin/nodes/1").to route_to("releaf/content/nodes#update", "id" => "1")
90
- expect(patch: "/admin/other_nodes/1").to route_to("admin/other_site/other_nodes#update", "id" => "1")
91
- end
92
-
93
- it "draws #copy_dialog route" do
94
- expect( copy_dialog_releaf_content_node_path(1) ).to eq "/admin/nodes/1/copy_dialog"
95
- expect( copy_dialog_admin_other_site_other_node_path(1) ).to eq "/admin/other_nodes/1/copy_dialog"
96
- expect(get: "/admin/nodes/1/copy_dialog").to route_to("releaf/content/nodes#copy_dialog", "id" => "1")
97
- expect(get: "/admin/other_nodes/1/copy_dialog").to route_to("admin/other_site/other_nodes#copy_dialog", "id" => "1")
98
- end
99
-
100
- it "draws #copy route" do
101
- expect( copy_releaf_content_node_path(1) ).to eq "/admin/nodes/1/copy"
102
- expect( copy_admin_other_site_other_node_path(1) ).to eq "/admin/other_nodes/1/copy"
103
- expect(post: "/admin/nodes/1/copy").to route_to("releaf/content/nodes#copy", "id" => "1")
104
- expect(post: "/admin/other_nodes/1/copy").to route_to("admin/other_site/other_nodes#copy", "id" => "1")
105
- end
106
-
107
- it "draws #move_dialog route" do
108
- expect( move_dialog_releaf_content_node_path(1) ).to eq "/admin/nodes/1/move_dialog"
109
- expect( move_dialog_admin_other_site_other_node_path(1) ).to eq "/admin/other_nodes/1/move_dialog"
110
- expect(get: "/admin/nodes/1/move_dialog").to route_to("releaf/content/nodes#move_dialog", "id" => "1")
111
- expect(get: "/admin/other_nodes/1/move_dialog").to route_to("admin/other_site/other_nodes#move_dialog", "id" => "1")
112
- end
113
-
114
- it "draws #move route" do
115
- expect( move_releaf_content_node_path(1) ).to eq "/admin/nodes/1/move"
116
- expect( move_admin_other_site_other_node_path(1) ).to eq "/admin/other_nodes/1/move"
117
- expect(post: "/admin/nodes/1/move").to route_to("releaf/content/nodes#move", "id" => "1")
118
- expect(post: "/admin/other_nodes/1/move").to route_to("admin/other_site/other_nodes#move", "id" => "1")
119
- end
120
-
121
- it "draws #toolbox route" do
122
- expect( toolbox_releaf_content_node_path(1) ).to eq "/admin/nodes/1/toolbox"
123
- expect( toolbox_admin_other_site_other_node_path(1) ).to eq "/admin/other_nodes/1/toolbox"
124
- expect(get: "/admin/nodes/1/toolbox").to route_to("releaf/content/nodes#toolbox", "id" => "1")
125
- expect(get: "/admin/other_nodes/1/toolbox").to route_to("admin/other_site/other_nodes#toolbox", "id" => "1")
126
- end
127
-
128
- it "draws #confirm_destroy route" do
129
- expect( confirm_destroy_releaf_content_node_path(1) ).to eq "/admin/nodes/1/confirm_destroy"
130
- expect( confirm_destroy_admin_other_site_other_node_path(1) ).to eq "/admin/other_nodes/1/confirm_destroy"
131
- expect(get: "/admin/nodes/1/confirm_destroy").to route_to("releaf/content/nodes#confirm_destroy", "id" => "1")
132
- expect(get: "/admin/other_nodes/1/confirm_destroy").to route_to("admin/other_site/other_nodes#confirm_destroy", "id" => "1")
133
- end
134
-
135
- it "draws #destroy route" do
136
- expect(delete: "/admin/nodes/1").to route_to("releaf/content/nodes#destroy", "id" => "1")
137
- expect(delete: "/admin/other_nodes/1").to route_to("admin/other_site/other_nodes#destroy", "id" => "1")
138
- end
139
-
140
- it "does not draw #show route" do
141
- expect(get: "/admin/nodes/1").to route_to("releaf/root#page_not_found", "path" => "nodes/1")
142
- expect(get: "/admin/other_nodes/1").to route_to("releaf/root#page_not_found", "path" => "other_nodes/1")
143
- end
144
-
145
- end
146
-
147
- end
148
-
149
- end
@@ -1,591 +0,0 @@
1
- require "rails_helper"
2
-
3
- describe Node do
4
- class PlainNode < ActiveRecord::Base
5
- include Releaf::Content::Node
6
- self.table_name = "nodes"
7
- end
8
-
9
- let(:plain_subject){ PlainNode.new }
10
-
11
- it { is_expected.to accept_nested_attributes_for(:content) }
12
- it { is_expected.to belong_to(:content) }
13
-
14
- it "includes Releaf::Content::Node module" do
15
- expect( Node.included_modules ).to include Releaf::Content::Node
16
- end
17
-
18
- describe "validations" do
19
- it { is_expected.to validate_presence_of(:name) }
20
- it { is_expected.to validate_presence_of(:slug) }
21
- it { is_expected.to validate_presence_of(:content_type) }
22
- it { is_expected.to validate_uniqueness_of(:slug).scoped_to(:parent_id) }
23
- it { is_expected.to validate_length_of(:name).is_at_most(255) }
24
- it { is_expected.to validate_length_of(:slug).is_at_most(255) }
25
- end
26
-
27
- describe "after save" do
28
- it "sets node update to current time" do
29
- expect( Node ).to receive(:updated).once
30
- create(:node)
31
- end
32
- end
33
-
34
- describe ".active (scope)" do
35
- it "returns active nodes" do
36
- expect( Node ).to receive(:where).with(active: true).and_return('foo')
37
- expect( Node.active ).to eq 'foo'
38
- end
39
- end
40
-
41
- describe "#content_class" do
42
- context 'when #content_type is nil' do
43
- it 'returns nil' do
44
- subject.content_type = nil
45
- expect( subject.content_class ).to be nil
46
- end
47
- end
48
-
49
- context "when #content_type is blank string" do
50
- it 'returns nil' do
51
- subject.content_type = ""
52
- expect( subject.content_class ).to be nil
53
- end
54
- end
55
-
56
- context "when #content_type is not blank" do
57
- it "constantizes it" do
58
- subject.content_type = "Node"
59
- expect( subject.content_class ).to eq Node
60
- end
61
- end
62
- end
63
-
64
- describe "#to_s" do
65
- it "returns name" do
66
- expect(subject.to_s).to eq(subject.name)
67
- end
68
- end
69
-
70
- describe "#locale" do
71
- before do
72
- root = create(:node, locale: "lv")
73
- parent = create(:text_page_node, locale: nil, parent_id: root.id)
74
- @child1 = create(:text_page_node, locale: nil, parent_id: parent.id)
75
- @child2 = create(:text_page_node, parent_id: parent.id, locale: "en")
76
- end
77
-
78
- context "when node locale is nil" do
79
- it "uses closest parent locale" do
80
- expect(@child1.locale).to eq("lv")
81
- end
82
- end
83
-
84
- context "when object node have locale" do
85
- it "uses closest parent locale" do
86
- expect(@child2.locale).to eq("en")
87
- end
88
- end
89
- end
90
-
91
- describe "#destroy" do
92
- context "when content object class exists" do
93
- let!(:node) { create(:home_page_node) }
94
-
95
- it "deletes record" do
96
- expect { node.destroy }.to change { Node.count }.by(-1)
97
- end
98
-
99
- it "deletes associated record" do
100
- expect { node.destroy }.to change { HomePage.count }.by(-1)
101
- end
102
- end
103
-
104
- context "when content object class doesn't exists" do
105
- let!(:node) { create(:home_page_node) }
106
- before do
107
- node.update_columns(content_type: 'NonExistingTestModel')
108
- end
109
-
110
- it "deletes record" do
111
- expect { node.destroy }.to change { Node.count }.by(-1)
112
- end
113
- end
114
-
115
- it "sets node update to current time" do
116
- node = create(:node)
117
- expect( Node ).to receive(:updated).once
118
- node.destroy
119
- end
120
- end
121
-
122
- describe "#attributes_to_not_copy" do
123
- it "returns array with attributes" do
124
- subject.locale = "lv"
125
- expect( subject.attributes_to_not_copy ).to match_array %w[content_id depth id item_position lft rgt created_at updated_at]
126
- end
127
-
128
- context "when locale is blank" do
129
- it "includes locale within returned list" do
130
- expect( subject.attributes_to_not_copy ).to match_array %w[content_id depth id item_position lft rgt created_at updated_at locale]
131
- end
132
- end
133
- end
134
-
135
- describe "#attributes_to_copy" do
136
- it "returns object attributes excluding #attributes_to_not_copy" do
137
- node = Node.new
138
- allow( node ).to receive(:attributes_to_not_copy).and_return(%w[lft rgt])
139
- expect( node.attributes_to_copy ).to eq(Node.column_names - %w[lft rgt])
140
- end
141
- end
142
-
143
- describe "#reasign_slug" do
144
- it "updates slug" do
145
- node = create(:node)
146
- old_slug = node.slug
147
- node.name = 'woo hoo'
148
- expect { node.reasign_slug }.to change { node.slug }.from(old_slug).to('woo-hoo')
149
- end
150
- end
151
-
152
- describe "#assign_attributes_from" do
153
- let(:source_node) { create(:node, active: false) }
154
-
155
- it "copies #attributes_to_copy attributes" do
156
-
157
- allow( source_node ).to receive(:attributes_to_copy).and_return(['name'])
158
-
159
- expect( source_node ).to receive(:name).and_call_original
160
- expect( source_node ).to_not receive(:parent_id)
161
- expect( source_node ).to_not receive(:content_type)
162
- expect( source_node ).to_not receive(:active)
163
-
164
- new_node = Node.new
165
-
166
- new_node.assign_attributes_from source_node
167
-
168
- expect( new_node.parent_id ).to be nil
169
- expect( new_node.content_type ).to be nil
170
- expect( new_node.active ).to be true
171
- end
172
- end
173
-
174
- describe ".children_max_item_position" do
175
- before do
176
- @home_page_node = create(:home_page_node, item_position: 1, locale: "lv")
177
- @home_page_node_2 = create(:home_page_node, item_position: 2, locale: "en")
178
- @text_page_node_3 = create(:text_page_node, parent_id: @home_page_node_2.id, item_position: 1)
179
- @text_page_node_4 = create(:text_page_node, parent_id: @home_page_node_2.id, item_position: 2)
180
-
181
- # it is important to reload nodes, otherwise associations will return empty set
182
- @home_page_node.reload
183
- @home_page_node_2.reload
184
- @text_page_node_3.reload
185
- @text_page_node_4.reload
186
- end
187
-
188
- context "when passing nil" do
189
- it "returns max item_position of root nodes" do
190
- expect( Node.children_max_item_position(nil) ).to eq 2
191
- end
192
- end
193
-
194
- context "when passing node with children" do
195
- it "returns max item_position of node children" do
196
- expect( Node.children_max_item_position(@home_page_node_2) ).to eq 2
197
- end
198
- end
199
-
200
- context "when passing node without children" do
201
- it "returns 0" do
202
- expect( Node.children_max_item_position(@text_page_node_4) ).to eq 0
203
- end
204
- end
205
- end
206
-
207
- describe "#move" do
208
- it "calls Node move service" do
209
- expect(Releaf::Content::Node::Move).to receive(:call).with(node: subject, parent_id: 12)
210
- subject.move(12)
211
- end
212
- end
213
-
214
- describe "#maintain_name" do
215
- let(:root) { create(:home_page_node) }
216
- let(:node) { create(:text_page_node, parent_id: root.id, name: "Test node") }
217
- let(:sibling) { create(:text_page_node, parent_id: root.id, name: "Test node(1)") }
218
-
219
- context "when node don't have sibling/s with same name" do
220
- it "does not changes node's name" do
221
- new_node = Node.new(name: "another name", parent_id: root.id)
222
- expect{ new_node.maintain_name }.to_not change{new_node.name}
223
- end
224
- end
225
-
226
- context "when node have sibling/s with same name" do
227
- it "changes node's name" do
228
- new_node = Node.new(name: node.name, parent_id: root.id)
229
- expect{ new_node.maintain_name }.to change{new_node.name}.from(node.name).to("#{node.name}(1)")
230
- end
231
-
232
- it "increments node's name number" do
233
- sibling
234
- new_node = Node.new(name: node.name, parent_id: root.id)
235
- expect{ new_node.maintain_name }.to change{new_node.name}.from(node.name).to("#{node.name}(2)")
236
- end
237
- end
238
- end
239
-
240
- describe "#maintain_slug" do
241
- let(:root) { create(:home_page_node) }
242
- let(:node) { create(:text_page_node, parent_id: root.id, name: "Test node", slug: "test-node") }
243
- let(:sibling) { create(:text_page_node, parent_id: root.id, name: "Test node(1)", slug: "test-node-1") }
244
-
245
- context "when node don't have sibling/s with same name" do
246
- it "does not changes node's slug" do
247
- new_node = Node.new(name: "another name", parent_id: root.id)
248
- expect{ new_node.maintain_slug }.to_not change{new_node.slug}
249
- end
250
- end
251
-
252
- context "when node have sibling/s with same slug" do
253
- it "changes node's slug" do
254
- new_node = Node.new(name: node.name, slug: node.slug, parent_id: root.id)
255
- expect{ new_node.maintain_slug }.to change{new_node.slug}.from(node.slug).to("#{node.slug}-1")
256
- end
257
-
258
- it "increments node's slug number" do
259
- sibling
260
- new_node = Node.new(name: node.name, slug: node.slug, parent_id: root.id)
261
- expect{ new_node.maintain_slug }.to change{new_node.slug}.from(node.slug).to("#{node.slug}-2")
262
- end
263
- end
264
- end
265
-
266
- describe ".updated_at" do
267
- it "returns last node update time" do
268
- expect( Releaf::Settings ).to receive(:[]).with('releaf.content.nodes.updated_at').and_return('test')
269
- expect( Node.updated_at ).to eq 'test'
270
- end
271
- end
272
-
273
- describe ".updated" do
274
- it "returns last node update time" do
275
- allow(Time).to receive(:now).and_return("asd")
276
- expect( Releaf::Settings ).to receive(:[]=).with('releaf.content.nodes.updated_at', "asd")
277
- Node.updated
278
- end
279
- end
280
-
281
- describe "#available?" do
282
- let(:root) { create(:home_page_node, active: true) }
283
- let(:node_ancestor) { create(:text_page_node, parent_id: root.id, active: true) }
284
- let(:node) { create(:text_page_node, parent_id: node_ancestor.id, active: true) }
285
-
286
- context "when object and all its ancestors are active" do
287
- it "returns true" do
288
- expect( node ).to be_available
289
- end
290
- end
291
-
292
- context "when object itself is not active" do
293
- it "returns false" do
294
- node.update_attribute(:active, false)
295
- expect( node ).to_not be_available
296
- end
297
- end
298
-
299
- context "when any of object ancestors are not active" do
300
- it "returns false" do
301
- node_ancestor.update_attribute(:active, false)
302
- expect( node ).to_not be_available
303
- end
304
- end
305
- end
306
-
307
- describe ".valid_node_content_classes" do
308
- it "returns array of constantized .valid_node_content_class_names" do
309
- expect( Node ).to receive(:valid_node_content_class_names).with(42).and_return(['TextPage', 'HomePagesController'])
310
- expect( Node.valid_node_content_classes(42) ).to eq [TextPage, HomePagesController]
311
- end
312
- end
313
-
314
- describe ".valid_node_content_class_names" do
315
- it "returns class names for Node#content_type that can be used to create valid node" do
316
- expect( ActsAsNode ).to receive(:classes).and_return(%w[BadNode GoodNode])
317
-
318
- node_1 = double('BadNode')
319
- allow(node_1).to receive(:valid?)
320
- node_1_errors = double("Node 1 Errors object")
321
- expect( node_1_errors ).to receive(:[]).with(:content_type).and_return(['some error'])
322
- allow(node_1).to receive(:errors).and_return(node_1_errors)
323
-
324
- node_2 = double('GoodNode')
325
- allow(node_2).to receive(:valid?)
326
- node_2_errors = double("Node 2 Errors object")
327
- expect( node_2_errors ).to receive(:[]).with(:content_type).and_return(nil)
328
- allow(node_2).to receive(:errors).and_return(node_2_errors)
329
-
330
- expect( Node ).to receive(:new).with(hash_including(parent_id: 52, content_type: 'BadNode')).and_return(node_1)
331
- expect( Node ).to receive(:new).with(hash_including(parent_id: 52, content_type: 'GoodNode')).and_return(node_2)
332
-
333
- expect( Node.valid_node_content_class_names(52) ).to eq %w[GoodNode]
334
- end
335
-
336
- end
337
-
338
- describe "#locale_selection_enabled?" do
339
- it "returns false" do
340
- expect( plain_subject.locale_selection_enabled? ).to be false
341
- end
342
- end
343
-
344
- describe "#build_content" do
345
- it "builds new content and assigns to #content" do
346
- subject.content_type = 'TextPage'
347
- params = {text_html: 'test'}
348
- expect( TextPage ).to receive(:new).with(params).and_call_original
349
- subject.build_content(params)
350
- expect( subject.content ).to be_an_instance_of TextPage
351
- expect( subject.content.text_html ).to eq 'test'
352
- end
353
- end
354
-
355
- describe "#validate_root_locale_uniqueness?" do
356
- before do
357
- allow( subject ).to receive(:root?).and_return(true)
358
- allow( subject ).to receive(:locale_selection_enabled?).and_return(true)
359
- end
360
-
361
- context "when #locale_selection_enabled? and #root? both are true" do
362
- it "returns true" do
363
- expect( subject.send(:validate_root_locale_uniqueness?) ).to be_truthy
364
- end
365
- end
366
-
367
- context "when #locale_selection_enabled? is false" do
368
- it "returns false" do
369
- allow( subject ).to receive(:locale_selection_enabled?).and_return(false)
370
- expect( subject.send(:validate_root_locale_uniqueness?) ).to be_falsy
371
- end
372
- end
373
-
374
- context "when #root? is false" do
375
- it "returns false" do
376
- allow( subject ).to receive(:root?).and_return(false)
377
- expect( subject.send(:validate_root_locale_uniqueness?) ).to be_falsy
378
- end
379
- end
380
- end
381
-
382
- describe "#validate_slug" do
383
- it "is called during validations" do
384
- expect( subject ).to receive(:validate_slug)
385
- subject.valid?
386
- end
387
-
388
- context "when invalid slug" do
389
- it "adds format error on slug" do
390
- allow(subject).to receive(:invalid_slug_format?).and_return(true)
391
- expect{ subject.send(:validate_slug) }.to change{ subject.errors[:slug] }.to(["is invalid"])
392
- end
393
- end
394
-
395
- context "when valid slug" do
396
- it "does not add format error on slug" do
397
- allow(subject).to receive(:invalid_slug_format?).and_return(false)
398
- expect{ subject.send(:validate_slug) }.to_not change{ subject.errors[:slug] }.from([])
399
- end
400
- end
401
- end
402
-
403
- describe "#invalid_slug_format?" do
404
- context "when slug value converted to url differs" do
405
- it "returns true" do
406
- subject.slug = "asd xx"
407
- expect(subject.invalid_slug_format?).to be true
408
- end
409
- end
410
-
411
- context "when slug value converted to url is same" do
412
- it "returns false" do
413
- subject.slug = "asd-xx"
414
- expect(subject.invalid_slug_format?).to be false
415
- end
416
- end
417
-
418
- context "when slug value is nil" do
419
- it "returns false" do
420
- subject.slug = nil
421
- expect(subject.invalid_slug_format?).to be false
422
- end
423
- end
424
- end
425
-
426
- describe "#validate_parent_node_is_not_self" do
427
- let(:node1) { create(:node, locale: "lv") }
428
-
429
- it "is called during validations" do
430
- expect( subject ).to receive(:validate_parent_node_is_not_self)
431
- subject.valid?
432
- end
433
-
434
- context "when #parent_id matches #id" do
435
- it "adds error on #parent_id" do
436
- node1.parent_id = node1.id
437
- node1.send(:validate_parent_node_is_not_self)
438
- expect( node1.errors[:parent_id] ).to include "can't be parent to itself"
439
- end
440
- end
441
-
442
- context "when parent is nil" do
443
- it "does nothing" do
444
- node1.parent_id = nil
445
- node1.send(:validate_parent_node_is_not_self)
446
- expect( node1.errors[:parent_id] ).to be_blank
447
- end
448
- end
449
-
450
- context "#id matches #parent_id" do
451
- it "does nothing" do
452
- node2 = create(:node, locale: "en")
453
- node1.parent_id = node2.id
454
- node1.send(:validate_parent_is_not_descendant)
455
- expect( node1.errors[:parent_id] ).to be_blank
456
- end
457
- end
458
-
459
-
460
- end
461
-
462
- describe "#validate_parent_is_not_descendant" do
463
- before with_tree: true do
464
- @node1 = create(:home_page_node, locale: "en")
465
- @node2 = create(:text_page_node, parent: @node1)
466
- @node3 = create(:text_page_node, parent: @node2)
467
- @node4 = create(:home_page_node, locale: "lv")
468
-
469
- @node1.reload
470
- @node2.reload
471
- @node3.reload
472
- end
473
-
474
- it "is called during validations" do
475
- expect( subject ).to receive(:validate_parent_is_not_descendant)
476
- subject.valid?
477
- end
478
-
479
- context "when #parent_id matches #id of one of descadents", with_tree: true do
480
- it "adds error on #parent_id" do
481
- @node1.parent_id = @node3.id
482
- @node1.send(:validate_parent_is_not_descendant)
483
- expect( @node1.errors[:parent_id] ).to include "descendant can't be parent"
484
- end
485
- end
486
-
487
- context "when parent is nil", with_tree: true do
488
- it "does nothing" do
489
- @node1.parent_id = nil
490
- @node1.send(:validate_parent_is_not_descendant)
491
- expect( @node1.errors[:parent_id] ).to be_blank
492
- end
493
- end
494
-
495
- context "when there are no descadents with #id is #self#parent_id", with_tree: true do
496
- it "does nothing" do
497
- @node1.parent_id = @node4.id
498
- @node1.send(:validate_parent_is_not_descendant)
499
- expect( @node1.errors[:parent_id] ).to be_blank
500
- end
501
- end
502
- end
503
-
504
- describe "#prevent_auto_update_settings_timestamp" do
505
- it "sets #prevent_auto_update_settings_timestamp? to true within block" do
506
- expect do
507
- subject.prevent_auto_update_settings_timestamp do
508
- expect( subject.send(:prevent_auto_update_settings_timestamp?) ).to be_truthy
509
- end
510
- end.to_not change { subject.send(:prevent_auto_update_settings_timestamp?) }.from(false)
511
- end
512
- end
513
-
514
- describe "#update_settings_timestamp" do
515
- it "calls .updated" do
516
- expect( Node ).to receive(:updated).and_call_original
517
- subject.send(:update_settings_timestamp)
518
- end
519
-
520
- context "when #prevent_auto_update_settings_timestamp? is false" do
521
- it "is called after save" do
522
- node = FactoryGirl.build(:node)
523
- allow( node ).to receive(:prevent_auto_update_settings_timestamp?).and_return(false)
524
- expect( node ).to receive(:update_settings_timestamp).and_call_original
525
- node.save!
526
- end
527
- end
528
-
529
- context "when #prevent_auto_update_settings_timestamp? is true" do
530
- it "is not called after save" do
531
- node = FactoryGirl.build(:node)
532
- allow( node ).to receive(:prevent_auto_update_settings_timestamp?).and_return(true)
533
- expect( node ).to_not receive(:update_settings_timestamp)
534
- node.save!
535
- end
536
- end
537
- end
538
-
539
- describe "#path" do
540
- before do
541
- allow(subject).to receive(:path_parts).and_return(["some", "bar", "foo"])
542
- end
543
-
544
- it "returns relative node path built ny joining node path parts" do
545
- expect(subject.path).to eq("/some/bar/foo")
546
- end
547
-
548
- context "when node has parent" do
549
- it "ads trailing slash to returned node path" do
550
- allow(subject).to receive(:trailing_slash_for_path?).and_return(true)
551
- expect(subject.path).to eq("/some/bar/foo/")
552
- end
553
- end
554
- end
555
-
556
-
557
- describe "#trailing_slash_for_path?" do
558
- context "when rails route has trailing slash enabled" do
559
- it "returns true" do
560
- allow(Rails.application.routes).to receive(:default_url_options).and_return(trailing_slash: true)
561
- expect(subject.trailing_slash_for_path?).to be true
562
- end
563
- end
564
-
565
- context "when rails route has trailing slash disabled" do
566
- it "returns false" do
567
- allow(Rails.application.routes).to receive(:default_url_options).and_return({})
568
- expect(subject.trailing_slash_for_path?).to be false
569
- end
570
- end
571
- end
572
-
573
- describe "#path_parts" do
574
- it "returns array with slug" do
575
- expect(subject.path_parts).to eq([""])
576
- subject.slug = "foo"
577
- expect(subject.path_parts).to eq(["foo"])
578
- end
579
-
580
- context "when node has parent" do
581
- it "prepends parent path parts to returned array" do
582
- parent = described_class.new
583
- allow(parent).to receive(:path_parts).and_return(%w(some bar))
584
-
585
- subject.slug = "foo"
586
- subject.parent = parent
587
- expect(subject.path_parts).to eq(["some", "bar", "foo"])
588
- end
589
- end
590
- end
591
- end