mongoid_translate 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.md +113 -0
- data/lib/mongoid/translate.rb +82 -0
- data/lib/mongoid/translate/slug.rb +42 -0
- data/lib/mongoid/translate/version.rb +6 -0
- data/lib/mongoid/translation.rb +33 -0
- data/lib/mongoid/translation/slug.rb +58 -0
- data/lib/mongoid_translate.rb +14 -0
- metadata +98 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2011 YOURNAME
|
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.md
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
# Mongoid Translate
|
2
|
+
|
3
|
+
## Installation
|
4
|
+
|
5
|
+
Ruby 1.9.2 is required.
|
6
|
+
|
7
|
+
Install it with rubygems:
|
8
|
+
|
9
|
+
gem install mongoid_translate
|
10
|
+
|
11
|
+
With bundler, add it to your `Gemfile`:
|
12
|
+
|
13
|
+
``` ruby
|
14
|
+
gem "mongoid_translate", "~>0.1.0"
|
15
|
+
```
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
### Exemple :
|
20
|
+
|
21
|
+
``` ruby
|
22
|
+
class Article
|
23
|
+
include Mongoid::Document
|
24
|
+
|
25
|
+
# add Translate module
|
26
|
+
include Mongoid::Translate
|
27
|
+
|
28
|
+
# declare fields to be translate
|
29
|
+
translate :title, :content
|
30
|
+
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
34
|
+
Create a Namespace model. You can add callbacks validation and so on.
|
35
|
+
|
36
|
+
``` ruby
|
37
|
+
class Translation::Article
|
38
|
+
include Mongoid::Document
|
39
|
+
|
40
|
+
# add Translation module
|
41
|
+
include Mongoid::Translation
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
That'all folks.
|
46
|
+
|
47
|
+
Display translation :
|
48
|
+
|
49
|
+
``` ruby
|
50
|
+
article = Article.first
|
51
|
+
# return title according to I18n.locale, or main_translation.
|
52
|
+
article.title
|
53
|
+
|
54
|
+
# return list of existing translation for this resource
|
55
|
+
article.languages
|
56
|
+
|
57
|
+
# return main_translation
|
58
|
+
article.main_translation
|
59
|
+
|
60
|
+
# return english translation
|
61
|
+
article.en
|
62
|
+
```
|
63
|
+
|
64
|
+
Persist translation. It's just an embeds_many relation.
|
65
|
+
|
66
|
+
``` ruby
|
67
|
+
article = Article.new
|
68
|
+
article.translations << Translation::Article.new(:title => 'My title',
|
69
|
+
:language => :en)
|
70
|
+
```
|
71
|
+
|
72
|
+
Slug
|
73
|
+
----
|
74
|
+
|
75
|
+
Slug are generated on translation creation. No change are made after.
|
76
|
+
|
77
|
+
Slug feature can be added to translated model:
|
78
|
+
|
79
|
+
``` ruby
|
80
|
+
class Article
|
81
|
+
include Mongoid::Document
|
82
|
+
|
83
|
+
# add Translate module
|
84
|
+
include Mongoid::Translate
|
85
|
+
|
86
|
+
# add Slug module
|
87
|
+
include Mongoid::Translate::Slug
|
88
|
+
|
89
|
+
# declare fields to be translate
|
90
|
+
translate :title, :content
|
91
|
+
|
92
|
+
# Define field on which slug will be build.
|
93
|
+
slug_field :title
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
class Translation::Article
|
98
|
+
include Mongoid::Document
|
99
|
+
|
100
|
+
# add Translation module
|
101
|
+
include Mongoid::Translation
|
102
|
+
|
103
|
+
# add Slug module
|
104
|
+
include Mongoid::Translation::Slug
|
105
|
+
|
106
|
+
end
|
107
|
+
```
|
108
|
+
|
109
|
+
## Copyright
|
110
|
+
|
111
|
+
Copyright (c) 2012 af83
|
112
|
+
|
113
|
+
Released under the MIT license
|
@@ -0,0 +1,82 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid
|
3
|
+
module Translate
|
4
|
+
extend ::ActiveSupport::Concern
|
5
|
+
|
6
|
+
# Include translations embeds relation.
|
7
|
+
# Add main_language field. [ Symbol ]
|
8
|
+
# Add delegate on translated_fields.
|
9
|
+
# Add index on translations.language.
|
10
|
+
#
|
11
|
+
included do
|
12
|
+
if self.to_s.include?('::')
|
13
|
+
class_name = self.to_s.split('::').insert(-2, "Translation").join('::')
|
14
|
+
else
|
15
|
+
class_name = "Translation::#{self}"
|
16
|
+
end
|
17
|
+
field :main_language, type: Symbol, default: lambda { I18n.locale }
|
18
|
+
embeds_many :translations, class_name: class_name
|
19
|
+
delegate :translated_fields, to: "self.class"
|
20
|
+
accepts_nested_attributes_for :translations
|
21
|
+
index 'translations.language'
|
22
|
+
end
|
23
|
+
|
24
|
+
module InstanceMethods
|
25
|
+
# Return list of existing languages
|
26
|
+
#
|
27
|
+
# @return [ Array ]
|
28
|
+
#
|
29
|
+
def languages
|
30
|
+
translations.map(&:language)
|
31
|
+
end
|
32
|
+
|
33
|
+
# Return main translation object
|
34
|
+
#
|
35
|
+
# @return [ Document ]
|
36
|
+
#
|
37
|
+
def main_translation
|
38
|
+
translations.where(language: main_language).one
|
39
|
+
end
|
40
|
+
|
41
|
+
def method_missing(method, *args, &block)
|
42
|
+
if self.languages.include?(method)
|
43
|
+
translations.where(language: method).one
|
44
|
+
else
|
45
|
+
super(method, *args, &block)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
end # InstanceMethods
|
50
|
+
|
51
|
+
module ClassMethods
|
52
|
+
attr_accessor :translated_fields
|
53
|
+
|
54
|
+
# creates accessor methods
|
55
|
+
#
|
56
|
+
def translate(*argv)
|
57
|
+
self.translated_fields = *argv
|
58
|
+
|
59
|
+
translated_fields.each do |field|
|
60
|
+
# Return field for current locale.
|
61
|
+
#
|
62
|
+
# Otherwise, fallbacks to main_language.
|
63
|
+
#
|
64
|
+
# @return [ Object ]
|
65
|
+
#
|
66
|
+
define_method field do
|
67
|
+
locale = languages.include?(I18n.locale) ? I18n.locale : main_language
|
68
|
+
translations.where(language: locale).one.try(field)
|
69
|
+
end
|
70
|
+
|
71
|
+
# Setter on field for current locale.
|
72
|
+
#
|
73
|
+
# @return [ Object ]
|
74
|
+
#
|
75
|
+
define_method "#{field}=" do |arg|
|
76
|
+
translations.where(language: I18n.locale).one.try("#{field}=", arg)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end # ClassMethods
|
81
|
+
end # Translate
|
82
|
+
end # Mongoid
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid
|
3
|
+
module Translate
|
4
|
+
module Slug
|
5
|
+
extend ::ActiveSupport::Concern
|
6
|
+
|
7
|
+
# Add scope on slug.
|
8
|
+
# Add index on slug.
|
9
|
+
#
|
10
|
+
included do
|
11
|
+
scope :by_slug, lambda {|slug| where('translations.slug' => slug )}
|
12
|
+
index 'translations.slug'
|
13
|
+
end
|
14
|
+
|
15
|
+
module InstanceMethods
|
16
|
+
# Return slug.
|
17
|
+
# Didn't want to mess with to_param.
|
18
|
+
#
|
19
|
+
# @example
|
20
|
+
# document.to_slug
|
21
|
+
#
|
22
|
+
# @return [ String ]
|
23
|
+
#
|
24
|
+
def to_slug
|
25
|
+
locale = languages.include?(I18n.locale) ? I18n.locale : main_language
|
26
|
+
translations.where(language: locale).one.slug
|
27
|
+
end
|
28
|
+
end # InstanceMethods
|
29
|
+
|
30
|
+
module ClassMethods
|
31
|
+
attr_accessor :slugged
|
32
|
+
|
33
|
+
# Defines slug field.
|
34
|
+
#
|
35
|
+
def slug_field(slug_field)
|
36
|
+
self.slugged = slug_field
|
37
|
+
end
|
38
|
+
|
39
|
+
end # ClassMethods
|
40
|
+
end # Slug
|
41
|
+
end # Translate
|
42
|
+
end # Mongoid
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid
|
3
|
+
module Translation
|
4
|
+
extend ::ActiveSupport::Concern
|
5
|
+
|
6
|
+
# Add translated fields to model.
|
7
|
+
# Add language field.
|
8
|
+
# Add uniqueness validation on language.
|
9
|
+
# Set relation on parent.
|
10
|
+
#
|
11
|
+
included do
|
12
|
+
class_name = self.to_s.gsub('Translation::', '')
|
13
|
+
name = self.to_s.sub(/^.*::/, '')
|
14
|
+
class_name.constantize.translated_fields.each do |field|
|
15
|
+
field field
|
16
|
+
end
|
17
|
+
field :language, type: Symbol, default: lambda { I18n.locale }
|
18
|
+
validates_uniqueness_of :language
|
19
|
+
embedded_in name.underscore.to_sym, class_name: class_name
|
20
|
+
end
|
21
|
+
|
22
|
+
module InstanceMethods
|
23
|
+
# Check if current translation is main_translation.
|
24
|
+
#
|
25
|
+
# @return [ Boolean ]
|
26
|
+
#
|
27
|
+
def main_translation?
|
28
|
+
parent = self.class.to_s.sub(/^.*::/, '').underscore
|
29
|
+
self.send(parent).main_language == self.language
|
30
|
+
end
|
31
|
+
end # InstanceMethods
|
32
|
+
end # Translation
|
33
|
+
end # Mongoid
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid
|
3
|
+
module Translation
|
4
|
+
module Slug
|
5
|
+
extend ::ActiveSupport::Concern
|
6
|
+
|
7
|
+
# Add slug field.
|
8
|
+
# Add callback for slug creation.
|
9
|
+
#
|
10
|
+
included do
|
11
|
+
field :slug, type: String
|
12
|
+
after_validation :set_slug
|
13
|
+
end
|
14
|
+
|
15
|
+
module InstanceMethods
|
16
|
+
# Slug creation.
|
17
|
+
#
|
18
|
+
# @return [ Object ]
|
19
|
+
#
|
20
|
+
def set_slug
|
21
|
+
if self.slug.blank? && slugged_field.present?
|
22
|
+
if translation_parent_class.send(:by_slug, slugged).one
|
23
|
+
self.slug = slugged_with_token
|
24
|
+
else
|
25
|
+
self.slug = slugged
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def slugged_field
|
33
|
+
@slugged_field ||= self.send(translation_parent_class.slugged)
|
34
|
+
end
|
35
|
+
|
36
|
+
def slugged
|
37
|
+
@slugged ||= slugged_field.parameterize.blank? ? slugged_field : slugged_field.parameterize
|
38
|
+
end
|
39
|
+
|
40
|
+
def slugged_with_token
|
41
|
+
@slugged_with_token ||= [slugged, generate_token].join('-')
|
42
|
+
end
|
43
|
+
|
44
|
+
def translation_parent_class
|
45
|
+
self.class.to_s.sub('Translation::', '').constantize
|
46
|
+
end
|
47
|
+
|
48
|
+
def translation_parent
|
49
|
+
self.send(self.class.to_s.sub(/^.*::/, '').underscore)
|
50
|
+
end
|
51
|
+
|
52
|
+
def generate_token
|
53
|
+
SecureRandom.base64(4).tr('+/=', '-_ ').strip.delete("\n")
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end # InstanceMethods
|
57
|
+
end # Translation
|
58
|
+
end # Mongoid
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'mongoid'
|
3
|
+
require 'mongoid/translate'
|
4
|
+
require 'mongoid/translation'
|
5
|
+
|
6
|
+
module Mongoid
|
7
|
+
module Translate
|
8
|
+
autoload :Slug, 'mongoid/translate/slug'
|
9
|
+
end # Translate
|
10
|
+
|
11
|
+
module Translation
|
12
|
+
autoload :Slug, 'mongoid/translation/slug'
|
13
|
+
end # Translation
|
14
|
+
end # Mongoid
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid_translate
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- chatgris
|
9
|
+
- klacointe
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2012-02-14 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activesupport
|
17
|
+
requirement: &76394790 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ~>
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '3.1'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *76394790
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: mongoid
|
28
|
+
requirement: &76394330 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.3'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *76394330
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: bson_ext
|
39
|
+
requirement: &76393870 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '1.4'
|
45
|
+
type: :runtime
|
46
|
+
prerelease: false
|
47
|
+
version_requirements: *76393870
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: rspec
|
50
|
+
requirement: &76393200 !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ~>
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '2.6'
|
56
|
+
type: :development
|
57
|
+
prerelease: false
|
58
|
+
version_requirements: *76393200
|
59
|
+
description: Translate mongoid models.
|
60
|
+
email: jboyer@af83.com
|
61
|
+
executables: []
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files: []
|
64
|
+
files:
|
65
|
+
- MIT-LICENSE
|
66
|
+
- README.md
|
67
|
+
- lib/mongoid/translate.rb
|
68
|
+
- lib/mongoid/translate/slug.rb
|
69
|
+
- lib/mongoid/translate/version.rb
|
70
|
+
- lib/mongoid/translation.rb
|
71
|
+
- lib/mongoid/translation/slug.rb
|
72
|
+
- lib/mongoid_translate.rb
|
73
|
+
homepage: https://github.com/AF83/mongoid_translate
|
74
|
+
licenses: []
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ! '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
none: false
|
87
|
+
requirements:
|
88
|
+
- - ! '>='
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 1.8.12
|
94
|
+
signing_key:
|
95
|
+
specification_version: 3
|
96
|
+
summary: Translate mongoid models.
|
97
|
+
test_files: []
|
98
|
+
has_rdoc:
|