mobility 0.1.14 → 0.1.15

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
  SHA1:
3
- metadata.gz: 118e3d6777198cb2f765275f22636876be026bc6
4
- data.tar.gz: f30e14ab8bbf9e3123494232c26fc41d5daa3901
3
+ metadata.gz: 94e0c57f9d1cfb688811e00504ad03d685752867
4
+ data.tar.gz: d6a95ba806b2f5773616ddb8203fd3c9645ced61
5
5
  SHA512:
6
- metadata.gz: 866b6af79e61063c29ee5b4791cbf47433fb82f53c40f7603b466e7941c43444804eb4bbda197105be8d1572a6d6dfa03d427386a38867672837ce73c85d56cb
7
- data.tar.gz: a90b4deb486ff6e23cc9d83ba6e068460cfb394cecc9c651c19ebe65b8493d1cdee01b83570de6e1d9c1d0d091fd84ee8fed4591abd4d3bd2e32ebe49ae99cf7
6
+ metadata.gz: 04691839710e519cd7a85b15419ed85a2f1f7cdacfa290248b6d4ea2010af2f46e360ae575e238eabc220910f5e2b8dc493690b8605668e70e18b9df3e296f4a
7
+ data.tar.gz: 587a828deb6b91d7de8bf4021be37ca0726907e5957f59e0638064eeb34bef48f8b2618e1a51c3a07686b7ac78df1478966162f0830de35fd012c69157e49c8b
data/CHANGELOG.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  ## 0.1
4
4
 
