active_data 1.0.0 → 1.1.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 (115) hide show
  1. checksums.yaml +5 -5
  2. data/.codeclimate.yml +13 -0
  3. data/.rubocop.yml +56 -0
  4. data/.rubocop_todo.yml +53 -0
  5. data/.rvmrc +1 -1
  6. data/.travis.yml +15 -2
  7. data/Appraisals +1 -1
  8. data/CHANGELOG.md +31 -0
  9. data/Guardfile +8 -8
  10. data/README.md +256 -0
  11. data/Rakefile +2 -4
  12. data/active_data.gemspec +8 -7
  13. data/gemfiles/rails.4.0.gemfile +1 -1
  14. data/gemfiles/rails.4.1.gemfile +1 -1
  15. data/gemfiles/rails.4.2.gemfile +1 -1
  16. data/gemfiles/rails.5.0.gemfile +1 -1
  17. data/gemfiles/rails.5.1.gemfile +14 -0
  18. data/lib/active_data/active_record/associations.rb +18 -13
  19. data/lib/active_data/active_record/nested_attributes.rb +8 -14
  20. data/lib/active_data/base.rb +13 -0
  21. data/lib/active_data/config.rb +4 -4
  22. data/lib/active_data/errors.rb +29 -13
  23. data/lib/active_data/extensions.rb +22 -21
  24. data/lib/active_data/model/associations/base.rb +22 -6
  25. data/lib/active_data/model/associations/embeds_any.rb +17 -0
  26. data/lib/active_data/model/associations/embeds_many.rb +29 -19
  27. data/lib/active_data/model/associations/embeds_one.rb +30 -26
  28. data/lib/active_data/model/associations/nested_attributes.rb +82 -50
  29. data/lib/active_data/model/associations/persistence_adapters/active_record/referenced_proxy.rb +31 -0
  30. data/lib/active_data/model/associations/persistence_adapters/active_record.rb +66 -0
  31. data/lib/active_data/model/associations/persistence_adapters/base.rb +53 -0
  32. data/lib/active_data/model/associations/references_any.rb +41 -0
  33. data/lib/active_data/model/associations/references_many.rb +51 -37
  34. data/lib/active_data/model/associations/references_one.rb +43 -41
  35. data/lib/active_data/model/associations/reflections/base.rb +19 -29
  36. data/lib/active_data/model/associations/reflections/embeds_any.rb +43 -0
  37. data/lib/active_data/model/associations/reflections/embeds_many.rb +3 -13
  38. data/lib/active_data/model/associations/reflections/embeds_one.rb +5 -37
  39. data/lib/active_data/model/associations/reflections/references_any.rb +62 -0
  40. data/lib/active_data/model/associations/reflections/references_many.rb +7 -7
  41. data/lib/active_data/model/associations/reflections/references_one.rb +9 -7
  42. data/lib/active_data/model/associations/reflections/singular.rb +35 -0
  43. data/lib/active_data/model/associations/validations.rb +2 -27
  44. data/lib/active_data/model/associations.rb +12 -10
  45. data/lib/active_data/model/attributes/attribute.rb +10 -10
  46. data/lib/active_data/model/attributes/base.rb +8 -7
  47. data/lib/active_data/model/attributes/localized.rb +4 -4
  48. data/lib/active_data/model/attributes/reference_many.rb +6 -8
  49. data/lib/active_data/model/attributes/reference_one.rb +17 -9
  50. data/lib/active_data/model/attributes/reflections/attribute.rb +2 -2
  51. data/lib/active_data/model/attributes/reflections/base.rb +8 -11
  52. data/lib/active_data/model/attributes/reflections/localized.rb +2 -2
  53. data/lib/active_data/model/attributes/reflections/reference_one.rb +11 -22
  54. data/lib/active_data/model/attributes/reflections/represents.rb +5 -6
  55. data/lib/active_data/model/attributes/represents.rb +6 -5
  56. data/lib/active_data/model/attributes.rb +33 -87
  57. data/lib/active_data/model/callbacks.rb +6 -7
  58. data/lib/active_data/model/conventions.rb +2 -0
  59. data/lib/active_data/model/dirty.rb +4 -4
  60. data/lib/active_data/model/lifecycle.rb +18 -20
  61. data/lib/active_data/model/localization.rb +5 -2
  62. data/lib/active_data/model/persistence.rb +2 -2
  63. data/lib/active_data/model/primary.rb +19 -14
  64. data/lib/active_data/model/representation.rb +81 -0
  65. data/lib/active_data/model/scopes.rb +22 -12
  66. data/lib/active_data/model/validations/associated.rb +3 -2
  67. data/lib/active_data/model/validations/nested.rb +6 -1
  68. data/lib/active_data/model/validations.rb +3 -3
  69. data/lib/active_data/model.rb +2 -1
  70. data/lib/active_data/undefined_class.rb +9 -0
  71. data/lib/active_data/version.rb +1 -1
  72. data/lib/active_data.rb +40 -17
  73. data/spec/lib/active_data/active_record/associations_spec.rb +107 -45
  74. data/spec/lib/active_data/active_record/nested_attributes_spec.rb +1 -2
  75. data/spec/lib/active_data/config_spec.rb +37 -15
  76. data/spec/lib/active_data/model/associations/embeds_many_spec.rb +475 -172
  77. data/spec/lib/active_data/model/associations/embeds_one_spec.rb +353 -96
  78. data/spec/lib/active_data/model/associations/nested_attributes_spec.rb +108 -12
  79. data/spec/lib/active_data/model/associations/persistence_adapters/active_record_spec.rb +58 -0
  80. data/spec/lib/active_data/model/associations/references_many_spec.rb +440 -64
  81. data/spec/lib/active_data/model/associations/references_one_spec.rb +347 -36
  82. data/spec/lib/active_data/model/associations/reflections/embeds_many_spec.rb +8 -7
  83. data/spec/lib/active_data/model/associations/reflections/embeds_one_spec.rb +7 -6
  84. data/spec/lib/active_data/model/associations/reflections/references_many_spec.rb +81 -33
  85. data/spec/lib/active_data/model/associations/reflections/references_one_spec.rb +116 -37
  86. data/spec/lib/active_data/model/associations/validations_spec.rb +27 -43
  87. data/spec/lib/active_data/model/associations_spec.rb +34 -25
  88. data/spec/lib/active_data/model/attributes/attribute_spec.rb +26 -23
  89. data/spec/lib/active_data/model/attributes/base_spec.rb +5 -6
  90. data/spec/lib/active_data/model/attributes/collection_spec.rb +7 -8
  91. data/spec/lib/active_data/model/attributes/dictionary_spec.rb +40 -33
  92. data/spec/lib/active_data/model/attributes/localized_spec.rb +27 -28
  93. data/spec/lib/active_data/model/attributes/reflections/attribute_spec.rb +6 -6
  94. data/spec/lib/active_data/model/attributes/represents_spec.rb +10 -78
  95. data/spec/lib/active_data/model/attributes_spec.rb +150 -45
  96. data/spec/lib/active_data/model/callbacks_spec.rb +69 -70
  97. data/spec/lib/active_data/model/conventions_spec.rb +0 -1
  98. data/spec/lib/active_data/model/dirty_spec.rb +22 -13
  99. data/spec/lib/active_data/model/lifecycle_spec.rb +49 -23
  100. data/spec/lib/active_data/model/persistence_spec.rb +5 -6
  101. data/spec/lib/active_data/model/representation_spec.rb +126 -0
  102. data/spec/lib/active_data/model/scopes_spec.rb +1 -3
  103. data/spec/lib/active_data/model/typecasting_spec.rb +6 -5
  104. data/spec/lib/active_data/model/validations/associated_spec.rb +26 -18
  105. data/spec/lib/active_data/model/validations/nested_spec.rb +89 -18
  106. data/spec/lib/active_data/model_spec.rb +1 -2
  107. data/spec/lib/active_data_spec.rb +0 -1
  108. data/spec/shared/nested_attribute_examples.rb +332 -0
  109. data/spec/spec_helper.rb +3 -0
  110. data/spec/support/model_helpers.rb +2 -2
  111. data/spec/support/muffle_helper.rb +7 -0
  112. metadata +52 -18
  113. data/lib/active_data/model/associations/collection/referenced.rb +0 -26
  114. data/lib/active_data/model/associations/reflections/reference_reflection.rb +0 -45
  115. data/spec/lib/active_data/model/nested_attributes.rb +0 -202
