rails_db_localize 0.0.4 → 0.0.5
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.
- checksums.yaml +4 -4
- data/app/models/rails_db_localize/translation.rb +1 -1
- data/lib/ext/active_record_ext.rb +75 -72
- data/lib/rails_db_localize/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5ffe7275fcadc410cdb449128a78f0fcda3f8a25
|
4
|
+
data.tar.gz: 549306712cdcfc50321a5d1838adfbe6f683253f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 541b730cce0772dbf5ba8cffe78808d66aef4b55f1d49a7dc1dbae7ddafdf95832f442e66fb537f850b8c9b350805f42f19fef89dea3ce30c8f6ed8ca27618dd
|
7
|
+
data.tar.gz: 772a936d176975a6a0d9eddeab8de5c70b0299c33854da622c3bd30210878a7a9691a1ff4055800199ffa95147d517fbb433f56490f572df747c9f8162b3dbe1
|
@@ -18,7 +18,7 @@ class RailsDbLocalize::Translation < ActiveRecord::Base
|
|
18
18
|
|
19
19
|
def self.generate_ck resource_type, resource_id
|
20
20
|
hash_long = [resource_type.to_s.underscore, resource_id].join("|").chars.map(&:ord).inject(5381) do |h, v|
|
21
|
-
h
|
21
|
+
h += ((h<<5)+h)+v
|
22
22
|
end
|
23
23
|
|
24
24
|
#Keep it signed 32bits.
|
@@ -2,88 +2,91 @@ class ActiveRecord::Base
|
|
2
2
|
class << self
|
3
3
|
|
4
4
|
def has_translations *fields
|
5
|
+
class_eval do
|
6
|
+
|
7
|
+
unless respond_to?(:missing_translation)
|
8
|
+
# Register it mostly to remove the translations once you delete an object
|
9
|
+
has_many :translations, class_name: "RailsDbLocalize::Translation", as: :resource, dependent: :destroy
|
10
|
+
|
11
|
+
scope :__rails_db_translations_sub_query, lambda{ |lang|
|
12
|
+
ttable = RailsDbLocalize::Translation.arel_table.name
|
13
|
+
number_of_fields_to_translates = RailsDbLocalize.schema[self.to_s].count
|
14
|
+
|
15
|
+
# We can unscope, but problems tend to appears
|
16
|
+
# with some gems like paranoid.
|
17
|
+
table = respond_to?(:klass) ? table.klass : self
|
18
|
+
|
19
|
+
table.select(:id).joins("INNER JOIN \"#{ttable}\"
|
20
|
+
ON (\"#{ttable}\".resource_id = \"#{arel_table.name}\".id
|
21
|
+
AND \"#{ttable}\".resource_type = '#{to_s}')")
|
22
|
+
.group(:resource_type, :resource_id)
|
23
|
+
.having("COUNT(*) == #{number_of_fields_to_translates}")
|
24
|
+
.where(:"rails_db_localize_translations.lang" => lang)
|
25
|
+
}
|
26
|
+
|
27
|
+
# Return all rows with missing translation for a selected language.
|
28
|
+
scope :missing_translation, lambda{ |lang|
|
29
|
+
where("\"#{arel_table.name}\".id NOT IN (#{__rails_db_translations_sub_query(lang).to_sql})")
|
30
|
+
}
|
31
|
+
|
32
|
+
# Return all rows with translation OK for a selected language.
|
33
|
+
scope :having_translation, lambda{ |lang|
|
34
|
+
where("\"#{arel_table.name}\".id IN (#{__rails_db_translations_sub_query(lang).to_sql})")
|
35
|
+
}
|
36
|
+
|
37
|
+
def self.preload_translations
|
38
|
+
RailsDbLocalize::TranslationCache.instance.prefetch_collections(self)
|
39
|
+
self
|
40
|
+
end
|
41
|
+
end
|
5
42
|
|
6
|
-
unless respond_to?(:missing_translation)
|
7
|
-
|
8
|
-
# Register it mostly to remove the translations once you delete an object
|
9
|
-
self.has_many :translations, class_name: "RailsDbLocalize::Translation", as: :resource, dependent: :destroy
|
10
|
-
|
11
|
-
scope :__rails_db_translations_sub_query, lambda{ |lang|
|
12
|
-
ttable = RailsDbLocalize::Translation.arel_table.name
|
13
|
-
number_of_fields_to_translates = RailsDbLocalize.schema[self.to_s].count
|
14
43
|
|
15
|
-
|
16
|
-
#
|
17
|
-
|
44
|
+
fields.each do |field|
|
45
|
+
# Add a marker to the schema of the application translations.
|
46
|
+
RailsDbLocalize::add_to_schema(self, field)
|
18
47
|
|
19
|
-
|
20
|
-
|
21
|
-
AND \"#{ttable}\".resource_type = '#{to_s}')")
|
22
|
-
.group(:resource_type, :resource_id)
|
23
|
-
.having("COUNT(*) == #{number_of_fields_to_translates}")
|
24
|
-
.where(:"rails_db_localize_translations.lang" => lang)
|
25
|
-
}
|
48
|
+
# Not sure if I would have to put dependent: :destroy here.
|
49
|
+
self.has_many :"#{field}_translations", -> { where(field: field) }, as: :resource
|
26
50
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
51
|
+
# Making the magic happends.
|
52
|
+
# I should really learn how to use the Reflection helpers in ActiveRecord, because
|
53
|
+
# ruby eval is not the most readable stuff... :o)
|
54
|
+
self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
55
|
+
def #{field}_translated_exists?(lang=nil)
|
56
|
+
lang ||= I18n.locale
|
57
|
+
!!RailsDbLocalize::TranslationCache.instance.get_translation_for(self.class, self.id, "#{field}", lang, nil )
|
58
|
+
end
|
31
59
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
60
|
+
def #{field}_translated (lang=nil)
|
61
|
+
lang ||= I18n.locale
|
62
|
+
RailsDbLocalize::TranslationCache.instance.get_translation_for(self.class, self.id, "#{field}", lang, self.#{field} )
|
63
|
+
end
|
36
64
|
|
37
|
-
|
38
|
-
|
39
|
-
|
65
|
+
def #{field}_translated= args
|
66
|
+
if args.is_a?(Array)
|
67
|
+
value, lang = args
|
68
|
+
else
|
69
|
+
value = args
|
70
|
+
lang = I18n.locale
|
71
|
+
end
|
72
|
+
|
73
|
+
if self.id
|
74
|
+
translated = RailsDbLocalize::Translation.where(
|
75
|
+
resource_type: self.class.to_s, resource_id: self.id, field: "#{field}", lang: lang
|
76
|
+
).first_or_create
|
77
|
+
|
78
|
+
translated.content = value
|
79
|
+
|
80
|
+
translated.save!
|
81
|
+
else
|
82
|
+
translations.build field: "#{field}", lang: lang, content: value
|
83
|
+
end
|
84
|
+
end
|
85
|
+
RUBY
|
40
86
|
end
|
41
87
|
|
42
88
|
end
|
43
89
|
|
44
|
-
fields.each do |field|
|
45
|
-
# Add a marker to the schema of the application translations.
|
46
|
-
RailsDbLocalize::add_to_schema(self, field)
|
47
|
-
|
48
|
-
# Not sure if I would have to put dependent: :destroy here.
|
49
|
-
self.has_many :"#{field}_translations", -> { where(field: field) }, as: :resource
|
50
|
-
|
51
|
-
# Making the magic happends.
|
52
|
-
# I should really learn how to use the Reflection helpers in ActiveRecord, because
|
53
|
-
# ruby eval is not the most readable stuff... :o)
|
54
|
-
self.class_eval <<-CODE, __FILE__, __LINE__ + 1
|
55
|
-
def #{field}_translated_exists?(lang=nil)
|
56
|
-
lang ||= I18n.locale
|
57
|
-
!!RailsDbLocalize::TranslationCache.instance.get_translation_for(self.class, self.id, "#{field}", lang, nil )
|
58
|
-
end
|
59
|
-
|
60
|
-
def #{field}_translated (lang=nil)
|
61
|
-
lang ||= I18n.locale
|
62
|
-
RailsDbLocalize::TranslationCache.instance.get_translation_for(self.class, self.id, "#{field}", lang, self.#{field} )
|
63
|
-
end
|
64
|
-
|
65
|
-
def #{field}_translated= args
|
66
|
-
if args.is_a?(Array)
|
67
|
-
value, lang = args
|
68
|
-
else
|
69
|
-
value = args
|
70
|
-
lang = I18n.locale
|
71
|
-
end
|
72
|
-
|
73
|
-
if self.id
|
74
|
-
translated = RailsDbLocalize::Translation.where(
|
75
|
-
resource_type: self.class.to_s, resource_id: self.id, field: "#{field}", lang: lang
|
76
|
-
).first_or_create
|
77
|
-
|
78
|
-
translated.content = value
|
79
|
-
|
80
|
-
translated.save!
|
81
|
-
else
|
82
|
-
translations.build field: "#{field}", lang: lang, content: value
|
83
|
-
end
|
84
|
-
end
|
85
|
-
CODE
|
86
|
-
end
|
87
90
|
end
|
88
91
|
end
|
89
92
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_db_localize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yacine PETITPREZ
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-09-
|
11
|
+
date: 2015-09-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|