assignable_values 0.10.0 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NmQwMjA2ZmZiMjQyOTRhOWMwNWViYTI4OTg4NTljMWFjMTRhNDUyYg==
5
+ data.tar.gz: !binary |-
6
+ NDM4YzA1NTk3ZGU2NjNiMmUyNjZiNDI2MWIwYTIxMTdkZTNjZmU1NA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZWFkZTk3NzkyMjk5ZjA3MmM0NDA3ZGQwMDA0ZTc0MDc3NjFjYjgzYzMyNjg2
10
+ ODQ2YWE1YTI0ZjNhZmQ5NDc2YjNjOTUxMWQ0ZjhjM2U4MzhjMDIxYmI5ZGNk
11
+ MzM4OWUyMTY5MTIwZGE0YTZmMTRiMjY0MDBlZDkyYjNkMWY3NGI=
12
+ data.tar.gz: !binary |-
13
+ OGEyZWQ2OTBhN2ZhMTNjNDg3ZTFhNzdmNzcyNDQ1NmIyNTJiZTQwNzEwNmZh
14
+ ZDdiYTQ2OWFlYTQ2NTYwMzA2ZmNhM2EyNjdjYmRlMzRhMTRhNjQ1OWFkYWZm
15
+ ZTNlMDViOTNmZmQ4Mjg4ZmE0YzQxYTQ5NTZhNmNmNmE5MzNlOWY=
data/README.md CHANGED
@@ -158,11 +158,15 @@ If a value has been saved before, it will remain valid, even if it is no longer
158
158
 
159
159
  It will also be returned when obtaining the list of assignable values:
160
160
 
161
- song.assignable_genres # => [2010, 2011, 2012, 1985]
161
+ song.assignable_years # => [2010, 2011, 2012, 1985]
162
+
163
+ However, if you want only those values that are actually intended to be assignable, e.g. when updating a `<select>` via AJAX, pass an option:
164
+
165
+ song.assignable_years(include_stored_value: false) # => [2010, 2011, 2012]
162
166
 
163
167
  Once a changed value has been saved, the previous value disappears from the list of assignable values:
164
168
 
165
- song.genre = 'pop'
169
+ song.year = '2010'
166
170
  song.save!
167
171
  song.assignable_years # => [2010, 2011, 2012]
168
172
  song.year = 1985
@@ -176,7 +180,7 @@ Restricting belongs_to associations
176
180
 
177
181
  You can restrict `belongs_to` associations in the same manner as scalar attributes:
178
182
 
179
- class Song
183
+ class Song < ActiveRecord::Base
180
184
 
181
185
  belongs_to :artist
182
186
 
@@ -202,7 +206,7 @@ Listing and validating als works the same:
202
206
  song.valid? # => false
203
207
 
204
208
  Similiar to scalar attributes, associations are only validated when the foreign key (`artist_id` in the example above) changes.
205
- Previously saved values will remain assignable until another association has been saved.
209
+ Values stored in the database will remain assignable until they are changed, and you can query actually assignable values with `song.assignable_artists(include_stored_value: false)`.
206
210
 
207
211
  Validation errors will be attached to the association's foreign key (`artist_id` in the example above).
208
212
 
@@ -45,19 +45,23 @@ module AssignableValues
45
45
  assignable_values(record).include?(value)
46
46
  end
47
47
 
48
- def assignable_values(record, decorate = false)
48
+ def assignable_values(record, options = {})
49
49
  assignable_values = []
50
50
  parsed_values = parsed_assignable_values(record)
51
51
  current_values = parsed_values.delete(:values)
52
- old_value = previously_saved_value(record)
53
- if old_value.present? && !current_values.include?(old_value)
54
- assignable_values << old_value
52
+
53
+ if options.fetch(:include_old_value, true)
54
+ old_value = previously_saved_value(record)
55
+ if old_value.present? && !current_values.include?(old_value)
56
+ assignable_values << old_value
57
+ end
55
58
  end
59
+
56
60
  assignable_values += current_values
57
61
  parsed_values.each do |meta_name, meta_content|
58
62
  assignable_values.singleton_class.send(:define_method, meta_name) { meta_content }
59
63
  end
60
- if decorate
64
+ if options[:decorate]
61
65
  assignable_values = decorate_values(assignable_values)
62
66
  end
63
67
  assignable_values
@@ -179,8 +183,11 @@ module AssignableValues
179
183
  restriction = self
180
184
  enhance_model do
