easy_globalize_accessors 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 831a100cb31518b7ca616bfaa32fc61c4865b5f5
4
+ data.tar.gz: bcda133f4e25418063f88774b4a66fab63257023
5
+ SHA512:
6
+ metadata.gz: 77b7fb7c387808ec7401e28b7fd6418c1b3a03550988f89e5271b6aaf21c5ff47af7ee70d0f2bb662742d5eaa06c7c6aa5101881104f4a5671fcf3780de02296
7
+ data.tar.gz: 49c783c71657ba03b468016b6c0396b68c007e098d08d08f17d7efb90207b4138b16432e8e457435c6ec2ee9118d1c1e27839e5fc7c3cf41404e77b738834e58
@@ -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,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in global_boolean.gemspec
4
+ gemspec
@@ -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.
@@ -0,0 +1,54 @@
1
+ = EasyGlobalizeAccessors
2
+
3
+ == Introduction
4
+
5
+ Generator of easy accessor methods for models using Globalize.
6
+
7
+ Use globalize_accessors with list of translated fields you want easily access to and extra :locales array listing locales for which you want the accessors to be generated.
8
+
9
+ This way a single form can be used to edit given model fields with all anticipated translations.
10
+
11
+
12
+ == Installation
13
+
14
+ gem install easy_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 similar set of description_* methods). And 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
+ :locales => I18n.available_locales
29
+ :attributes => translated_attribute_names
30
+
31
+ which means that skipping all options will generate you accessor method for all translated fields and available languages.
32
+
33
+ You can also get locales for class with "globalize_locales" method:
34
+ Product.globalize_locales # => [:en, :pl]
35
+
36
+ == Always define accessors
37
+
38
+ If you wish to always define accessors and don't want to call globalize_accessors method in every class, you can extend ActiveRecord::Base with such module:
39
+
40
+ module TranslatesWithAccessors
41
+
42
+ def translates(*params)
43
+ options = params.extract_options!
44
+ options.reverse_merge!(:globalize_accessors => true)
45
+ accessors = options.delete(:globalize_accessors)
46
+ super
47
+ globalize_accessors if accessors
48
+ end
49
+
50
+ end
51
+
52
+ == Licence
53
+
54
+ Copyright (c) 2009-2010 Tomek "Tomash" Stachewicz (http://tomash.wrug.eu), Robert Pankowecki (http://robert.pankowecki.pl) released under the MIT license
@@ -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/easy_globalize_accessors/version", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "easy_globalize_accessors"
6
+ s.version = EasyGlobalizeAccessors::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Tomasz Stachewicz", "Wojciech Pietrzak", "Steve Verlinden", "Robert Pankowecki"]
9
+ s.email = ["tomekrs@o2.pl", "steve.verlinden@gmail.com", "robert.pankowecki@gmail.com", "rpa@gavdi.com"]
10
+ s.homepage = "http://rubygems.org/gems/easy_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 = "easy_globalize_accessors"
16
+
17
+ s.add_dependency "globalize", ">= 4.0.0.alpha.1"
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,47 @@
1
+ require 'globalize'
2
+
3
+ module EasyGlobalizeAccessors
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
+ end
36
+
37
+ def each_attribute_and_locale(options)
38
+ options[:attributes].each do |attr_name|
39
+ options[:locales].each do |locale|
40
+ yield attr_name, locale
41
+ end
42
+ end
43
+ end
44
+
45
+ end
46
+
47
+ ActiveRecord::Base.extend EasyGlobalizeAccessors
@@ -0,0 +1,3 @@
1
+ module EasyGlobalizeAccessors
2
+ VERSION = '1.4.0'
3
+ end
@@ -0,0 +1,21 @@
1
+ sqlite3:
2
+ adapter: sqlite3
3
+ dbfile: easy_globalize.sqlite3
4
+ database: easy_globalize.sqlite3
5
+ sqlite3mem:
6
+ adapter: sqlite3
7
+ dbfile: ":memory:"
8
+ database: easy_globalize.sqlite3
9
+ postgresql:
10
+ adapter: postgresql
11
+ username: postgres
12
+ password: postgres
13
+ database: easy_globalize
14
+ min_messages: ERROR
15
+ mysql:
16
+ adapter: mysql
17
+ host: localhost
18
+ username: root
19
+ password:
20
+ database: easy_globalize
21
+
@@ -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,117 @@
1
+ require 'test_helper'
2
+
3
+ class EasyGlobalizeAccessorsTest < 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 UnitWithDashedLocales < ActiveRecord::Base
17
+ self.table_name = :units
18
+ translates :name
19
+ globalize_accessors :locales => [:"pt-BR", :"en-AU"], :attributes => [:name]
20
+ end
21
+
22
+ setup do
23
+ assert_equal :en, I18n.locale
24
+ end
25
+
26
+ test "read and write on new object" do
27
+ u = Unit.new(:name_en => "Name en", :title_pl => "Title pl")
28
+
29
+ assert_equal "Name en", u.name
30
+ assert_equal "Name en", u.name_en
31
+ assert_equal "Title pl", u.title_pl
32
+
33
+ assert_nil u.name_pl
34
+ assert_nil u.title_en
35
+ end
36
+
37
+ test "write on new object and read on saved" do
38
+ u = Unit.create!(:name_en => "Name en", :title_pl => "Title pl")
39
+
40
+ assert_equal "Name en", u.name
41
+ assert_equal "Name en", u.name_en
42
+ assert_equal "Title pl", u.title_pl
43
+
44
+ assert_nil u.name_pl
45
+ assert_nil u.title_en
46
+ end
47
+
48
+ test "read on existing object" do
49
+ u = Unit.create!(:name_en => "Name en", :title_pl => "Title pl")
50
+ u = Unit.find(u.id)
51
+
52
+ assert_equal "Name en", u.name
53
+ assert_equal "Name en", u.name_en
54
+ assert_equal "Title pl", u.title_pl
55
+
56
+ assert_nil u.name_pl
57
+ assert_nil u.title_en
58
+ end
59
+
60
+ test "read and write on existing object" do
61
+ u = Unit.create!(:name_en => "Name en", :title_pl => "Title pl")
62
+ u = Unit.find(u.id)
63
+
64
+ u.name_pl = "Name pl"
65
+ u.name_en = "Name en2"
66
+ u.save!
67
+
68
+ assert_equal "Name en2", u.name
69
+ assert_equal "Name en2", u.name_en
70
+ assert_equal "Name pl", u.name_pl
71
+ assert_equal "Title pl", u.title_pl
72
+
73
+ assert_nil u.title_en
74
+ end
75
+
76
+ test "read and write on class with options" do
77
+ u = UnitTranslatedWithOptions.new()
78
+
79
+ assert u.respond_to?(:name_pl)
80
+ assert u.respond_to?(:name_pl=)
81
+
82
+ assert ! u.respond_to?(:name_en)
83
+ assert ! u.respond_to?(:name_en=)
84
+
85
+ u.name = "Name en"
86
+ u.name_pl = "Name pl"
87
+
88
+ assert_equal "Name en", u.name
89
+ assert_equal "Name pl", u.name_pl
90
+ end
91
+
92
+ test "globalize locales on class without locales specified in options" do
93
+ assert_equal [:en, :pl], Unit.globalize_locales
94
+ end
95
+
96
+ test "globalize locales on class with locales specified in options" do
97
+ assert_equal [:pl], UnitTranslatedWithOptions.globalize_locales
98
+ end
99
+
100
+ test "read and write on class with dashed locales" do
101
+ u = UnitWithDashedLocales.new()
102
+
103
+ assert u.respond_to?(:name_pt_br)
104
+ assert u.respond_to?(:name_pt_br=)
105
+
106
+ assert u.respond_to?(:name_en_au)
107
+ assert u.respond_to?(:name_en_au=)
108
+
109
+ u.name = "Name en"
110
+ u.name_pt_br = "Name pt-BR"
111
+ u.name_en_au = "Name en-AU"
112
+
113
+ assert_equal "Name en", u.name
114
+ assert_equal "Name pt-BR", u.name_pt_br
115
+ assert_equal "Name en-AU", u.name_en_au
116
+ end
117
+ 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 'easy_globalize_accessors' # => globalize => active_record
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,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easy_globalize_accessors
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.4.0
5
+ platform: ruby
6
+ authors:
7
+ - Tomasz Stachewicz
8
+ - Wojciech Pietrzak
9
+ - Steve Verlinden
10
+ - Robert Pankowecki
11
+ autorequire:
12
+ bindir: bin
13
+ cert_chain: []
14
+ date: 2013-11-05 00:00:00.000000000 Z
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: globalize
18
+ requirement: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - '>='
21
+ - !ruby/object:Gem::Version
22
+ version: 4.0.0.alpha.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 4.0.0.alpha.1
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ~>
35
+ - !ruby/object:Gem::Version
36
+ version: 1.3.5
37
+ type: :development
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 1.3.5
44
+ - !ruby/object:Gem::Dependency
45
+ name: rake
46
+ requirement: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ~>
49
+ - !ruby/object:Gem::Version
50
+ version: 0.9.2
51
+ type: :development
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ~>
56
+ - !ruby/object:Gem::Version
57
+ version: 0.9.2
58
+ - !ruby/object:Gem::Dependency
59
+ name: sqlite3
60
+ requirement: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ type: :development
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ description: Define methods for accessing translated attributes
73
+ email:
74
+ - tomekrs@o2.pl
75
+ - steve.verlinden@gmail.com
76
+ - robert.pankowecki@gmail.com
77
+ - rpa@gavdi.com
78
+ executables: []
79
+ extensions: []
80
+ extra_rdoc_files: []
81
+ files:
82
+ - .gitignore
83
+ - Gemfile
84
+ - MIT-LICENSE
85
+ - README.rdoc
86
+ - Rakefile
87
+ - easy_globalize_accessors.gemspec
88
+ - lib/easy_globalize_accessors.rb
89
+ - lib/easy_globalize_accessors/version.rb
90
+ - test/db/database.yml.example
91
+ - test/db/schema.rb
92
+ - test/easy_globalize_accessors_test.rb
93
+ - test/test_helper.rb
94
+ homepage: http://rubygems.org/gems/easy_globalize_accessors
95
+ licenses: []
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: 1.3.6
111
+ requirements: []
112
+ rubyforge_project: easy_globalize_accessors
113
+ rubygems_version: 2.0.6
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: Define methods for accessing translated attributes
117
+ test_files: []