globalize-accessors 0.1.3 → 0.3.0

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
- SHA1:
3
- metadata.gz: fb81591d0892a68040b226ca603d12e538aaa430
4
- data.tar.gz: 4b4e3a579c5eff87286b320b7177f86fa8313b1f
2
+ SHA256:
3
+ metadata.gz: b24dfdcadad659639d275ef9598edd54b74623afcddc067bb3539b9379516d50
4
+ data.tar.gz: b6f234105d5bc9911feba08771b05dd82cd796fc1d767234aec5d1982a0a059b
5
5
  SHA512:
6
- metadata.gz: 89c74b3362d3fcadeb5999bd19a55ff8695cb421efa1553f08a6dedf2c8375d19956a4cba63cdaf393e4edcf6cd65cf276e9ed9096dd3533498075e74d71fa50
7
- data.tar.gz: 52283201db4341df4b3bb5bef75f77be4d1247a8ccff3f29cd3e79968f9e631b9089e75634f370ef857bbd5fb6ecc442d0b4baf2d1952ad7b4c393ca37c1736e
6
+ metadata.gz: 94a313445016b3967875ac18778e52f80c344936914fe4bbef3083c2e8d596be7013f9ff55c520079ad78351ed445671ca427060957255bab4531cdc0eafd7e0
7
+ data.tar.gz: ad260c54dd05b22081f6c3ac42588c73f634952beeda208f6534584133d07d3b8f686112fd69fb2af68b5c6d401c908858628bf5b58a295baa89a64bfa07fe9d
@@ -1,10 +1,10 @@
1
1
  language: ruby
2
+ sudo: false
3
+ cache: bundler
2
4
  before_script:
3
5
  - "cp test/db/database.yml.travis test/db/database.yml"
4
6
  script: "bundle exec rake"
5
- env:
6
- - RAILS_3=true
7
- - RAILS_4=true
8
7
  rvm:
8
+ - 2.2
9
+ - 2.1
9
10
  - 2.0.0
10
- - 1.9.3
@@ -1,4 +1,5 @@
1
- Copyright (c) 2009 [name of plugin creator]
1
+ Copyright (c) 2009, 2010, 2011, 2012, 2013 Tomek "Tomash" Stachewicz,
2
+ Robert Pankowecki, Chris Salzberg
2
3
 
3
4
  Permission is hereby granted, free of charge, to any person obtaining