181
185
  assignable_values_method = "assignable_#{restriction.property.to_s.pluralize}"
182
- define_method assignable_values_method do
183
- restriction.assignable_values(self, true)
186
+ define_method assignable_values_method do |*args|
187
+ # Ruby 1.8.7 does not support optional block arguments :(
188
+ options = args.first || {}
189
+ options.merge!({:decorate => true})
190
+ restriction.assignable_values(self, options)
184
191
  end
185
192
  end
186
193
  end
@@ -1,3 +1,3 @@
1
1
  module AssignableValues
2
- VERSION = '0.10.0'
2
+ VERSION = '0.11.0'
3
3
  end
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../..
3
3
  specs:
4
- assignable_values (0.10.0)
4
+ assignable_values (0.11.0)
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.10.0)
4
+ assignable_values (0.11.0)
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.10.0)
4
+ assignable_values (0.11.0)
5
5
  activerecord
6
6
 
7
7
  GEM
@@ -8,5 +8,5 @@ desc "Run all specs for a specific rails version"
8
8
  RSpec::Core::RakeTask.new(:spec) do |t|
9
9
  t.pattern = defined?(SPEC) ? SPEC : ['**/*_spec.rb', '../shared/**/*_spec.rb']
10
10
  t.verbose = false
11
- t.rspec_opts = '--backtrace'
11
+ # t.rspec_opts = '--backtrace'
12
12
  end
@@ -13,7 +13,7 @@ describe AssignableValues::ActiveRecord do
13
13
  end.to raise_error(AssignableValues::NoValuesGiven)
14
14
  end
15
15
 
16
- context 'when validation virtual attributes' do
16
+ context 'when validating virtual attributes' do
17
17
 
18
18
  before :each do
19
19
  @klass = Song.disposable_copy do
@@ -71,7 +71,7 @@ describe AssignableValues::ActiveRecord do
71
71
 
72
72
  it 'should allow a previously saved value even if that value is no longer allowed' do
73
73
  record = @klass.create!(:genre => 'pop')
74
- @klass.update_all(:genre => 'disallowed value') # update without validations for the sake of this test
74
+ @klass.update_all(:genre => 'pretend previously valid value') # update without validations for the sake of this test
75
75
  record.reload.should be_valid
76
76
  end
77
77
 
@@ -516,6 +516,31 @@ describe AssignableValues::ActiveRecord do
516
516
  record.assignable_genres.should == %w[pop rock]
517
517
  end
518
518
 
519
+ it 'should allow omitting a previously saved value' do
520
+ klass = Song.disposable_copy do
521
+ assignable_values_for :genre do
522
+ %w[pop rock]
523
+ end
524
+ end
525
+ record = klass.create!(:genre => 'pop')
526
+ klass.update_all(:genre => 'ballad') # update without validation for the sake of this test
527
+ record.reload.assignable_genres(:include_old_value => false).should == %w[pop rock]
528
+ end
529
+
530
+ it 'should allow omitting a previously saved association' do
531
+ allowed_association = Artist.create!
532
+ disallowed_association = Artist.create!
533
+ klass = Song.disposable_copy
534
+
535
+ record = klass.create!(:artist => disallowed_association)
536
+ klass.class_eval do
537
+ assignable_values_for :artist do
538
+ [allowed_association]
539
+ end
540
+ end
541
+ record.assignable_artists(:include_old_value => false).should =~ [allowed_association]
542
+ end
543
+
519
544
  context 'humanization' do
520
545
 
521
546
  it 'should define a method that return pairs of values and their humanization' do
metadata CHANGED
@@ -1,104 +1,92 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: assignable_values
3
- version: !ruby/object:Gem::Version
4
- hash: 55
5
- prerelease:
6
- segments:
7
- - 0
8
- - 10
9
- - 0
10
- version: 0.10.0
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.11.0
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - Henning Koch
14
8
  autorequire:
15
9
  bindir: bin
16
10
  cert_chain: []
17
-
18
- date: 2014-06-10 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
11
+ date: 2014-06-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
21
14
  name: activerecord
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
- none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 3
29
- segments:
30
- - 0
31
- version: "0"
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
32
20
  type: :runtime
33
- version_requirements: *id001
34
- - !ruby/object:Gem::Dependency
35
- name: rails
36
21
  prerelease: false
37
- requirement: &id002 !ruby/object:Gem::Requirement
38
- none: false
39
- requirements:
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
40
31
  - - ~>
