i18n_scopes 0.2.0 → 0.2.1

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.
@@ -13,9 +13,12 @@ module I18nScopes
13
13
 
14
14
  extend ActiveSupport::Concern
15
15
 
16
- module ClassMethods
16
+ included do
17
+ # NOTE: class_attribute :i18n_scope_helper, instance_writer: false - will not clone the class attribute, so Hashes wont work
18
+ inheritable_attr :i18n_scope_helper
19
+ end
17
20
 
18
- attr_accessor :i18n_scope_helper
21
+ module ClassMethods
19
22
 
20
23
  # Use this method to configure how the scope will be built when using translations
21
24
  # === Params:
@@ -35,21 +38,17 @@ module I18nScopes
35
38
  def i18n_scope(name, *path)
36
39
  @i18n_scope_initialized ||= begin
37
40
  self.send :include, I18nScopes::TranslationMethods
41
+ # self.i18n_scope_helper ||= self.i18n_scope_helper.nil? ? {} : self.i18n_scope_helper.dup
38
42
  self.i18n_scope_helper ||= {}
39
43
  true
40
44
  end
45
+
41
46
  options = path.extract_options!
42
47
  as = options.delete(:as) || :"t_#{name}"
43
48
  self.i18n_scope_helper[name] = I18nScopes::TranslationHelper.new(path.flatten, options)
44
49
 
45
50
  define_method(as) do |*args|
46
- i18n_scope_helper = self.class.i18n_scope_helper[name]
47
- klass = self.class
48
- while(!i18n_scope_helper && klass.superclass != Object)
49
- klass = klass.superclass
50
- i18n_scope_helper = klass.i18n_scope_helper[name]
51
- end
52
- scoped_translation(i18n_scope_helper, *args)
51
+ scoped_translation(self.class.i18n_scope_helper[name], *args)
53
52
  end
54
53
  end
55
54
 
@@ -62,6 +61,22 @@ module I18nScopes
62
61
  path << options
63
62
  i18n_scope(:default, *path)
64
63
  end
64
+
65
+ # defines an inheriteable attribut which clones the parents value
66
+ # === Params:
67
+ # * +name+: the attribtues name
68
+ def inheritable_attr(name)
69
+ instance_eval <<-eos
70
+ def #{name}=(v)
71
+ @#{name} = v
72
+ end
73
+
74
+ def #{name}
75
+ return @#{name} unless superclass.respond_to?(:#{name}) and value = superclass.#{name}
76
+ @#{name} ||= value.clone # only do this once.
77
+ end
78
+ eos
79
+ end
65
80
 
66
81
  end
67
82
 
@@ -35,7 +35,7 @@ module I18nScopes
35
35
  return I18n.translate *args, options.merge(scope: scope)
36
36
  end
37
37
 
38
- # retusn the i18n_scope_helper instance of TranslationHelper
38
+ # returns the i18n_scope_helper instance of TranslationHelper
39
39
  def i18n_scope_helper
40
40
  return self.class.i18n_scope_helper
41
41
  end
@@ -1,3 +1,3 @@
1
1
  module I18nScopes
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
data/lib/i18n_scopes.rb CHANGED
@@ -1,10 +1,11 @@
1
+ require 'active_support/concern'
2
+ require 'active_support/core_ext/string/inflections'
3
+ require 'active_support/core_ext/class/attribute'
1
4
 
2
5
  require 'i18n_scopes/scope'
3
6
  require 'i18n_scopes/option_accessor'
4
7
  require 'i18n_scopes/configuration'
5
8
  require 'i18n_scopes/translation_methods'
6
9
  require 'i18n_scopes/translation_helper'
7
- require 'active_support/concern'
8
- require 'active_support/core_ext/string/inflections'
9
10
 
10
11
  Object.send :include, I18nScopes::Scope
@@ -0,0 +1,3 @@
1
+ class InheritFromParent < ScopeTWithPath
2
+
3
+ end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+ require 'example_classes/scope_t_with_path'
3
+ require 'example_classes/respondable'
4
+ require 'example_classes/inherit_from_parent'
5
+
6
+ describe InheritFromParent do
7
+
8
+ its(:translate_local) { should == "ScopeTWithPathLocallyOverwritten"}
9
+
10
+ end
data/spec/spec_helper.rb CHANGED
@@ -5,6 +5,7 @@ require 'i18n'
5
5
 
6
6
  require 'active_support/concern'
7
7
  require 'active_support/core_ext/string/inflections'
8
+ require 'active_support/core_ext/class/attribute'
8
9
 
9
10
  require 'i18n_scopes'
10
11
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i18n_scopes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-15 00:00:00.000000000 Z
12
+ date: 2012-07-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -49,6 +49,7 @@ files:
49
49
  - lib/i18n_scopes/translation_methods.rb
50
50
  - lib/i18n_scopes/version.rb
51
51
  - spec/configuration_spec.rb
52
+ - spec/example_classes/inherit_from_parent.rb
52
53
  - spec/example_classes/inherited.rb
53
54
  - spec/example_classes/respondable.rb
54
55
  - spec/example_classes/scope_t_with_path.rb
@@ -56,6 +57,7 @@ files:
56
57
  - spec/example_classes/should_define_scope.rb
57
58
  - spec/example_classes/should_have_attached_scoped_t.rb
58
59
  - spec/include_scope_spec.rb
60
+ - spec/inherit_from_parent_spec.rb
59
61
  - spec/inherited_spec.rb
60
62
  - spec/locales/en.yml
61
63
  - spec/scope_spec.rb
@@ -89,6 +91,7 @@ summary: This gem will attach a t_scoped method to your class. The scope will be
89
91
  in your class or globally.
90
92
  test_files:
91
93
  - spec/configuration_spec.rb
94
+ - spec/example_classes/inherit_from_parent.rb
92
95
  - spec/example_classes/inherited.rb
93
96
  - spec/example_classes/respondable.rb
94
97
  - spec/example_classes/scope_t_with_path.rb
@@ -96,6 +99,7 @@ test_files:
96
99
  - spec/example_classes/should_define_scope.rb
97
100
  - spec/example_classes/should_have_attached_scoped_t.rb
98
101
  - spec/include_scope_spec.rb
102
+ - spec/inherit_from_parent_spec.rb
99
103
  - spec/inherited_spec.rb
100
104
  - spec/locales/en.yml
101
105
  - spec/scope_spec.rb