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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 858a5fd338c0dd5904ccda969f0bd6bb69c3d85f
4
- data.tar.gz: 3bcfd46cd96babb4db0dec062cf360a9fef02f26
3
+ metadata.gz: ab853364bad7142e6f776a822a36766f25612b4a
4
+ data.tar.gz: c11eb5939d0a4e35887cde2e2e6f189cd5e5ca4f
5
5
  SHA512:
6
- metadata.gz: 010c9fc1bf40a437c86fcfdfd9284f78a80b7fe4aea70568448626f9aef3740b2762c03c4cec40534ffc9e82999f163c66811d73d6e785452cc980bd1fa4c7c7
7
- data.tar.gz: 6f062f43bf0aa91c747d330922b1f4023eeb07a14b8cef6458773f10e6def04a9030502853fd15ec217db53f1464c8a005c17ae3abdebc5b5c4aaff5ef2c5498
6
+ metadata.gz: 73cac04060acfa4c3785f1c9838db1f8c1b7dc891a0659a6ed219781205b1cadc371a12aedf8382016cb713caded0e729f293daf9e80b83df4f78097bedd9fae
7
+ data.tar.gz: 26294b91c0ce9e85b3a56442fdf38f0c95380df5ec594d261fdb697885c6880ab53cd7c9e0224580a617ca086ddeeeb74cae1f293a60872ba9ab6979314dce28
data/Gemfile CHANGED
@@ -4,4 +4,5 @@ gemspec
4
4
 
5
5
  group :test do
6
6
  gem 'rake'
7
+ gem 'codeclimate-test-reporter'
7
8
  end
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- acts_as_read_only_i18n_localised (0.0.2)
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.11.2
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: /^[a-z]+[\-?[a-z]*]*$/},
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.class.name.pluralize}.#{slug}.name".downcase.to_sym
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: /^[a-z]+[\-?[a-z]*]*$/},
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 :children, class_name: 'Category', foreign_key: :parent_id
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}.children.#{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("#{self.class.name.gsub(/::/, '/')
36
- .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
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 custom_slug_method
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
@@ -1,3 +1,3 @@
1
1
  module ActsAsReadOnlyI18nLocalised
2
- VERSION = '0.0.2'.freeze
2
+ VERSION = '0.0.3'.freeze
3
3
  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
- "slug_it_up"
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) { HierarchicalTestModel.new(slug: 'child', parent: parent) }
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) { CustomSlugModel.new }
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
@@ -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'
@@ -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.2
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-06 00:00:00.000000000 Z
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.4.8
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.