assignable_values 0.14.0 → 0.15.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dd2475ca27683328810e810b40276043bf60b442c621be57f29885e58647183a
4
- data.tar.gz: f3e17c9e48ce5e73faf2dfe3b3529b8275c603beef2551c6b3d83cac01a3343e
3
+ metadata.gz: d0359f423877ba1c7d9b303053434f7d80077da0cf314a964b6577f31b97fd01
4
+ data.tar.gz: e3818b1b35b173d4db2a896092a7cf0ee7f8d93381631cd1cfbbf62f64fc6783
5
5
  SHA512:
6
- metadata.gz: d2f513338f30247aba1d2b8f436266e3f8865c3c367a0367268131f4f61df7ee80a21e049a4a96b7ce65e90e38068ee759ca6829e23e620635d4595cfa6cc825
7
- data.tar.gz: be46b3706a7fc5fece9c43b4ac47164ae9b1cd67a43227707c6182d8ae5755eed601aed0f79d25a5fbfdca88519df64f1323104999265725f0992fb6662417f5
6
+ metadata.gz: 479c5751e8086031feadcc70a06b878d04020f5fc023b69afd790c2a684f219d56e6d44abfa31387aa342a5c08b4ecfdcea6da085bc9060685ac04750fef3fba
7
+ data.tar.gz: c38e04dc6003f5f3ab055eeb2b4a9183351c4a7101da42750bd381263e190cdbca0e9638c5e44a88e6510f6aa741eac1612285b1bbfb5347dbb599fdefa95388
@@ -6,11 +6,18 @@ This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html
6
6
 
7
7
  ### Breaking changes
8
8
 
9
- -
9
+ ### Compatible changes
10
+
11
+
12
+ ## 0.15.0 - 2018-10-26
13
+
14
+ ### Breaking changes
15
+
16
+ - `#humanized_values` is deprecated, in favour of `#humanized_assignablevalues`
10
17
 
11
18
  ### Compatible changes
12
19
 
13
- -
20
+ - `#humanized_value(value)` and `#humanized_assignable_values` now also works for the `multiple: true` case
14
21
 
15
22
 
16
23
  ## 0.14.0 - 2018-09-17
data/README.md CHANGED
@@ -65,13 +65,13 @@ Or you can retrieve the humanized version of any given value by passing it as an
65
65
 
66
66
  You can obtain a list of all assignable values with their humanizations:
67
67
 
68
- song.humanized_genres.size # => 3
69
- song.humanized_genres.first.value # => "pop"
70
- song.humanized_genres.first.humanized # => "Pop music"
68
+ song.humanized_assignable_genres.size # => 3
69
+ song.humanized_assignable_genres.first.value # => "pop"
70
+ song.humanized_assignable_genres.first.humanized # => "Pop music"
71
71
 
72
72
  A good way to populate a `<select>` tag with pairs of internal values and human labels is to use the `collection_select` helper from Rails:
73
73
 
74
- form.collection_select :genre, form.object.humanized_genres, :value, :humanized
74
+ form.collection_select :genre, form.object.humanized_assignable_genres, :value, :humanized
75
75
 
76
76
 
77
77
  ### Defining default values
@@ -166,7 +166,7 @@ This is to prevent records from becoming invalid as the list of assignable value
166
166
 
167
167
  ### Array values
168
168
 
169
- Assignable values can also be used for array values. This works when you use Rails 5+ and PostgreSQL with an array column, or with ActiveRecord's `serialize`.
169
+ Assignable values can also be used for array values. This works when you use Rails 5+ and PostgreSQL with an array column, or on older Rails versions and other databases using ActiveRecord's `serialize`.
170
170
 
171
171
  To validate array values, pass `multiple: true`:
172
172
 
@@ -182,6 +182,13 @@ end
182
182
 
183
183
  In this case, every *subset* of the given values is valid, for example `['pop', 'electronic']`.
184
184
 
