rails_core_extensions 0.1.1 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c0a2d1edb6ec0ac6591e211c6d2b7ac83d1a5c84
4
- data.tar.gz: 512cae180b1e0e5d33c4c5601d381fd37982f171
3
+ metadata.gz: 20b022eccf968998d9236870dd9255b5ca5eff46
4
+ data.tar.gz: a82a4adf1601c57035016df31d53d6a2fbc5e41e
5
5
  SHA512:
6
- metadata.gz: 0d2730d04532398da8a1257a0b75dbcbaf34db1f15c4ffc4677f9ce615317fd1f3ac63b5304ad39e4d3992ae1ca47412ee80acbe4fbdea5791ffb668ea318a2f
7
- data.tar.gz: dba790cfdbaaf05e1d74daa1852398787dbe41a2daaca3a7302c1bed5a6a4e71db7d2b7343c61e32de84aded19973bbefcfa90000db51ad4e160474b34edb4fb
6
+ metadata.gz: 53bfc4fb4f56485453989c62428fb370748e6cda40bd082ce98e21a544518bc5354b70cc6b3294b59f897664e08b146e21bd6ce385a64880e10a3184649ad0bc
7
+ data.tar.gz: d1d3a4637dff550333f1eba6fb3ae7ca9d19bfa6aad31f8deb4344ab60a9e99ac519b139e356ee9e203f4aa9a2ec98a707774d4c6b1841780f74000574194c64
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.2.0
4
+
5
+ * Extract and test translations
6
+
3
7
  ## 0.1.1
4
8
 
5
9
  * Fixes issue with sortable controllers
@@ -27,11 +27,13 @@ module RailsCoreExtensions
27
27
  require 'rails_core_extensions/active_record_cache_all_attributes'
28
28
  require 'rails_core_extensions/active_record_extensions'
29
29
  require 'rails_core_extensions/active_record_liquid_extensions'
30
+ require 'rails_core_extensions/translations'
30
31
 
31
32
  ActiveRecord::Base.send(:include, ActiveRecordCloning)
32
33
  ActiveRecord::Base.send(:include, ActiveRecordExtensions)
33
34
  ActiveRecord::Base.send(:include, RailsCoreExtensions::ActiveRecordLiquidExtensions)
34
35
  ActiveRecord::Base.send(:include, ActiveRecordExtensions::InstanceMethods)
36
+ ActiveRecord::Base.send(:include, RailsCoreExtensions::Translations)
35
37
 
36
38
  if ActiveRecord::VERSION::MAJOR >= 3
37
39
  require 'rails_core_extensions/active_model_extensions'
@@ -144,16 +144,6 @@ module ActiveRecordExtensions
144
144
  # longer alive.
145
145
  ActiveRecord::Base.clear_active_connections!
146
146
  end
147
-
148
- def translate(key, options = {})
149
- klass = self
150
- klass = klass.superclass while klass.superclass != ActiveRecord::Base
151
- I18n.translate key, options.merge(:scope => klass.name.tableize.singularize)
152
- end
153
-
154
- def t(key, options = {})
155
- self.translate(key, options)
156
- end
157
147
  end
158
148
 
159
149
  module InstanceMethods
@@ -184,11 +174,5 @@ module ActiveRecordExtensions
184
174
  def audit_log
185
175
  return (self.methods.include?('audits') ? self.audits : [])
186
176
  end
187
-
188
- private
189
-
190
- def t(key, options = {})
191
- self.class.translate(key, options)
192
- end
193
177
  end
194
178
  end
@@ -0,0 +1,32 @@
1
+ module RailsCoreExtensions
2
+ module Translations
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ def translate(key, options = {})
9
+ I18n.translate key, options.merge(scope: translation_key)
10
+ end
11
+
12
+ def translation_key
13
+ @translation_key ||= base_translation_class.name.tableize.singularize.gsub('/', '.')
14
+ end
15
+
16
+ def base_translation_class
17
+ return base_class if defined?(ActiveRecord) && ancestors.include?(ActiveRecord::Base)
18
+ self
19
+ end
20
+
21
+ def t(key, options = {})
22
+ self.translate(key, options)
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def t(key, options = {})
29
+ self.class.translate(key, options)
30
+ end
31
+ end
32
+ end
@@ -1,3 +1,3 @@
1
1
  module RailsCoreExtensions
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -0,0 +1,8 @@
1
+ en:
2
+ translation_model:
3
+ display_me: 'My name is %{name}'
4
+ translation:
5
+ sub_model:
6
+ display_me: 'My subname is %{name}'
7
+ party:
8
+ display_me: 'Party %{name}'
@@ -6,6 +6,10 @@ ActiveRecord::Schema.define(:version => 1) do
6
6
  t.integer :category_id
7
7
  end
8
8
 
9
+ create_table :parties do |t|
10
+ t.string :name, :type
11
+ end
12
+
9
13
  create_table :parents do |t|
10
14
  end
11
15
 
@@ -13,6 +13,7 @@ require 'action_controller'
13
13
  require 'active_record'
14
14
 
15
15
  require 'rails_core_extensions'
16
+ I18n.load_path << [File.expand_path('../en.yml', __FILE__)]
16
17
 
17
18
  RSpec.configure do |config|
18
19
  config.raise_errors_for_deprecations!
@@ -1,4 +1,4 @@
1
- MINIMUM_COVERAGE = 79.4
1
+ MINIMUM_COVERAGE = 81.1
2
2
 
3
3
  unless ENV['COVERAGE'] == 'off'
4
4
  require 'simplecov'
@@ -18,8 +18,9 @@ unless ENV['COVERAGE'] == 'off'
18
18
  SimpleCov.at_exit do
19
19
  SimpleCov.result.format!
20
20
  percent = SimpleCov.result.covered_percent
21
+ puts "Coverage is #{"%.2f" % percent}%"
21
22
  unless percent >= MINIMUM_COVERAGE
22
- puts "Coverage must be above #{MINIMUM_COVERAGE}%. It is #{"%.2f" % percent}%"
23
+ puts "Coverage must be above #{MINIMUM_COVERAGE}%"
23
24
  Kernel.exit(1)
24
25
  end
25
26
  end
