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.
Files changed (41) hide show
  1. data/.rubocop.yml +13 -0
  2. data/CHANGES.md +5 -0
  3. data/Gemfile.lock +46 -14
  4. data/Guardfile +24 -0
  5. data/README.md +27 -1
  6. data/bin/autospec +16 -0
  7. data/bin/guard +16 -0
  8. data/bin/rake +16 -0
  9. data/bin/rspec +16 -0
  10. data/lib/page_record/attribute_accessors.rb +57 -54
  11. data/lib/page_record/base.rb +27 -22
  12. data/lib/page_record/class_actions.rb +47 -50
  13. data/lib/page_record/class_methods.rb +252 -261
  14. data/lib/page_record/errors.rb +81 -82
  15. data/lib/page_record/finders.rb +129 -131
  16. data/lib/page_record/form_builder.rb +4 -4
  17. data/lib/page_record/formtastic.rb +20 -9
  18. data/lib/page_record/helpers.rb +192 -131
  19. data/lib/page_record/inspector.rb +36 -0
  20. data/lib/page_record/instance_actions.rb +44 -46
  21. data/lib/page_record/rspec.rb +1 -1
  22. data/lib/page_record/validation.rb +46 -0
  23. data/lib/page_record/version.rb +1 -1
  24. data/lib/page_record.rb +13 -15
  25. data/page_record.gemspec +4 -1
  26. data/spec/.rubocop.yml +4 -0
  27. data/spec/helpers_spec.rb +109 -100
  28. data/spec/inspector_spec.rb +70 -0
  29. data/spec/page_record_spec.rb +357 -388
  30. data/spec/spec_helper.rb +1 -3
  31. data/spec/support/shared_contexts.rb +24 -2
  32. data/spec/support/shared_examples.rb +41 -45
  33. data/spec/support/team.rb +4 -4
  34. data/spec/support/test_app.rb +10 -13
  35. data/spec/support/views/page-with-1-error.erb +5 -0
  36. data/spec/support/views/page-with-2-errors-on-different-attributes.erb +9 -0
  37. data/spec/support/views/page-with-2-errors-on-same-attribute.erb +6 -0
  38. data/spec/support/views/page-without-errors.erb +4 -0
  39. data/spec/validation_spec.rb +142 -0
  40. data/tmp/rspec_guard_result +1 -0
  41. 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
- before(:each) do
7
- self.extend PageRecord::Helpers
8
- self.extend ActionView::Helpers
9
- self.extend ActionView::Helpers::FormHelper
10
- self.extend ActionView::Helpers::FormTagHelper
11
- self.extend ActionView::Helpers::UrlHelper
12
- self.extend ActionView::Context
13
- class Car; end
14
- end
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
- describe '#form_record_for' do
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
- context "without a variable argument" do
22
+ context "with a new record" do
21
23
 
22
- subject { form_record_for(:car)}
24
+ before do
25
+ @car = stub_model(Car, id: nil)
26
+ end
23
27
 
24
- context "with a new record" do
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
- before do
27
- @car = stub_model(Car, :id => nil)
28
- end
33
+ context "with a saved record" do
29
34
 
30
- it "returns a Hash with data-`type`-name=`new`" do
31
- expect(subject).to eq( {'data-car-id' => 'new' })
32
- end
33
- end
35
+ before do
36
+ @car = stub_model(Car, id: 10)
37
+ end
34
38
 
35
- context "with a saved record" do
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
- before do
38
- @car = stub_model(Car, :id => 10)
39
- end
44
+ end
40
45
 
41
- it "returns a Hash with data-`type`-name=id" do
42
- expect(subject).to eq( {'data-car-id' => 10 })
43
- end
44
- end
46
+ context "with a variable argument" do
45
47
 
46
- end
48
+ subject { form_record_for(:car, other_car) }
49
+ let(:other_car) { @other_car = stub_model(Car, id: nil) }
47
50
 
48
- context "with a variable argument" do
51
+ context 'with a new record' do
49
52
 
50
- subject { form_record_for(:car, other_car)}
51
- let(:other_car) { @other_car = stub_model(Car, :id => nil) }
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
- context "with a new record" do
58
+ context "with a saved record" do
54
59
 
55
- it "returns a Hash with data-`type`-name=`new`" do
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
- context "with a saved record" do
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
- let(:other_car) { @other_car = stub_model(Car, :id => 10) }
67
+ end
63
68
 
64
- it "returns a Hash with data-`type`-name=id" do
65
- expect(subject).to eq( {'data-car-id' => 10 })
66
- end
67
- end
69
+ end
68
70
 
69
- end
71
+ describe '#attribute_for' do
72
+ subject { attribute_for('name') }
70
73
 
71
- end
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
- describe '#attribute_for' do
74
- subject { attribute_for('name')}
78
+ end
75
79
 
76
- it "returns a Hash with data-attribute-for => 'attribute' " do
77
- expect(subject).to eq({'data-attribute-for' => 'name'})
78
- end
80
+ describe '#action_for' do
79
81
 
80
- end
82
+ context "a submit action" do
81
83
 
82
- describe "#action_for" do
83
- subject { action_for('submit')}
84
+ subject { action_for('submit') }
84
85
 
85
- it "returns a Hash with data-action-for => 'attribute' " do
86
- expect(subject).to eq({'data-action-for' => 'submit'})
87
- end
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
- end
91
+ context "an other action" do
90
92
 
91
- describe "#attribute_tag_for" do
92
- subject { attribute_tag_for(:td, :name)}
93
+ subject { action_for('other') }
93
94
 
94
- it "returns a tag with data-attribute-for='name' set" do
95
- expect(subject).to eq "<td data-attribute-for=\"name\"></td>"
96
- end
97
- end
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
- before do
102
- @car = stub_model(Car, :id => 20)
103
- def protect_against_forgery?; end
104
- end
101
+ end
105
102
 
106
- subject {record_form_for(@car, :as => :car, :url => "/") {}}
103
+ describe '#attribute_tag_for' do
104
+ subject { attribute_tag_for(:td, :name) }
107
105
 
108
- it "returns a PageRecordFormBuilder" do
109
- expect(subject).to include " data-car-id=\"20\" "
110
- end
111
- end
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
- describe PageRecord::FormBuilder do
113
+ before do
114
+ @car = stub_model(Car, id: 20)
115
+ def protect_against_forgery?; end
116
+ end
115
117
 
116
- before do
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
- describe "#text_field" do
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
- it "automaticaly renders the data-attribute-for='name' html-tag" do
130
- expect(subject).to include " data-attribute-for=\"name\" "
131
- end
132
- end
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
- describe "#submit" do
136
- subject do
137
- record_form_for(@car, :as => :car, :url => "/") do |f|
138
- f.submit "Save"
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
- it "automaticaly renders the data-action-for='submit' html-tag" do
143
- expect(subject).to include " data-action-for=\"save\" "
144
- end
145
- end
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
- end
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
+