sequel_translatable 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +2 -0
- data/README.md +53 -0
- data/Rakefile +6 -0
- data/lib/sequel/plugins/translatable.rb +40 -0
- data/lib/sequel_translatable.rb +1 -0
- data/sequel_translatable.gemspec +22 -0
- data/spec/spec_helper.rb +3 -0
- data/spec/translatable_spec.rb +54 -0
- metadata +100 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
sequel_translatable
|
2
|
+
===================
|
3
|
+
|
4
|
+
Translate model attributes for sequel.
|
5
|
+
|
6
|
+
Dependencies
|
7
|
+
------------
|
8
|
+
|
9
|
+
* Ruby >= 1.9.2
|
10
|
+
* gem "i18n"
|
11
|
+
* gem "sequel"
|
12
|
+
|
13
|
+
Usage
|
14
|
+
-----
|
15
|
+
|
16
|
+
* Declare translatable attribute inside your model:
|
17
|
+
|
18
|
+
class MyModel < Sequel::Model
|
19
|
+
plugin :translatable, :value
|
20
|
+
end
|
21
|
+
|
22
|
+
* or for multiple attributes:
|
23
|
+
|
24
|
+
class MyModel < Sequel::Model
|
25
|
+
plugin :translatable, [:name, :value]
|
26
|
+
end
|
27
|
+
|
28
|
+
* then:
|
29
|
+
|
30
|
+
I18n.locale
|
31
|
+
-> :en
|
32
|
+
m = MyModel.new value_en: "Item", value_fr: "Objet"
|
33
|
+
m.value
|
34
|
+
-> "Item"
|
35
|
+
I18n.locale = :fr
|
36
|
+
m.value
|
37
|
+
-> "Objet"
|
38
|
+
I18n.locale = :en_master
|
39
|
+
m.value
|
40
|
+
-> "Item"
|
41
|
+
m.value = "Object"
|
42
|
+
m.all_values
|
43
|
+
-> {en: "Object", fr: "Objet"}
|
44
|
+
|
45
|
+
Build Status
|
46
|
+
------------
|
47
|
+
|
48
|
+
[![Build Status](http://travis-ci.org/TalentBox/sequel_bitemporal.png)](http://travis-ci.org/TalentBox/sequel_translatable)
|
49
|
+
|
50
|
+
License
|
51
|
+
-------
|
52
|
+
|
53
|
+
sequel_translatable is Copyright © 2011 TalentBox SA. It is free software, and may be redistributed under the terms specified in the LICENSE file.
|
data/Rakefile
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
module Sequel
|
2
|
+
module Plugins
|
3
|
+
module Translatable
|
4
|
+
def self.configure(model, attributes = [])
|
5
|
+
attributes = [*attributes]
|
6
|
+
raise Error, "please specify attributes to use for translatable plugin" if attributes.empty?
|
7
|
+
attributes.each do |attribute|
|
8
|
+
model.class_eval <<-EOS, __FILE__, __LINE__
|
9
|
+
def #{attribute}=(value)
|
10
|
+
send "#{attribute}_\#{base_locale}=", value
|
11
|
+
end
|
12
|
+
def #{attribute}
|
13
|
+
send "#{attribute}_\#{base_locale}"
|
14
|
+
end
|
15
|
+
def #{attribute}_hash
|
16
|
+
@#{attribute}_columns ||= columns.collect do |column|
|
17
|
+
$1 if column=~/\\A#{Regexp.escape attribute}_(.+)\\z/
|
18
|
+
end.compact.sort.collect(&:to_sym)
|
19
|
+
hash = {}
|
20
|
+
@#{attribute}_columns.each do |column|
|
21
|
+
hash[column] = send "#{attribute}_\#{column}"
|
22
|
+
end
|
23
|
+
hash
|
24
|
+
end
|
25
|
+
EOS
|
26
|
+
end
|
27
|
+
end
|
28
|
+
module ClassMethods
|
29
|
+
end
|
30
|
+
module DatasetMethods
|
31
|
+
end
|
32
|
+
module InstanceMethods
|
33
|
+
private
|
34
|
+
def base_locale
|
35
|
+
I18n.locale.to_s[0..1]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require "sequel/plugins/translatable"
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "sequel_translatable"
|
6
|
+
s.version = "0.1.0"
|
7
|
+
s.authors = ["Joseph HALTER"]
|
8
|
+
s.email = ["joseph.halter@thetalentbox.com"]
|
9
|
+
s.homepage = "https://github.com/TalentBox/sequel_translatable"
|
10
|
+
s.summary = "Translate model attributes for sequel."
|
11
|
+
s.description = "Translate model attributes for sequel, fully tested."
|
12
|
+
|
13
|
+
s.files = `git ls-files`.split("\n")
|
14
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
15
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
|
18
|
+
s.add_development_dependency "sqlite3"
|
19
|
+
s.add_development_dependency "rspec"
|
20
|
+
s.add_runtime_dependency "i18n"
|
21
|
+
s.add_runtime_dependency "sequel"
|
22
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "Sequel::Plugins::Translatable" do
|
4
|
+
before :all do
|
5
|
+
DB.create_table! :items do
|
6
|
+
primary_key :id
|
7
|
+
String :name_fr
|
8
|
+
String :name_en
|
9
|
+
String :value_fr
|
10
|
+
String :value_en
|
11
|
+
end
|
12
|
+
@klass = Class.new Sequel::Model do
|
13
|
+
set_dataset :items
|
14
|
+
end
|
15
|
+
end
|
16
|
+
after{ @klass.truncate }
|
17
|
+
it "checks at least one attribute is given" do
|
18
|
+
lambda{
|
19
|
+
@klass.plugin :translatable
|
20
|
+
}.should raise_error Sequel::Error, "please specify attributes to use for translatable plugin"
|
21
|
+
end
|
22
|
+
it "checks value isn't defined before plugin" do
|
23
|
+
@klass.new.should_not respond_to :value
|
24
|
+
end
|
25
|
+
it "allows defining a single attribute" do
|
26
|
+
@klass.plugin :translatable, :value
|
27
|
+
@klass.new.should respond_to :value
|
28
|
+
end
|
29
|
+
it "allows defining multiple attributes" do
|
30
|
+
@klass.plugin :translatable, [:name, :value]
|
31
|
+
@klass.new.should respond_to :name
|
32
|
+
@klass.new.should respond_to :value
|
33
|
+
end
|
34
|
+
it "gets value from current locale" do
|
35
|
+
I18n.locale = :en
|
36
|
+
@klass.plugin :translatable, :value
|
37
|
+
m = @klass.new value_en: "Items", value_fr: "Objets"
|
38
|
+
m.value.should == "Items"
|
39
|
+
I18n.locale = :fr
|
40
|
+
m.value.should == "Objets"
|
41
|
+
I18n.locale = :en_master
|
42
|
+
m.value.should == "Items"
|
43
|
+
I18n.locale = :nl
|
44
|
+
lambda{ m.value }.should raise_error NoMethodError
|
45
|
+
end
|
46
|
+
it "sets value from current locale" do
|
47
|
+
I18n.locale = :en
|
48
|
+
@klass.plugin :translatable, :value
|
49
|
+
m = @klass.new value_en: "Items", value_fr: "Objets"
|
50
|
+
m.value = "Objects"
|
51
|
+
m.value_en.should == "Objects"
|
52
|
+
m.value_hash.should == {en: "Objects", fr: "Objets"}
|
53
|
+
end
|
54
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sequel_translatable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Joseph HALTER
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-11-07 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: sqlite3
|
16
|
+
requirement: &2157761100 !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: *2157761100
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &2157760660 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2157760660
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: i18n
|
38
|
+
requirement: &2157760240 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *2157760240
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: sequel
|
49
|
+
requirement: &2157759820 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :runtime
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *2157759820
|
58
|
+
description: Translate model attributes for sequel, fully tested.
|
59
|
+
email:
|
60
|
+
- joseph.halter@thetalentbox.com
|
61
|
+
executables: []
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files: []
|
64
|
+
files:
|
65
|
+
- .gitignore
|
66
|
+
- Gemfile
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- lib/sequel/plugins/translatable.rb
|
70
|
+
- lib/sequel_translatable.rb
|
71
|
+
- sequel_translatable.gemspec
|
72
|
+
- spec/spec_helper.rb
|
73
|
+
- spec/translatable_spec.rb
|
74
|
+
homepage: https://github.com/TalentBox/sequel_translatable
|
75
|
+
licenses: []
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ! '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 1.8.6
|
95
|
+
signing_key:
|
96
|
+
specification_version: 3
|
97
|
+
summary: Translate model attributes for sequel.
|
98
|
+
test_files:
|
99
|
+
- spec/spec_helper.rb
|
100
|
+
- spec/translatable_spec.rb
|