acts_as_read_only_i18n_localised 0.0.1 → 0.0.2

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: d49d07db700e026f139a9734fd231b13d8305d86
4
- data.tar.gz: 1a3fd6440e06b304458e741a1e6ccb3dca0bf0bd
3
+ metadata.gz: 858a5fd338c0dd5904ccda969f0bd6bb69c3d85f
4
+ data.tar.gz: 3bcfd46cd96babb4db0dec062cf360a9fef02f26
5
5
  SHA512:
6
- metadata.gz: 4383f8c270f5ba8a6638968e69efa5b5d2911a78e6c70b174b403da30b51caae2a631cf2399a69b5b5d43e0ffea73a80ad1e8cb92cd600b21c4605ab6b64ba8f
7
- data.tar.gz: 7d10db3e855a7f976ae40c4945f83b05ebfb2fdb553cfe93f955f6c5092ba24a343f3306e1953102150792661f14ddadfb8e1bbfa5002ef763ce91ec80d913b3
6
+ metadata.gz: 010c9fc1bf40a437c86fcfdfd9284f78a80b7fe4aea70568448626f9aef3740b2762c03c4cec40534ffc9e82999f163c66811d73d6e785452cc980bd1fa4c7c7
7
+ data.tar.gz: 6f062f43bf0aa91c747d330922b1f4023eeb07a14b8cef6458773f10e6def04a9030502853fd15ec217db53f1464c8a005c17ae3abdebc5b5c4aaff5ef2c5498
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.1)
4
+ acts_as_read_only_i18n_localised (0.0.2)
5
5
  i18n
6
6
 
7
7
  GEM
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(:slug)}.#{attribute}"
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
@@ -1,3 +1,3 @@
1
1
  module ActsAsReadOnlyI18nLocalised
2
- VERSION = '0.0.1'.freeze
2
+ VERSION = '0.0.2'.freeze
3
3
  end
@@ -12,11 +12,33 @@ class TestModel
12
12
  acts_as_read_only_i18n_localised :name
13
13
  end
14
14
 
15
- describe 'ActsAsReadOnlyI18nLocalised' do
16
- let(:model) { TestModel.new(slug: 'test') }
17
- let(:key) { :'test_model.test.name' }
18
- let(:expected) { 'test-result' }
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
- it 'has a name method' do
30
- expect(model).to respond_to :name
31
- expect(model.name).to eq expected
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
metadata CHANGED
@@ -1,7 +1,7 @@
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.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dave Sag