4
5
  a copy of this software and associated documentation files (the
data/Rakefile CHANGED
@@ -13,7 +13,7 @@ require 'rake'
13
13
  desc 'Default: run unit tests.'
14
14
  task :default => :test
15
15
 
16
- desc 'Test the easy_globalize_accessors plugin.'
16
+ desc 'Test globalize-accessors gem.'
17
17
  Rake::TestTask.new(:test) do |t|
18
18
  t.libs << 'lib'
19
19
  t.libs << 'test'
@@ -21,7 +21,7 @@ Rake::TestTask.new(:test) do |t|
21
21
  t.verbose = true
22
22
  end
23
23
 
24
- desc 'Generate documentation for the easy_globalize_accessors plugin.'
24
+ desc 'Generate documentation for globalize-accessors gem.'
25
25
  Rake::RDocTask.new(:rdoc) do |rdoc|
26
26
  rdoc.rdoc_dir = 'rdoc'
27
27
  rdoc.title = 'EasyGlobalizeAccessors'
@@ -3,7 +3,7 @@ require File.expand_path("../lib/globalize-accessors/version", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "globalize-accessors"
6
- s.version = GlobalizeAccessors::VERSION
6
+ s.version = Globalize::Accessors::VERSION
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.authors = ["Tomasz Stachewicz", "Wojciech Pietrzak", "Steve Verlinden", "Robert Pankowecki", "Chris Salzberg"]
9
9
  s.email = ["tomekrs@o2.pl", "steve.verlinden@gmail.com", "robert.pankowecki@gmail.com", "rpa@gavdi.com", "chrissalzberg@gmail.com"]
@@ -11,21 +11,14 @@ Gem::Specification.new do |s|
11
11
  s.summary = "Define methods for accessing translated attributes"
12
12
  s.description = "Define methods for accessing translated attributes"
13
13
 
14
- s.required_rubygems_version = ">= 1.3.6"
14
+ s.required_rubygems_version = ">= 1.3"
15
15
  s.rubyforge_project = "globalize-accessors"
16
16
 
17
- if ENV['RAILS_3']
18
- s.add_dependency "globalize", "~> 3.0.0"
19
- elsif ENV['RAILS_4']
20
- s.add_dependency "globalize", "~> 4.0.0.alpha.1"
21
- else
22
- s.add_dependency "globalize", ">= 3"
23
- end
17
+ s.add_dependency "globalize", ">= 5.0.0"
24
18
 
25
- s.add_development_dependency "bundler", "~> 1.3.5"
26
- s.add_development_dependency "rake", "~> 0.9.2"
27
19
  s.add_development_dependency "sqlite3"
28
-
20
+ s.add_development_dependency "rake"
21
+ s.add_development_dependency "minitest", "~> 5.1"
29
22
 
30
23
  s.files = `git ls-files`.split("\n")
31
24
  s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
@@ -11,29 +11,34 @@ module Globalize::Accessors
11
11
  each_attribute_and_locale(options) do |attr_name, locale|
12
12
  define_accessors(attr_name, locale)
13
13
  end
14
+
15
+ include InstanceMethods
14
16
  end
15
17
 
18
+ def localized_attr_name_for(attr_name, locale)
19
+ "#{attr_name}_#{locale.to_s.underscore}"
20
+ end
16
21
 
17
22
  private
18
23
 
19
-
20
24
  def define_accessors(attr_name, locale)
21
25
  define_getter(attr_name, locale)
22
26
  define_setter(attr_name, locale)
23
27
  end
24
28
 
25
-
26
29
  def define_getter(attr_name, locale)
27
- define_method :"#{attr_name}_#{locale.to_s.underscore}" do
28
- read_attribute(attr_name, :locale => locale)
30
+ define_method localized_attr_name_for(attr_name, locale) do
31
+ globalize.stash.contains?(locale, attr_name) ? globalize.send(:fetch_stash, locale, attr_name) : globalize.send(:fetch_attribute, locale, attr_name)
29
32
  end
30
33
  end
31
34
 
32
35
  def define_setter(attr_name, locale)
33
- localized_attr_name = "#{attr_name}_#{locale.to_s.underscore}"
36
+ localized_attr_name = localized_attr_name_for(attr_name, locale)
34
37
 
35
38
  define_method :"#{localized_attr_name}=" do |value|
39
+ attribute_will_change!(localized_attr_name) if value != send(localized_attr_name)
36
40
  write_attribute(attr_name, value, :locale => locale)
41
+ translation_for(locale)[attr_name] = value
37
42
  end
38
43
  if respond_to?(:accessible_attributes) && accessible_attributes.include?(attr_name)
39
44
  attr_accessible :"#{localized_attr_name}"
@@ -49,6 +54,11 @@ module Globalize::Accessors
49
54
  end
50
55
  end
51
56
 
57
+ module InstanceMethods
58
+ def localized_attr_name_for(attr_name, locale)
59
+ self.class.localized_attr_name_for(attr_name, locale)
60
+ end
61
+ end
52
62
  end
53
63
 
54
64
  ActiveRecord::Base.extend Globalize::Accessors
@@ -1,3 +1,5 @@
1
- module GlobalizeAccessors
2
- VERSION = '0.1.3'
1
+ module Globalize
2
+ module Accessors
3
+ VERSION = '0.3.0'
4
+ end
3
5
  end
data/readme.md CHANGED
@@ -26,7 +26,7 @@ class Product
26
26
  end
27
27
  ````
28
28
 
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).
29
+ Gives you access to methods: `title_pl`, `title_en`, `title_pl=`, `title_en=`. These work seamlessly with Globalize (not even touching the "core" `title`, `title=` methods used by Globalize itself).
30
30
 
31
31
  The `:locales` and `:attributes` options are optional. Their default values are:
32
32
 
@@ -70,7 +70,7 @@ If you wish to always define accessors and don't want to call the `globalize_acc
70
70
  module TranslatesWithAccessors
71
71
 
72
72
  def translates(*params)
73
- options = params.extract_options!
73
+ options = params.dup.extract_options!
74
74
  options.reverse_merge!(:globalize_accessors => true)
75
75
  accessors = options.delete(:globalize_accessors)
76
76
  super
@@ -82,4 +82,6 @@ end
82
82
 
83
83
  ## Licence
84
84
 
85
- Copyright (c) 2009-2013 Tomek "Tomash" Stachewicz (http://tomash.wrug.eu), Robert Pankowecki (http://robert.pankowecki.pl) released under the MIT license
85
+ Copyright (c) 2009-2013 Tomek "Tomash" Stachewicz (http://tomash.wrug.eu),
86
+ Robert Pankowecki (http://robert.pankowecki.pl), Chris Salzberg (http://dejimata.com)
87
+ released under the MIT license
@@ -17,29 +17,33 @@ class GlobalizeAccessorsTest < ActiveSupport::TestCase
17
17
  end
18
18
 
19
19
  class UnitInheritedWithOptions < ActiveRecord::Base
20
+ self.table_name = :units
20
21
  translates :color
21
22
  globalize_accessors :locales => [:de], :attributes => [:color]
22
23
  end
23
24
 
24
- if ENV['RAILS_3']
25
- class UnitWithAttrAccessible < ActiveRecord::Base
26
- self.table_name = :units
27
- attr_accessible :name
28
- translates :name, :title
29
- globalize_accessors
30
- end
31
- end
32
-
33
25
  class UnitWithDashedLocales < ActiveRecord::Base
34
26
  self.table_name = :units
35
27
  translates :name
36
28
  globalize_accessors :locales => [:"pt-BR", :"en-AU"], :attributes => [:name]
37
29
  end
38
30
 
31
+ class UnitWithoutAccessors < ActiveRecord::Base
32
+ self.table_name = :units
33
+ end
34
+
39
35
  setup do
40
36
  assert_equal :en, I18n.locale
41
37
  end
42
38
 
39
+ test "return localized attribute names" do
40
+ u = UnitWithDashedLocales.new
41
+
42
+ assert_equal "name_en", u.localized_attr_name_for(:name, :en)
43
+ assert_equal "name_pt_br", u.localized_attr_name_for(:name, :"pt-BR")
44
+ assert_equal "name_zh_cn", u.class.localized_attr_name_for(:name, :"zh-CN")
45
+ end
46
+
43
47
  test "read and write on new object" do
44
48
  u = Unit.new(:name_en => "Name en", :title_pl => "Title pl")
45
49
 
@@ -51,6 +55,12 @@ class GlobalizeAccessorsTest < ActiveSupport::TestCase
51
55
  assert_nil u.title_en
52
56
  end
53
57
 
58
+ test "build translations for a new object" do
59
+ u = Unit.new(:name_en => "Name en", :title_pl => "Title pl")
60
+ assert_equal 2, u.translations.size # size of the collection
61
+ assert_equal 0, u.translations.count # count from database
62
+ end
63
+
54
64
  test "write on new object and read on saved" do
55
65
  u = Unit.create!(:name_en => "Name en", :title_pl => "Title pl")
56
66
 
@@ -62,6 +72,14 @@ class GlobalizeAccessorsTest < ActiveSupport::TestCase
62
72
  assert_nil u.title_en
63
73
  end
64
74
 
75
+ test "updates translations for existing object" do
76
+ u = Unit.create!(:name_en => "Name en", :title_pl => "Title pl")
77
+ u.name_en = "New name en"
78
+ assert_equal "New name en", u.translations.find {|t| t.locale == :en }.name
79
+ assert_equal "Title pl", u.translations.find {|t| t.locale == :pl }.title
80
+ assert_equal 2, u.translations.count
81
+ end
82
+
65
83
  test "read on existing object" do
66
84
  u = Unit.create!(:name_en => "Name en", :title_pl => "Title pl")
67
85
  u = Unit.find(u.id)
@@ -106,18 +124,6 @@ class GlobalizeAccessorsTest < ActiveSupport::TestCase
106
124
  assert_equal "Name pl", u.name_pl
107
125
  end
108
126
 
109
- if ENV['RAILS_3']
110
- test "whitelist locale accessors if the original attribute is whitelisted" do
111
- u = UnitWithAttrAccessible.new()
112
- u.update_attributes(:name => "Name en", :name_pl => "Name pl", :title => "Title en", :title_pl => "Title pl")
113
-
114
- assert_equal "Name en", u.name
115
- assert_equal "Name pl", u.name_pl
116
- assert_nil u.title
117
- assert_nil u.title_pl
118
- end
119
- end
120
-
121
127
  test "globalize locales on class without locales specified in options" do
122
128
  assert_equal [:en, :pl], Unit.globalize_locales
123
129
  end
@@ -144,6 +150,17 @@ class GlobalizeAccessorsTest < ActiveSupport::TestCase
144
150
  assert_equal "Name en-AU", u.name_en_au
145
151
  end
146
152
 
153
+ test "read when fallbacks present" do
154
+ u = Unit.create!(:name_en => "Name en", :title_pl => "Title pl")
155
+ u = Unit.find(u.id)
156
+ def u.globalize_fallbacks(locale)
157
+ locale == :pl ? [:pl, :en] : [:en, :pl]
158
+ end
159
+
160
+ assert_equal "Name en", u.name
161
+ assert_nil u.title_en
162
+ end
163
+
147
164
  test "globalize attribute names on class without attributes specified in options" do
148
165
  assert_equal [:name_en, :name_pl, :title_en, :title_pl], Unit.globalize_attribute_names
149
166
  end
@@ -166,4 +183,17 @@ class GlobalizeAccessorsTest < ActiveSupport::TestCase
166
183
  assert_raise(NoMethodError) { Unit.new.globalize_attribute_names = [:name] }
167
184
  assert_raise(NoMethodError) { Unit.new.globalize_locales = [:en, :de] }
168
185
  end
186
+
187
+ test "instance without accessors" do
188
+ refute UnitWithoutAccessors.new.respond_to?(:localized_attr_name_for)
189
+ end
190
+
191
+ test "dirty checking" do
192
+ u = Unit.create!(:name_en => "Name en")
193
+ u.name_en = "New name en"
194
+ assert u.changed.include?("name_en")
195
+ assert ["Name en", "New name en"], u.changes["name_en"]
196
+ u.save!
197
+ assert ! u.changed.include?("name_en")
198
+ end
169
199
  end
@@ -1,7 +1,7 @@
1
1
  require 'i18n'
2
2
  I18n.available_locales = [:en, :pl]
3
3
 
4
- require 'test/unit'
4
+ require 'minitest/autorun'
5
5
  require 'active_support'
6
6
  require 'active_support/test_case'
7
7
  require 'logger'
@@ -12,6 +12,7 @@ plugin_test_dir = File.dirname(__FILE__)
12
12
 
13
13
  ActiveRecord::Base.logger = Logger.new(File.join(plugin_test_dir, "debug.log"))
14
14
  ActiveRecord::Base.configurations = YAML::load( IO.read( File.join(plugin_test_dir, 'db', 'database.yml') ) )
15
- ActiveRecord::Base.establish_connection("sqlite3mem")
15
+ ActiveRecord::Base.establish_connection(:sqlite3mem)
16
16
  ActiveRecord::Migration.verbose = false
17
+ ActiveSupport::TestCase.test_order = :sorted
17
18
  load(File.join(plugin_test_dir, "db", "schema.rb"))
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.3
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomasz Stachewicz
@@ -9,67 +9,67 @@ authors:
9
9
  - Steve Verlinden
10
10
  - Robert Pankowecki
11
11
  - Chris Salzberg
12
- autorequire:
12
+ autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2013-12-25 00:00:00.000000000 Z
15
+ date: 2021-01-25 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: '3'
23
+ version: 5.0.0
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: '3'
30
+ version: 5.0.0
31
31
  - !ruby/object:Gem::Dependency
32
- name: bundler
32
+ name: sqlite3
33
33
  requirement: !ruby/object:Gem::Requirement
34
34
  requirements:
35
- - - ~>
35
+ - - ">="
36
36
  - !ruby/object:Gem::Version
37
- version: 1.3.5
37
+ version: '0'
38
38
  type: :development
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
41
41
  requirements:
42
- - - ~>
42
+ - - ">="
43
43
  - !ruby/object:Gem::Version
44
- version: 1.3.5
44
+ version: '0'
45
45
  - !ruby/object:Gem::Dependency
46
46
  name: rake
47
47
  requirement: !ruby/object:Gem::Requirement
48
48
  requirements:
49
- - - ~>
49
+ - - ">="
50
50
  - !ruby/object:Gem::Version
51
- version: 0.9.2
51
+ version: '0'
52
52
  type: :development
53
53
  prerelease: false
54
54
  version_requirements: !ruby/object:Gem::Requirement
55
55
  requirements:
56
- - - ~>
56
+ - - ">="
57
57
  - !ruby/object:Gem::Version
58
- version: 0.9.2
58
+ version: '0'
59
59
  - !ruby/object:Gem::Dependency
60
- name: sqlite3
60
+ name: minitest
61
61
  requirement: !ruby/object:Gem::Requirement
62
62
  requirements:
63
- - - '>='
63
+ - - "~>"
64
64
  - !ruby/object:Gem::Version
65
- version: '0'
65
+ version: '5.1'
66
66
  type: :development
67
67
  prerelease: false
68
68
  version_requirements: !ruby/object:Gem::Requirement
69
69
  requirements:
70
- - - '>='
70
+ - - "~>"
71
71
  - !ruby/object:Gem::Version
72
- version: '0'
72
+ version: '5.1'
73
73
  description: Define methods for accessing translated attributes
74
74
  email:
75
75
  - tomekrs@o2.pl
@@ -81,8 +81,8 @@ executables: []
81
81
  extensions: []
82
82
  extra_rdoc_files: []
83
83
  files:
84
- - .gitignore
85
- - .travis.yml
84
+ - ".gitignore"
85
+ - ".travis.yml"
86
86
  - Gemfile
87
87
  - MIT-LICENSE
88
88
  - Rakefile
@@ -98,24 +98,23 @@ files:
98
98
  homepage: http://rubygems.org/gems/globalize-accessors
99
99
  licenses: []
100
100
  metadata: {}
101
- post_install_message:
101
+ post_install_message:
102
102
  rdoc_options: []
103
103
  require_paths:
104
104
  - lib
105
105
  required_ruby_version: !ruby/object:Gem::Requirement
106
106
  requirements:
107
- - - '>='
107
+ - - ">="
108
108
  - !ruby/object:Gem::Version
109
109
  version: '0'
110
110
  required_rubygems_version: !ruby/object:Gem::Requirement
111
111
  requirements:
112
- - - '>='
112
+ - - ">="
113
113
  - !ruby/object:Gem::Version
114
- version: 1.3.6
114
+ version: '1.3'
115
115
  requirements: []
116
- rubyforge_project: globalize-accessors
117
- rubygems_version: 2.0.3
118
- signing_key:
116
+ rubygems_version: 3.2.3
117
+ signing_key:
119
118
  specification_version: 4
120
119
  summary: Define methods for accessing translated attributes
121
120
  test_files: []