globalize-accessors 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/globalize-accessors.gemspec +4 -2
- data/lib/globalize-accessors.rb +9 -4
- data/lib/globalize-accessors/version.rb +1 -1
- data/readme.md +30 -9
- data/test/globalize_accessors_test.rb +8 -0
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ebf9c4a52aea9e8ff546ee109e2ca3cf1a7641e7
|
4
|
+
data.tar.gz: c2ee6386a210d5a63595901e61118ec3be0e9e4d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2d0d5c1297a0ecd11cdc3d31c2e142170e06744966c073eebbfbb0caf87b1bfe6ff11e3f64a28689e94bf7e92ece20db164136195890a4ade450a9b05e6d6b46
|
7
|
+
data.tar.gz: 71cc829fdebf3540953aa07fd1c568d9f441edd7309b291f3ac5184111de19402d516f164ea6d8d46cd3ca921d3f079025079aca4119154da4f4662296f10b9a
|
data/globalize-accessors.gemspec
CHANGED
@@ -15,9 +15,11 @@ Gem::Specification.new do |s|
|
|
15
15
|
s.rubyforge_project = "globalize-accessors"
|
16
16
|
|
17
17
|
if ENV['RAILS_3']
|
18
|
-
s.add_dependency "
|
19
|
-
|
18
|
+
s.add_dependency "globalize", "~> 3.0.0"
|
19
|
+
elsif ENV['RAILS_4']
|
20
20
|
s.add_dependency "globalize", "~> 4.0.0.alpha.1"
|
21
|
+
else
|
22
|
+
s.add_dependency "globalize", ">= 3"
|
21
23
|
end
|
22
24
|
|
23
25
|
s.add_development_dependency "bundler", "~> 1.3.5"
|
data/lib/globalize-accessors.rb
CHANGED
@@ -1,11 +1,13 @@
|
|
1
1
|
require 'globalize'
|
2
2
|
|
3
|
-
module
|
3
|
+
module Globalize::Accessors
|
4
4
|
attr_reader :globalize_locales
|
5
|
+
attr_reader :globalize_attribute_names
|
5
6
|
|
6
7
|
def globalize_accessors(options = {})
|
7
8
|
options.reverse_merge!(:locales => I18n.available_locales, :attributes => translated_attribute_names)
|
8
9
|
@globalize_locales = options[:locales]
|
10
|
+
@globalize_attribute_names = []
|
9
11
|
|
10
12
|
each_attribute_and_locale(options) do |attr_name, locale|
|
11
13
|
define_accessors(attr_name, locale)
|
@@ -29,12 +31,15 @@ module GlobalizeAccessors
|
|
29
31
|
end
|
30
32
|
|
31
33
|
def define_setter(attr_name, locale)
|
32
|
-
|
34
|
+
localized_attr_name = "#{attr_name}_#{locale.to_s.underscore}"
|
35
|
+
|
36
|
+
define_method :"#{localized_attr_name}=" do |value|
|
33
37
|
write_attribute(attr_name, value, :locale => locale)
|
34
38
|
end
|
35
39
|
if respond_to?(:accessible_attributes) && accessible_attributes.include?(attr_name)
|
36
|
-
attr_accessible :"#{
|
40
|
+
attr_accessible :"#{localized_attr_name}"
|
37
41
|
end
|
42
|
+
@globalize_attribute_names << localized_attr_name.to_sym
|
38
43
|
end
|
39
44
|
|
40
45
|
def each_attribute_and_locale(options)
|
@@ -47,4 +52,4 @@ module GlobalizeAccessors
|
|
47
52
|
|
48
53
|
end
|
49
54
|
|
50
|
-
ActiveRecord::Base.extend
|
55
|
+
ActiveRecord::Base.extend Globalize::Accessors
|
data/readme.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
## Introduction
|
4
4
|
|
5
|
-
Generator of accessor methods for models using Globalize. Use globalize-accessors with a list of translated fields you want easily access to and extra
|
5
|
+
Generator of accessor methods for models using Globalize. Use `globalize-accessors` with a list of translated fields you want easily access to and extra `locales` array listing locales for which you want the accessors to be generated.
|
6
6
|
|
7
7
|
This way a single form can be used to edit given model fields with all anticipated translations.
|
8
8
|
|
@@ -11,7 +11,9 @@ This way a single form can be used to edit given model fields with all anticipat
|
|
11
11
|
|
12
12
|
## Installation
|
13
13
|
|
14
|
-
|
14
|
+
````ruby
|
15
|
+
gem install globalize-accessors
|
16
|
+
````
|
15
17
|
|
16
18
|
## Example
|
17
19
|
|
@@ -24,26 +26,45 @@ class Product
|
|
24
26
|
end
|
25
27
|
````
|
26
28
|
|
27
|
-
Gives you access to methods: `title_pl`, `title_en`, `title_pl=`, `title_en=` (and a similar set of description_* methods).
|
29
|
+
Gives you access to methods: `title_pl`, `title_en`, `title_pl=`, `title_en=` (and a similar set of description_* methods). These work seamlessly with Globalize (not even touching the "core" `title`, `title=` methods used by Globalize itself).
|
28
30
|
|
29
|
-
`:locales` and `:attributes` are optional.
|
31
|
+
The `:locales` and `:attributes` options are optional. Their default values are:
|
30
32
|
|
31
33
|
````ruby
|
32
|
-
|
33
|
-
|
34
|
+
:locales => I18n.available_locales
|
35
|
+
:attributes => translated_attribute_names
|
34
36
|
````
|
35
37
|
|
36
|
-
|
38
|
+
Calling `globalize_accessors` with no options will therefore generate accessor methods for all translated fields and available languages.
|
37
39
|
|
38
40
|
You can also get the accessor locales for a class with the `globalize_locales` method:
|
39
41
|
|
40
42
|
````ruby
|
41
|
-
|
43
|
+
Product.globalize_locales # => [:en, :pl]
|
44
|
+
````
|
45
|
+
|
46
|
+
You can also get modified attribute names -- ideal for use with strong parameters -- with the `globalize_attribute_names` method:
|
47
|
+
|
48
|
+
````ruby
|
49
|
+
Product.globalize_attribute_names # => [:title_en, :title_pl]
|
50
|
+
````
|
51
|
+
|
52
|
+
Example with strong parameters:
|
53
|
+
|
54
|
+
````ruby
|
55
|
+
params.require(:product).permit(*Product.globalize_attribute_names)
|
56
|
+
````
|
57
|
+
|
58
|
+
If you need to permit non-translatable attributes as well, you could include them with:
|
59
|
+
|
60
|
+
````ruby
|
61
|
+
permitted = Product.globalize_attribute_names + [:position]
|
62
|
+
params.require(:product).permit(*permitted)
|
42
63
|
````
|
43
64
|
|
44
65
|
## Always define accessors
|
45
66
|
|
46
|
-
If you wish to always define accessors and don't want to call the globalize_accessors method in every class, you can extend ActiveRecord::Base with a module:
|
67
|
+
If you wish to always define accessors and don't want to call the `globalize_accessors` method in every class, you can extend `ActiveRecord::Base` with a module:
|
47
68
|
|
48
69
|
````ruby
|
49
70
|
module TranslatesWithAccessors
|
@@ -133,4 +133,12 @@ class GlobalizeAccessorsTest < ActiveSupport::TestCase
|
|
133
133
|
assert_equal "Name pt-BR", u.name_pt_br
|
134
134
|
assert_equal "Name en-AU", u.name_en_au
|
135
135
|
end
|
136
|
+
|
137
|
+
test "globalize attribute names on class without attributes specified in options" do
|
138
|
+
assert_equal [:name_en, :name_pl, :title_en, :title_pl], Unit.globalize_attribute_names
|
139
|
+
end
|
140
|
+
|
141
|
+
test "globalize attribute names on class with attributes specified in options" do
|
142
|
+
assert_equal [:name_pl], UnitTranslatedWithOptions.globalize_attribute_names
|
143
|
+
end
|
136
144
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: globalize-accessors
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tomasz Stachewicz
|
@@ -12,22 +12,22 @@ authors:
|
|
12
12
|
autorequire:
|
13
13
|
bindir: bin
|
14
14
|
cert_chain: []
|
15
|
-
date: 2013-
|
15
|
+
date: 2013-12-13 00:00:00.000000000 Z
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|
18
18
|
name: globalize
|
19
19
|
requirement: !ruby/object:Gem::Requirement
|
20
20
|
requirements:
|
21
|
-
- -
|
21
|
+
- - '>='
|
22
22
|
- !ruby/object:Gem::Version
|
23
|
-
version:
|
23
|
+
version: '3'
|
24
24
|
type: :runtime
|
25
25
|
prerelease: false
|
26
26
|
version_requirements: !ruby/object:Gem::Requirement
|
27
27
|
requirements:
|
28
|
-
- -
|
28
|
+
- - '>='
|
29
29
|
- !ruby/object:Gem::Version
|
30
|
-
version:
|
30
|
+
version: '3'
|
31
31
|
- !ruby/object:Gem::Dependency
|
32
32
|
name: bundler
|
33
33
|
requirement: !ruby/object:Gem::Requirement
|