globalize-accessors 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7defb92b751ffc09ccb3dfe0dc0c8df08488e1dd
4
+ data.tar.gz: a620943951380974008d867118a3f82ccd6c505b
5
+ SHA512:
6
+ metadata.gz: 0c29be7111eafd912a409f0e5fd35b8d67d36ed2fff702c7505e79c5530c824eed6b829bd142d65105d0bb035e64326b23ce0b6fdfbeb176d8f60ef84b2d702a
7
+ data.tar.gz: 26615c51f8318312099d7ff03704d516d627b51d1c71e6b960f8c3d0b8ba3d73064e88132bf6d301ef3d7c150e7b236d0fd051259aa7e00fd9a233ebf76023ba
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ nbproject
5
+ *.sqlite3
6
+ test/debug.log
7
+ test/db/database.yml
8
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,56 @@
1
+ = Globalize Accessors
2
+
3
+ == Introduction
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.
6
+
7
+ This way a single form can be used to edit given model fields with all anticipated translations.
8
+
9
+ Note that globalize-accessors is currently only compatible with the globalize3 gem for Rails 3.x.
10
+
11
+
12
+ == Installation
13
+
14
+ gem install globalize-accessors
15
+
16
+ == Example
17
+
18
+ Definition like this:
19
+
20
+ class Product
21
+ translates :title, :description
22
+ globalize_accessors :locales => [:en, :pl], :attributes => [:title]
23
+ end
24
+
25
+ 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).
26
+
27
+ `:locales` and `:attributes` are optional. Default values are:
28
+
29
+ :locales => I18n.available_locales
30
+ :attributes => translated_attribute_names
31
+
32
+ which means that skipping all options will generate you accessor method for all translated fields and available languages.
33
+
34
+ You can also get the accessor locales for a class with the `globalize_locales` method:
35
+
36
+ Product.globalize_locales # => [:en, :pl]
37
+
38
+ == Always define accessors
39
+
40
+ 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:
41
+
42
+ module TranslatesWithAccessors
43
+
44
+ def translates(*params)
45
+ options = params.extract_options!
46
+ options.reverse_merge!(:globalize_accessors => true)
47
+ accessors = options.delete(:globalize_accessors)
48
+ super
49
+ globalize_accessors if accessors
50
+ end
51
+
52
+ end
53
+
54
+ == Licence
55
+
56
+ Copyright (c) 2009-2013 Tomek "Tomash" Stachewicz (http://tomash.wrug.eu), Robert Pankowecki (http://robert.pankowecki.pl) released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+
8
+ Bundler::GemHelper.install_tasks
9
+ require 'rdoc/task'
10
+ require 'rake/testtask'
11
+ require 'rake'
12
+
13
+ desc 'Default: run unit tests.'
14
+ task :default => :test
15
+
16
+ desc 'Test the easy_globalize_accessors plugin.'
17
+ Rake::TestTask.new(:test) do |t|
18
+ t.libs << 'lib'
19
+ t.libs << 'test'
20
+ t.pattern = 'test/**/*_test.rb'
21
+ t.verbose = true
22
+ end
23
+
24
+ desc 'Generate documentation for the easy_globalize_accessors plugin.'
25
+ Rake::RDocTask.new(:rdoc) do |rdoc|
26
+ rdoc.rdoc_dir = 'rdoc'
27
+ rdoc.title = 'EasyGlobalizeAccessors'
28
+ rdoc.options << '--line-numbers' << '--inline-source'
29
+ rdoc.rdoc_files.include('README')
30
+ rdoc.rdoc_files.include('lib/**/*.rb')
31
+ end
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path("../lib/globalize-accessors/version", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "globalize-accessors"
6
+ s.version = GlobalizeAccessors::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Tomasz Stachewicz", "Wojciech Pietrzak", "Steve Verlinden", "Robert Pankowecki", "Chris Salzberg"]
9
+ s.email = ["tomekrs@o2.pl", "steve.verlinden@gmail.com", "robert.pankowecki@gmail.com", "rpa@gavdi.com", "chrissalzberg@gmail.com"]
10
+ s.homepage = "http://rubygems.org/gems/globalize-accessors"
11
+ s.summary = "Define methods for accessing translated attributes"
12
+ s.description = "Define methods for accessing translated attributes"
13
+
14
+ s.required_rubygems_version = ">= 1.3.6"
15
+ s.rubyforge_project = "globalize-accessors"
16
+
17
+ s.add_dependency "globalize3", "~> 0.3.0"
18
+
19
+ s.add_development_dependency "bundler", "~> 1.3.5"
20
+ s.add_development_dependency "rake", "~> 0.9.2"
21
+ s.add_development_dependency "sqlite3"
22
+
23
+
24
+ s.files = `git ls-files`.split("\n")
25
+ s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
26
+ s.require_path = 'lib'
27
+ end
@@ -0,0 +1,50 @@
1
+ require 'globalize3'
2
+
3
+ module GlobalizeAccessors
4
+ attr_reader :globalize_locales
5
+
6
+ def globalize_accessors(options = {})
7
+ options.reverse_merge!(:locales => I18n.available_locales, :attributes => translated_attribute_names)
8
+ @globalize_locales = options[:locales]
9
+
10
+ each_attribute_and_locale(options) do |attr_name, locale|
11
+ define_accessors(attr_name, locale)
12
+ end
13
+ end
14
+
15
+
16
+ private
17
+
18
+
19
+ def define_accessors(attr_name, locale)
20
+ define_getter(attr_name, locale)
21
+ define_setter(attr_name, locale)
22
+ end
23
+
24
+
25
+ def define_getter(attr_name, locale)
26
+ define_method :"#{attr_name}_#{locale.to_s.underscore}" do
27
+ read_attribute(attr_name, :locale => locale)
28
+ end
29
+ end
30
+
31
+ def define_setter(attr_name, locale)
32
+ define_method :"#{attr_name}_#{locale.to_s.underscore}=" do |value|
33
+ write_attribute(attr_name, value, :locale => locale)
34
+ end
35
+ if respond_to?(:accessible_attributes) && accessible_attributes.include?(attr_name)
36
+ attr_accessible :"#{attr_name}_#{locale.to_s.underscore}"
37
+ end
38
+ end
39
+
40
+ def each_attribute_and_locale(options)
41
+ options[:attributes].each do |attr_name|
42
+ options[:locales].each do |locale|
43
+ yield attr_name, locale
44
+ end
45
+ end
46
+ end
47
+
48
+ end
49
+
50
+ ActiveRecord::Base.extend GlobalizeAccessors
@@ -0,0 +1,3 @@
1
+ module GlobalizeAccessors
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,21 @@
1
+ sqlite3:
2
+ adapter: sqlite3
3
+ dbfile: globalize.sqlite3
4
+ database: globalize.sqlite3
5
+ sqlite3mem:
6
+ adapter: sqlite3
7
+ dbfile: ":memory:"
8
+ database: globalize.sqlite3
9
+ postgresql:
10
+ adapter: postgresql
11
+ username: postgres
12
+ password: postgres
13
+ database: globalize
14
+ min_messages: ERROR
15
+ mysql:
16
+ adapter: mysql
17
+ host: localhost
18
+ username: root
19
+ password:
20
+ database: globalize
21
+
data/test/db/schema.rb ADDED
@@ -0,0 +1,14 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+
3
+ create_table "units", :force => true do |t|
4
+ t.integer "capacity"
5
+ end
6
+
7
+ create_table "unit_translations", :force => true do |t|
8
+ t.references :unit
9
+ t.string "name"
10
+ t.string "title"
11
+ t.string "locale"
12
+ end
13
+
14
+ end
@@ -0,0 +1,134 @@
1
+ require 'test_helper'
2
+
3
+ class GlobalizeAccessorsTest < ActiveSupport::TestCase
4
+
5
+ class Unit < ActiveRecord::Base
6
+ translates :name, :title
7
+ globalize_accessors
8
+ end
9
+
10
+ class UnitTranslatedWithOptions < ActiveRecord::Base
11
+ self.table_name = :units
12
+ translates :name
13
+ globalize_accessors :locales => [:pl], :attributes => [:name]
14
+ end
15
+
16
+ class UnitWithAttrAccessible < ActiveRecord::Base
17
+ self.table_name = :units
18
+ attr_accessible :name
19
+ translates :name, :title
20
+ globalize_accessors
21
+ end
22
+
23
+ class UnitWithDashedLocales < ActiveRecord::Base
24
+ self.table_name = :units
25
+ translates :name
26
+ globalize_accessors :locales => [:"pt-BR", :"en-AU"], :attributes => [:name]
27
+ end
28
+
29
+ setup do
30
+ assert_equal :en, I18n.locale
31
+ end
32
+
33
+ test "read and write on new object" do
34
+ u = Unit.new(:name_en => "Name en", :title_pl => "Title pl")
35
+
36
+ assert_equal "Name en", u.name
37
+ assert_equal "Name en", u.name_en
38
+ assert_equal "Title pl", u.title_pl
39
+
40
+ assert_nil u.name_pl
41
+ assert_nil u.title_en
42
+ end
43
+
44
+ test "write on new object and read on saved" do
45
+ u = Unit.create!(:name_en => "Name en", :title_pl => "Title pl")
46
+
47
+ assert_equal "Name en", u.name
48
+ assert_equal "Name en", u.name_en
49
+ assert_equal "Title pl", u.title_pl
50
+
51
+ assert_nil u.name_pl
52
+ assert_nil u.title_en
53
+ end
54
+
55
+ test "read on existing object" do
56
+ u = Unit.create!(:name_en => "Name en", :title_pl => "Title pl")
57
+ u = Unit.find(u.id)
58
+
59
+ assert_equal "Name en", u.name
60
+ assert_equal "Name en", u.name_en
61
+ assert_equal "Title pl", u.title_pl
62
+
63
+ assert_nil u.name_pl
64
+ assert_nil u.title_en
65
+ end
66
+
67
+ test "read and write on existing object" do
68
+ u = Unit.create!(:name_en => "Name en", :title_pl => "Title pl")
69
+ u = Unit.find(u.id)
70
+
71
+ u.name_pl = "Name pl"
72
+ u.name_en = "Name en2"
73
+ u.save!
74
+
75
+ assert_equal "Name en2", u.name
76
+ assert_equal "Name en2", u.name_en
77
+ assert_equal "Name pl", u.name_pl
78
+ assert_equal "Title pl", u.title_pl
79
+
80
+ assert_nil u.title_en
81
+ end
82
+
83
+ test "read and write on class with options" do
84
+ u = UnitTranslatedWithOptions.new()
85
+
86
+ assert u.respond_to?(:name_pl)
87
+ assert u.respond_to?(:name_pl=)
88
+
89
+ assert ! u.respond_to?(:name_en)
90
+ assert ! u.respond_to?(:name_en=)
91
+
92
+ u.name = "Name en"
93
+ u.name_pl = "Name pl"
94
+
95
+ assert_equal "Name en", u.name
96
+ assert_equal "Name pl", u.name_pl
97
+ end
98
+
99
+ test "whitelist locale accessors if the original attribute is whitelisted" do
100
+ u = UnitWithAttrAccessible.new()
101
+ u.update_attributes(:name => "Name en", :name_pl => "Name pl", :title => "Title en", :title_pl => "Title pl")
102
+
103
+ assert_equal "Name en", u.name
104
+ assert_equal "Name pl", u.name_pl
105
+ assert_nil u.title
106
+ assert_nil u.title_pl
107
+ end
108
+
109
+ test "globalize locales on class without locales specified in options" do
110
+ assert_equal [:en, :pl], Unit.globalize_locales
111
+ end
112
+
113
+ test "globalize locales on class with locales specified in options" do
114
+ assert_equal [:pl], UnitTranslatedWithOptions.globalize_locales
115
+ end
116
+
117
+ test "read and write on class with dashed locales" do
118
+ u = UnitWithDashedLocales.new()
119
+
120
+ assert u.respond_to?(:name_pt_br)
121
+ assert u.respond_to?(:name_pt_br=)
122
+
123
+ assert u.respond_to?(:name_en_au)
124
+ assert u.respond_to?(:name_en_au=)
125
+
126
+ u.name = "Name en"
127
+ u.name_pt_br = "Name pt-BR"
128
+ u.name_en_au = "Name en-AU"
129
+
130
+ assert_equal "Name en", u.name
131
+ assert_equal "Name pt-BR", u.name_pt_br
132
+ assert_equal "Name en-AU", u.name_en_au
133
+ end
134
+ end
@@ -0,0 +1,17 @@
1
+ require 'i18n'
2
+ I18n.available_locales = [:en, :pl]
3
+
4
+ require 'test/unit'
5
+ require 'active_support'
6
+ require 'active_support/test_case'
7
+ require 'logger'
8
+
9
+ require 'globalize-accessors'
10
+
11
+ plugin_test_dir = File.dirname(__FILE__)
12
+
13
+ ActiveRecord::Base.logger = Logger.new(File.join(plugin_test_dir, "debug.log"))
14
+ ActiveRecord::Base.configurations = YAML::load( IO.read( File.join(plugin_test_dir, 'db', 'database.yml') ) )
15
+ ActiveRecord::Base.establish_connection("sqlite3mem")
16
+ ActiveRecord::Migration.verbose = false
17
+ load(File.join(plugin_test_dir, "db", "schema.rb"))
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: globalize-accessors
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tomasz Stachewicz
8
+ - Wojciech Pietrzak
9
+ - Steve Verlinden
10
+ - Robert Pankowecki
11
+ - Chris Salzberg
12
+ autorequire:
13
+ bindir: bin
14
+ cert_chain: []
15
+ date: 2013-10-16 00:00:00.000000000 Z
16
+ dependencies:
17
+ - !ruby/object:Gem::Dependency
18
+ name: globalize3
19
+ requirement: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: 0.3.0
24
+ type: :runtime
25
+ prerelease: false
26
+ version_requirements: !ruby/object:Gem::Requirement
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ version: 0.3.0
31
+ - !ruby/object:Gem::Dependency
32
+ name: bundler
33
+ requirement: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 1.3.5
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: 1.3.5
45
+ - !ruby/object:Gem::Dependency
46
+ name: rake
47
+ requirement: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ~>
50
+ - !ruby/object:Gem::Version
51
+ version: 0.9.2
52
+ type: :development
53
+ prerelease: false
54
+ version_requirements: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ~>
57
+ - !ruby/object:Gem::Version
58
+ version: 0.9.2
59
+ - !ruby/object:Gem::Dependency
60
+ name: sqlite3
61
+ requirement: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ description: Define methods for accessing translated attributes
74
+ email:
75
+ - tomekrs@o2.pl
76
+ - steve.verlinden@gmail.com
77
+ - robert.pankowecki@gmail.com
78
+ - rpa@gavdi.com
79
+ - chrissalzberg@gmail.com
80
+ executables: []
81
+ extensions: []
82
+ extra_rdoc_files: []
83
+ files:
84
+ - .gitignore
85
+ - Gemfile
86
+ - MIT-LICENSE
87
+ - README.rdoc
88
+ - Rakefile
89
+ - globalize-accessors.gemspec
90
+ - lib/globalize-accessors.rb
91
+ - lib/globalize-accessors/version.rb
92
+ - test/db/database.yml.example
93
+ - test/db/schema.rb
94
+ - test/globalize_accessors_test.rb
95
+ - test/test_helper.rb
96
+ homepage: http://rubygems.org/gems/globalize-accessors
97
+ licenses: []
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - '>='
111
+ - !ruby/object:Gem::Version
112
+ version: 1.3.6
113
+ requirements: []
114
+ rubyforge_project: globalize-accessors
115
+ rubygems_version: 2.0.3
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Define methods for accessing translated attributes
119
+ test_files: []