page_record 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.rubocop.yml +13 -0
- data/CHANGES.md +5 -0
- data/Gemfile.lock +46 -14
- data/Guardfile +24 -0
- data/README.md +27 -1
- data/bin/autospec +16 -0
- data/bin/guard +16 -0
- data/bin/rake +16 -0
- data/bin/rspec +16 -0
- data/lib/page_record/attribute_accessors.rb +57 -54
- data/lib/page_record/base.rb +27 -22
- data/lib/page_record/class_actions.rb +47 -50
- data/lib/page_record/class_methods.rb +252 -261
- data/lib/page_record/errors.rb +81 -82
- data/lib/page_record/finders.rb +129 -131
- data/lib/page_record/form_builder.rb +4 -4
- data/lib/page_record/formtastic.rb +20 -9
- data/lib/page_record/helpers.rb +192 -131
- data/lib/page_record/inspector.rb +36 -0
- data/lib/page_record/instance_actions.rb +44 -46
- data/lib/page_record/rspec.rb +1 -1
- data/lib/page_record/validation.rb +46 -0
- data/lib/page_record/version.rb +1 -1
- data/lib/page_record.rb +13 -15
- data/page_record.gemspec +4 -1
- data/spec/.rubocop.yml +4 -0
- data/spec/helpers_spec.rb +109 -100
- data/spec/inspector_spec.rb +70 -0
- data/spec/page_record_spec.rb +357 -388
- data/spec/spec_helper.rb +1 -3
- data/spec/support/shared_contexts.rb +24 -2
- data/spec/support/shared_examples.rb +41 -45
- data/spec/support/team.rb +4 -4
- data/spec/support/test_app.rb +10 -13
- data/spec/support/views/page-with-1-error.erb +5 -0
- data/spec/support/views/page-with-2-errors-on-different-attributes.erb +9 -0
- data/spec/support/views/page-with-2-errors-on-same-attribute.erb +6 -0
- data/spec/support/views/page-without-errors.erb +4 -0
- data/spec/validation_spec.rb +142 -0
- data/tmp/rspec_guard_result +1 -0
- metadata +80 -5
data/spec/helpers_spec.rb
CHANGED
@@ -3,147 +3,156 @@ require_relative './spec_helper'
|
|
3
3
|
|
4
4
|
describe "Rails helpers" do
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
6
|
+
before(:each) do
|
7
|
+
extend PageRecord::Helpers
|
8
|
+
extend ActionView::Helpers
|
9
|
+
extend ActionView::Helpers::FormHelper
|
10
|
+
extend ActionView::Helpers::FormTagHelper
|
11
|
+
extend ActionView::Helpers::UrlHelper
|
12
|
+
extend ActionView::Context
|
13
|
+
class Car; end
|
14
|
+
end
|
15
15
|
|
16
|
-
|
16
|
+
describe '#form_record_for' do
|
17
17
|
|
18
|
+
context "without a variable argument" do
|
18
19
|
|
20
|
+
subject { form_record_for(:car) }
|
19
21
|
|
20
|
-
|
22
|
+
context "with a new record" do
|
21
23
|
|
22
|
-
|
24
|
+
before do
|
25
|
+
@car = stub_model(Car, id: nil)
|
26
|
+
end
|
23
27
|
|
24
|
-
|
28
|
+
it "returns a Hash with data-`type`-name=`new`" do
|
29
|
+
expect(subject).to eq({ 'data-car-id' => 'new' })
|
30
|
+
end
|
31
|
+
end
|
25
32
|
|
26
|
-
|
27
|
-
@car = stub_model(Car, :id => nil)
|
28
|
-
end
|
33
|
+
context "with a saved record" do
|
29
34
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
end
|
35
|
+
before do
|
36
|
+
@car = stub_model(Car, id: 10)
|
37
|
+
end
|
34
38
|
|
35
|
-
|
39
|
+
it "returns a Hash with data-`type`-name=id" do
|
40
|
+
expect(subject).to eq({ 'data-car-id' => 10 })
|
41
|
+
end
|
42
|
+
end
|
36
43
|
|
37
|
-
|
38
|
-
@car = stub_model(Car, :id => 10)
|
39
|
-
end
|
44
|
+
end
|
40
45
|
|
41
|
-
|
42
|
-
expect(subject).to eq( {'data-car-id' => 10 })
|
43
|
-
end
|
44
|
-
end
|
46
|
+
context "with a variable argument" do
|
45
47
|
|
46
|
-
|
48
|
+
subject { form_record_for(:car, other_car) }
|
49
|
+
let(:other_car) { @other_car = stub_model(Car, id: nil) }
|
47
50
|
|
48
|
-
|
51
|
+
context 'with a new record' do
|
49
52
|
|
50
|
-
|
51
|
-
|
53
|
+
it "returns a Hash with data-`type`-name=`new`" do
|
54
|
+
expect(subject).to eq({ 'data-car-id' => 'new' })
|
55
|
+
end
|
56
|
+
end
|
52
57
|
|
53
|
-
|
58
|
+
context "with a saved record" do
|
54
59
|
|
55
|
-
|
56
|
-
expect(subject).to eq( {'data-car-id' => 'new' })
|
57
|
-
end
|
58
|
-
end
|
60
|
+
let(:other_car) { @other_car = stub_model(Car, id: 10) }
|
59
61
|
|
60
|
-
|
62
|
+
it "returns a Hash with data-`type`-name=id" do
|
63
|
+
expect(subject).to eq({ 'data-car-id' => 10 })
|
64
|
+
end
|
65
|
+
end
|
61
66
|
|
62
|
-
|
67
|
+
end
|
63
68
|
|
64
|
-
|
65
|
-
expect(subject).to eq( {'data-car-id' => 10 })
|
66
|
-
end
|
67
|
-
end
|
69
|
+
end
|
68
70
|
|
69
|
-
|
71
|
+
describe '#attribute_for' do
|
72
|
+
subject { attribute_for('name') }
|
70
73
|
|
71
|
-
|
74
|
+
it "returns a Hash with data-attribute-for => 'attribute' " do
|
75
|
+
expect(subject).to eq({ 'data-attribute-for' => 'name' })
|
76
|
+
end
|
72
77
|
|
73
|
-
|
74
|
-
subject { attribute_for('name')}
|
78
|
+
end
|
75
79
|
|
76
|
-
|
77
|
-
expect(subject).to eq({'data-attribute-for' => 'name'})
|
78
|
-
end
|
80
|
+
describe '#action_for' do
|
79
81
|
|
80
|
-
|
82
|
+
context "a submit action" do
|
81
83
|
|
82
|
-
|
83
|
-
subject { action_for('submit')}
|
84
|
+
subject { action_for('submit') }
|
84
85
|
|
85
|
-
|
86
|
-
|
87
|
-
|
86
|
+
it "returns a Hash with data-action-for => 'save' " do
|
87
|
+
expect(subject).to eq({ 'data-action-for' => 'save' })
|
88
|
+
end
|
89
|
+
end
|
88
90
|
|
89
|
-
|
91
|
+
context "an other action" do
|
90
92
|
|
91
|
-
|
92
|
-
subject { attribute_tag_for(:td, :name)}
|
93
|
+
subject { action_for('other') }
|
93
94
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
95
|
+
it "returns a Hash with data-action-for => 'attribute' " do
|
96
|
+
expect(subject).to eq({ 'data-action-for' => 'other' })
|
97
|
+
end
|
98
|
+
end
|
98
99
|
|
99
|
-
describe "#record_form_for" do
|
100
100
|
|
101
|
-
|
102
|
-
@car = stub_model(Car, :id => 20)
|
103
|
-
def protect_against_forgery?; end
|
104
|
-
end
|
101
|
+
end
|
105
102
|
|
106
|
-
|
103
|
+
describe '#attribute_tag_for' do
|
104
|
+
subject { attribute_tag_for(:td, :name) }
|
107
105
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
106
|
+
it "returns a tag with data-attribute-for='name' set" do
|
107
|
+
expect(subject).to eq "<td data-attribute-for=\"name\"></td>"
|
108
|
+
end
|
109
|
+
end
|
112
110
|
|
111
|
+
describe '#record_form_for' do
|
113
112
|
|
114
|
-
|
113
|
+
before do
|
114
|
+
@car = stub_model(Car, id: 20)
|
115
|
+
def protect_against_forgery?; end
|
116
|
+
end
|
115
117
|
|
116
|
-
|
117
|
-
@car = stub_model(Car, :id => 20, :name => 'Piet')
|
118
|
-
def protect_against_forgery?; end
|
119
|
-
end
|
118
|
+
subject { record_form_for(@car, as: :car, url: '/') {} }
|
120
119
|
|
120
|
+
it 'returns a PageRecordFormBuilder' do
|
121
|
+
expect(subject).to include " data-car-id=\"20\" "
|
122
|
+
end
|
123
|
+
end
|
121
124
|
|
122
|
-
|
123
|
-
subject do
|
124
|
-
record_form_for(@car, :as => :car, :url => "/") do |f|
|
125
|
-
f.text_field :name
|
126
|
-
end
|
127
|
-
end
|
125
|
+
describe PageRecord::FormBuilder do
|
128
126
|
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
127
|
+
before do
|
128
|
+
@car = stub_model(Car, id: 20, name: 'Piet')
|
129
|
+
def protect_against_forgery?; end
|
130
|
+
end
|
133
131
|
|
132
|
+
describe '#text_field' do
|
133
|
+
subject do
|
134
|
+
record_form_for(@car, as: :car, url: '/') do |f|
|
135
|
+
f.text_field :name
|
136
|
+
end
|
137
|
+
end
|
134
138
|
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
end
|
140
|
-
end
|
139
|
+
it "automaticaly renders the data-attribute-for='name' html-tag" do
|
140
|
+
expect(subject).to include " data-attribute-for=\"name\" "
|
141
|
+
end
|
142
|
+
end
|
141
143
|
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
144
|
+
describe '#submit' do
|
145
|
+
subject do
|
146
|
+
record_form_for(@car, as: :car, url: '/') do |f|
|
147
|
+
f.submit 'Save'
|
148
|
+
end
|
149
|
+
end
|
146
150
|
|
147
|
-
|
151
|
+
it "automaticaly renders the data-action-for='submit' html-tag" do
|
152
|
+
expect(subject).to include " data-action-for=\"save\" "
|
153
|
+
end
|
154
|
+
end
|
148
155
|
|
149
|
-
end
|
156
|
+
end
|
157
|
+
|
158
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require_relative './spec_helper'
|
2
|
+
|
3
|
+
describe PageRecord::Inspector do
|
4
|
+
|
5
|
+
|
6
|
+
describe '.inspect' do
|
7
|
+
before do
|
8
|
+
class TeamPage < PageRecord::Base
|
9
|
+
selector '.selector'
|
10
|
+
filter '.filter'
|
11
|
+
attributes %w(a b c d e)
|
12
|
+
end
|
13
|
+
PageRecord::Base.page page
|
14
|
+
end
|
15
|
+
|
16
|
+
after do
|
17
|
+
Object.send(:remove_const, :TeamPage)
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
shared_context "page with all elements"
|
22
|
+
|
23
|
+
it "returns the type" do
|
24
|
+
expect(TeamPage.inspect[:type]).to eq 'team'
|
25
|
+
end
|
26
|
+
|
27
|
+
it "returns the host_class" do
|
28
|
+
expect(TeamPage.inspect[:host_class]).to eq Team
|
29
|
+
end
|
30
|
+
|
31
|
+
it "returns the selector" do
|
32
|
+
expect(TeamPage.inspect[:selector]).to eq '.selector'
|
33
|
+
end
|
34
|
+
|
35
|
+
it "returns the filter" do
|
36
|
+
expect(TeamPage.inspect[:filter]).to eq '.filter'
|
37
|
+
end
|
38
|
+
|
39
|
+
it "returns all attributes" do
|
40
|
+
expect(TeamPage.inspect[:attributes]).to eq %w(a b c d e)
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
describe '#inspect' do
|
47
|
+
|
48
|
+
before do
|
49
|
+
class TeamPage < PageRecord::Base; end
|
50
|
+
PageRecord::Base.page page
|
51
|
+
end
|
52
|
+
|
53
|
+
after do
|
54
|
+
Object.send(:remove_const, :TeamPage)
|
55
|
+
end
|
56
|
+
|
57
|
+
include_context "page with single table with 3 records"
|
58
|
+
|
59
|
+
subject { TeamPage.find(1).inspect }
|
60
|
+
|
61
|
+
it 'returns all attributes' do
|
62
|
+
expect(subject['ranking']).to eq '1'
|
63
|
+
expect(subject['name']).to eq 'Ajax'
|
64
|
+
expect(subject['points']).to eq '10'
|
65
|
+
expect(subject['goals']).to eq '--not found on page--'
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|