185
+ For humanization, you can still use
186
+
187
+ ```
188
+ song.humanizable_genre('pop') # => "Pop music"
189
+ song.assignable_humanizable_genres.last.humanized # => "Electronic music"
190
+ ```
191
+
185
192
 
186
193
  Restricting belongs_to associations
187
194
  -----------------------------------
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- assignable_values (0.14.0)
4
+ assignable_values (0.15.0)
5
5
  activerecord (>= 2.3)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- assignable_values (0.14.0)
4
+ assignable_values (0.15.0)
5
5
  activerecord (>= 2.3)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- assignable_values (0.14.0)
4
+ assignable_values (0.15.0)
5
5
  activerecord (>= 2.3)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- assignable_values (0.14.0)
4
+ assignable_values (0.15.0)
5
5
  activerecord (>= 2.3)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- assignable_values (0.14.0)
4
+ assignable_values (0.15.0)
5
5
  activerecord (>= 2.3)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- assignable_values (0.14.0)
4
+ assignable_values (0.15.0)
5
5
  activerecord (>= 2.3)
6
6
 
7
7
  GEM
@@ -1,4 +1,5 @@
1
1
  require 'active_record'
2
+ require 'active_support/deprecation'
2
3
  require 'assignable_values/errors'
3
4
  require 'assignable_values/active_record'
4
5
  require 'assignable_values/active_record/restriction/base'
@@ -7,7 +7,7 @@ module AssignableValues
7
7
  super
8
8
  define_humanized_value_instance_method
9
9
  define_humanized_value_class_method
10
- define_humanized_values_instance_method
10
+ define_humanized_assignable_values_instance_method
11
11
  end
12
12
 
13
13
  def humanized_value(value)
@@ -17,7 +17,7 @@ module AssignableValues
17
17
  end
18
18
  end
19
19
 
20
- def humanized_values(record)
20
+ def humanized_assignable_values(record)
21
21
  values = assignable_values(record)
22
22
  values.collect do |value|
23
23
  HumanizedValue.new(value, humanized_value(value))
@@ -45,8 +45,12 @@ module AssignableValues
45
45
 
46
46
  def define_humanized_value_instance_method
47
47
  restriction = self
48
+ multiple = @options[:multiple]
48
49
  enhance_model do
49
- define_method :"humanized_#{restriction.property}" do |*args|
50
+ define_method :"humanized_#{restriction.property.to_s.singularize}" do |*args|
51
+ if args.size > 1 || (multiple && args.size == 0)
52
+ raise ArgumentError.new("wrong number of arguments (#{args.size} for #{multiple ? '1' : '0..1'})")
53
+ end
50
54
  given_value = args[0]
51
55
  value = given_value || send(restriction.property)
52
56
  restriction.humanized_value(value)
@@ -54,11 +58,19 @@ module AssignableValues
54
58
  end
55
59
  end
56
60
 
57
- def define_humanized_values_instance_method
61
+ def define_humanized_assignable_values_instance_method
58
62
  restriction = self
63
+ multiple = @options[:multiple]
59
64
  enhance_model do
60
- define_method :"humanized_#{restriction.property.to_s.pluralize}" do
61
- restriction.humanized_values(self)
65
+ define_method :"humanized_assignable_#{restriction.property.to_s.pluralize}" do
66
+ restriction.humanized_assignable_values(self)
67
+ end
68
+
69
+ unless multiple
70
+ define_method :"humanized_#{restriction.property.to_s.pluralize}" do
71
+ ActiveSupport::Deprecation.warn("humanized_<value>s is deprecated, use humanized_assignable_<value>s instead", caller)
72
+ restriction.humanized_assignable_values(self)
73
+ end
62
74
  end
63
75
  end
64
76
  end
@@ -8,7 +8,8 @@ class HumanizableString < String
8
8
  end
9
9
 
10
10
  def humanized
11
+ ActiveSupport::Deprecation.warn("assignable_<value>.humanized is deprecated, use humanized_assignable_<value>s.humanized instead", caller)
11
12
  @humanization
