releaf-content 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.
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,593 +0,0 @@
1
- require "rails_helper"
2
-
3
- describe Node, type: :model 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).required(false) }
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).case_insensitive }
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
- item_1 = create(:node, active: true, locale: "en")
37
- item_2 = create(:node, active: false, locale: "de")
38
- item_3 = create(:node, active: true, locale: "lv")
39
- expect(Node.active).to eq [item_1, item_3]
40
- end
41
- end
42
-
43
- describe "#content_class" do
44
- context 'when #content_type is nil' do
45
- it 'returns nil' do
46
- subject.content_type = nil
47
- expect( subject.content_class ).to be nil
48
- end
49
- end
50
-
51
- context "when #content_type is blank string" do
52
- it 'returns nil' do
53
- subject.content_type = ""
54
- expect( subject.content_class ).to be nil
55
- end
56
- end
57
-
58
- context "when #content_type is not blank" do
59
- it "constantizes it" do
60
- subject.content_type = "Node"
61
- expect( subject.content_class ).to eq Node
62
- end
63
- end
64
- end
65
-
66
- describe "#to_s" do
67
- it "returns name" do
68
- expect(subject.to_s).to eq(subject.name)
69
- end
70
- end
71
-
72
- describe "#locale" do
73
- before do
74
- root = create(:node, locale: "lv")
75
- parent = create(:text_page_node, locale: nil, parent_id: root.id)
76
- @child1 = create(:text_page_node, locale: nil, parent_id: parent.id)
77
- @child2 = create(:text_page_node, parent_id: parent.id, locale: "en")
78
- end
79
-
80
- context "when node locale is nil" do
81
- it "uses closest parent locale" do
82
- expect(@child1.locale).to eq("lv")
83
- end
84
- end
85
-
86
- context "when object node have locale" do
87
- it "uses closest parent locale" do
88
- expect(@child2.locale).to eq("en")
89
- end
90
- end
91
- end
92
-
93
- describe "#destroy" do
94
- context "when content object class exists" do
95
- let!(:node) { create(:home_page_node) }
96
-
97
- it "deletes record" do
98
- expect { node.destroy }.to change { Node.count }.by(-1)
99
- end
100
-
101
- it "deletes associated record" do
102
- expect { node.destroy }.to change { HomePage.count }.by(-1)
103
- end
104
- end
105
-
106
- context "when content object class doesn't exists" do
107
- let!(:node) { create(:home_page_node) }
108
- before do
109
- node.update_columns(content_type: 'NonExistingTestModel')
110
- end
111
-
112
- it "deletes record" do
113
- expect { node.destroy }.to change { Node.count }.by(-1)
114
- end
115
- end
116
-
117
- it "sets node update to current time" do
118
- node = create(:node)
119
- expect( Node ).to receive(:updated).once
120
- node.destroy
121
- end
122
- end
123
-
124
- describe "#attributes_to_not_copy" do
125
- it "returns array with attributes" do
126
- subject.locale = "lv"
127
- expect( subject.attributes_to_not_copy ).to match_array %w[content_id depth id item_position lft rgt created_at updated_at]
128
- end
129
-
130
- context "when locale is blank" do
131
- it "includes locale within returned list" do
132
- expect( subject.attributes_to_not_copy ).to match_array %w[content_id depth id item_position lft rgt created_at updated_at locale]
133
- end
134
- end
135
- end
136
-
137
- describe "#attributes_to_copy" do
138
- it "returns object attributes excluding #attributes_to_not_copy" do
139
- node = Node.new
140
- allow( node ).to receive(:attributes_to_not_copy).and_return(%w[lft rgt])
141
- expect( node.attributes_to_copy ).to eq(Node.column_names - %w[lft rgt])
142
- end
143
- end
144
-
145
- describe "#reasign_slug" do
146
- it "updates slug" do
147
- node = create(:node)
148
- old_slug = node.slug
149
- node.name = 'woo hoo'
150
- expect { node.reasign_slug }.to change { node.slug }.from(old_slug).to('woo-hoo')
151
- end
152
- end
153
-
154
- describe "#assign_attributes_from" do
155
- let(:source_node) { create(:node, active: false) }
156
-
157
- it "copies #attributes_to_copy attributes" do
158
-
159
- allow( source_node ).to receive(:attributes_to_copy).and_return(['name'])
160
-
161
- expect( source_node ).to receive(:name).and_call_original
162
- expect( source_node ).to_not receive(:parent_id)
163
- expect( source_node ).to_not receive(:content_type)
164
- expect( source_node ).to_not receive(:active)
165
-
166
- new_node = Node.new
167
-
168
- new_node.assign_attributes_from source_node
169
-
170
- expect( new_node.parent_id ).to be nil
171
- expect( new_node.content_type ).to be nil
172
- expect( new_node.active ).to be true
173
- end
174
- end
175
-
176
- describe ".children_max_item_position" do
177
- before do
178
- @home_page_node = create(:home_page_node, item_position: 1, locale: "lv")
179
- @home_page_node_2 = create(:home_page_node, item_position: 2, locale: "en")
180
- @text_page_node_3 = create(:text_page_node, parent_id: @home_page_node_2.id, item_position: 1)
181
- @text_page_node_4 = create(:text_page_node, parent_id: @home_page_node_2.id, item_position: 2)
182
-
183
- # it is important to reload nodes, otherwise associations will return empty set
184
- @home_page_node.reload
185
- @home_page_node_2.reload
186
- @text_page_node_3.reload
187
- @text_page_node_4.reload
188
- end
189
-
190
- context "when passing nil" do
191
- it "returns max item_position of root nodes" do
192
- expect( Node.children_max_item_position(nil) ).to eq 2
193
- end
194
- end
195
-
196
- context "when passing node with children" do
197
- it "returns max item_position of node children" do
198
- expect( Node.children_max_item_position(@home_page_node_2) ).to eq 2
199
- end
200
- end
201
-
202
- context "when passing node without children" do
203
- it "returns 0" do
204
- expect( Node.children_max_item_position(@text_page_node_4) ).to eq 0
205
- end
206
- end
207
- end
208
-
209
- describe "#move" do
210
- it "calls Node move service" do
211
- expect(Releaf::Content::Node::Move).to receive(:call).with(node: subject, parent_id: 12)
212
- subject.move(12)
213
- end
214
- end
215
-
216
- describe "#maintain_name" do
217
- let(:root) { create(:home_page_node) }
218
- let(:node) { create(:text_page_node, parent_id: root.id, name: "Test node") }
219
- let(:sibling) { create(:text_page_node, parent_id: root.id, name: "Test node(1)") }
220
-
221
- context "when node don't have sibling/s with same name" do
222
- it "does not changes node's name" do
223
- new_node = Node.new(name: "another name", parent_id: root.id)
224
- expect{ new_node.maintain_name }.to_not change{new_node.name}
225
- end
226
- end
227
-
228
- context "when node have sibling/s with same name" do
229
- it "changes node's name" do
230
- new_node = Node.new(name: node.name, parent_id: root.id)
231
- expect{ new_node.maintain_name }.to change{new_node.name}.from(node.name).to("#{node.name}(1)")
232
- end
233
-
234
- it "increments node's name number" do
235
- sibling
236
- new_node = Node.new(name: node.name, parent_id: root.id)
237
- expect{ new_node.maintain_name }.to change{new_node.name}.from(node.name).to("#{node.name}(2)")
238
- end
239
- end
240
- end
241
-
242
- describe "#maintain_slug" do
243
- let(:root) { create(:home_page_node) }
244
- let(:node) { create(:text_page_node, parent_id: root.id, name: "Test node", slug: "test-node") }
245
- let(:sibling) { create(:text_page_node, parent_id: root.id, name: "Test node(1)", slug: "test-node-1") }
246
-
247
- context "when node don't have sibling/s with same name" do
248
- it "does not changes node's slug" do
249
- new_node = Node.new(name: "another name", parent_id: root.id)
250
- expect{ new_node.maintain_slug }.to_not change{new_node.slug}
251
- end
252
- end
253
-
254
- context "when node have sibling/s with same slug" do
255
- it "changes node's slug" do
256
- new_node = Node.new(name: node.name, slug: node.slug, parent_id: root.id)
257
- expect{ new_node.maintain_slug }.to change{new_node.slug}.from(node.slug).to("#{node.slug}-1")
258
- end
259
-
260
- it "increments node's slug number" do
261
- sibling
262
- new_node = Node.new(name: node.name, slug: node.slug, parent_id: root.id)
263
- expect{ new_node.maintain_slug }.to change{new_node.slug}.from(node.slug).to("#{node.slug}-2")
264
- end
265
- end
266
- end
267
-
268
- describe ".updated_at" do
269
- it "returns last node update time" do
270
- expect( Releaf::Settings ).to receive(:[]).with('releaf.content.nodes.updated_at').and_return('test')
271
- expect( Node.updated_at ).to eq 'test'
272
- end
273
- end
274
-
275
- describe ".updated" do
276
- it "returns last node update time" do
277
- allow(Time).to receive(:now).and_return("asd")
278
- expect( Releaf::Settings ).to receive(:[]=).with('releaf.content.nodes.updated_at', "asd")
279
- Node.updated
280
- end
281
- end
282
-
283
- describe "#available?" do
284
- let(:root) { create(:home_page_node, active: true) }
285
- let(:node_ancestor) { create(:text_page_node, parent_id: root.id, active: true) }
286
- let(:node) { create(:text_page_node, parent_id: node_ancestor.id, active: true) }
287
-
288
- context "when object and all its ancestors are active" do
289
- it "returns true" do
290
- expect( node ).to be_available
291
- end
292
- end
293
-
294
- context "when object itself is not active" do
295
- it "returns false" do
296
- node.update_attribute(:active, false)
297
- expect( node ).to_not be_available
298
- end
299
- end
300
-
301
- context "when any of object ancestors are not active" do
302
- it "returns false" do
303
- node_ancestor.update_attribute(:active, false)
304
- expect( node ).to_not be_available
305
- end
306
- end
307
- end
308
-
309
- describe ".valid_node_content_classes" do
310
- it "returns array of constantized .valid_node_content_class_names" do
311
- expect( Node ).to receive(:valid_node_content_class_names).with(42).and_return(['TextPage', 'HomePagesController'])
312
- expect( Node.valid_node_content_classes(42) ).to eq [TextPage, HomePagesController]
313
- end
314
- end
315
-
316
- describe ".valid_node_content_class_names" do
317
- it "returns class names for Node#content_type that can be used to create valid node" do
318
- expect( ActsAsNode ).to receive(:classes).and_return(%w[BadNode GoodNode])
319
-
320
- node_1 = double('BadNode')
321
- allow(node_1).to receive(:valid?)
322
- node_1_errors = double("Node 1 Errors object")
323
- expect( node_1_errors ).to receive(:[]).with(:content_type).and_return(['some error'])
324
- allow(node_1).to receive(:errors).and_return(node_1_errors)
325
-
326
- node_2 = double('GoodNode')
327
- allow(node_2).to receive(:valid?)
328
- node_2_errors = double("Node 2 Errors object")
329
- expect( node_2_errors ).to receive(:[]).with(:content_type).and_return(nil)
330
- allow(node_2).to receive(:errors).and_return(node_2_errors)
331
-
332
- expect( Node ).to receive(:new).with(hash_including(parent_id: 52, content_type: 'BadNode')).and_return(node_1)
333
- expect( Node ).to receive(:new).with(hash_including(parent_id: 52, content_type: 'GoodNode')).and_return(node_2)
334
-
335
- expect( Node.valid_node_content_class_names(52) ).to eq %w[GoodNode]
336
- end
337
-
338
- end
339
-
340
- describe "#locale_selection_enabled?" do
341
- it "returns false" do
342
- expect( plain_subject.locale_selection_enabled? ).to be false
343
- end
344
- end
345
-
346
- describe "#build_content" do
347
- it "builds new content and assigns to #content" do
348
- subject.content_type = 'TextPage'
349
- params = {text_html: 'test'}
350
- expect( TextPage ).to receive(:new).with(params).and_call_original
351
- subject.build_content(params)
352
- expect( subject.content ).to be_an_instance_of TextPage
353
- expect( subject.content.text_html ).to eq 'test'
354
- end
355
- end
356
-
357
- describe "#validate_root_locale_uniqueness?" do
358
- before do
359
- allow( subject ).to receive(:root?).and_return(true)
360
- allow( subject ).to receive(:locale_selection_enabled?).and_return(true)
361
- end
362
-
363
- context "when #locale_selection_enabled? and #root? both are true" do
364
- it "returns true" do
365
- expect( subject.send(:validate_root_locale_uniqueness?) ).to be_truthy
366
- end
367
- end
368
-
369
- context "when #locale_selection_enabled? is false" do
370
- it "returns false" do
371
- allow( subject ).to receive(:locale_selection_enabled?).and_return(false)
372
- expect( subject.send(:validate_root_locale_uniqueness?) ).to be_falsy
373
- end
374
- end
375
-
376
- context "when #root? is false" do
377
- it "returns false" do
378
- allow( subject ).to receive(:root?).and_return(false)
379
- expect( subject.send(:validate_root_locale_uniqueness?) ).to be_falsy
380
- end
381
- end
382
- end
383
-
384
- describe "#validate_slug" do
385
- it "is called during validations" do
386
- expect( subject ).to receive(:validate_slug)
387
- subject.valid?
388
- end
389
-
390
- context "when invalid slug" do
391
- it "adds format error on slug" do
392
- allow(subject).to receive(:invalid_slug_format?).and_return(true)
393
- expect{ subject.send(:validate_slug) }.to change{ subject.errors[:slug] }.to(["is invalid"])
394
- end
395
- end
396
-
397
- context "when valid slug" do
398
- it "does not add format error on slug" do
399
- allow(subject).to receive(:invalid_slug_format?).and_return(false)
400
- expect{ subject.send(:validate_slug) }.to_not change{ subject.errors[:slug] }.from([])
401
- end
402
- end
403
- end
404
-
405
- describe "#invalid_slug_format?" do
406
- context "when slug value converted to url differs" do
407
- it "returns true" do
408
- subject.slug = "asd xx"
409
- expect(subject.invalid_slug_format?).to be true
410
- end
411
- end
412
-
413
- context "when slug value converted to url is same" do
414
- it "returns false" do
415
- subject.slug = "asd-xx"
416
- expect(subject.invalid_slug_format?).to be false
417
- end
418
- end
419
-
420
- context "when slug value is nil" do
421
- it "returns false" do
422
- subject.slug = nil
423
- expect(subject.invalid_slug_format?).to be false
424
- end
425
- end
426
- end
427
-
428
- describe "#validate_parent_node_is_not_self" do
429
- let(:node1) { create(:node, locale: "lv") }
430
-
431
- it "is called during validations" do
432
- expect( subject ).to receive(:validate_parent_node_is_not_self)
433
- subject.valid?
434
- end
435
-
436
- context "when #parent_id matches #id" do
437
- it "adds error on #parent_id" do
438
- node1.parent_id = node1.id
439
- node1.send(:validate_parent_node_is_not_self)
440
- expect( node1.errors[:parent_id] ).to include "can't be parent to itself"
441
- end
442
- end
443
-
444
- context "when parent is nil" do
445
- it "does nothing" do
446
- node1.parent_id = nil
447
- node1.send(:validate_parent_node_is_not_self)
448
- expect( node1.errors[:parent_id] ).to be_blank
449
- end
450
- end
451
-
452
- context "#id matches #parent_id" do
453
- it "does nothing" do
454
- node2 = create(:node, locale: "en")
455
- node1.parent_id = node2.id
456
- node1.send(:validate_parent_is_not_descendant)
457
- expect( node1.errors[:parent_id] ).to be_blank
458
- end
459
- end
460
-
461
-
462
- end
463
-
464
- describe "#validate_parent_is_not_descendant" do
465
- before with_tree: true do
466
- @node1 = create(:home_page_node, locale: "en")
467
- @node2 = create(:text_page_node, parent: @node1)
468
- @node3 = create(:text_page_node, parent: @node2)
469
- @node4 = create(:home_page_node, locale: "lv")
470
-
471
- @node1.reload
472
- @node2.reload
473
- @node3.reload
474
- end
475
-
476
- it "is called during validations" do
477
- expect( subject ).to receive(:validate_parent_is_not_descendant)
478
- subject.valid?
479
- end
480
-
481
- context "when #parent_id matches #id of one of descadents", with_tree: true do
482
- it "adds error on #parent_id" do
483
- @node1.parent_id = @node3.id
484
- @node1.send(:validate_parent_is_not_descendant)
485
- expect( @node1.errors[:parent_id] ).to include "descendant can't be parent"
486
- end
487
- end
488
-
489
- context "when parent is nil", with_tree: true do
490
- it "does nothing" do
491
- @node1.parent_id = nil
492
- @node1.send(:validate_parent_is_not_descendant)
493
- expect( @node1.errors[:parent_id] ).to be_blank
494
- end
495
- end
496
-
497
- context "when there are no descadents with #id is #self#parent_id", with_tree: true do
498
- it "does nothing" do
499
- @node1.parent_id = @node4.id
500
- @node1.send(:validate_parent_is_not_descendant)
501
- expect( @node1.errors[:parent_id] ).to be_blank
502
- end
503
- end
504
- end
505
-
506
- describe "#prevent_auto_update_settings_timestamp" do
507
- it "sets #prevent_auto_update_settings_timestamp? to true within block" do
508
- expect do
509
- subject.prevent_auto_update_settings_timestamp do
510
- expect( subject.send(:prevent_auto_update_settings_timestamp?) ).to be_truthy
511
- end
512
- end.to_not change { subject.send(:prevent_auto_update_settings_timestamp?) }.from(false)
513
- end
514
- end
515
-
516
- describe "#update_settings_timestamp" do
517
- it "calls .updated" do
518
- expect( Node ).to receive(:updated).and_call_original
519
- subject.send(:update_settings_timestamp)
520
- end
521
-
522
- context "when #prevent_auto_update_settings_timestamp? is false" do
523
- it "is called after save" do
524
- node = build(:node)
525
- allow( node ).to receive(:prevent_auto_update_settings_timestamp?).and_return(false)
526
- expect( node ).to receive(:update_settings_timestamp).and_call_original
527
- node.save!
528
- end
529
- end
530
-
531
- context "when #prevent_auto_update_settings_timestamp? is true" do
532
- it "is not called after save" do
533
- node = build(:node)
534
- allow( node ).to receive(:prevent_auto_update_settings_timestamp?).and_return(true)
535
- expect( node ).to_not receive(:update_settings_timestamp)
536
- node.save!
537
- end
538
- end
539
- end
540
-
541
- describe "#path" do
542
- before do
543
- allow(subject).to receive(:path_parts).and_return(["some", "bar", "foo"])
544
- end
545
-
546
- it "returns relative node path built ny joining node path parts" do
547
- expect(subject.path).to eq("/some/bar/foo")
548
- end
549
-
550
- context "when node has parent" do
551
- it "ads trailing slash to returned node path" do
552
- allow(subject).to receive(:trailing_slash_for_path?).and_return(true)
553
- expect(subject.path).to eq("/some/bar/foo/")
554
- end
555
- end
556
- end
557
-
558
-
559
- describe "#trailing_slash_for_path?" do
560
- context "when rails route has trailing slash enabled" do
561
- it "returns true" do
562
- allow(Rails.application.routes).to receive(:default_url_options).and_return(trailing_slash: true)
563
- expect(subject.trailing_slash_for_path?).to be true
564
- end
565
- end
566
-
567
- context "when rails route has trailing slash disabled" do
568
- it "returns false" do
569
- allow(Rails.application.routes).to receive(:default_url_options).and_return({})
570
- expect(subject.trailing_slash_for_path?).to be false
571
- end
572
- end
573
- end
574
-
575
- describe "#path_parts" do
576
- it "returns array with slug" do
577
- expect(subject.path_parts).to eq([""])
578
- subject.slug = "foo"
579
- expect(subject.path_parts).to eq(["foo"])
580
- end
581
-
582
- context "when node has parent" do
583
- it "prepends parent path parts to returned array" do
584
- parent = described_class.new
585
- allow(parent).to receive(:path_parts).and_return(%w(some bar))
586
-
587
- subject.slug = "foo"
588
- subject.parent = parent
589
- expect(subject.path_parts).to eq(["some", "bar", "foo"])
590
- end
591
- end
592
- end
593
- end