record_collection 0.4.4 → 0.4.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ffed31f67a7d2df694b8b0314dc9ae69606d7839
4
- data.tar.gz: 00d178e4963eeff61b02443841eb0d1624f0a896
3
+ metadata.gz: 69468333eb7b36ccfe4e161a27349d8c0c88eb97
4
+ data.tar.gz: f11c4e74135a921ff668565f2806bd1e21dbd21e
5
5
  SHA512:
6
- metadata.gz: 8070ced31dfc91c1bbce60dbce564226634f21a33aaac223139fb52d22ab4c2ec5df5473e8e5036c660d15fcae78827afaf5ef62b328f3fa1a547ab9ab742d81
7
- data.tar.gz: 7fe040d93fceb173d528f2c3a8e0701404c58417dad7cb8a346917ffbf633e7b221b96511825a0c40339fd15dd16bad864b1eb547a9c8a862eadfe292915b746
6
+ metadata.gz: 6efdd0029314ff4867e3c894b7bb634fedcdbb23dfc1d8bdcd29531fe429820236ae0d68357074ef6d37770de8ae952f8d7d4266edece99ba9f6a94e4c9e18e6
7
+ data.tar.gz: a5cff88bcf12c8e0739cac8f94b61981afe9383ff1f5119f519d2d43822041e2c562611b158bea30f8fc43854ae9a0a3d8aa97207e01aac3c49bc3a3228c1397
@@ -11,6 +11,7 @@ class ActionView::Helpers::FormBuilder
11
11
  # Return inputs for the collection ids
12
12
  def collection_ids
13
13
  @collection_ids_already_added = true
14
+ return "".html_safe unless object.respond_to?(:map)
14
15
  object.map{|record| @template.hidden_field_tag('ids[]', record.id, id: nil) }.join.html_safe
15
16
  end
16
17
 
@@ -1,3 +1,3 @@
1
1
  module RecordCollection
2
- VERSION = "0.4.4"
2
+ VERSION = "0.4.5"
3
3
  end
@@ -0,0 +1,85 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Employee::Collection do
4
+ subject { described_class.new }
5
+
6
+ describe '#one?' do
7
+ it 'is false without a collection' do
8
+ expect( subject.one? ).to be false
9
+ end
10
+
11
+ it 'is true with exactly one record' do
12
+ expect( described_class.new([Object.new]).one? ).to be true
13
+ end
14
+
15
+ it 'is false with more than one records' do
16
+ expect( described_class.new([Object.new, Object.new]).one? ).to be false
17
+ end
18
+ end
19
+
20
+ describe '#first and #last' do
21
+ it 'returns the first and last object of the collection' do
22
+ o1 = Object.new
23
+ o2 = Object.new
24
+ expect( described_class.new([o1, o2]).first ).to be o1
25
+ expect( described_class.new([o1, o2]).last ).to be o2
26
+ end
27
+ end
28
+
29
+ describe 'form representation' do
30
+ it 'has prefix collection for all types in forms' do
31
+ expect( described_class.model_name.param_key ).to eq 'collection'
32
+ end
33
+ end
34
+
35
+ describe '#uniform_collection_attribute' do
36
+ let(:collection_class){ described_class }
37
+ describe 'boolean attribute' do
38
+ let(:record) { Struct.new(:admin) }
39
+
40
+ it 'returns false for mixed falsy boolean values' do
41
+ expect( collection_class.new([record.new, record.new(false)]).uniform_collection_attribute(:admin)).to be false
42
+ end
43
+
44
+ it 'returns nil for mixes boolean values' do
45
+ expect( collection_class.new([record.new, record.new(true)]).uniform_collection_attribute(:admin)).to be nil
46
+ end
47
+
48
+ it 'returns true for all truthy values' do
49
+ expect( collection_class.new([record.new(true), record.new(true)]).uniform_collection_attribute(:admin)).to be true
50
+ end
51
+ end
52
+
53
+ describe 'untyped attribute' do
54
+ let(:record) { Struct.new(:section) }
55
+ it 'returns nil for mixed values truthy and falsy' do
56
+ collection = collection_class.new([record.new, record.new('NT2')])
57
+ expect( collection.uniform_collection_attribute :section ).to be nil
58
+ end
59
+
60
+ it 'returns nil for mixed truthy values' do
61
+ collection = collection_class.new([record.new('NT1'), record.new('NT2')])
62
+ expect( collection.uniform_collection_attribute :section ).to be nil
63
+ end
64
+
65
+ it 'returns the value for all the same values, but does not set the value' do
66
+ collection = collection_class.new([record.new('NT2'), record.new('NT2')])
67
+ expect( collection.uniform_collection_attribute :section ).to eq 'NT2'
68
+ expect( collection.section ).to be nil
69
+ end
70
+
71
+ it 'returns the value for all the same values, and sets the value if set_if_nil is given' do
72
+ collection = collection_class.new([record.new('NT2'), record.new('NT2')])
73
+ expect( collection.uniform_collection_attribute :section, set_if_nil: true ).to eq 'NT2'
74
+ expect( collection.section ).to eq 'NT2'
75
+ end
76
+
77
+ it 'returns the value for all the same values, and does not set the value if set_if_nil is given, but already set (eg: by invalid form)' do
78
+ collection = collection_class.new([record.new('NT2'), record.new('NT2')], section: 'Invalid set form value')
79
+ expect( collection.uniform_collection_attribute :section, set_if_nil: true ).to eq 'NT2'
80
+ expect( collection.section ).to eq 'Invalid set form value'
81
+ end
82
+ end
83
+ end
84
+ end
85
+
@@ -1,9 +1,9 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  RSpec.describe 'Validations' do
4
+ let(:collection_class) { Employee::Collection }
4
5
  describe 'conditional validations' do
