easy_globalize3_accessors 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ nbproject
5
+ *.sqlite3
6
+ test/debug.log
7
+ test/db/database.yml
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source :gemcutter
2
+
3
+ # Specify your gem's dependencies in global_boolean.gemspec
4
+ 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,33 @@
1
+ = EasyGlobalizeAccessors
2
+
3
+ == Introduction
4
+
5
+ Generator of easy accessor methods for models using Globalize2.
6
+
7
+ Use globalize_accessors with list of translated fields you want easy 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
+ script/plugin install git://github.com/tomash/easy_globalize2_accessors.git
15
+
16
+ == Example
17
+
18
+ Definition like this:
19
+
20
+ class Product
21
+ translates :title, :description
22
+ globalize_accessors :pl, :en, :de
23
+ end
24
+
25
+ Gives you access to methods: title_pl, title_en, title_de, title_pl=, title_en=, title_de= (and similar set of description_* methods). And they work seamlessly with Globalize2 (not even touching the "core" title, title= methods used by Globalize2 itself).
26
+
27
+ == TODO
28
+
29
+ * Make it work with full-blown locales including dash character (minus sign) like en-US, en-GB, pl-PL etc.
30
+
31
+ == blah blah
32
+
33
+ Copyright (c) 2009-2010 Tomek "Tomash" Stachewicz (http://tomash.wrug.eu), released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ require 'bundler'
2
+ #Bundler.require
3
+ #require 'rake'
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ require 'rake'
7
+ require 'rake/testtask'
8
+ require 'rake/rdoctask'
9
+
10
+ desc 'Default: run unit tests.'
11
+ task :default => :test
12
+
13
+ desc 'Test the easy_globalize_accessors plugin.'
14
+ Rake::TestTask.new(:test) do |t|
15
+ t.libs << 'lib'
16
+ t.libs << 'test'
17
+ t.pattern = 'test/**/*_test.rb'
18
+ t.verbose = true
19
+ end
20
+
21
+ desc 'Generate documentation for the easy_globalize_accessors plugin.'
22
+ Rake::RDocTask.new(:rdoc) do |rdoc|
23
+ rdoc.rdoc_dir = 'rdoc'
24
+ rdoc.title = 'EasyGlobalizeAccessors'
25
+ rdoc.options << '--line-numbers' << '--inline-source'
26
+ rdoc.rdoc_files.include('README')
27
+ rdoc.rdoc_files.include('lib/**/*.rb')
28
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path("../lib/easy_globalize3_accessors/version", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "easy_globalize3_accessors"
6
+ s.version = EasyGlobalize3Accessors::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_globalize3_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_globalize3_accessors"
16
+
17
+ s.add_dependency "globalize3", "~> 0.1.0"
18
+
19
+ s.add_development_dependency "bundler", "~> 1.0.7"
20
+
21
+ s.files = `git ls-files`.split("\n")
22
+ s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
23
+ s.require_path = 'lib'
24
+ end
@@ -0,0 +1,46 @@
1
+ require 'active_record'
2
+ require 'globalize3'
3
+
4
+ module EasyGlobalize3Accessors
5
+
6
+ def globalize_accessors(options = {})
7
+ options.reverse_merge!(:languages => I18n.available_locales, :attributes => translated_attribute_names)
8
+
9
+ each_attribute_and_language(options) do |attr_name, locale|
10
+ define_accessors(attr_name, locale)
11
+ end
12
+ end
13
+
14
+
15
+ private
16
+
17
+
18
+ def define_accessors(attr_name, locale)
19
+ define_getter(attr_name, locale)
20
+ define_setter(attr_name, locale)
21
+ end
22
+
23
+
24
+ def define_getter(attr_name, locale)
25
+ define_method :"#{attr_name}_#{locale}" do
26
+ read_attribute(attr_name, :locale => locale)
27
+ end
28
+ end
29
+
30
+ def define_setter(attr_name, locale)
31
+ define_method :"#{attr_name}_#{locale}=" do |value|
32
+ write_attribute(attr_name, value, :locale => locale)
33
+ end
34
+ end
35
+
36
+ def each_attribute_and_language(options)
37
+ options[:attributes].each do |attr_name|
38
+ options[:languages].each do |locale|
39
+ yield attr_name, locale
40
+ end
41
+ end
42
+ end
43
+
44
+ end
45
+
46
+ ActiveRecord::Base.extend EasyGlobalize3Accessors
@@ -0,0 +1,3 @@
1
+ module EasyGlobalize3Accessors
2
+ VERSION = '1.0.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
+
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,64 @@
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
+ setup do
11
+ assert_equal :en, I18n.locale
12
+ end
13
+
14
+ test "read and write on new object" do
15
+ u = Unit.new(:name_en => "Name en", :title_pl => "Title pl")
16
+
17
+ assert_equal "Name en", u.name
18
+ assert_equal "Name en", u.name_en
19
+ assert_equal "Title pl", u.title_pl
20
+
21
+ assert_nil u.name_pl
22
+ assert_nil u.title_en
23
+ end
24
+
25
+ test "write on new object and read on saved" do
26
+ u = Unit.create!(:name_en => "Name en", :title_pl => "Title pl")
27
+
28
+ assert_equal "Name en", u.name
29
+ assert_equal "Name en", u.name_en
30
+ assert_equal "Title pl", u.title_pl
31
+
32
+ assert_nil u.name_pl
33
+ assert_nil u.title_en
34
+ end
35
+
36
+ test "read on existing object" do
37
+ u = Unit.create!(:name_en => "Name en", :title_pl => "Title pl")
38
+ u = Unit.find(u.id)
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 and write on existing object" do
49
+ u = Unit.create!(:name_en => "Name en", :title_pl => "Title pl")
50
+ u = Unit.find(u.id)
51
+
52
+ u.name_pl = "Name pl"
53
+ u.name_en = "Name en2"
54
+ u.save!
55
+
56
+ assert_equal "Name en2", u.name
57
+ assert_equal "Name en2", u.name_en
58
+ assert_equal "Name pl", u.name_pl
59
+ assert_equal "Title pl", u.title_pl
60
+
61
+ assert_nil u.title_en
62
+ end
63
+
64
+ end
@@ -0,0 +1,17 @@
1
+ require 'test/unit'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
4
+ require 'active_record'
5
+ require 'logger'
6
+
7
+
8
+ plugin_test_dir = File.dirname(__FILE__)
9
+
10
+ ActiveRecord::Base.logger = Logger.new(File.join(plugin_test_dir, "debug.log"))
11
+ ActiveRecord::Base.configurations = YAML::load( IO.read( File.join(plugin_test_dir, 'db', 'database.yml') ) )
12
+ ActiveRecord::Base.establish_connection("sqlite3mem")
13
+ ActiveRecord::Migration.verbose = false
14
+ load(File.join(plugin_test_dir, "db", "schema.rb"))
15
+
16
+ require 'easy_globalize3_accessors'
17
+ I18n.available_locales = [:en, :pl]
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easy_globalize3_accessors
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
10
+ platform: ruby
11
+ authors:
12
+ - Tomasz Stachewicz
13
+ - Wojciech Pietrzak
14
+ - Steve Verlinden
15
+ - Robert Pankowecki
16
+ autorequire:
17
+ bindir: bin
18
+ cert_chain: []
19
+
20
+ date: 2011-02-01 00:00:00 +01:00
21
+ default_executable:
22
+ dependencies:
23
+ - !ruby/object:Gem::Dependency
24
+ name: globalize3
25
+ prerelease: false
26
+ requirement: &id001 !ruby/object:Gem::Requirement
27
+ none: false
28
+ requirements:
29
+ - - ~>
30
+ - !ruby/object:Gem::Version
31
+ segments:
32
+ - 0
33
+ - 1
34
+ - 0
35
+ version: 0.1.0
36
+ type: :runtime
37
+ version_requirements: *id001
38
+ - !ruby/object:Gem::Dependency
39
+ name: bundler
40
+ prerelease: false
41
+ requirement: &id002 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ segments:
47
+ - 1
48
+ - 0
49
+ - 7
50
+ version: 1.0.7
51
+ type: :development
52
+ version_requirements: *id002
53
+ description: Define methods for accessing translated attributes
54
+ email:
55
+ - tomekrs@o2.pl
56
+ - steve.verlinden@gmail.com
57
+ - robert.pankowecki@gmail.com
58
+ - rpa@gavdi.com
59
+ executables: []
60
+
61
+ extensions: []
62
+
63
+ extra_rdoc_files: []
64
+
65
+ files:
66
+ - .gitignore
67
+ - Gemfile
68
+ - MIT-LICENSE
69
+ - README.rdoc
70
+ - Rakefile
71
+ - easy_globalize3_accessors.gemspec
72
+ - lib/easy_globalize3_accessors.rb
73
+ - lib/easy_globalize3_accessors/version.rb
74
+ - test/db/database.yml.example
75
+ - test/db/schema.rb
76
+ - test/easy_globalize_accessors_test.rb
77
+ - test/test_helper.rb
78
+ has_rdoc: true
79
+ homepage: http://rubygems.org/gems/easy_globalize3_accessors
80
+ licenses: []
81
+
82
+ post_install_message:
83
+ rdoc_options: []
84
+
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ segments:
93
+ - 0
94
+ version: "0"
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ segments:
101
+ - 1
102
+ - 3
103
+ - 6
104
+ version: 1.3.6
105
+ requirements: []
106
+
107
+ rubyforge_project: easy_globalize3_accessors
108
+ rubygems_version: 1.3.7
109
+ signing_key:
110
+ specification_version: 3
111
+ summary: Define methods for accessing translated attributes
112
+ test_files: []
113
+