mongoid_translate 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mongoid/translate.rb +20 -23
- data/lib/mongoid/translate/slug.rb +20 -13
- data/lib/mongoid/translate/version.rb +1 -1
- data/lib/mongoid/translation.rb +8 -10
- data/lib/mongoid/translation/slug.rb +30 -32
- metadata +11 -12
data/lib/mongoid/translate.rb
CHANGED
@@ -21,32 +21,29 @@ module Mongoid
|
|
21
21
|
index 'translations.language'
|
22
22
|
end
|
23
23
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
end
|
24
|
+
# Return list of existing languages
|
25
|
+
#
|
26
|
+
# @return [ Array ]
|
27
|
+
#
|
28
|
+
def languages
|
29
|
+
translations.map(&:language)
|
30
|
+
end
|
32
31
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
32
|
+
# Return main translation object
|
33
|
+
#
|
34
|
+
# @return [ Document ]
|
35
|
+
#
|
36
|
+
def main_translation
|
37
|
+
translations.where(language: main_language).one
|
38
|
+
end
|
40
39
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
end
|
40
|
+
def method_missing(method, *args, &block)
|
41
|
+
if self.languages.include?(method)
|
42
|
+
translations.where(language: method).one
|
43
|
+
else
|
44
|
+
super(method, *args, &block)
|
47
45
|
end
|
48
|
-
|
49
|
-
end # InstanceMethods
|
46
|
+
end
|
50
47
|
|
51
48
|
module ClassMethods
|
52
49
|
attr_accessor :translated_fields
|
@@ -9,23 +9,30 @@ module Mongoid
|
|
9
9
|
#
|
10
10
|
included do
|
11
11
|
scope :by_slug, lambda {|slug| where('translations.slug' => slug )}
|
12
|
+
validate :slug_uniqueness
|
12
13
|
index 'translations.slug'
|
13
14
|
end
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
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
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def slug_uniqueness
|
32
|
+
if self.class.excludes(id: self.id).any_in('translations.slug' => self.translations.map(&:slug)).exists?
|
33
|
+
errors.add(:translations, :slug_uniqueness)
|
27
34
|
end
|
28
|
-
end
|
35
|
+
end
|
29
36
|
|
30
37
|
module ClassMethods
|
31
38
|
attr_accessor :slugged
|
data/lib/mongoid/translation.rb
CHANGED
@@ -19,15 +19,13 @@ module Mongoid
|
|
19
19
|
embedded_in name.underscore.to_sym, class_name: class_name
|
20
20
|
end
|
21
21
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
end
|
31
|
-
end # InstanceMethods
|
22
|
+
# Check if current translation is main_translation.
|
23
|
+
#
|
24
|
+
# @return [ Boolean ]
|
25
|
+
#
|
26
|
+
def main_translation?
|
27
|
+
parent = self.class.to_s.sub(/^.*::/, '').underscore
|
28
|
+
self.send(parent).main_language == self.language
|
29
|
+
end
|
32
30
|
end # Translation
|
33
31
|
end # Mongoid
|
@@ -12,47 +12,45 @@ module Mongoid
|
|
12
12
|
after_validation :set_slug
|
13
13
|
end
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
if
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
self.slug = slugged
|
26
|
-
end
|
15
|
+
# Slug creation.
|
16
|
+
#
|
17
|
+
# @return [ Object ]
|
18
|
+
#
|
19
|
+
def set_slug
|
20
|
+
if self.slug.blank? && slugged_field.present?
|
21
|
+
if translation_parent_class.send(:by_slug, slugged).one
|
22
|
+
self.slug = slugged_with_token
|
23
|
+
else
|
24
|
+
self.slug = slugged
|
27
25
|
end
|
28
26
|
end
|
27
|
+
end
|
29
28
|
|
30
|
-
|
29
|
+
private
|
31
30
|
|
32
|
-
|
33
|
-
|
34
|
-
|
31
|
+
def slugged_field
|
32
|
+
@slugged_field ||= self.send(translation_parent_class.slugged)
|
33
|
+
end
|
35
34
|
|
36
|
-
|
37
|
-
|
38
|
-
|
35
|
+
def slugged
|
36
|
+
@slugged ||= slugged_field.parameterize.blank? ? slugged_field : slugged_field.parameterize
|
37
|
+
end
|
39
38
|
|
40
|
-
|
41
|
-
|
42
|
-
|
39
|
+
def slugged_with_token
|
40
|
+
@slugged_with_token ||= [slugged, generate_token].join('-')
|
41
|
+
end
|
43
42
|
|
44
|
-
|
45
|
-
|
46
|
-
|
43
|
+
def translation_parent_class
|
44
|
+
self.class.to_s.sub('Translation::', '').constantize
|
45
|
+
end
|
47
46
|
|
48
|
-
|
49
|
-
|
50
|
-
|
47
|
+
def translation_parent
|
48
|
+
self.send(self.class.to_s.sub(/^.*::/, '').underscore)
|
49
|
+
end
|
51
50
|
|
52
|
-
|
53
|
-
|
54
|
-
|
51
|
+
def generate_token
|
52
|
+
SecureRandom.base64(4).tr('+/=', '-_ ').strip.delete("\n")
|
53
|
+
end
|
55
54
|
end
|
56
|
-
end # InstanceMethods
|
57
55
|
end # Translation
|
58
56
|
end # Mongoid
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid_translate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,11 +10,11 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-
|
13
|
+
date: 2012-04-05 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activesupport
|
17
|
-
requirement: &
|
17
|
+
requirement: &10000200 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ~>
|
@@ -22,10 +22,10 @@ dependencies:
|
|
22
22
|
version: '3.1'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
|
-
version_requirements: *
|
25
|
+
version_requirements: *10000200
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
27
|
name: mongoid
|
28
|
-
requirement: &
|
28
|
+
requirement: &9999380 !ruby/object:Gem::Requirement
|
29
29
|
none: false
|
30
30
|
requirements:
|
31
31
|
- - ~>
|
@@ -33,10 +33,10 @@ dependencies:
|
|
33
33
|
version: '2.3'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
|
-
version_requirements: *
|
36
|
+
version_requirements: *9999380
|
37
37
|
- !ruby/object:Gem::Dependency
|
38
38
|
name: bson_ext
|
39
|
-
requirement: &
|
39
|
+
requirement: &9998880 !ruby/object:Gem::Requirement
|
40
40
|
none: false
|
41
41
|
requirements:
|
42
42
|
- - ~>
|
@@ -44,10 +44,10 @@ dependencies:
|
|
44
44
|
version: '1.4'
|
45
45
|
type: :runtime
|
46
46
|
prerelease: false
|
47
|
-
version_requirements: *
|
47
|
+
version_requirements: *9998880
|
48
48
|
- !ruby/object:Gem::Dependency
|
49
49
|
name: rspec
|
50
|
-
requirement: &
|
50
|
+
requirement: &9998420 !ruby/object:Gem::Requirement
|
51
51
|
none: false
|
52
52
|
requirements:
|
53
53
|
- - ~>
|
@@ -55,7 +55,7 @@ dependencies:
|
|
55
55
|
version: '2.6'
|
56
56
|
type: :development
|
57
57
|
prerelease: false
|
58
|
-
version_requirements: *
|
58
|
+
version_requirements: *9998420
|
59
59
|
description: Translate mongoid models.
|
60
60
|
email: jboyer@af83.com
|
61
61
|
executables: []
|
@@ -90,9 +90,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
90
90
|
version: '0'
|
91
91
|
requirements: []
|
92
92
|
rubyforge_project:
|
93
|
-
rubygems_version: 1.8.
|
93
|
+
rubygems_version: 1.8.10
|
94
94
|
signing_key:
|
95
95
|
specification_version: 3
|
96
96
|
summary: Translate mongoid models.
|
97
97
|
test_files: []
|
98
|
-
has_rdoc:
|