@@ -0,0 +1,104 @@
1
+ require 'spec_helper'
2
+ require 'i18n'
3
+
4
+ describe 'translations' do
5
+ let(:model_class) {
6
+ Class.new do
7
+ include RailsCoreExtensions::Translations
8
+
9
+ def initialize(name)
10
+ @name = name
11
+ end
12
+
13
+ def to_s
14
+ t('display_me', name: @name)
15
+ end
16
+ end
17
+ }
18
+
19
+ before do
20
+ stub_const 'TranslationModel', model_class
21
+ end
22
+
23
+ let(:class_translation) { TranslationModel.t('display_me', name: 'Class') }
24
+ subject { TranslationModel.new('Ruby') }
25
+
26
+ specify { expect(TranslationModel.translation_key).to eq 'translation_model' }
27
+ specify { expect(subject.to_s).to eq 'My name is Ruby' }
28
+ specify { expect(class_translation).to eq 'My name is Class' }
29
+
30
+ context 'performance' do
31
+ before do
32
+ allow(TranslationModel).to receive(:base_translation_class).and_call_original
33
+ 5.times { subject.to_s }
34
+ end
35
+
36
+ specify {
37
+ expect(TranslationModel).to have_received(:base_translation_class).once
38
+ }
39
+ end
40
+
41
+ context 'non AR subclass' do
42
+ let(:model_subclass) {
43
+ Class.new(model_class)
44
+ }
45
+
46
+ before do
47
+ stub_const 'Translation::SubModel', model_subclass
48
+ end
49
+
50
+ let(:class_translation) { Translation::SubModel.t('display_me', name: 'Class') }
51
+ subject { Translation::SubModel.new('Ruby') }
52
+
53
+ specify { expect(Translation::SubModel.translation_key).to eq 'translation.sub_model' }
54
+ specify { expect(subject.to_s).to eq 'My subname is Ruby' }
55
+ specify { expect(class_translation).to eq 'My subname is Class' }
56
+ end
57
+
58
+ context 'AR base class' do
59
+ let(:ar_model_class) {
60
+ Class.new(ActiveRecord::Base) do
61
+ self.table_name = 'parties'
62
+
63
+ include RailsCoreExtensions::Translations
64
+
65
+ def to_s
66
+ t('display_me', name: name)
67
+ end
68
+ end
69
+ }
70
+ let(:ar_model_subclass) {
71
+ Class.new(Party) do
72
+ self.table_name = 'parties'
73
+
74
+ include RailsCoreExtensions::Translations
75
+
76
+ def to_s
77
+ t('display_me', name: name)
78
+ end
79
+ end
80
+ }
81
+
82
+ before do
83
+ connect_to_sqlite
84
+ stub_const 'Party', ar_model_class
85
+ stub_const 'Person', ar_model_subclass
86
+ end
87
+
88
+ let(:class_translation) { Party.t('display_me', name: 'Class') }
89
+ subject { Party.new(name: 'Ruby') }
90
+
91
+ specify { expect(Party.translation_key).to eq 'party' }
92
+ specify { expect(subject.to_s).to eq 'Party Ruby' }
93
+ specify { expect(class_translation).to eq 'Party Class' }
94
+
95
+ context 'AR sub class' do
96
+ let(:class_translation) { Person.t('display_me', name: 'Class') }
97
+ subject { Person.new(name: 'Ruby') }
98
+
99
+ specify { expect(Person.translation_key).to eq 'party' }
100
+ specify { expect(subject.to_s).to eq 'Party Ruby' }
101
+ specify { expect(class_translation).to eq 'Party Class' }
102
+ end
103
+ end
104
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_core_extensions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Noack
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-08-01 00:00:00.000000000 Z
12
+ date: 2016-08-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -234,6 +234,7 @@ files:
234
234
  - lib/rails_core_extensions/tasks/position_initializer.rake
235
235
  - lib/rails_core_extensions/time_with_zone.rb
236
236
  - lib/rails_core_extensions/transfer_records.rb
237
+ - lib/rails_core_extensions/translations.rb
237
238
  - lib/rails_core_extensions/version.rb
238
239
  - rails_core_extensions.gemspec
239
240
  - spec/action_controller_sortable_spec.rb
@@ -243,12 +244,14 @@ files:
243
244
  - spec/active_record_extensions_spec.rb
244
245
  - spec/breadcrumb_spec.rb
245
246
  - spec/concurrency_spec.rb
247
+ - spec/en.yml
246
248
  - spec/position_initializer_spec.rb
247
249
  - spec/schema.rb
248
250
  - spec/spec_helper.rb
249
251
  - spec/spec_helper_model_base.rb
250
252
  - spec/support/coverage_loader.rb
251
253
  - spec/transfer_records_spec.rb
254
+ - spec/translation_spec.rb
252
255
  homepage: http://github.com/sealink/rails_core_extensions
253
256
  licenses:
254
257
  - MIT
@@ -269,7 +272,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
269
272
  version: '0'
270
273
  requirements: []
271
274
  rubyforge_project:
272
- rubygems_version: 2.4.5.1
275
+ rubygems_version: 2.4.8
273
276
  signing_key:
274
277
  specification_version: 4
275
278
  summary: Set of extensions to core rails libraries.
@@ -281,9 +284,11 @@ test_files:
281
284
  - spec/active_record_extensions_spec.rb
282
285
  - spec/breadcrumb_spec.rb
283
286
  - spec/concurrency_spec.rb
287
+ - spec/en.yml
284
288
  - spec/position_initializer_spec.rb
285
289
  - spec/schema.rb
286
290
  - spec/spec_helper.rb
287
291
  - spec/spec_helper_model_base.rb
288
292
  - spec/support/coverage_loader.rb
289
293
  - spec/transfer_records_spec.rb
294
+ - spec/translation_spec.rb