12
13
  end
13
14
 
14
- end
15
+ end
@@ -1,3 +1,3 @@
1
1
  module AssignableValues
2
- VERSION = '0.14.0'
2
+ VERSION = '0.15.0'
3
3
  end
@@ -20,6 +20,10 @@ describe AssignableValues::ActiveRecord do
20
20
  assignable_values_for :sub_genre do
21
21
  %w[pop rock]
22
22
  end
23
+
24
+ assignable_values_for :multi_genres, :allow_blank => true do
25
+ %w[pop rock]
26
+ end
23
27
  end
24
28
  end
25
29
 
@@ -35,6 +39,8 @@ describe AssignableValues::ActiveRecord do
35
39
  it 'should generate a method returning the humanized value' do
36
40
  song = @klass.new(:sub_genre => 'pop')
37
41
  song.humanized_sub_genre.should == 'Pop music'
42
+ song.humanized_sub_genre('rock').should == 'Rock music'
43
+ song.humanized_multi_genre('rock').should == 'Rock music'
38
44
  end
39
45
 
40
46
  end
@@ -45,21 +51,21 @@ describe AssignableValues::ActiveRecord do
45
51
 
46
52
  before :each do
47
53
  @klass = Song.disposable_copy do
48
- assignable_values_for :sub_genres,:multiple => true do
54
+ assignable_values_for :multi_genres, :multiple => true do
49
55
  %w[pop rock]
50
56
  end
51
57
  end
52
58
  end
53
59
 
54
60
  it 'should validate that the attribute is a subset' do
55
- @klass.new(:sub_genres => ['pop']).should be_valid
56
- @klass.new(:sub_genres => ['pop', 'rock']).should be_valid
57
- @klass.new(:sub_genres => ['pop', 'disallowed value']).should_not be_valid
61
+ @klass.new(:multi_genres => ['pop']).should be_valid
62
+ @klass.new(:multi_genres => ['pop', 'rock']).should be_valid
63
+ @klass.new(:multi_genres => ['pop', 'disallowed value']).should_not be_valid
58
64
  end
59
65
 
60
66
  it 'should not allow nil or [] for allow_blank: false' do
61
- @klass.new(:sub_genres => nil).should_not be_valid
62
- @klass.new(:sub_genres => []).should_not be_valid
67
+ @klass.new(:multi_genres => nil).should_not be_valid
68
+ @klass.new(:multi_genres => []).should_not be_valid
63
69
  end
64
70
 
65
71
  end
@@ -68,15 +74,15 @@ describe AssignableValues::ActiveRecord do
68
74
 
69
75
  before :each do
70
76
  @klass = Song.disposable_copy do
71
- assignable_values_for :sub_genres, :multiple => true, :allow_blank => true do
77
+ assignable_values_for :multi_genres, :multiple => true, :allow_blank => true do
72
78
  %w[pop rock]
73
79
  end
74
80
  end
75
81
  end
76
82
 
77
83
  it 'should allow nil or [] for allow_blank: false' do
78
- @klass.new(:sub_genres => nil).should be_valid
79
- @klass.new(:sub_genres => []).should be_valid
84
+ @klass.new(:multi_genres => nil).should be_valid
85
+ @klass.new(:multi_genres => []).should be_valid
80
86
  end
81
87
 
82
88
  end
@@ -791,11 +797,18 @@ describe AssignableValues::ActiveRecord do
791
797
  assignable_values_for :genre do
792
798
  %w[pop rock]
793
799
  end
800
+
801
+ assignable_values_for :multi_genres, :multiple => true do
802
+ %w[pop rock]
803
+ end
794
804
  end
795
- genres = klass.new.humanized_genres
805
+ genres = klass.new.humanized_assignable_genres
796
806
  genres.collect(&:value).should == ['pop', 'rock']
797
807
  genres.collect(&:humanized).should == ['Pop music', 'Rock music']
798
808
  genres.collect(&:to_s).should == ['Pop music', 'Rock music']
809
+
810
+ multi_genres = klass.new.humanized_assignable_multi_genres
811
+ multi_genres.collect(&:humanized).should == ['Pop music', 'Rock music']
799
812
  end
800
813
 
801
814
  it 'should use String#humanize as a default translation' do
@@ -804,7 +817,7 @@ describe AssignableValues::ActiveRecord do
804
817
  %w[electronic]
805
818
  end
806
819
  end
807
- klass.new.humanized_genres.collect(&:humanized).should == ['Electronic']
820
+ klass.new.humanized_assignable_genres.collect(&:humanized).should == ['Electronic']
808
821
  end
809
822
 
810
823
  it 'should allow to define humanizations for values that are not strings' do
@@ -813,7 +826,7 @@ describe AssignableValues::ActiveRecord do
813
826
  [1977, 1980, 1983]
814
827
  end
815
828
  end
816
- years = klass.new.humanized_years
829
+ years = klass.new.humanized_assignable_years
817
830
  years.collect(&:value).should == [1977, 1980, 1983]
818
831
  years.collect(&:humanized).should == ['The year a new hope was born', 'The year the Empire stroke back', 'The year the Jedi returned']
819
832
  end
@@ -824,18 +837,30 @@ describe AssignableValues::ActiveRecord do
824
837
  [1977, 1980, 1983]
825
838
  end
826
839
  end
827
- years = klass.new.humanized_years
840
+ years = klass.new.humanized_assignable_years
828
841
  years.collect(&:humanized).should == ['The year a new hope was born', 'The year the Empire stroke back', 'The year the Jedi returned']
829
842
  end
830
843
 
831
844
  context 'legacy methods for API compatibility' do
832
845
 
846
+ it 'should define a method that return pairs of values and their humanization' do
847
+ klass = Song.disposable_copy do
848
+ assignable_values_for :genre do
849
+ %w[pop rock]
850
+ end
851
+ end
852
+ ActiveSupport::Deprecation.should_receive(:warn)
853
+ genres = klass.new.humanized_genres
854
+ genres.collect(&:humanized).should == ['Pop music', 'Rock music']
855
+ end
856
+
833
857
  it "should define a method #humanized on assignable string values, which return up the value's' translation" do
834
858
  klass = Song.disposable_copy do
835
859
  assignable_values_for :genre do
836
860
  %w[pop rock]
837
861
  end
838
862
  end
863
+ ActiveSupport::Deprecation.should_receive(:warn).at_least(:once)
839
864
  klass.new.assignable_genres.collect(&:humanized).should == ['Pop music', 'Rock music']
840
865
  end
841
866
 
@@ -12,6 +12,9 @@ en:
12
12
  genre:
13
13
  pop: 'Pop music'
14
14
  rock: 'Rock music'
15
+ multi_genres:
16
+ pop: 'Pop music'
17
+ rock: 'Rock music'
15
18
  sub_genre:
16
19
  pop: 'Pop music'
17
20
  rock: 'Rock music'
@@ -8,7 +8,7 @@ class Song < ActiveRecord::Base
8
8
 
9
9
  belongs_to :artist
10
10
 
11
- attr_accessor :sub_genre, :sub_genres
11
+ attr_accessor :sub_genre, :sub_genres, :multi_genres
12
12
 
13
13
  if ActiveRecord::VERSION::MAJOR < 4 || !Song.new(:genres => ['test']).genres.is_a?(Array)
14
14
  # Rails 4 or not postgres
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: assignable_values
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.14.0
4
+ version: 0.15.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henning Koch
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-09-17 00:00:00.000000000 Z
11
+ date: 2018-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -90,7 +90,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
90
  version: '0'
91
91
  requirements: []
92
92
  rubyforge_project:
93
- rubygems_version: 2.7.6
93
+ rubygems_version: 2.7.7
94
94
  signing_key:
95
95
  specification_version: 4
96
96
  summary: Restrict the values assignable to ActiveRecord attributes or associations