@@ -1,23 +1,119 @@
1
- # encoding: UTF-8
2
1
  require 'spec_helper'
3
- require 'lib/active_data/model/nested_attributes'
2
+ require 'shared/nested_attribute_examples'
4
3
 
5
4
  describe ActiveData::Model::Associations::NestedAttributes do
6
- before do
7
- stub_model :user do
8
- include ActiveData::Model::Associations
5
+ context '' do
6
+ before do
7
+ stub_model :user do
8
+ include ActiveData::Model::Associations
9
9
 
10
- attribute :email, String
11
- embeds_one :profile
12
- embeds_many :projects
10
+ attribute :email, String
11
+ embeds_one :profile
12
+ embeds_many :projects
13
13
 
14
- accepts_nested_attributes_for :profile, :projects
14
+ accepts_nested_attributes_for :profile, :projects
15
15
 
16
- def save
17
- apply_association_changes!
16
+ def save
17
+ apply_association_changes!
18
+ end
18
19
  end
19
20
  end
21
+
22
+ include_examples 'nested attributes'
23
+ end
24
+
25
+ xcontext 'references_one' do
26
+ before do
27
+ stub_class(:author, ActiveRecord::Base)
28
+ stub_class(:user, ActiveRecord::Base)
29
+
30
+ stub_model :book do
31
+ include ActiveData::Model::Associations
32
+
33
+ references_one :author
34
+ references_many :users
35
+
36
+ accepts_nested_attributes_for :author, :users
37
+ end
38
+ end
39
+
40
+ context 'references_one' do
41
+ let(:book) { Book.new }
42
+
43
+ specify { expect { book.author_attributes = {} }.to change { book.author }.to(an_instance_of(Author)) }
44
+ specify { expect { book.author_attributes = {name: 'Author'} }.to change { book.author.try(:name) }.to('Author') }
45
+ specify { expect { book.author_attributes = {id: 42, name: 'Author'} }.to raise_error ActiveData::ObjectNotFound }
46
+
47
+ context ':reject_if' do
48
+ context do
49
+ before { Book.accepts_nested_attributes_for :author, reject_if: :all_blank }
50
+ specify { expect { book.author_attributes = {name: ''} }.not_to change { book.author } }
51
+ end
52
+
53
+ context do
54
+ before { Book.accepts_nested_attributes_for :author, reject_if: ->(attributes) { attributes['name'].blank? } }
55
+ specify { expect { book.author_attributes = {name: ''} }.not_to change { book.author } }
56
+ end
57
+ end
58
+
59
+ context 'existing' do
60
+ let(:author) { Author.new(name: 'Author') }
61
+ let(:book) { Book.new author: author }
62
+
63
+ specify { expect { book.author_attributes = {id: 42, name: 'Author'} }.to raise_error ActiveData::ObjectNotFound }
64
+ specify { expect { book.author_attributes = {id: author.id.to_s, name: 'Author 1'} }.to change { book.author.name }.to('Author 1') }
65
+ specify { expect { book.author_attributes = {name: 'Author 1'} }.to change { book.author.name }.to('Author 1') }
66
+ specify { expect { book.author_attributes = {name: 'Author 1', _destroy: '1'} }.not_to change { book.author.name } }
67
+ specify do
68
+ expect do
69
+ book.author_attributes = {name: 'Author 1', _destroy: '1'}
70
+ book.save { true }
71
+ end.not_to change { book.author.name }
72
+ end
73
+ specify { expect { book.author_attributes = {id: author.id.to_s, name: 'Author 1', _destroy: '1'} }.to change { book.author.name }.to('Author 1') }
74
+ specify do
75
+ expect do
76
+ book.author_attributes = {id: author.id.to_s, name: 'Author 1', _destroy: '1'}
77
+ book.save { true }
78
+ end.to change { book.author.name }.to('Author 1')
79
+ end
80
+
81
+ context ':allow_destroy' do
82
+ before { Book.accepts_nested_attributes_for :author, allow_destroy: true }
83
+
84
+ specify { expect { book.author_attributes = {name: 'Author 1', _destroy: '1'} }.not_to change { book.author.name } }
85
+ specify do
86
+ expect do
87
+ book.author_attributes = {name: 'Author 1', _destroy: '1'}
88
+ book.save { true }
89
+ end.not_to change { book.author.name }
90
+ end
91
+ specify { expect { book.author_attributes = {id: author.id.to_s, name: 'Author 1', _destroy: '1'} }.to change { book.author.name }.to('Author 1') }
92
+ specify do
93
+ expect do
94
+ book.author_attributes = {id: author.id.to_s, name: 'Author 1', _destroy: '1'}
95
+ book.save { true }
96
+ end.to change { book.author }.to(nil)
97
+ end
98
+ end
99
+
100
+ context ':update_only' do
101
+ before { Book.accepts_nested_attributes_for :author, update_only: true }
102
+
103
+ specify do
104
+ expect { book.author_attributes = {id: 42, name: 'Author 1'} }
105
+ .to change { book.author.name }.to('Author 1')
106
+ end
107
+ end
108
+ end
109
+ end
110
+
111
+ context 'references_many' do
112
+ let(:book) { Book.new }
113
+ end
20
114
  end
