globalize-accessors 0.1.1 → 0.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 367386484b2db24a7568c6227d7ed4297cc2c5ee
4
- data.tar.gz: 3c123202c983f00e83277d512f21b3c55af51795
3
+ metadata.gz: ebf9c4a52aea9e8ff546ee109e2ca3cf1a7641e7
4
+ data.tar.gz: c2ee6386a210d5a63595901e61118ec3be0e9e4d
5
5
  SHA512:
6
- metadata.gz: 1226e51ce8c7c56608aae8555bf34fd1b5b076f988a9188636d10b3bfd1c18456ef6afb4ab2d5f4679aa7ab963a25cc0ddc36a064c84496240621bd7ce6f10a5
7
- data.tar.gz: 329f021d11c44d77b566778b3fa93c0e59e92b77300cc2e2e2b2cf430d6591a108d0d3b7f4ad1896f08b7a5b3b4cb6a3f0e5af7540f57fdf2836ed963d162b1b
6
+ metadata.gz: 2d0d5c1297a0ecd11cdc3d31c2e142170e06744966c073eebbfbb0caf87b1bfe6ff11e3f64a28689e94bf7e92ece20db164136195890a4ade450a9b05e6d6b46
7
+ data.tar.gz: 71cc829fdebf3540953aa07fd1c568d9f441edd7309b291f3ac5184111de19402d516f164ea6d8d46cd3ca921d3f079025079aca4119154da4f4662296f10b9a
@@ -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 "globalize3", "~> 0.3.0"
19
- else
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"
@@ -1,11 +1,13 @@
1
1
  require 'globalize'
2
2
 
3
- module GlobalizeAccessors
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
- define_method :"#{attr_name}_#{locale.to_s.underscore}=" do |value|
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 :"#{attr_name}_#{locale.to_s.underscore}"
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 GlobalizeAccessors
55
+ ActiveRecord::Base.extend Globalize::Accessors
@@ -1,3 +1,3 @@
1
1
  module GlobalizeAccessors
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
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 :locales array listing locales for which you want the accessors to be generated.
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
- gem install globalize-accessors
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). They work seamlessly with Globalize (not even touching the "core" title, title= methods used by Globalize itself).
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. Default values are:
31
+ The `:locales` and `:attributes` options are optional. Their default values are:
30
32
 
31
33
  ````ruby
32
- :locales => I18n.available_locales
33
- :attributes => translated_attribute_names
34
+ :locales => I18n.available_locales
35
+ :attributes => translated_attribute_names
34
36
  ````
35
37
 
36
- which means that skipping all options will generate you accessor method for all translated fields and available languages.
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
- Product.globalize_locales # => [:en, :pl]
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.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-10-22 00:00:00.000000000 Z
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: 4.0.0.alpha.1
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: 4.0.0.alpha.1
30
+ version: '3'
31
31
  - !ruby/object:Gem::Dependency
32
32
  name: bundler
33
33
  requirement: !ruby/object:Gem::Requirement