sequel-localize 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/.gitignore +14 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +31 -0
- data/LICENSE +22 -0
- data/Rakefile +1 -0
- data/TODO +8 -0
- data/lib/sequel/localize/language.rb +27 -0
- data/lib/sequel/localize/version.rb +7 -0
- data/lib/sequel/localize.rb +123 -0
- data/sequel-localize.gemspec +25 -0
- data/spec/localize_spec.rb +87 -0
- data/spec/spec.opts +3 -0
- data/spec/spec_helper.rb +25 -0
- data/spec/support/classes.rb +38 -0
- metadata +150 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
sequel-localize (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.2.2)
|
10
|
+
rake (10.0.4)
|
11
|
+
rspec (2.13.0)
|
12
|
+
rspec-core (~> 2.13.0)
|
13
|
+
rspec-expectations (~> 2.13.0)
|
14
|
+
rspec-mocks (~> 2.13.0)
|
15
|
+
rspec-core (2.13.1)
|
16
|
+
rspec-expectations (2.13.0)
|
17
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
18
|
+
rspec-mocks (2.13.1)
|
19
|
+
sequel (3.46.0)
|
20
|
+
sqlite3 (1.3.7)
|
21
|
+
|
22
|
+
PLATFORMS
|
23
|
+
ruby
|
24
|
+
|
25
|
+
DEPENDENCIES
|
26
|
+
bundler
|
27
|
+
rake
|
28
|
+
rspec
|
29
|
+
sequel
|
30
|
+
sequel-localize!
|
31
|
+
sqlite3
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Jonas von Andrian
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/TODO
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
class Language < Sequel::Model
|
2
|
+
plugin :validation_helpers
|
3
|
+
def validate
|
4
|
+
super
|
5
|
+
validates_presence [:code, :name]
|
6
|
+
validates_unique :code
|
7
|
+
# locale string like 'en'
|
8
|
+
validates_format /^[a-z]{2}$/, :code
|
9
|
+
end
|
10
|
+
|
11
|
+
def after_create
|
12
|
+
Sequel::Plugins::Localize.localized_models.each do |m|
|
13
|
+
m.add_translation_accessors code
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class << self
|
18
|
+
def [](code)
|
19
|
+
if code.respond_to? :to_sym
|
20
|
+
code = code.to_sym
|
21
|
+
(@cache ||= {})[code] ||= super(:code => code.to_s) || create(:code => code, :name => code)
|
22
|
+
else
|
23
|
+
super
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,123 @@
|
|
1
|
+
require 'sequel/localize/language'
|
2
|
+
require "sequel/localize/version"
|
3
|
+
|
4
|
+
module Sequel
|
5
|
+
module Plugins
|
6
|
+
module Localize
|
7
|
+
def self.configure(model, opts={})
|
8
|
+
model._init_translations
|
9
|
+
localized_models << model
|
10
|
+
end
|
11
|
+
def self.localized_models
|
12
|
+
@localized_models ||= []
|
13
|
+
end
|
14
|
+
module ClassMethods
|
15
|
+
def _init_translations
|
16
|
+
@@_lowercase_name = underscore(demodulize(self.to_s))
|
17
|
+
create_translation_class
|
18
|
+
create_translated_field_methods
|
19
|
+
create_translation_accessors
|
20
|
+
end
|
21
|
+
def localized_fields
|
22
|
+
@@localized_fields ||= translation_class.columns - [translation_class.primary_key, :"#{@@_lowercase_name}_id", :language_id]
|
23
|
+
end
|
24
|
+
def translation_class
|
25
|
+
@@translation_class ||= Object.const_get("#{self}Translation")
|
26
|
+
end
|
27
|
+
def add_translation_accessors(code)
|
28
|
+
create_translation_writer(code)
|
29
|
+
create_translation_reader(code)
|
30
|
+
end
|
31
|
+
|
32
|
+
protected
|
33
|
+
|
34
|
+
def create_translation_class
|
35
|
+
one_to_many :"#{@@_lowercase_name}_translations"
|
36
|
+
alias_method :translations_dataset, :"#{@@_lowercase_name}_translations_dataset"
|
37
|
+
alias_method :translations, :"#{@@_lowercase_name}_translations"
|
38
|
+
alias_method :add_translation, :"add_#{@@_lowercase_name}_translation"
|
39
|
+
self.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
|
40
|
+
class ::#{self}Translation < Sequel::Model
|
41
|
+
many_to_one :#{@@_lowercase_name}
|
42
|
+
many_to_one :language
|
43
|
+
end
|
44
|
+
RUBY
|
45
|
+
end
|
46
|
+
def create_translated_field_methods
|
47
|
+
localized_fields.each do |field_name|
|
48
|
+
self.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
|
49
|
+
def #{field_name}(locale = I18n.site_locale)
|
50
|
+
(translation(locale) || translation(default_locale)).#{field_name}
|
51
|
+
end
|
52
|
+
def #{field_name}=(value, locale)
|
53
|
+
translation(locale).#{field_name}= value
|
54
|
+
end
|
55
|
+
RUBY
|
56
|
+
end
|
57
|
+
end
|
58
|
+
def create_translation_accessors
|
59
|
+
Language.all.each do |l|
|
60
|
+
create_translation_writer(l.code)
|
61
|
+
create_translation_reader(l.code)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def create_translation_writer(locale)
|
66
|
+
self.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
|
67
|
+
def #{locale}=(attributes = {})
|
68
|
+
attributes.each do |method, value|
|
69
|
+
modified!
|
70
|
+
self.send :"\#{method}=", value, :#{locale}
|
71
|
+
end
|
72
|
+
end
|
73
|
+
RUBY
|
74
|
+
end
|
75
|
+
|
76
|
+
def create_translation_reader(locale)
|
77
|
+
self.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
|
78
|
+
def #{locale}
|
79
|
+
translation :#{locale}
|
80
|
+
end
|
81
|
+
RUBY
|
82
|
+
end
|
83
|
+
end
|
84
|
+
module InstanceMethods
|
85
|
+
def after_save
|
86
|
+
each_translation do |locale, translation|
|
87
|
+
if translation.pk
|
88
|
+
translation.save
|
89
|
+
else
|
90
|
+
add_translation(translation)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def default_locale
|
96
|
+
'de'
|
97
|
+
end
|
98
|
+
|
99
|
+
def translation(locale)
|
100
|
+
l = Language[locale]
|
101
|
+
(@_translations ||= {})[locale.to_sym] ||=
|
102
|
+
if pk
|
103
|
+
translations_dataset.where(:language_id => l.id).first
|
104
|
+
end || self.class.translation_class.new(:language_id => l.id)
|
105
|
+
end
|
106
|
+
|
107
|
+
def validate
|
108
|
+
super
|
109
|
+
each_translation do |locale, translation|
|
110
|
+
errors.add(locale, translation.errors) unless translation.valid?
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
private
|
115
|
+
|
116
|
+
def each_translation(&block)
|
117
|
+
return unless @_translations
|
118
|
+
@_translations.each(&block)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sequel/localize/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "sequel-localize"
|
8
|
+
spec.version = Sequel::Plugins::Localize::VERSION
|
9
|
+
spec.authors = ["Jonas von Andrian"]
|
10
|
+
spec.email = ["jvadev@gmail.com"]
|
11
|
+
spec.description = %q{Localize plugin for Sequel}
|
12
|
+
spec.summary = %q{Adds support to localize Sequel::Model. Uses one tabel per model}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler"
|
21
|
+
spec.add_development_dependency "rake"
|
22
|
+
spec.add_development_dependency "sqlite3"
|
23
|
+
spec.add_development_dependency "sequel"
|
24
|
+
spec.add_development_dependency "rspec"
|
25
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper.rb')
|
2
|
+
|
3
|
+
describe 'description' do
|
4
|
+
|
5
|
+
it 'should create translations' do
|
6
|
+
a = create_post
|
7
|
+
a.title(:de).should == 'de'
|
8
|
+
a.title('en').should == 'en'
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should hold translations before save' do
|
12
|
+
x = new_post
|
13
|
+
x.title(:de).should == 'de'
|
14
|
+
x.title('en').should == 'en'
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should update translations' do
|
18
|
+
a = create_post
|
19
|
+
a.update(:en => {:title => 'en up'}, :de => {:title => 'de up'})
|
20
|
+
a.title(:de).should == 'de up'
|
21
|
+
a.title(:en).should == 'en up'
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'should add error if attribute missing' do
|
25
|
+
a = Post.new(:de => {:title => ''}, :uri => '')
|
26
|
+
a.save
|
27
|
+
a.errors.on(:uri).should be_true
|
28
|
+
a.errors.on(:de).should be_true
|
29
|
+
a.errors.on(:de).first.on(:title).should be_true
|
30
|
+
a.errors.on(:en).should be_false
|
31
|
+
|
32
|
+
a = Post.new('de' => {:title => ''}, 'en' => {:subtitle => 'asd'}, :uri => '')
|
33
|
+
a.save
|
34
|
+
a.errors.on(:uri).should be_true
|
35
|
+
a.errors.on(:de).should be_true
|
36
|
+
a.errors.on(:de).first.on(:title).should be_true
|
37
|
+
a.errors.on(:en).should be_true
|
38
|
+
a.errors.on(:en).first.on(:title).should be_true
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'should add error if a new translation is not valid' do
|
42
|
+
a = Post.new(:uri => 'test',
|
43
|
+
:de => {
|
44
|
+
:title => 'de'
|
45
|
+
})
|
46
|
+
a.save.should be_true
|
47
|
+
a.update(:en => { :subtitle => 'bla'}).should be(false)
|
48
|
+
|
49
|
+
a.update(:en => {:title => 'en up'}, :de => { :title => '' }).should be(false)
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'should work with single table inheritance' do
|
53
|
+
a = SpecialPost.new(:uri => 'test',
|
54
|
+
:de => {
|
55
|
+
:title => 'de'
|
56
|
+
})
|
57
|
+
a.save.should be_true
|
58
|
+
a.title(:de).should == 'de'
|
59
|
+
SpecialPost.localized_fields.should == Post.localized_fields
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'should create new setters on the fly' do
|
63
|
+
l = Language[:xy]
|
64
|
+
a = Post.new(:uri => 'test',
|
65
|
+
:xy => {
|
66
|
+
:title => 'xy'
|
67
|
+
})
|
68
|
+
a.title(:xy).should == 'xy'
|
69
|
+
a.xy.should be_true
|
70
|
+
end
|
71
|
+
|
72
|
+
def new_post
|
73
|
+
Post.new(:uri => 'test',
|
74
|
+
:de => {
|
75
|
+
:title => 'de'
|
76
|
+
},
|
77
|
+
:en => {
|
78
|
+
:title => 'en'
|
79
|
+
}
|
80
|
+
)
|
81
|
+
end
|
82
|
+
def create_post
|
83
|
+
post = new_post
|
84
|
+
post.save.should be_true
|
85
|
+
post
|
86
|
+
end
|
87
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
require 'rubygems'
|
3
|
+
|
4
|
+
require 'sequel'
|
5
|
+
require 'rspec'
|
6
|
+
require 'rspec/autorun'
|
7
|
+
|
8
|
+
DB = Sequel.sqlite
|
9
|
+
Sequel::Model.plugin :validation_helpers
|
10
|
+
Sequel::Model.raise_on_save_failure = false
|
11
|
+
|
12
|
+
DB.create_table :languages do
|
13
|
+
primary_key :id
|
14
|
+
String :code
|
15
|
+
String :name
|
16
|
+
end
|
17
|
+
|
18
|
+
require 'sequel/localize'
|
19
|
+
|
20
|
+
require 'logger'
|
21
|
+
DB.logger = Logger.new('db.log')
|
22
|
+
|
23
|
+
require 'support/classes'
|
24
|
+
#require 'shared_specs'
|
25
|
+
|
@@ -0,0 +1,38 @@
|
|
1
|
+
DB.create_table :post_translations do
|
2
|
+
primary_key :id
|
3
|
+
String :title
|
4
|
+
String :subtitle
|
5
|
+
integer :language_id
|
6
|
+
integer :post_id
|
7
|
+
end
|
8
|
+
|
9
|
+
DB.create_table :posts do
|
10
|
+
primary_key :id
|
11
|
+
String :uri
|
12
|
+
String :type
|
13
|
+
end
|
14
|
+
|
15
|
+
DB[:languages] << {"name"=>"en", "code" => 'en'}
|
16
|
+
DB[:languages] << {"name"=>"de", "code" => 'de'}
|
17
|
+
|
18
|
+
|
19
|
+
class Post < Sequel::Model
|
20
|
+
plugin :localize
|
21
|
+
plugin :single_table_inheritance, :type
|
22
|
+
|
23
|
+
def validate
|
24
|
+
super
|
25
|
+
validates_presence [:uri]
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class SpecialPost < Post
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
class PostTranslation < Sequel::Model
|
34
|
+
def validate
|
35
|
+
super
|
36
|
+
validates_presence [:title]
|
37
|
+
end
|
38
|
+
end
|
metadata
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sequel-localize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jonas von Andrian
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-23 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: sqlite3
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: sequel
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rspec
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
description: Localize plugin for Sequel
|
95
|
+
email:
|
96
|
+
- jvadev@gmail.com
|
97
|
+
executables: []
|
98
|
+
extensions: []
|
99
|
+
extra_rdoc_files: []
|
100
|
+
files:
|
101
|
+
- .gitignore
|
102
|
+
- Gemfile
|
103
|
+
- Gemfile.lock
|
104
|
+
- LICENSE
|
105
|
+
- Rakefile
|
106
|
+
- TODO
|
107
|
+
- lib/sequel/localize.rb
|
108
|
+
- lib/sequel/localize/language.rb
|
109
|
+
- lib/sequel/localize/version.rb
|
110
|
+
- sequel-localize.gemspec
|
111
|
+
- spec/localize_spec.rb
|
112
|
+
- spec/spec.opts
|
113
|
+
- spec/spec_helper.rb
|
114
|
+
- spec/support/classes.rb
|
115
|
+
homepage: ''
|
116
|
+
licenses:
|
117
|
+
- MIT
|
118
|
+
post_install_message:
|
119
|
+
rdoc_options: []
|
120
|
+
require_paths:
|
121
|
+
- lib
|
122
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
123
|
+
none: false
|
124
|
+
requirements:
|
125
|
+
- - ! '>='
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: '0'
|
128
|
+
segments:
|
129
|
+
- 0
|
130
|
+
hash: 3081936343475499294
|
131
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
132
|
+
none: false
|
133
|
+
requirements:
|
134
|
+
- - ! '>='
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: '0'
|
137
|
+
segments:
|
138
|
+
- 0
|
139
|
+
hash: 3081936343475499294
|
140
|
+
requirements: []
|
141
|
+
rubyforge_project:
|
142
|
+
rubygems_version: 1.8.25
|
143
|
+
signing_key:
|
144
|
+
specification_version: 3
|
145
|
+
summary: Adds support to localize Sequel::Model. Uses one tabel per model
|
146
|
+
test_files:
|
147
|
+
- spec/localize_spec.rb
|
148
|
+
- spec/spec.opts
|
149
|
+
- spec/spec_helper.rb
|
150
|
+
- spec/support/classes.rb
|