assignable_values 0.12.1 → 0.13.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.
- checksums.yaml +7 -0
- data/.travis.yml +1 -0
- data/Gemfile +1 -0
- data/Gemfile.lock +1 -0
- data/README.md +0 -10
- data/gemfiles/Gemfile.2.3.lock +2 -2
- data/gemfiles/Gemfile.3.2.lock +2 -2
- data/gemfiles/Gemfile.4.2.lock +2 -2
- data/gemfiles/Gemfile.5.0.lock +2 -2
- data/lib/assignable_values/active_record/restriction/base.rb +4 -13
- data/lib/assignable_values/active_record/restriction/scalar_attribute.rb +7 -21
- data/lib/assignable_values/version.rb +1 -1
- data/spec/assignable_values/active_record_spec.rb +12 -30
- metadata +36 -57
- data/Gemfile +0 -16
- data/Gemfile.lock +0 -70
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 862c6116c78266cdb897dccb736eb3a8f52c1059
|
4
|
+
data.tar.gz: b7824b4245a5a1bd369ff4ed1e30bcacb64d47f9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a6107dd6fb457c1a7d862086858d5d32b91432102738695f258f646f851143e7d4111c9dfa424de0ada4cc4689443e488555ad7ab6d76a777118e9328513b07f
|
7
|
+
data.tar.gz: 573fb9aae2cfa6331328f7953c02e5ed8deae65e8633c50c9af05295da428df0a9fb24025491056c4ee17cdd15e938617cf5cd4b7d1cbe824fb629a31668c8dd
|
data/.travis.yml
CHANGED
@@ -36,6 +36,7 @@ notifications:
|
|
36
36
|
|
37
37
|
before_script:
|
38
38
|
- mysql -e 'create database IF NOT EXISTS assignable_values_test;'
|
39
|
+
- mysql -e "GRANT ALL PRIVILEGES ON assignable_values_test.* TO 'travis'@'%';"
|
39
40
|
|
40
41
|
install:
|
41
42
|
# Old Travis CI bundler explodes when lockfile version doesn't match recently bumped version
|
data/Gemfile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
./gemfiles/Gemfile.4.2
|
data/Gemfile.lock
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
./gemfiles/Gemfile.4.2.lock
|
data/README.md
CHANGED
@@ -73,16 +73,6 @@ A good way to populate a `<select>` tag with pairs of internal values and human
|
|
73
73
|
|
74
74
|
form.collection_select :genre, form.object.humanized_genres, :value, :humanized
|
75
75
|
|
76
|
-
If you don't like to use your I18n dictionary for humanizations, you can also declare them directly in your model like this:
|
77
|
-
|
78
|
-
class Song < ActiveRecord::Base
|
79
|
-
assignable_values_for :genre do
|
80
|
-
{ 'pop' => 'Pop music',
|
81
|
-
'rock' => 'Rock music',
|
82
|
-
'electronic' => 'Electronic music' }
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
76
|
|
87
77
|
### Defining default values
|
88
78
|
|
data/gemfiles/Gemfile.2.3.lock
CHANGED
data/gemfiles/Gemfile.3.2.lock
CHANGED
data/gemfiles/Gemfile.4.2.lock
CHANGED
data/gemfiles/Gemfile.5.0.lock
CHANGED
@@ -49,8 +49,7 @@ module AssignableValues
|
|
49
49
|
|
50
50
|
def assignable_values(record, options = {})
|
51
51
|
assignable_values = []
|
52
|
-
|
53
|
-
current_values = parsed_values.delete(:values)
|
52
|
+
current_values = assignable_values_from_record_or_delegate(record)
|
54
53
|
|
55
54
|
if options.fetch(:include_old_value, true) && has_previously_saved_value?(record)
|
56
55
|
old_value = previously_saved_value(record)
|
@@ -60,9 +59,6 @@ module AssignableValues
|
|
60
59
|
end
|
61
60
|
|
62
61
|
assignable_values += current_values
|
63
|
-
parsed_values.each do |meta_name, meta_content|
|
64
|
-
assignable_values.singleton_class.send(:define_method, meta_name) { meta_content }
|
65
|
-
end
|
66
62
|
if options[:decorate]
|
67
63
|
assignable_values = decorate_values(assignable_values)
|
68
64
|
end
|
@@ -97,10 +93,6 @@ module AssignableValues
|
|
97
93
|
end
|
98
94
|
end
|
99
95
|
|
100
|
-
def parse_values(values)
|
101
|
-
{ :values => Array(values) }
|
102
|
-
end
|
103
|
-
|
104
96
|
def current_value(record)
|
105
97
|
record.send(property)
|
106
98
|
end
|
@@ -202,13 +194,12 @@ module AssignableValues
|
|
202
194
|
end
|
203
195
|
end
|
204
196
|
|
205
|
-
def
|
197
|
+
def assignable_values_from_record_or_delegate(record)
|
206
198
|
if delegate?
|
207
|
-
|
199
|
+
assignable_values_from_delegate(record).to_a
|
208
200
|
else
|
209
|
-
|
201
|
+
record.instance_exec(&@values).to_a
|
210
202
|
end
|
211
|
-
parse_values(values)
|
212
203
|
end
|
213
204
|
|
214
205
|
def delegate(record)
|
@@ -10,21 +10,17 @@ module AssignableValues
|
|
10
10
|
define_humanized_values_instance_method
|
11
11
|
end
|
12
12
|
|
13
|
-
def humanized_value(
|
13
|
+
def humanized_value(value)
|
14
14
|
if value.present?
|
15
|
-
|
16
|
-
|
17
|
-
else
|
18
|
-
dictionary_scope = "assignable_values.#{model.name.underscore}.#{property}"
|
19
|
-
I18n.t(value, :scope => dictionary_scope, :default => default_humanization_for_value(value))
|
20
|
-
end
|
15
|
+
dictionary_scope = "assignable_values.#{model.name.underscore}.#{property}"
|
16
|
+
I18n.t(value, :scope => dictionary_scope, :default => default_humanization_for_value(value))
|
21
17
|
end
|
22
18
|
end
|
23
19
|
|
24
20
|
def humanized_values(record)
|
25
21
|
values = assignable_values(record)
|
26
22
|
values.collect do |value|
|
27
|
-
HumanizedValue.new(value, humanized_value(
|
23
|
+
HumanizedValue.new(value, humanized_value(value))
|
28
24
|
end
|
29
25
|
end
|
30
26
|
|
@@ -38,20 +34,11 @@ module AssignableValues
|
|
38
34
|
end
|
39
35
|
end
|
40
36
|
|
41
|
-
def parse_values(values)
|
42
|
-
if values.is_a?(Hash)
|
43
|
-
{ :values => values.keys,
|
44
|
-
:humanizations => values }
|
45
|
-
else
|
46
|
-
super
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
37
|
def define_humanized_value_class_method
|
51
38
|
restriction = self
|
52
39
|
enhance_model_singleton do
|
53
40
|
define_method :"humanized_#{restriction.property}" do |given_value|
|
54
|
-
restriction.humanized_value(
|
41
|
+
restriction.humanized_value(given_value)
|
55
42
|
end
|
56
43
|
end
|
57
44
|
end
|
@@ -60,10 +47,9 @@ module AssignableValues
|
|
60
47
|
restriction = self
|
61
48
|
enhance_model do
|
62
49
|
define_method :"humanized_#{restriction.property}" do |*args|
|
63
|
-
values = restriction.assignable_values(self)
|
64
50
|
given_value = args[0]
|
65
51
|
value = given_value || send(restriction.property)
|
66
|
-
restriction.humanized_value(
|
52
|
+
restriction.humanized_value(value)
|
67
53
|
end
|
68
54
|
end
|
69
55
|
end
|
@@ -81,7 +67,7 @@ module AssignableValues
|
|
81
67
|
restriction = self
|
82
68
|
values.collect do |value|
|
83
69
|
if value.is_a?(String)
|
84
|
-
humanization = restriction.humanized_value(
|
70
|
+
humanization = restriction.humanized_value(value)
|
85
71
|
value = HumanizableString.new(value, humanization)
|
86
72
|
end
|
87
73
|
value
|
@@ -373,14 +373,20 @@ describe AssignableValues::ActiveRecord do
|
|
373
373
|
klass.new.assignable_years.should == [1977, 1980, 1983]
|
374
374
|
end
|
375
375
|
|
376
|
-
|
377
|
-
klass
|
378
|
-
|
379
|
-
|
380
|
-
nil
|
376
|
+
context 'when the delegation method returns nil' do
|
377
|
+
let(:klass) do
|
378
|
+
Song.disposable_copy do
|
379
|
+
assignable_values_for :genre, :through => proc { nil }
|
381
380
|
end
|
382
381
|
end
|
383
|
-
|
382
|
+
|
383
|
+
it 'should skip the validation' do
|
384
|
+
klass.new(:genre => 'pop').should be_valid
|
385
|
+
end
|
386
|
+
|
387
|
+
it 'should still be able to humanize values on the instance' do
|
388
|
+
klass.new(:genre => 'pop').humanized_genre.should == 'Pop music'
|
389
|
+
end
|
384
390
|
end
|
385
391
|
|
386
392
|
end
|
@@ -652,30 +658,6 @@ describe AssignableValues::ActiveRecord do
|
|
652
658
|
years.collect(&:humanized).should == ['The year a new hope was born', 'The year the Empire stroke back', 'The year the Jedi returned']
|
653
659
|
end
|
654
660
|
|
655
|
-
context 'hardcoded humanizations' do
|
656
|
-
|
657
|
-
it 'should allow to directly declare humanized values by passing a hash to assignable_values_for' do
|
658
|
-
klass = Song.disposable_copy do
|
659
|
-
assignable_values_for :genre do
|
660
|
-
{ 'pop' => 'Pop music', 'rock' => 'Rock music' }
|
661
|
-
end
|
662
|
-
end
|
663
|
-
klass.new.humanized_genres.collect(&:humanized).sort.should =~ ['Pop music', 'Rock music']
|
664
|
-
end
|
665
|
-
|
666
|
-
it "should correctly humanize values if the humanizations were declared using a hash, the values are not strings, and the list of humanized values hasn't been called before (bugfix)" do
|
667
|
-
klass = Song.disposable_copy do
|
668
|
-
assignable_values_for :duration do
|
669
|
-
{ 60 => '1:00',
|
670
|
-
90 => '1:30' }
|
671
|
-
end
|
672
|
-
end
|
673
|
-
klass.new(:duration => 60).humanized_duration.should == '1:00'
|
674
|
-
klass.new(:duration => 90).humanized_duration.should == '1:30'
|
675
|
-
end
|
676
|
-
|
677
|
-
end
|
678
|
-
|
679
661
|
it 'should properly look up humanizations for namespaced models' do
|
680
662
|
klass = Recording::Vinyl.disposable_copy do
|
681
663
|
assignable_values_for :year do
|
metadata
CHANGED
@@ -1,50 +1,39 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: assignable_values
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 12
|
9
|
-
- 1
|
10
|
-
version: 0.12.1
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.13.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
|
-
|
19
|
-
|
20
|
-
- !ruby/object:Gem::Dependency
|
11
|
+
date: 2017-09-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
21
14
|
name: activerecord
|
22
|
-
|
23
|
-
|
24
|
-
none: false
|
25
|
-
requirements:
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
26
17
|
- - ">="
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
|
29
|
-
segments:
|
30
|
-
- 2
|
31
|
-
- 3
|
32
|
-
version: "2.3"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.3'
|
33
20
|
type: :runtime
|
34
|
-
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.3'
|
35
27
|
description: Restrict the values assignable to ActiveRecord attributes or associations
|
36
28
|
email: henning.koch@makandra.de
|
37
29
|
executables: []
|
38
|
-
|
39
30
|
extensions: []
|
40
|
-
|
41
31
|
extra_rdoc_files: []
|
42
|
-
|
43
|
-
|
44
|
-
- .
|
45
|
-
- .
|
46
|
-
- .
|
47
|
-
- .travis.yml
|
32
|
+
files:
|
33
|
+
- ".gitignore"
|
34
|
+
- ".rspec"
|
35
|
+
- ".ruby-version"
|
36
|
+
- ".travis.yml"
|
48
37
|
- Gemfile
|
49
38
|
- Gemfile.lock
|
50
39
|
- LICENSE
|
@@ -77,39 +66,30 @@ files:
|
|
77
66
|
- spec/support/i18n.yml
|
78
67
|
- spec/support/models.rb
|
79
68
|
homepage: https://github.com/makandra/assignable_values
|
80
|
-
licenses:
|
69
|
+
licenses:
|
81
70
|
- MIT
|
71
|
+
metadata: {}
|
82
72
|
post_install_message:
|
83
73
|
rdoc_options: []
|
84
|
-
|
85
|
-
require_paths:
|
74
|
+
require_paths:
|
86
75
|
- lib
|
87
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
-
|
89
|
-
requirements:
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
90
78
|
- - ">="
|
91
|
-
- !ruby/object:Gem::Version
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
version: "0"
|
96
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
-
none: false
|
98
|
-
requirements:
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
99
83
|
- - ">="
|
100
|
-
- !ruby/object:Gem::Version
|
101
|
-
|
102
|
-
segments:
|
103
|
-
- 0
|
104
|
-
version: "0"
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
105
86
|
requirements: []
|
106
|
-
|
107
87
|
rubyforge_project:
|
108
|
-
rubygems_version:
|
88
|
+
rubygems_version: 2.6.13
|
109
89
|
signing_key:
|
110
|
-
specification_version:
|
90
|
+
specification_version: 4
|
111
91
|
summary: Restrict the values assignable to ActiveRecord attributes or associations
|
112
|
-
test_files:
|
92
|
+
test_files:
|
113
93
|
- spec/assignable_values/active_record_spec.rb
|
114
94
|
- spec/assignable_values/humanized_value_spec.rb
|
115
95
|
- spec/spec_helper.rb
|
@@ -118,4 +98,3 @@ test_files:
|
|
118
98
|
- spec/support/database.travis.yml
|
119
99
|
- spec/support/i18n.yml
|
120
100
|
- spec/support/models.rb
|
121
|
-
has_rdoc:
|
data/Gemfile
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
# Runtime dependencies
|
4
|
-
gem 'activerecord', '~>4.2.0'
|
5
|
-
gem 'i18n', '~>0.7.0'
|
6
|
-
gem 'mysql2', '~> 0.3.17'
|
7
|
-
|
8
|
-
# Development dependencies
|
9
|
-
gem 'rake'
|
10
|
-
gem 'database_cleaner'
|
11
|
-
gem 'rspec', '~> 3.4'
|
12
|
-
gem 'rspec_candy'
|
13
|
-
gem 'gemika'
|
14
|
-
|
15
|
-
# Gem under test
|
16
|
-
gem 'assignable_values', :path => '..'
|
data/Gemfile.lock
DELETED
@@ -1,70 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: ..
|
3
|
-
specs:
|
4
|
-
assignable_values (0.12.1)
|
5
|
-
activerecord (>= 2.3)
|
6
|
-
|
7
|
-
GEM
|
8
|
-
remote: https://rubygems.org/
|
9
|
-
specs:
|
10
|
-
activemodel (4.2.7.1)
|
11
|
-
activesupport (= 4.2.7.1)
|
12
|
-
builder (~> 3.1)
|
13
|
-
activerecord (4.2.7.1)
|
14
|
-
activemodel (= 4.2.7.1)
|
15
|
-
activesupport (= 4.2.7.1)
|
16
|
-
arel (~> 6.0)
|
17
|
-
activesupport (4.2.7.1)
|
18
|
-
i18n (~> 0.7)
|
19
|
-
json (~> 1.7, >= 1.7.7)
|
20
|
-
minitest (~> 5.1)
|
21
|
-
thread_safe (~> 0.3, >= 0.3.4)
|
22
|
-
tzinfo (~> 1.1)
|
23
|
-
arel (6.0.3)
|
24
|
-
builder (3.2.2)
|
25
|
-
database_cleaner (1.5.3)
|
26
|
-
diff-lcs (1.2.5)
|
27
|
-
gemika (0.3.2)
|
28
|
-
i18n (0.7.0)
|
29
|
-
json (1.8.3)
|
30
|
-
minitest (5.9.1)
|
31
|
-
mysql2 (0.3.20)
|
32
|
-
rake (10.3.2)
|
33
|
-
rspec (3.5.0)
|
34
|
-
rspec-core (~> 3.5.0)
|
35
|
-
rspec-expectations (~> 3.5.0)
|
36
|
-
rspec-mocks (~> 3.5.0)
|
37
|
-
rspec-core (3.5.4)
|
38
|
-
rspec-support (~> 3.5.0)
|
39
|
-
rspec-expectations (3.5.0)
|
40
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
41
|
-
rspec-support (~> 3.5.0)
|
42
|
-
rspec-mocks (3.5.0)
|
43
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
44
|
-
rspec-support (~> 3.5.0)
|
45
|
-
rspec-support (3.5.0)
|
46
|
-
rspec_candy (0.3.1)
|
47
|
-
rspec
|
48
|
-
sneaky-save
|
49
|
-
sneaky-save (0.0.5)
|
50
|
-
activerecord (>= 3.2.0)
|
51
|
-
thread_safe (0.3.5)
|
52
|
-
tzinfo (1.2.2)
|
53
|
-
thread_safe (~> 0.1)
|
54
|
-
|
55
|
-
PLATFORMS
|
56
|
-
ruby
|
57
|
-
|
58
|
-
DEPENDENCIES
|
59
|
-
activerecord (~> 4.2.0)
|
60
|
-
assignable_values!
|
61
|
-
database_cleaner
|
62
|
-
gemika
|
63
|
-
i18n (~> 0.7.0)
|
64
|
-
mysql2 (~> 0.3.17)
|
65
|
-
rake
|
66
|
-
rspec (~> 3.4)
|
67
|
-
rspec_candy
|
68
|
-
|
69
|
-
BUNDLED WITH
|
70
|
-
1.12.5
|