21
115
 
22
- include_examples 'nested attributes'
116
+ describe '#assign_attributes' do
117
+ specify 'invent a good example'
118
+ end
23
119
  end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActiveData::Model::Associations::PersistenceAdapters::ActiveRecord do
4
+ before do
5
+ stub_class(:author, ActiveRecord::Base)
6
+ end
7
+
8
+ subject(:adapter) { described_class.new(Author, primary_key, scope_proc) }
9
+ let(:primary_key) { :id }
10
+ let(:scope_proc) { nil }
11
+
12
+ describe '#build' do
13
+ subject { adapter.build(name: name) }
14
+ let(:name) { 'John Doe' }
15
+
16
+ its(:name) { should == name }
17
+ it { is_expected.to be_a Author }
18
+ end
19
+
20
+ describe '#find_one' do
21
+ subject { adapter.find_one(nil, author.id) }
22
+ let(:author) { Author.create }
23
+
24
+ it { should == author }
25
+ end
26
+
27
+ describe '#find_all' do
28
+ subject { adapter.find_all(nil, authors.map(&:id)) }
29
+ let(:authors) { Array.new(2) { Author.create } }
30
+
31
+ it { should == authors }
32
+ end
33
+
34
+ describe '#scope' do
35
+ subject { adapter.scope(owner, source) }
36
+ let(:authors) { ['John Doe', 'Sam Smith', 'John Smith'].map { |name| Author.create(name: name) } }
37
+ let(:source) { authors[0..1].map(&:id) }
38
+ let(:owner) { nil }
39
+
40
+ it { is_expected.to be_a ActiveRecord::Relation }
41
+
42
+ context 'without scope_proc' do
43
+ it { should == Author.where(primary_key => source) }
44
+ end
45
+
46
+ context 'with scope_proc' do
47
+ let(:scope_proc) { -> { where("name LIKE 'John%'") } }
48
+
49
+ its(:to_a) { should == [Author.first] }
50
+ end
51
+ end
52
+
53
+ describe '#primary_key_type' do
54
+ subject { adapter.primary_key_type }
55
+
56
+ it { should == Integer }
57
+ end
58
+ end