5
+ ### 0.1.15 (May 21, 2017)
6
+ * Add support for uniqueness validation ([#28](https://github.com/shioyama/mobility/pull/28))
7
+ * Inherit translated attributes in subclasses ([#30](https://github.com/shioyama/mobility/pull/30))
8
+
5
9
  ### 0.1.14 (April 27, 2017)
6
10
  * Reset memoized backends when duplicating ([#26](https://github.com/shioyama/mobility/issues/25))
7
11
 
data/Gemfile CHANGED
@@ -10,7 +10,8 @@ group :development, :test do
10
10
  end
11
11
 
12
12
  if ENV['ORM'] == 'sequel'
13
- gem 'sequel', '>= 4.0.0', '< 5.0'
13
+ # some interal API changes are breaking specs, limit to 4.45.x for now
14
+ gem 'sequel', '>= 4.41.0', '< 4.46.0'
14
15
  end
15
16
 
16
17
  platforms :ruby do
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mobility (0.1.13)
4
+ mobility (0.1.14)
5
5
  i18n (>= 0.6.10, < 0.9)
6
6
  request_store (~> 1.0)
7
7
 
@@ -10,7 +10,7 @@ GEM
10
10
  specs:
11
11
  byebug (9.0.6)
12
12
  coderay (1.1.1)
13
- database_cleaner (1.5.3)
13
+ database_cleaner (1.6.0)
14
14
  diff-lcs (1.3)
15
15
  ffi (1.9.18)
16
16
  formatador (0.2.5)
@@ -33,7 +33,7 @@ GEM
33
33
  rb-fsevent (~> 0.9, >= 0.9.4)
34
34
  rb-inotify (~> 0.9, >= 0.9.7)
35
35
  ruby_dep (~> 1.2)
36
- lumberjack (1.0.11)
36
+ lumberjack (1.0.12)
37
37
  method_source (0.8.2)
38
38
  mysql2 (0.3.21)
39
39
  nenv (0.3.0)
@@ -53,25 +53,25 @@ GEM
53
53
  rb-inotify (0.9.8)
54
54
  ffi (>= 0.5.0)
55
55
  request_store (1.3.2)
56
- rspec (3.5.0)
57
- rspec-core (~> 3.5.0)
58
- rspec-expectations (~> 3.5.0)
59
- rspec-mocks (~> 3.5.0)
60
- rspec-core (3.5.4)
61
- rspec-support (~> 3.5.0)
62
- rspec-expectations (3.5.0)
56
+ rspec (3.6.0)
57
+ rspec-core (~> 3.6.0)
58
+ rspec-expectations (~> 3.6.0)
59
+ rspec-mocks (~> 3.6.0)
60
+ rspec-core (3.6.0)
61
+ rspec-support (~> 3.6.0)
62
+ rspec-expectations (3.6.0)
63
63
  diff-lcs (>= 1.2.0, < 2.0)
64
- rspec-support (~> 3.5.0)
65
- rspec-mocks (3.5.0)
64
+ rspec-support (~> 3.6.0)
65
+ rspec-mocks (3.6.0)
66
66
  diff-lcs (>= 1.2.0, < 2.0)
67
- rspec-support (~> 3.5.0)
68
- rspec-support (3.5.0)
67
+ rspec-support (~> 3.6.0)
68
+ rspec-support (3.6.0)
69
69
  ruby_dep (1.5.0)
70
70
  shellany (0.0.1)
71
71
  slop (3.6.0)
72
72
  sqlite3 (1.3.13)
73
73
  thor (0.19.4)
74
- yard (0.9.8)
74
+ yard (0.9.9)
75
75
 
76
76
  PLATFORMS
77
77
  ruby
data/README.md CHANGED
@@ -46,7 +46,7 @@ Installation
46
46
  Add this line to your application's Gemfile:
47
47
 
48
48
  ```ruby
49
- gem 'mobility', '~> 0.1.14'
49
+ gem 'mobility', '~> 0.1.15'
50
50
  ```
51
51
 
52
52
  To translate attributes on a model, include (or extend) `Mobility`, then call
data/lib/mobility.rb CHANGED
@@ -92,19 +92,11 @@ module Mobility
92
92
  end
93
93
 
94
94
  class << self
95
- include Translates
95
+ include ClassMethods
96
96
 
97
97
  if translates = Mobility.config.accessor_method
98
98
  alias_method translates, :mobility_accessor
99
99
  end
100
-
101
- def mobility
102
- @mobility ||= Mobility::Wrapper.new(self)
103
- end
104
-
105
- def translated_attribute_names
106
- mobility.translated_attribute_names
107
- end
108
100
  end
109
101
  end
110
102
 
@@ -244,6 +236,22 @@ module Mobility
244
236
  end
245
237
  end
246
238
 
239
+ module ClassMethods
240
+ include Translates
241
+
242
+ def mobility
243
+ @mobility ||= Mobility::Wrapper.new(self)
244
+ end
245
+
246
+ def translated_attribute_names
247
+ mobility.translated_attribute_names
248
+ end
249
+
250
+ def inherited(subclass)
251
+ subclass.instance_variable_set(:@mobility, @mobility)
252
+ end
253
+ end
254
+
247
255
  class BackendRequired < ArgumentError; end
248
256
  class InvalidLocale < I18n::InvalidLocale; end
249
257
  end
@@ -5,11 +5,12 @@ Module loading ActiveRecord-specific classes for Mobility models.
5
5
 
6
6
  =end
7
7
  module ActiveRecord
8
- autoload :BackendResetter, "mobility/active_record/backend_resetter"
9
- autoload :ModelTranslation, "mobility/active_record/model_translation"
10
- autoload :StringTranslation, "mobility/active_record/string_translation"
11
- autoload :TextTranslation, "mobility/active_record/text_translation"
12
- autoload :Translation, "mobility/active_record/translation"
8
+ autoload :BackendResetter, "mobility/active_record/backend_resetter"
9
+ autoload :ModelTranslation, "mobility/active_record/model_translation"
10
+ autoload :StringTranslation, "mobility/active_record/string_translation"
11
+ autoload :TextTranslation, "mobility/active_record/text_translation"
12
+ autoload :Translation, "mobility/active_record/translation"
13
+ autoload :UniquenessValidator, "mobility/active_record/uniqueness_validator"
13
14
 
14
15
  def changes_applied
15
16
  @previously_changed = changes
@@ -27,6 +28,8 @@ Module loading ActiveRecord-specific classes for Mobility models.
27
28
 
28
29
  def self.included(model_class)
29
30
  model_class.extend(ClassMethods)
31
+ model_class.const_set(:UniquenessValidator,
32
+ Class.new(::Mobility::ActiveRecord::UniquenessValidator))
30
33
  end
31
34
 
32
35
  module ClassMethods
@@ -0,0 +1,35 @@
1
+ module Mobility
2
+ module ActiveRecord
3
+ class UniquenessValidator < ::ActiveRecord::Validations::UniquenessValidator
4
+ def validate_each(record, attribute, value)
5
+ klass = record.class
6
+
7
+ if ((Array(options[:scope]) + [attribute]).map(&:to_s) & klass.translated_attribute_names).present?
8
+ return unless value.present?
9
+ relation = klass.send(Mobility.query_method).where(attribute => value)
10
+ relation = relation.where.not(klass.primary_key => record.id) if record.persisted?
11
+ relation = mobility_scope_relation(record, relation)
12
+ relation = relation.merge(options[:conditions]) if options[:conditions]
13
+
14
+ if relation.exists?
15
+ error_options = options.except(:case_sensitive, :scope, :conditions)
16
+ error_options[:value] = value
17
+
18
+ record.errors.add(attribute, :taken, error_options)
19
+ end
20
+ else
21
+ super
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def mobility_scope_relation(record, relation)
28
+ Array(options[:scope]).each do |scope_item|
29
+ relation = relation.where(scope_item => record.send(scope_item))
30
+ end
31
+ relation
32
+ end
33
+ end
34
+ end
35
+ end
@@ -32,9 +32,6 @@ Internal class used by ActiveRecord backends that store values as a hash.
32
32
 
33
33
  setup do |attributes, options|
34
34
  attributes.each { |attribute| store attribute, coder: Coder }
35
- before_validation do
36
- attributes.each { |attribute| self.send(:"#{attribute}=", {}) if send(attribute).nil? }
37
- end
38
35
  end
39
36
 
40
37
  class Coder
@@ -1,3 +1,3 @@
1
1
  module Mobility
2
- VERSION = "0.1.14"
2
+ VERSION = "0.1.15"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mobility
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.14
4
+ version: 0.1.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Salzberg
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-04-27 00:00:00.000000000 Z
11
+ date: 2017-05-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: request_store
@@ -157,6 +157,7 @@ files:
157
157
  - lib/mobility/active_record/string_translation.rb
158
158
  - lib/mobility/active_record/text_translation.rb
159
159
  - lib/mobility/active_record/translation.rb
160
+ - lib/mobility/active_record/uniqueness_validator.rb
160
161
  - lib/mobility/attributes.rb
161
162
  - lib/mobility/backend.rb
162
163
  - lib/mobility/backend/active_model.rb
@@ -243,7 +244,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
243
244
  version: '0'
244
245
  requirements: []
245
246
  rubyforge_project:
246
- rubygems_version: 2.4.5.1
247
+ rubygems_version: 2.6.12
247
248
  signing_key:
248
249
  specification_version: 4
249
250
  summary: Pluggable Ruby translation framework