5
6
  let(:record_class){ Employee }
6
- let(:collection_class) { Employee::Collection }
7
7
  it 'is valid without attribute present' do
8
8
  collection_class.new([]).should be_valid
9
9
  end
@@ -15,5 +15,12 @@ RSpec.describe 'Validations' do
15
15
  it 'is invalid with invalid section attribute' do
16
16
  collection_class.new([], section: 'SECTION3').should be_invalid
17
17
  end
18
+ describe '#save' do
19
+ it 'does not trigger update_collection_attributes! for invalid collection' do
20
+ collection = collection_class.new [], section: 'INVALID_SECTION_NAME'
21
+ collection.should_not receive :update_collection_attributes!
22
+ collection.save
23
+ end
24
+ end
18
25
  end
19
26
  end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe ActionView::Helpers::FormBuilder do
4
+ let(:employee){ Employee.create section: 'SE1' }
5
+ subject{ described_class.new :collection, Employee::Collection.new([employee]), @template, {}}
6
+ before do
7
+ # http://pivotallabs.com/testing-custom-form-builder/
8
+ @template = Object.new
9
+ @template.extend ActionView::Helpers::FormTagHelper
10
+ @template.extend ActionView::Helpers::FormOptionsHelper
11
+ @template.extend ActionView::Helpers::FormHelper
12
+ end
13
+
14
+ describe '.collection_ids' do
15
+ it 'returns the collection ids as hidden fields' do
16
+ subject.collection_ids.should eq %{<input type="hidden" name="ids[]" value="#{employee.id}" />}
17
+ end
18
+
19
+ it "does not raise when the object is not a collection object" do
20
+ form_builder = described_class.new :employee, employee, @template, {}
21
+ expect{ form_builder.collection_ids }.not_to raise_error
22
+ end
23
+ end
24
+ 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.4
4
+ version: 0.4.5
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-02-06 00:00:00.000000000 Z
11
+ date: 2015-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -307,6 +307,7 @@ files:
307
307
  - lib/record_collection/rails/routes.rb
308
308
  - lib/record_collection/version.rb
309
309
  - record_collection.gemspec
310
+ - spec/base/delegation_spec.rb
310
311
  - spec/base/finding_records_spec.rb
311
312
  - spec/base/validations_spec.rb
312
313
  - spec/dummy/README.rdoc
@@ -375,6 +376,7 @@ files:
375
376
  - spec/features/multi_select_spec.rb
376
377
  - spec/features/translations_spec.rb
377
378
  - spec/fixtures/collections.rb
379
+ - spec/rails/form_builder_spec.rb
378
380
  - spec/record_selection/base_spec.rb
379
381
  - spec/spec_helper.rb
380
382
  homepage: https://github.com/bterkuile/record_collection
@@ -402,6 +404,7 @@ signing_key:
402
404
  specification_version: 4
403
405
  summary: Manage collections of records in Ruby on Rails
404
406
  test_files:
407
+ - spec/base/delegation_spec.rb
405
408
  - spec/base/finding_records_spec.rb
406
409
  - spec/base/validations_spec.rb
407
410
  - spec/dummy/README.rdoc
@@ -470,6 +473,7 @@ test_files:
470
473
  - spec/features/multi_select_spec.rb
471
474
  - spec/features/translations_spec.rb
472
475
  - spec/fixtures/collections.rb
476
+ - spec/rails/form_builder_spec.rb
473
477
  - spec/record_selection/base_spec.rb
474
478
  - spec/spec_helper.rb
475
479
  has_rdoc: