acts_as_read_only_i18n_localised 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -0
- data/Gemfile.lock +12 -2
- data/README.md +9 -7
- data/lib/acts_as_read_only_i18n_localised.rb +21 -8
- data/lib/acts_as_read_only_i18n_localised/version.rb +1 -1
- data/spec/acts_as_read_only_i18n_localised_spec.rb +34 -6
- data/spec/db_helper.rb +10 -0
- data/spec/spec_helper.rb +0 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ab853364bad7142e6f776a822a36766f25612b4a
|
4
|
+
data.tar.gz: c11eb5939d0a4e35887cde2e2e6f189cd5e5ca4f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 73cac04060acfa4c3785f1c9838db1f8c1b7dc891a0659a6ed219781205b1cadc371a12aedf8382016cb713caded0e729f293daf9e80b83df4f78097bedd9fae
|
7
|
+
data.tar.gz: 26294b91c0ce9e85b3a56442fdf38f0c95380df5ec594d261fdb697885c6880ab53cd7c9e0224580a617ca086ddeeeb74cae1f293a60872ba9ab6979314dce28
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
acts_as_read_only_i18n_localised (0.0.
|
4
|
+
acts_as_read_only_i18n_localised (0.0.3)
|
5
5
|
i18n
|
6
6
|
|
7
7
|
GEM
|
@@ -10,10 +10,14 @@ GEM
|
|
10
10
|
binding_of_caller (0.7.2)
|
11
11
|
debug_inspector (>= 0.0.1)
|
12
12
|
byebug (8.2.5)
|
13
|
+
codeclimate-test-reporter (0.5.0)
|
14
|
+
simplecov (>= 0.7.1, < 1.0.0)
|
13
15
|
coderay (1.1.1)
|
14
16
|
debug_inspector (0.0.2)
|
15
17
|
diff-lcs (1.2.5)
|
18
|
+
docile (1.1.5)
|
16
19
|
i18n (0.7.0)
|
20
|
+
json (1.8.3)
|
17
21
|
method_source (0.8.2)
|
18
22
|
pry (0.10.3)
|
19
23
|
coderay (~> 1.1.0)
|
@@ -39,6 +43,11 @@ GEM
|
|
39
43
|
diff-lcs (>= 1.2.0, < 2.0)
|
40
44
|
rspec-support (~> 3.4.0)
|
41
45
|
rspec-support (3.4.1)
|
46
|
+
simplecov (0.11.2)
|
47
|
+
docile (~> 1.1.0)
|
48
|
+
json (~> 1.8)
|
49
|
+
simplecov-html (~> 0.10.0)
|
50
|
+
simplecov-html (0.10.0)
|
42
51
|
slop (3.6.0)
|
43
52
|
|
44
53
|
PLATFORMS
|
@@ -47,6 +56,7 @@ PLATFORMS
|
|
47
56
|
DEPENDENCIES
|
48
57
|
acts_as_read_only_i18n_localised!
|
49
58
|
binding_of_caller
|
59
|
+
codeclimate-test-reporter
|
50
60
|
pry
|
51
61
|
pry-byebug
|
52
62
|
pry-remote
|
@@ -54,4 +64,4 @@ DEPENDENCIES
|
|
54
64
|
rspec
|
55
65
|
|
56
66
|
BUNDLED WITH
|
57
|
-
1.
|
67
|
+
1.12.3
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
A variant on the `acts_as_localized` theme for when you have static seed data in your system that must be localised.
|
4
4
|
|
5
|
-
[![Build Status](https://travis-ci.org/davesag/acts_as_read_only_i18n_localised.svg?branch=master)](https://travis-ci.org/davesag/acts_as_read_only_i18n_localised) [![Code Climate](https://codeclimate.com/github/davesag/acts_as_read_only_i18n_localised/badges/gpa.svg)](https://codeclimate.com/github/davesag/acts_as_read_only_i18n_localised)
|
5
|
+
[![Build Status](https://travis-ci.org/davesag/acts_as_read_only_i18n_localised.svg?branch=master)](https://travis-ci.org/davesag/acts_as_read_only_i18n_localised) [![Code Climate](https://codeclimate.com/github/davesag/acts_as_read_only_i18n_localised/badges/gpa.svg)](https://codeclimate.com/github/davesag/acts_as_read_only_i18n_localised) [![Test Coverage](https://codeclimate.com/github/davesag/acts_as_read_only_i18n_localised/badges/coverage.svg)](https://codeclimate.com/github/davesag/acts_as_read_only_i18n_localised/coverage)
|
6
6
|
|
7
7
|
## Why use it?
|
8
8
|
|
@@ -36,7 +36,7 @@ In `app/models/category.rb`
|
|
36
36
|
|
37
37
|
class Category < ActiveRecord::Base
|
38
38
|
include ActsAsReadOnlyI18nLocalised
|
39
|
-
validates :slug, format: {with:
|
39
|
+
validates :slug, format: {with: /\A[a-z]+[-?[a-z]]\z/},
|
40
40
|
uniqueness: true,
|
41
41
|
presence: true
|
42
42
|
has_many :products
|
@@ -48,7 +48,7 @@ In `app/models/category.rb`
|
|
48
48
|
This simply generates appropriate `name` and `description` methods along the lines of
|
49
49
|
|
50
50
|
def name
|
51
|
-
key = "#{self.
|
51
|
+
key = "#{self.table_name}.#{slug}.name".downcase.to_sym
|
52
52
|
return I18n.t(key)
|
53
53
|
end
|
54
54
|
|
@@ -56,27 +56,29 @@ with the effect that a call to `category.name` will always return the localised
|
|
56
56
|
|
57
57
|
Depending on how your code is configured, `I18n` will raise a `MissingTranslationData` exception if the key does correspond to any data. Exceptions on missing keys is usually turned on in `development` and `test` but not on `staging` or `production`. See The [Rails I18n Guide](http://guides.rubyonrails.org/i18n.html) for more.
|
58
58
|
|
59
|
+
*Note*: `acts_as_read_only_i18n_localised` will also work with non `active-record` classes. If there is no `self.table_name` method it will check to see if the `class.name` responds to `pluralize`, and use that if it can, otherwise it will just use the `class.name`.
|
60
|
+
|
59
61
|
### A more complex example
|
60
62
|
|
61
63
|
Say your `categories` have their own sub `categories`. Building on the previous example you might define the following
|
62
64
|
|
63
65
|
class Category < ActiveRecord::Base
|
64
66
|
include ActsAsReadOnlyI18nLocalised
|
65
|
-
validates :slug, format: {with:
|
67
|
+
validates :slug, format: {with: /\A[a-z]+[-?[a-z]]\z/},
|
66
68
|
uniqueness: true,
|
67
69
|
presence: true
|
68
70
|
has_many :products
|
69
71
|
validates_associated :products
|
70
72
|
|
71
|
-
has_many :
|
72
|
-
belongs_to :parent, class_name: 'Category
|
73
|
+
has_many :categories, foreign_key: :parent_id
|
74
|
+
belongs_to :parent, class_name: 'Category'
|
73
75
|
|
74
76
|
acts_as_read_only_i18n_localised :name, :description
|
75
77
|
use_custom_slug :slug_maker
|
76
78
|
|
77
79
|
def slug_maker
|
78
80
|
reurn slug if parent.nil?
|
79
|
-
"#{@parent.slug}.
|
81
|
+
"#{@parent.slug}.categories.#{slug}"
|
80
82
|
end
|
81
83
|
end
|
82
84
|
|
@@ -22,26 +22,39 @@ module ActsAsReadOnlyI18nLocalised
|
|
22
22
|
# Standard Ruby idiom for auto-adding class methods
|
23
23
|
#
|
24
24
|
module ClassMethods
|
25
|
-
|
26
|
-
def acts_as_read_only_i18n_localised(*attributes)
|
25
|
+
def _inject_standard_slug
|
27
26
|
unless methods.include?(:custom_slug)
|
28
27
|
define_method :custom_slug do
|
29
28
|
send(:slug) if respond_to?(:slug)
|
30
29
|
end
|
31
30
|
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def _inject_root_name
|
34
|
+
define_method :_root_name do
|
35
|
+
return table_name if respond_to?(:table_name)
|
36
|
+
root_name = self.class.name.gsub(/::/, '/')
|
37
|
+
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
|
38
|
+
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
|
39
|
+
.tr('-', '_')
|
40
|
+
return root_name.pluralize if root_name.respond_to?(:pluralize)
|
41
|
+
root_name
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def acts_as_read_only_i18n_localised(*attributes)
|
46
|
+
_inject_standard_slug
|
47
|
+
_inject_root_name
|
32
48
|
|
33
49
|
attributes.each do |attribute|
|
34
50
|
define_method attribute do
|
35
|
-
I18n.t("#{
|
36
|
-
|
37
|
-
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
|
38
|
-
.tr('-', '_')}.#{send(:custom_slug)}.#{attribute}"
|
39
|
-
.downcase.to_sym)
|
51
|
+
return I18n.t("#{_root_name}.#{send(:custom_slug)}.#{attribute}"
|
52
|
+
.downcase.to_sym)
|
40
53
|
end
|
41
54
|
end
|
42
55
|
end
|
43
56
|
|
44
|
-
def use_custom_slug
|
57
|
+
def use_custom_slug(custom_slug_method)
|
45
58
|
define_method :custom_slug do
|
46
59
|
send(custom_slug_method) if respond_to?(custom_slug_method)
|
47
60
|
end
|
@@ -14,7 +14,7 @@ end
|
|
14
14
|
|
15
15
|
class HierarchicalTestModel < TestModel
|
16
16
|
attr_accessor :children, :parent
|
17
|
-
|
17
|
+
|
18
18
|
def initialize(options)
|
19
19
|
super(options)
|
20
20
|
unless options[:parent].nil?
|
@@ -28,17 +28,16 @@ end
|
|
28
28
|
|
29
29
|
class CustomSlugModel
|
30
30
|
include ActsAsReadOnlyI18nLocalised
|
31
|
-
|
31
|
+
|
32
32
|
acts_as_read_only_i18n_localised :name
|
33
33
|
use_custom_slug :slug_maker
|
34
34
|
|
35
35
|
def slug_maker
|
36
|
-
|
36
|
+
'slug_it_up'
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
40
40
|
describe 'ActsAsReadOnlyI18nLocalised' do
|
41
|
-
|
42
41
|
before :all do
|
43
42
|
I18n.enforce_available_locales = false
|
44
43
|
I18n.locale = :en
|
@@ -79,7 +78,7 @@ describe 'ActsAsReadOnlyI18nLocalised' do
|
|
79
78
|
end
|
80
79
|
|
81
80
|
context 'child' do
|
82
|
-
let(:child)
|
81
|
+
let(:child) { HierarchicalTestModel.new(slug: 'child', parent: parent) }
|
83
82
|
let(:key) { :'hierarchical_test_model.parent.children.child.name' }
|
84
83
|
let(:expected) { 'test-child-result' }
|
85
84
|
|
@@ -94,7 +93,7 @@ describe 'ActsAsReadOnlyI18nLocalised' do
|
|
94
93
|
end
|
95
94
|
|
96
95
|
context 'with custom slug maker' do
|
97
|
-
let!(:model)
|
96
|
+
let!(:model) { CustomSlugModel.new }
|
98
97
|
let(:key) { :"custom_slug_model.slug_it_up.name" }
|
99
98
|
let(:expected) { 'test-custom-result' }
|
100
99
|
|
@@ -106,4 +105,33 @@ describe 'ActsAsReadOnlyI18nLocalised' do
|
|
106
105
|
expect(model.name).to eq expected
|
107
106
|
end
|
108
107
|
end
|
108
|
+
|
109
|
+
context 'with access to String.pluralize', verify_stubs: false do
|
110
|
+
let(:model) { TestModel.new(slug: 'test') }
|
111
|
+
let(:key) { :"test_models.test.name" }
|
112
|
+
let(:expected) { 'test-custom-result' }
|
113
|
+
|
114
|
+
before :each do
|
115
|
+
allow_any_instance_of(String).to receive(:pluralize)
|
116
|
+
.and_return('test_models')
|
117
|
+
end
|
118
|
+
|
119
|
+
it 'the name has the expected value' do
|
120
|
+
expect(model.name).to eq expected
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
context 'with access to self.table_name', verify_stubs: false do
|
125
|
+
let(:model) { TestModel.new(slug: 'test') }
|
126
|
+
let(:key) { :"test_models.test.name" }
|
127
|
+
let(:expected) { 'test-custom-result' }
|
128
|
+
|
129
|
+
before :each do
|
130
|
+
allow(model).to receive(:table_name).and_return('test_models')
|
131
|
+
end
|
132
|
+
|
133
|
+
it 'the name has the expected value' do
|
134
|
+
expect(model.name).to eq expected
|
135
|
+
end
|
136
|
+
end
|
109
137
|
end
|
data/spec/db_helper.rb
CHANGED
@@ -1,3 +1,13 @@
|
|
1
|
+
require 'codeclimate-test-reporter'
|
2
|
+
SimpleCov.start do
|
3
|
+
formatter SimpleCov::Formatter::MultiFormatter.new(
|
4
|
+
[
|
5
|
+
SimpleCov::Formatter::HTMLFormatter,
|
6
|
+
CodeClimate::TestReporter::Formatter
|
7
|
+
]
|
8
|
+
)
|
9
|
+
end
|
10
|
+
|
1
11
|
require 'rspec'
|
2
12
|
require 'pry'
|
3
13
|
require 'spec_helper'
|
data/spec/spec_helper.rb
CHANGED
@@ -26,7 +26,6 @@ RSpec.configure do |config|
|
|
26
26
|
mocks.verify_partial_doubles = true
|
27
27
|
end
|
28
28
|
|
29
|
-
# allow us to disable verify_partial_doubles on a case by case basis.
|
30
29
|
config.around(:each, verify_stubs: false) do |ex|
|
31
30
|
config.mock_with :rspec do |mocks|
|
32
31
|
mocks.verify_partial_doubles = false
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_read_only_i18n_localised
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dave Sag
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-05-
|
11
|
+
date: 2016-05-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: i18n
|
@@ -137,7 +137,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
137
137
|
version: '0'
|
138
138
|
requirements: []
|
139
139
|
rubyforge_project:
|
140
|
-
rubygems_version: 2.
|
140
|
+
rubygems_version: 2.5.1
|
141
141
|
signing_key:
|
142
142
|
specification_version: 4
|
143
143
|
summary: Simple mechanism to allow localised lookups of seed data.
|