acts_as_read_only_i18n_localised 0.0.1 → 0.0.2
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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +73 -0
- data/lib/acts_as_read_only_i18n_localised.rb +13 -4
- data/lib/acts_as_read_only_i18n_localised/version.rb +1 -1
- data/spec/acts_as_read_only_i18n_localised_spec.rb +83 -7
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 858a5fd338c0dd5904ccda969f0bd6bb69c3d85f
|
4
|
+
data.tar.gz: 3bcfd46cd96babb4db0dec062cf360a9fef02f26
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 010c9fc1bf40a437c86fcfdfd9284f78a80b7fe4aea70568448626f9aef3740b2762c03c4cec40534ffc9e82999f163c66811d73d6e785452cc980bd1fa4c7c7
|
7
|
+
data.tar.gz: 6f062f43bf0aa91c747d330922b1f4023eeb07a14b8cef6458773f10e6def04a9030502853fd15ec217db53f1464c8a005c17ae3abdebc5b5c4aaff5ef2c5498
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -12,6 +12,10 @@ A variant on the `acts_as_localized` theme for when you have static seed data in
|
|
12
12
|
|
13
13
|
## Example of use
|
14
14
|
|
15
|
+
In your `Gemfile`
|
16
|
+
|
17
|
+
gem 'acts_as_read_only_i18n_localised'
|
18
|
+
|
15
19
|
In `config/locales/categories.en.yml`
|
16
20
|
|
17
21
|
en:
|
@@ -52,8 +56,34 @@ with the effect that a call to `category.name` will always return the localised
|
|
52
56
|
|
53
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.
|
54
58
|
|
59
|
+
### A more complex example
|
60
|
+
|
61
|
+
Say your `categories` have their own sub `categories`. Building on the previous example you might define the following
|
62
|
+
|
63
|
+
class Category < ActiveRecord::Base
|
64
|
+
include ActsAsReadOnlyI18nLocalised
|
65
|
+
validates :slug, format: {with: /^[a-z]+[\-?[a-z]*]*$/},
|
66
|
+
uniqueness: true,
|
67
|
+
presence: true
|
68
|
+
has_many :products
|
69
|
+
validates_associated :products
|
70
|
+
|
71
|
+
has_many :children, class_name: 'Category', foreign_key: :parent_id
|
72
|
+
belongs_to :parent, class_name: 'Category
|
73
|
+
|
74
|
+
acts_as_read_only_i18n_localised :name, :description
|
75
|
+
use_custom_slug :slug_maker
|
76
|
+
|
77
|
+
def slug_maker
|
78
|
+
reurn slug if parent.nil?
|
79
|
+
"#{@parent.slug}.children.#{slug}"
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
55
83
|
## Seeding your database
|
56
84
|
|
85
|
+
### simple case
|
86
|
+
|
57
87
|
In `db/seeds.rb` add something like
|
58
88
|
|
59
89
|
require 'i18n'
|
@@ -62,8 +92,38 @@ In `db/seeds.rb` add something like
|
|
62
92
|
Category.create(slug: key)
|
63
93
|
end
|
64
94
|
|
95
|
+
### with a hierarchy
|
96
|
+
|
97
|
+
In `db/seeds.rb` add something like
|
98
|
+
|
99
|
+
require 'i18n'
|
100
|
+
|
101
|
+
I18n.t(:categories).each do |key, data|
|
102
|
+
cat = Category.where(slug: key).first_or_create!
|
103
|
+
if data[:categories]
|
104
|
+
data[:categories].each do |inner_key, inner_data|
|
105
|
+
Category.where(slug: inner_key, parent: cat).first_or_create!
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
Or preferably something recursive, though the above will do if you are only going 1 level deep.
|
111
|
+
|
65
112
|
# Development
|
66
113
|
|
114
|
+
This gem requires Ruby version 2.0.0 or better. It is tested against the following Rubies.
|
115
|
+
|
116
|
+
* `2.0.0`
|
117
|
+
* `2.1.6`
|
118
|
+
* `2.2.4`
|
119
|
+
* `2.3.0`
|
120
|
+
|
121
|
+
Before you do anything do this:
|
122
|
+
|
123
|
+
```sh
|
124
|
+
bundle install
|
125
|
+
```
|
126
|
+
|
67
127
|
## To build the gem
|
68
128
|
|
69
129
|
gem build acts_as_read_only_i18n_localised.gemspec
|
@@ -76,3 +136,16 @@ or
|
|
76
136
|
|
77
137
|
rake
|
78
138
|
|
139
|
+
You can also run the following QA tools against the codebase if you have them installed.
|
140
|
+
|
141
|
+
* `rubocop` (on its own without any of the `codeclimate` stuff.)
|
142
|
+
* `codeclimate` (which will also run `rubocop` itself.)
|
143
|
+
|
144
|
+
## Contributing
|
145
|
+
|
146
|
+
Contributions and ideas are welcome.
|
147
|
+
|
148
|
+
If you have ideas on how to improve the codebase or feature-set, please add them as issues so they can be discussed.
|
149
|
+
|
150
|
+
For contributing code please see [CONTRIBUTING](CONTRIBUTING.md) for details on how to do that.
|
151
|
+
|
@@ -22,20 +22,29 @@ module ActsAsReadOnlyI18nLocalised
|
|
22
22
|
# Standard Ruby idiom for auto-adding class methods
|
23
23
|
#
|
24
24
|
module ClassMethods
|
25
|
-
def underscore(string)
|
26
|
-
string
|
27
|
-
end
|
28
25
|
|
29
26
|
def acts_as_read_only_i18n_localised(*attributes)
|
27
|
+
unless methods.include?(:custom_slug)
|
28
|
+
define_method :custom_slug do
|
29
|
+
send(:slug) if respond_to?(:slug)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
30
33
|
attributes.each do |attribute|
|
31
34
|
define_method attribute do
|
32
35
|
I18n.t("#{self.class.name.gsub(/::/, '/')
|
33
36
|
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
|
34
37
|
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
|
35
|
-
.tr('-', '_')}.#{send(:
|
38
|
+
.tr('-', '_')}.#{send(:custom_slug)}.#{attribute}"
|
36
39
|
.downcase.to_sym)
|
37
40
|
end
|
38
41
|
end
|
39
42
|
end
|
43
|
+
|
44
|
+
def use_custom_slug custom_slug_method
|
45
|
+
define_method :custom_slug do
|
46
|
+
send(custom_slug_method) if respond_to?(custom_slug_method)
|
47
|
+
end
|
48
|
+
end
|
40
49
|
end
|
41
50
|
end
|
@@ -12,11 +12,33 @@ class TestModel
|
|
12
12
|
acts_as_read_only_i18n_localised :name
|
13
13
|
end
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
class HierarchicalTestModel < TestModel
|
16
|
+
attr_accessor :children, :parent
|
17
|
+
|
18
|
+
def initialize(options)
|
19
|
+
super(options)
|
20
|
+
unless options[:parent].nil?
|
21
|
+
@parent = options[:parent]
|
22
|
+
@children ||= []
|
23
|
+
@children << self
|
24
|
+
@slug = "#{@parent.slug}.children.#{slug}"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class CustomSlugModel
|
30
|
+
include ActsAsReadOnlyI18nLocalised
|
31
|
+
|
32
|
+
acts_as_read_only_i18n_localised :name
|
33
|
+
use_custom_slug :slug_maker
|
19
34
|
|
35
|
+
def slug_maker
|
36
|
+
"slug_it_up"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe 'ActsAsReadOnlyI18nLocalised' do
|
41
|
+
|
20
42
|
before :all do
|
21
43
|
I18n.enforce_available_locales = false
|
22
44
|
I18n.locale = :en
|
@@ -26,8 +48,62 @@ describe 'ActsAsReadOnlyI18nLocalised' do
|
|
26
48
|
allow(I18n).to receive(:t).with(key).and_return(expected)
|
27
49
|
end
|
28
50
|
|
29
|
-
|
30
|
-
|
31
|
-
|
51
|
+
context 'with no hierarchy' do
|
52
|
+
let(:model) { TestModel.new(slug: 'test') }
|
53
|
+
let(:key) { :'test_model.test.name' }
|
54
|
+
let(:expected) { 'test-result' }
|
55
|
+
|
56
|
+
it 'responds to name' do
|
57
|
+
expect(model).to respond_to :name
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'the name has the expected value' do
|
61
|
+
expect(model.name).to eq expected
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context 'with simple hierarchy' do
|
66
|
+
let(:parent) { HierarchicalTestModel.new(slug: 'parent') }
|
67
|
+
|
68
|
+
context 'parent' do
|
69
|
+
let(:key) { :'hierarchical_test_model.parent.name' }
|
70
|
+
let(:expected) { 'test-parent-result' }
|
71
|
+
|
72
|
+
it 'responds to name' do
|
73
|
+
expect(parent).to respond_to :name
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'the name has the expected value' do
|
77
|
+
expect(parent.name).to eq expected
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
context 'child' do
|
82
|
+
let(:child) { HierarchicalTestModel.new(slug: 'child', parent: parent) }
|
83
|
+
let(:key) { :'hierarchical_test_model.parent.children.child.name' }
|
84
|
+
let(:expected) { 'test-child-result' }
|
85
|
+
|
86
|
+
it 'responds to name' do
|
87
|
+
expect(child).to respond_to :name
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'the name has the expected value' do
|
91
|
+
expect(child.name).to eq expected
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
context 'with custom slug maker' do
|
97
|
+
let!(:model) { CustomSlugModel.new }
|
98
|
+
let(:key) { :"custom_slug_model.slug_it_up.name" }
|
99
|
+
let(:expected) { 'test-custom-result' }
|
100
|
+
|
101
|
+
it 'responds to name' do
|
102
|
+
expect(model).to respond_to :name
|
103
|
+
end
|
104
|
+
|
105
|
+
it 'the name has the expected value' do
|
106
|
+
expect(model.name).to eq expected
|
107
|
+
end
|
32
108
|
end
|
33
109
|
end
|