assignable_values 0.5.2 → 0.5.3

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/README.md CHANGED
@@ -193,7 +193,10 @@ Listing and validating als works the same:
193
193
  song.artist = lt2
194
194
  song.valid? # => false
195
195
 
196
- Similiar to scalar attributes, associations are only validated when the foreign key (`artist_id` in the example) changes. Previously saved values will remain assignable until another association has been saved.
196
+ Similiar to scalar attributes, associations are only validated when the foreign key (`artist_id` in the example above) changes.
197
+ Previously saved values will remain assignable until another association has been saved.
198
+
199
+ Validation errors will be attached to the association's foreign key (`artist_id` in the example above).
197
200
 
198
201
 
199
202
  How assignable values are evaluated
@@ -21,7 +21,7 @@ module AssignableValues
21
21
  unless allow_blank? && value.blank?
22
22
  begin
23
23
  unless assignable_value?(record, value)
24
- record.errors.add(property, not_included_error_message)
24
+ record.errors.add(error_property, not_included_error_message)
25
25
  end
26
26
  rescue DelegateUnavailable
27
27
  # if the delegate is unavailable, the validation is skipped
@@ -29,6 +29,10 @@ module AssignableValues
29
29
  end
30
30
  end
31
31
 
32
+ def error_property
33
+ property
34
+ end
35
+
32
36
  def not_included_error_message
33
37
  I18n.t('errors.messages.inclusion', :default => 'is not included in the list')
34
38
  end
@@ -13,6 +13,10 @@ module AssignableValues
13
13
  "#{property}_id"
14
14
  end
15
15
 
16
+ def error_property
17
+ association_id_method
18
+ end
19
+
16
20
  def association_id(record)
17
21
  record.send(association_id_method)
18
22
  end
@@ -1,3 +1,3 @@
1
1
  module AssignableValues
2
- VERSION = '0.5.2'
2
+ VERSION = '0.5.3'
3
3
  end
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../..
3
3
  specs:
4
- assignable_values (0.5.1)
4
+ assignable_values (0.5.2)
5
5
  activerecord
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../..
3
3
  specs:
4
- assignable_values (0.5.1)
4
+ assignable_values (0.5.2)
5
5
  activerecord
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../..
3
3
  specs:
4
- assignable_values (0.5.1)
4
+ assignable_values (0.5.2)
5
5
  activerecord
6
6
 
7
7
  GEM
@@ -105,6 +105,21 @@ describe AssignableValues::ActiveRecord do
105
105
  klass.new(:artist => disallowed_association).should_not be_valid
106
106
  end
107
107
 
108
+ it 'should attach errors to the foreign key of the association, not the association itself ' do
109
+ allowed_association = Artist.create!
110
+ disallowed_association = Artist.create!
111
+ klass = Song.disposable_copy do
112
+ assignable_values_for :artist do
113
+ [allowed_association]
114
+ end
115
+ end
116
+ record = klass.new(:artist => disallowed_association)
117
+ record.valid?
118
+ errors = record.errors[:artist_id]
119
+ error = errors.is_a?(Array) ? errors.first : errors # the return value sometimes was a string, sometimes an Array in Rails
120
+ error.should == I18n.t('errors.messages.inclusion')
121
+ end
122
+
108
123
  it 'should allow a nil association if the :allow_blank option is set' do
109
124
  klass = Song.disposable_copy do
110
125
  assignable_values_for :artist, :allow_blank => true do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: assignable_values
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 13
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 5
9
- - 2
10
- version: 0.5.2
9
+ - 3
10
+ version: 0.5.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Henning Koch
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-12-05 00:00:00 +01:00
18
+ date: 2012-12-19 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency