i18n_column 0.0.1

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.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
22
+ .rvmrc
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Philipp Ullmann
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,60 @@
1
+ = i18n_column
2
+
3
+ == Introduction
4
+
5
+ This extension provides the capabilities of storing and retrieving translations from a single database column. The translations are stored as a JSON object i.e. {"en":"Home","de":"Zuhause"}.
6
+
7
+ The current and default language are stored within the class <tt>I18nColumn::Language</tt>. On each request the current language must be set via <tt>I18nColumn::Language.current_lang = 'en'</tt>. If not set the default language is taken. <tt>I18nColumn::Language.current_lang</tt> is used as the JSON key to store a translation i.e. "en":"Home".
8
+
9
+ == Installation
10
+
11
+ * Recently a gem version will be provided on gemcutter.
12
+ * Run <tt>rails generate i18n_column:install</tt>. It will generate a i18n_column initializer file in order to set the default language.
13
+
14
+ == Example
15
+
16
+ <b>Migration</b>
17
+
18
+ class CreateNodes < ActiveRecord::Migration
19
+ def self.up
20
+ create_table(:nodes) do |t|
21
+ t.text(:name)
22
+ end
23
+ end
24
+
25
+ def self.down
26
+ drop_table :nodes
27
+ end
28
+ end
29
+
30
+ <b>Model</b>
31
+
32
+ class Node < ActiveRecord::Base
33
+ i18n_column(:name)
34
+ end
35
+
36
+ <b>Controller</b>
37
+
38
+ class ApplicationController < ActionController::Base
39
+ before_filter(:set_i18n_column_lang)
40
+
41
+ private
42
+
43
+ def set_i18n_column_lang
44
+ I18nColumn::Language.current_lang = params[:lang]
45
+ end
46
+ end
47
+
48
+ <b>Console</b>
49
+
50
+ I18nColumn::Language.current_lang = 'en'
51
+ node = Node.create!(:name => 'Home') => {"en":"Home"}
52
+ node.name => 'Home'
53
+ I18nColumn::Language.current_lang = 'de'
54
+ node.name = 'Zuhause'
55
+ node.save! => {"en":"Home","de":"Zuhause"}
56
+ node.name => 'Zuhause'
57
+
58
+ == Copyright
59
+
60
+ Copyright (c) 2010 Philipp Ullmann. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ require('rubygems')
2
+ require('rake')
3
+ require 'rspec/core/rake_task'
4
+
5
+ begin
6
+ require 'jeweler'
7
+ Jeweler::Tasks.new do |gem|
8
+ gem.name = "i18n_column"
9
+ gem.summary = %Q{Storing and retrieving translations from a single database column}
10
+ gem.description = %Q{This extension provides the capabilities of storing and retrieving translations from a single database column. The translations are serialized using JSON.}
11
+ gem.email = "philipp.ullmann@create.at"
12
+ gem.homepage = "http://github.com/create-philipp-ullmann/i18n_column"
13
+ gem.authors = ["Philipp Ullmann"]
14
+ gem.add_development_dependency('rspec', '>= 2.0.0')
15
+ gem.add_development_dependency('sqlite3-ruby', '>= 1.3.1')
16
+ gem.add_development_dependency('activerecord', '>= 3.0.0')
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts('Jeweler (or a dependency) not available. Install it with: gem install jeweler')
21
+ end
22
+
23
+ desc('Run RSpec')
24
+ RSpec::Core::RakeTask.new do |t|
25
+ t.verbose = false
26
+ end
27
+
28
+ task(:default => :spec)
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,65 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{i18n_column}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Philipp Ullmann"]
12
+ s.date = %q{2010-10-13}
13
+ s.description = %q{This extension provides the capabilities of storing and retrieving translations from a single database column. The translations are serialized using JSON.}
14
+ s.email = %q{philipp.ullmann@create.at}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "i18n_column.gemspec",
27
+ "lib/generators/i18n_column/install/install_generator.rb",
28
+ "lib/generators/i18n_column/install/templates/config/initializers/i18n_column.rb",
29
+ "lib/i18n_column.rb",
30
+ "lib/i18n_column/base.rb",
31
+ "lib/i18n_column/language.rb",
32
+ "spec/.rspec",
33
+ "spec/i18n_column_spec.rb",
34
+ "spec/spec_helper.rb"
35
+ ]
36
+ s.homepage = %q{http://github.com/create-philipp-ullmann/i18n_column}
37
+ s.rdoc_options = ["--charset=UTF-8"]
38
+ s.require_paths = ["lib"]
39
+ s.rubygems_version = %q{1.3.7}
40
+ s.summary = %q{Storing and retrieving translations from a single database column}
41
+ s.test_files = [
42
+ "spec/i18n_column_spec.rb",
43
+ "spec/spec_helper.rb"
44
+ ]
45
+
46
+ if s.respond_to? :specification_version then
47
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
48
+ s.specification_version = 3
49
+
50
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
51
+ s.add_development_dependency(%q<rspec>, [">= 2.0.0"])
52
+ s.add_development_dependency(%q<sqlite3-ruby>, [">= 1.3.1"])
53
+ s.add_development_dependency(%q<activerecord>, [">= 3.0.0"])
54
+ else
55
+ s.add_dependency(%q<rspec>, [">= 2.0.0"])
56
+ s.add_dependency(%q<sqlite3-ruby>, [">= 1.3.1"])
57
+ s.add_dependency(%q<activerecord>, [">= 3.0.0"])
58
+ end
59
+ else
60
+ s.add_dependency(%q<rspec>, [">= 2.0.0"])
61
+ s.add_dependency(%q<sqlite3-ruby>, [">= 1.3.1"])
62
+ s.add_dependency(%q<activerecord>, [">= 3.0.0"])
63
+ end
64
+ end
65
+
@@ -0,0 +1,19 @@
1
+ module I18nColumn
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+
5
+ desc <<DESC
6
+ Description:
7
+ Copy initialize file to your application.
8
+ DESC
9
+
10
+ def self.source_root
11
+ @source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
12
+ end
13
+
14
+ def copy_initializer_file
15
+ template('config/initializers/i18n_column.rb')
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,2 @@
1
+ # On each request the current language must be set via I18nColumn::Language.current_lang = 'de'. If not set the default language ('en') is returned by I18nColumn::Language.current_lang.
2
+ I18nColumn::Language.default_lang = 'en'
@@ -0,0 +1,44 @@
1
+ # This extension provides the capabilities of storing and retrieving translations from a single database column.
2
+ # The translations are serialized using JSON i.e. {"en":"Spindle axes","de":"Spindelachsen"}.
3
+ #
4
+ # Example
5
+ #
6
+ # class Node < ActiveRecord::Base
7
+ # i18n_column(:name)
8
+ # end
9
+ module I18nColumn
10
+ module Base
11
+ def self.included(base)
12
+ base.extend(ClassMethods)
13
+ end
14
+
15
+ module ClassMethods
16
+ # Creates i18n getter and setter methods for the given column names.
17
+ def i18n_column(*col_names)
18
+ for col_name in col_names
19
+ class_eval <<-EOV
20
+ def #{col_name}
21
+ json = #{col_name}_to_json
22
+ json.nil? ? nil : json[::I18nColumn::Language.current_lang]
23
+ end
24
+
25
+ def #{col_name}=(value)
26
+ json = #{col_name}_to_json || {}
27
+ json[::I18nColumn::Language.current_lang] = value
28
+ self[:#{col_name}] = json.to_json
29
+ value
30
+ end
31
+
32
+ private
33
+
34
+ def #{col_name}_to_json
35
+ ::ActiveSupport::JSON::decode(self[:#{col_name}].to_s) || nil
36
+ end
37
+ EOV
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ ActiveRecord::Base.class_eval { include I18nColumn::Base }
@@ -0,0 +1,21 @@
1
+ # Saves the current and default language.
2
+ module I18nColumn
3
+ class Language
4
+ @@default_lang = 'en'
5
+
6
+ # Sets the default language.
7
+ def self.default_lang=(lang)
8
+ @@default_lang = lang
9
+ end
10
+
11
+ # Returns the current language.
12
+ def self.current_lang
13
+ ::Thread.current[:i18n_column_current_lang] || @@default_lang
14
+ end
15
+
16
+ # Sets the current language. Must be set by each request.
17
+ def self.current_lang=(lang)
18
+ ::Thread.current[:i18n_column_current_lang] = lang
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ require('active_record')
2
+ require('i18n_column/base')
3
+ require('i18n_column/language')
data/spec/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,96 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe(I18nColumn::Base) do
4
+ before(:each) do
5
+ setup_db
6
+ end
7
+
8
+ after(:each) do
9
+ teardown_db
10
+ end
11
+
12
+ context('with no translation') do
13
+ before(:each) do
14
+ I18nColumn::Language.current_lang = 'en'
15
+ @node = Node.new
16
+ end
17
+
18
+ describe('#name') do
19
+ it('returns nil') do
20
+ @node.name.should(be_nil)
21
+ end
22
+ end
23
+
24
+ describe('#name=') do
25
+ it('sets the english name') do
26
+ @node.name = 'Home'
27
+ @node.name.should == 'Home'
28
+ end
29
+ end
30
+ end
31
+
32
+ context('with an english name only') do
33
+ before(:each) do
34
+ I18nColumn::Language.current_lang = 'en'
35
+ @node = Node.create!(:name => 'Home')
36
+ end
37
+
38
+ describe('#name') do
39
+ it('returns the english name') do
40
+ @node.name.should == 'Home'
41
+ end
42
+ end
43
+
44
+ describe('#name=') do
45
+ it('overrides the english name') do
46
+ @node.name = 'Home overridden'
47
+ @node.name.should == 'Home overridden'
48
+ end
49
+
50
+ it('translates the name into german') do
51
+ I18nColumn::Language.current_lang = 'de'
52
+ @node.name = 'Zuhause'
53
+ @node.name.should == 'Zuhause'
54
+ end
55
+ end
56
+ end
57
+
58
+ context('with an english and german name') do
59
+ before(:each) do
60
+ I18nColumn::Language.current_lang = 'en'
61
+ @node = Node.create!(:name => 'Home')
62
+ I18nColumn::Language.current_lang = 'de'
63
+ @node.update_attribute(:name, 'Zuhause')
64
+ end
65
+
66
+ describe('#name') do
67
+ it('returns the english name') do
68
+ I18nColumn::Language.current_lang = 'en'
69
+ @node.name.should == 'Home'
70
+ end
71
+
72
+ it('returns the german name') do
73
+ I18nColumn::Language.current_lang = 'de'
74
+ @node.name.should == 'Zuhause'
75
+ end
76
+ end
77
+
78
+ describe('#name=') do
79
+ it('overrides the english name only') do
80
+ I18nColumn::Language.current_lang = 'en'
81
+ @node.name = 'Home overridden'
82
+ @node.name.should == 'Home overridden'
83
+ I18nColumn::Language.current_lang = 'de'
84
+ @node.name.should == 'Zuhause'
85
+ end
86
+
87
+ it('overrides the german name only') do
88
+ I18nColumn::Language.current_lang = 'de'
89
+ @node.name = 'Zuhause ueberschrieben'
90
+ @node.name.should == 'Zuhause ueberschrieben'
91
+ I18nColumn::Language.current_lang = 'en'
92
+ @node.name.should == 'Home'
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,25 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'rubygems'
4
+ require 'active_record'
5
+ require 'i18n_column'
6
+
7
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
8
+
9
+ def setup_db
10
+ ActiveRecord::Schema.define(:version => 1) do
11
+ create_table(:nodes) do |t|
12
+ t.text(:name)
13
+ end
14
+ end
15
+ end
16
+
17
+ def teardown_db
18
+ ActiveRecord::Base.connection.tables.each do |table|
19
+ ActiveRecord::Base.connection.drop_table(table)
20
+ end
21
+ end
22
+
23
+ class Node < ActiveRecord::Base
24
+ i18n_column(:name)
25
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: i18n_column
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Philipp Ullmann
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-13 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 15
30
+ segments:
31
+ - 2
32
+ - 0
33
+ - 0
34
+ version: 2.0.0
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: sqlite3-ruby
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 25
46
+ segments:
47
+ - 1
48
+ - 3
49
+ - 1
50
+ version: 1.3.1
51
+ type: :development
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: activerecord
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 7
62
+ segments:
63
+ - 3
64
+ - 0
65
+ - 0
66
+ version: 3.0.0
67
+ type: :development
68
+ version_requirements: *id003
69
+ description: This extension provides the capabilities of storing and retrieving translations from a single database column. The translations are serialized using JSON.
70
+ email: philipp.ullmann@create.at
71
+ executables: []
72
+
73
+ extensions: []
74
+
75
+ extra_rdoc_files:
76
+ - LICENSE
77
+ - README.rdoc
78
+ files:
79
+ - .document
80
+ - .gitignore
81
+ - LICENSE
82
+ - README.rdoc
83
+ - Rakefile
84
+ - VERSION
85
+ - i18n_column.gemspec
86
+ - lib/generators/i18n_column/install/install_generator.rb
87
+ - lib/generators/i18n_column/install/templates/config/initializers/i18n_column.rb
88
+ - lib/i18n_column.rb
89
+ - lib/i18n_column/base.rb
90
+ - lib/i18n_column/language.rb
91
+ - spec/.rspec
92
+ - spec/i18n_column_spec.rb
93
+ - spec/spec_helper.rb
94
+ has_rdoc: true
95
+ homepage: http://github.com/create-philipp-ullmann/i18n_column
96
+ licenses: []
97
+
98
+ post_install_message:
99
+ rdoc_options:
100
+ - --charset=UTF-8
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ hash: 3
109
+ segments:
110
+ - 0
111
+ version: "0"
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ hash: 3
118
+ segments:
119
+ - 0
120
+ version: "0"
121
+ requirements: []
122
+
123
+ rubyforge_project:
124
+ rubygems_version: 1.3.7
125
+ signing_key:
126
+ specification_version: 3
127
+ summary: Storing and retrieving translations from a single database column
128
+ test_files:
129
+ - spec/i18n_column_spec.rb
130
+ - spec/spec_helper.rb