41
- - !ruby/object:Gem::Version
42
- hash: 5
43
- segments:
44
- - 3
45
- - 1
46
- version: "3.1"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.1'
47
34
  type: :development
48
- version_requirements: *id002
49
- - !ruby/object:Gem::Dependency
50
- name: rspec
51
35
  prerelease: false
52
- requirement: &id003 !ruby/object:Gem::Requirement
53
- none: false
54
- requirements:
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
55
38
  - - ~>
56
- - !ruby/object:Gem::Version
57
- hash: 19
58
- segments:
59
- - 2
60
- - 8
61
- version: "2.8"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '2.8'
62
48
  type: :development
63
- version_requirements: *id003
64
- - !ruby/object:Gem::Dependency
65
- name: rspec-rails
66
49
  prerelease: false
67
- requirement: &id004 !ruby/object:Gem::Requirement
68
- none: false
69
- requirements:
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
70
52
  - - ~>
71
- - !ruby/object:Gem::Version
72
- hash: 19
73
- segments:
74
- - 2
75
- - 8
76
- version: "2.8"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '2.8'
77
62
  type: :development
78
- version_requirements: *id004
79
- - !ruby/object:Gem::Dependency
80
- name: sqlite3
81
63
  prerelease: false
82
- requirement: &id005 !ruby/object:Gem::Requirement
83
- none: false
84
- requirements:
85
- - - ">="
86
- - !ruby/object:Gem::Version
87
- hash: 3
88
- segments:
89
- - 0
90
- version: "0"
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '2.8'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
91
76
  type: :development
92
- version_requirements: *id005
93
- description: Restrict the values assignable to ActiveRecord attributes or associations. Or enums on steroids.
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Restrict the values assignable to ActiveRecord attributes or associations.
84
+ Or enums on steroids.
94
85
  email: henning.koch@makandra.de
95
86
  executables: []
96
-
97
87
  extensions: []
98
-
99
88
  extra_rdoc_files: []
100
-
101
- files:
89
+ files:
102
90
  - .gitignore
103
91
  - .rspec
104
92
  - .ruby-version
@@ -214,39 +202,31 @@ files:
214
202
  - spec/shared/assignable_values/active_record_spec.rb
215
203
  - spec/shared/assignable_values/humanized_value_spec.rb
216
204
  homepage: https://github.com/makandra/assignable_values
217
- licenses:
205
+ licenses:
218
206
  - MIT
207
+ metadata: {}
219
208
  post_install_message:
220
209
  rdoc_options: []
221
-
222
- require_paths:
210
+ require_paths:
223
211
  - lib
224
- required_ruby_version: !ruby/object:Gem::Requirement
225
- none: false
226
- requirements:
227
- - - ">="
228
- - !ruby/object:Gem::Version
229
- hash: 3
230
- segments:
231
- - 0
232
- version: "0"
233
- required_rubygems_version: !ruby/object:Gem::Requirement
234
- none: false
235
- requirements:
236
- - - ">="
237
- - !ruby/object:Gem::Version
238
- hash: 3
239
- segments:
240
- - 0
241
- version: "0"
212
+ required_ruby_version: !ruby/object:Gem::Requirement
213
+ requirements:
214
+ - - ! '>='
215
+ - !ruby/object:Gem::Version
216
+ version: '0'
217
+ required_rubygems_version: !ruby/object:Gem::Requirement
218
+ requirements:
219
+ - - ! '>='
220
+ - !ruby/object:Gem::Version
221
+ version: '0'
242
222
  requirements: []
243
-
244
223
  rubyforge_project:
245
- rubygems_version: 1.8.29
224
+ rubygems_version: 2.2.1
246
225
  signing_key:
247
- specification_version: 3
248
- summary: Restrict the values assignable to ActiveRecord attributes or associations. Or enums on steroids.
249
- test_files:
226
+ specification_version: 4
227
+ summary: Restrict the values assignable to ActiveRecord attributes or associations.
228
+ Or enums on steroids.
229
+ test_files:
250
230
  - spec/rails-2.3/Gemfile
251
231
  - spec/rails-2.3/Gemfile.lock
252
232
  - spec/rails-2.3/Rakefile
@@ -344,4 +324,3 @@ test_files:
344
324
  - spec/shared/app_root/db/migrate/003_create_recordings.rb
345
325
  - spec/shared/assignable_values/active_record_spec.rb
346
326
  - spec/shared/assignable_values/humanized_value_spec.rb
347
- has_rdoc: