record_collection 0.7.5 → 0.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +4 -0
- data/Gemfile +12 -0
- data/README.md +4 -3
- data/app/assets/stylesheets/record_collection/optionals.sass +3 -0
- data/lib/record_collection/base.rb +5 -0
- data/lib/record_collection/rails/{form_options_helper.rb → form_builder.rb} +1 -3
- data/lib/record_collection/rails/form_helper.rb +27 -0
- data/lib/record_collection/version.rb +1 -1
- data/lib/record_collection.rb +2 -1
- data/record_collection.gemspec +2 -2
- data/spec/dummy/app/controllers/dashboard_controller.rb +4 -0
- data/spec/dummy/app/views/dashboard/home.html.slim +1 -0
- data/spec/dummy/app/views/layouts/application.html.slim +12 -0
- data/spec/dummy/config/application.rb +1 -1
- data/spec/dummy/config/routes.rb +1 -0
- data/spec/rails/form_builder_spec.rb +16 -2
- data/spec/rails/form_for_spec.rb +58 -0
- metadata +14 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1c445bd0e2e9f8299f807585a29a41710e568714
|
4
|
+
data.tar.gz: 1545d0887d9a46ca4d263143195c21dc7168e045
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 507893a2c300a9010b35f37e6068976cc5a49d4080ed4b7185f40b8aec08f5d921bb6297c3981902d5b5424138cea274f44c016fe46cddd5e510598bc6a7fdc0
|
7
|
+
data.tar.gz: 04fc98678723024797e6cb10e9932d05d26e3cae59ab9914da957df93465d7e4e47dd880d5bd627f2ae760a4b7f31a9325c74056e66bd562e25b65e8def173e0
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
@@ -3,6 +3,18 @@ source 'https://rubygems.org'
|
|
3
3
|
# Specify your gem's dependencies in record_collection.gemspec
|
4
4
|
gemspec
|
5
5
|
|
6
|
+
#http://aaronmiler.com/blog/testing-your-rails-engine-with-multiple-versions-of-rails/
|
7
|
+
rails_version = ENV["RAILS_VERSION"] || "default"
|
8
|
+
rails = case rails_version
|
9
|
+
when "master" then {github: "rails/rails"}
|
10
|
+
when "default" then ">= 3.2.0"
|
11
|
+
else "~> #{rails_version}"
|
12
|
+
end
|
13
|
+
gem "rails", rails
|
14
|
+
if rails_version == 'master'
|
15
|
+
gem 'arel', github: 'rails/arel'
|
16
|
+
end
|
17
|
+
|
6
18
|
group :test do
|
7
19
|
gem "codeclimate-test-reporter", require: nil
|
8
20
|
end
|
data/README.md
CHANGED
@@ -87,7 +87,7 @@ attribute definitions.
|
|
87
87
|
### Validations
|
88
88
|
The validations for the collection are exactly the same as your
|
89
89
|
active_model validations. The only difference is that the allow_nil:
|
90
|
-
true option standard true
|
90
|
+
true option is standard set to true. Since a nil value of a collection attribute
|
91
91
|
means you do not want to change that value for the individual records.
|
92
92
|
To make an attribute explicitly required for a collection add the
|
93
93
|
allow_nil option:
|
@@ -108,7 +108,7 @@ collection assumes that it is subclassed by the model, eg:
|
|
108
108
|
class Project::Prince2::Collection < RecordCollection::Base
|
109
109
|
end
|
110
110
|
|
111
|
-
Project::Prince2.record_class #=> Project::Prince2
|
111
|
+
Project::Prince2::Collection.record_class #=> Project::Prince2
|
112
112
|
```
|
113
113
|
|
114
114
|
If this is not the case, you have to define the record_class manually:
|
@@ -177,8 +177,9 @@ gem where you can replace `f.input :attribute, ...etc` with
|
|
177
177
|
`f.optional_input :attribute, ...etc`. Our current example works with
|
178
178
|
the standard [form_helpers](http://guides.rubyonrails.org/form_helpers.html)<br>
|
179
179
|
### currently supported helpers:
|
180
|
-
* `optional_boolean`
|
180
|
+
* `optional_boolean` with alias `optional_check_box`
|
181
181
|
* `optional_text_field`
|
182
|
+
* `optional_text_area`
|
182
183
|
* `optional_input` ([simple_form](https://github.com/plataformatec/simple_form))
|
183
184
|
|
184
185
|
The form you create typically looks like [app/views/employees/collection_edit.html.slim](spec/dummy/app/views/employees/collection_edit.html.slim):
|
@@ -1,5 +1,4 @@
|
|
1
|
-
|
2
|
-
class ActionView::Helpers::FormBuilder
|
1
|
+
ActionView::Helpers::FormBuilder.class_eval do
|
3
2
|
# This method adds the collection ids to the form if not already
|
4
3
|
# added. For the lazy peaple that forget to add them manually and
|
5
4
|
# use an optional tag
|
@@ -71,4 +70,3 @@ class ActionView::Helpers::FormBuilder
|
|
71
70
|
active ? 'active' : 'inactive'
|
72
71
|
end
|
73
72
|
end
|
74
|
-
|
@@ -0,0 +1,27 @@
|
|
1
|
+
ActionView::Helpers::FormHelper.class_eval do
|
2
|
+
private
|
3
|
+
|
4
|
+
alias_method :old_apply_form_for_options!, :apply_form_for_options!
|
5
|
+
def apply_form_for_options!(record, object, options) #:nodoc:
|
6
|
+
if record.is_a?(RecordCollection::Base)
|
7
|
+
object = convert_to_model(object)
|
8
|
+
|
9
|
+
as = options[:as]
|
10
|
+
namespace = options[:namespace]
|
11
|
+
action, method = [:collection_update, :post]
|
12
|
+
options[:html].reverse_merge!(
|
13
|
+
class: as ? "#{action}_#{as}" : dom_class(object, action),
|
14
|
+
id: (as ? [namespace, action, as] : [namespace, dom_id(object, action)]).compact.join("_").presence,
|
15
|
+
method: method
|
16
|
+
)
|
17
|
+
|
18
|
+
options[:url] ||= if options.key?(:format)
|
19
|
+
polymorphic_path([action, record], format: options.delete(:format))
|
20
|
+
else
|
21
|
+
polymorphic_path([action, record], {})
|
22
|
+
end
|
23
|
+
else
|
24
|
+
old_apply_form_for_options!(record, object, options)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/record_collection.rb
CHANGED
@@ -4,7 +4,8 @@ require "record_collection/version"
|
|
4
4
|
require "record_collection/name"
|
5
5
|
require "record_collection/base"
|
6
6
|
require 'record_collection/rails/routes'
|
7
|
-
require 'record_collection/rails/
|
7
|
+
require 'record_collection/rails/form_builder'
|
8
|
+
require 'record_collection/rails/form_helper'
|
8
9
|
require 'record_collection/engine'
|
9
10
|
|
10
11
|
module RecordCollection
|
data/record_collection.gemspec
CHANGED
@@ -42,7 +42,7 @@ Gem::Specification.new do |spec|
|
|
42
42
|
spec.add_development_dependency "quiet_assets"
|
43
43
|
|
44
44
|
|
45
|
-
spec.add_runtime_dependency 'active_attr', '
|
46
|
-
spec.add_runtime_dependency 'activemodel', '
|
45
|
+
spec.add_runtime_dependency 'active_attr', '>= 0.8'
|
46
|
+
spec.add_runtime_dependency 'activemodel', '>= 4.1'
|
47
47
|
spec.add_runtime_dependency "railties", ">= 3.1"
|
48
48
|
end
|
@@ -0,0 +1 @@
|
|
1
|
+
h2 Home page of the dummy app of the Record Collection gem
|
@@ -7,6 +7,18 @@ html
|
|
7
7
|
= javascript_include_tag 'application', 'data-turbolinks-track' => true
|
8
8
|
= csrf_meta_tags
|
9
9
|
body
|
10
|
+
nav.top-bar data-topbar="" role="navigation"
|
11
|
+
ul.title-area
|
12
|
+
li.name
|
13
|
+
h1
|
14
|
+
a href="/" Record collection
|
15
|
+
li.toggle-topbar.menu-icon
|
16
|
+
a href="#"
|
17
|
+
span Menu
|
18
|
+
section.top-bar-section
|
19
|
+
ul.left
|
20
|
+
li: a href=employees_path class=(controller_name=="employees" ? "active" : nil) Employees
|
21
|
+
li: a href=projects_path class=(controller_name=="projects" ? "active" : nil) Projects
|
10
22
|
.row
|
11
23
|
.small-12.columns
|
12
24
|
- flash.each do |name, msg|
|
@@ -20,7 +20,7 @@ module Dummy
|
|
20
20
|
# config.i18n.default_locale = :de
|
21
21
|
|
22
22
|
# Do not swallow errors in after_commit/after_rollback callbacks.
|
23
|
-
config.active_record.raise_in_transactional_callbacks = true
|
23
|
+
config.active_record.raise_in_transactional_callbacks = true if Gem::Requirement.new(">= 4.2").satisfied_by?(Gem::Version.new(Rails.version))
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
data/spec/dummy/config/routes.rb
CHANGED
@@ -14,7 +14,9 @@ describe ActionView::Helpers::FormBuilder do
|
|
14
14
|
|
15
15
|
describe '.collection_ids' do
|
16
16
|
it 'returns the collection ids as hidden fields' do
|
17
|
-
subject.collection_ids.should
|
17
|
+
subject.collection_ids.should match /^<input/
|
18
|
+
subject.collection_ids.should match /name="ids\[\]"/
|
19
|
+
subject.collection_ids.should match /value="#{employee.id}"/
|
18
20
|
end
|
19
21
|
|
20
22
|
it "does not raise when the object is not a collection object" do
|
@@ -23,6 +25,18 @@ describe ActionView::Helpers::FormBuilder do
|
|
23
25
|
end
|
24
26
|
end
|
25
27
|
|
28
|
+
describe '#optional_input simple_form support' do
|
29
|
+
it "generates proper output" do
|
30
|
+
expect( subject ).to receive(:input).and_return "<simple-form-content>Simple Form Content</simple-form-content>".html_safe
|
31
|
+
html = subject.optional_input(:section)
|
32
|
+
doc = Nokogiri::HTML(html)
|
33
|
+
doc.css("[name='ids[]'][value='#{employee.id}']").should be_present # add ids if not yet set
|
34
|
+
|
35
|
+
optional_div = doc.css("div.optional-input.optional-attribute-container.section.active.one[data-attribute='section'][data-one=true]").first
|
36
|
+
optional_div.text.should eq 'Simple Form Content'
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
26
40
|
describe '#get_optional_classes' do
|
27
41
|
|
28
42
|
describe 'active/inactive' do
|
@@ -52,7 +66,7 @@ describe ActionView::Helpers::FormBuilder do
|
|
52
66
|
subject.get_optional_classes(:section).should include'inactive'
|
53
67
|
end
|
54
68
|
end
|
55
|
-
|
69
|
+
|
56
70
|
describe 'one' do
|
57
71
|
it "includes one if only one record is present" do
|
58
72
|
subject.get_optional_classes(:section).should include'one'
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'form_for', type: :helper do
|
4
|
+
let(:form_html){ helper.form_for(subject){|f| f.text_field :section} }
|
5
|
+
let(:doc){ Nokogiri::HTML form_html }
|
6
|
+
let(:form){ doc.css('form').first }
|
7
|
+
let(:rails_form_method){ doc.css('[name="_method"]').first.try(:[], 'value') }
|
8
|
+
context 'record' do
|
9
|
+
context 'persisted' do
|
10
|
+
subject { Employee.create section: "SE1" }
|
11
|
+
it 'generates a proper route for a normal record' do
|
12
|
+
form['method'].should eq 'post'
|
13
|
+
form['action'].should eq "/employees/#{subject.id}"
|
14
|
+
rails_form_method.should eq 'patch'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'new record' do
|
19
|
+
subject { Employee.new section: "SE1" }
|
20
|
+
it 'generates a proper route for a normal record' do
|
21
|
+
form['method'].should eq 'post'
|
22
|
+
form['action'].should eq "/employees"
|
23
|
+
rails_form_method.should_not be_present
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'collection' do
|
29
|
+
subject{ Employee::Collection.new([], section: 'SE9') }
|
30
|
+
it 'generates a proper form for a collection record' do
|
31
|
+
form['method'].should eq 'post'
|
32
|
+
form['action'].should eq "/employees/collection_update"
|
33
|
+
rails_form_method.should_not be_present
|
34
|
+
end
|
35
|
+
|
36
|
+
context "with format option" do
|
37
|
+
let(:form_html){ helper.form_for(subject, format: :json){|f| f.text_field :section } }
|
38
|
+
it "generates an action including format" do
|
39
|
+
form['method'].should eq 'post'
|
40
|
+
form['action'].should eq "/employees/collection_update.json"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context "with custom as: my_collection option" do
|
45
|
+
let(:form_html){ helper.form_for(subject, as: 'my_collection'){|f| f.text_field :section } }
|
46
|
+
it 'generates a proper form for a collection record with as param set' do
|
47
|
+
form['method'].should eq 'post'
|
48
|
+
form['action'].should eq "/employees/collection_update"
|
49
|
+
rails_form_method.should_not be_present
|
50
|
+
form['id'].should eq "collection_update_my_collection"
|
51
|
+
form['class'].should eq "collection_update_my_collection"
|
52
|
+
|
53
|
+
# Check field naming
|
54
|
+
doc.css('[name="my_collection[section]"]').first['value'].should eq 'SE9'
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: record_collection
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Benjamin ter Kuile
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-10-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -308,28 +308,28 @@ dependencies:
|
|
308
308
|
name: active_attr
|
309
309
|
requirement: !ruby/object:Gem::Requirement
|
310
310
|
requirements:
|
311
|
-
- - "
|
311
|
+
- - ">="
|
312
312
|
- !ruby/object:Gem::Version
|
313
313
|
version: '0.8'
|
314
314
|
type: :runtime
|
315
315
|
prerelease: false
|
316
316
|
version_requirements: !ruby/object:Gem::Requirement
|
317
317
|
requirements:
|
318
|
-
- - "
|
318
|
+
- - ">="
|
319
319
|
- !ruby/object:Gem::Version
|
320
320
|
version: '0.8'
|
321
321
|
- !ruby/object:Gem::Dependency
|
322
322
|
name: activemodel
|
323
323
|
requirement: !ruby/object:Gem::Requirement
|
324
324
|
requirements:
|
325
|
-
- - "
|
325
|
+
- - ">="
|
326
326
|
- !ruby/object:Gem::Version
|
327
327
|
version: '4.1'
|
328
328
|
type: :runtime
|
329
329
|
prerelease: false
|
330
330
|
version_requirements: !ruby/object:Gem::Requirement
|
331
331
|
requirements:
|
332
|
-
- - "
|
332
|
+
- - ">="
|
333
333
|
- !ruby/object:Gem::Version
|
334
334
|
version: '4.1'
|
335
335
|
- !ruby/object:Gem::Dependency
|
@@ -392,7 +392,8 @@ files:
|
|
392
392
|
- lib/record_collection/base.rb
|
393
393
|
- lib/record_collection/engine.rb
|
394
394
|
- lib/record_collection/name.rb
|
395
|
-
- lib/record_collection/rails/
|
395
|
+
- lib/record_collection/rails/form_builder.rb
|
396
|
+
- lib/record_collection/rails/form_helper.rb
|
396
397
|
- lib/record_collection/rails/routes.rb
|
397
398
|
- lib/record_collection/version.rb
|
398
399
|
- record_collection.gemspec
|
@@ -415,6 +416,7 @@ files:
|
|
415
416
|
- spec/dummy/app/assets/stylesheets/normalize.css
|
416
417
|
- spec/dummy/app/controllers/application_controller.rb
|
417
418
|
- spec/dummy/app/controllers/concerns/.keep
|
419
|
+
- spec/dummy/app/controllers/dashboard_controller.rb
|
418
420
|
- spec/dummy/app/controllers/employees_controller.rb
|
419
421
|
- spec/dummy/app/controllers/projects_controller.rb
|
420
422
|
- spec/dummy/app/helpers/application_helper.rb
|
@@ -426,6 +428,7 @@ files:
|
|
426
428
|
- spec/dummy/app/models/project.rb
|
427
429
|
- spec/dummy/app/models/project/collection.rb
|
428
430
|
- spec/dummy/app/views/application/_form_errors.html.slim
|
431
|
+
- spec/dummy/app/views/dashboard/home.html.slim
|
429
432
|
- spec/dummy/app/views/employees/_form.html.erb
|
430
433
|
- spec/dummy/app/views/employees/collection_edit.html.slim
|
431
434
|
- spec/dummy/app/views/employees/edit.html.erb
|
@@ -482,6 +485,7 @@ files:
|
|
482
485
|
- spec/features/translations_spec.rb
|
483
486
|
- spec/fixtures/collections.rb
|
484
487
|
- spec/rails/form_builder_spec.rb
|
488
|
+
- spec/rails/form_for_spec.rb
|
485
489
|
- spec/record_selection/base_spec.rb
|
486
490
|
- spec/record_selection/name_spec.rb
|
487
491
|
- spec/spec_helper.rb
|
@@ -529,6 +533,7 @@ test_files:
|
|
529
533
|
- spec/dummy/app/assets/stylesheets/normalize.css
|
530
534
|
- spec/dummy/app/controllers/application_controller.rb
|
531
535
|
- spec/dummy/app/controllers/concerns/.keep
|
536
|
+
- spec/dummy/app/controllers/dashboard_controller.rb
|
532
537
|
- spec/dummy/app/controllers/employees_controller.rb
|
533
538
|
- spec/dummy/app/controllers/projects_controller.rb
|
534
539
|
- spec/dummy/app/helpers/application_helper.rb
|
@@ -540,6 +545,7 @@ test_files:
|
|
540
545
|
- spec/dummy/app/models/project.rb
|
541
546
|
- spec/dummy/app/models/project/collection.rb
|
542
547
|
- spec/dummy/app/views/application/_form_errors.html.slim
|
548
|
+
- spec/dummy/app/views/dashboard/home.html.slim
|
543
549
|
- spec/dummy/app/views/employees/_form.html.erb
|
544
550
|
- spec/dummy/app/views/employees/collection_edit.html.slim
|
545
551
|
- spec/dummy/app/views/employees/edit.html.erb
|
@@ -596,6 +602,7 @@ test_files:
|
|
596
602
|
- spec/features/translations_spec.rb
|
597
603
|
- spec/fixtures/collections.rb
|
598
604
|
- spec/rails/form_builder_spec.rb
|
605
|
+
- spec/rails/form_for_spec.rb
|
599
606
|
- spec/record_selection/base_spec.rb
|
600
607
|
- spec/record_selection/name_spec.rb
|
601
608
|
- spec/spec_helper.rb
|