from_clause_translate 0.1.3 → 0.1.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f799d086bb331e73f238a80b3dd0f80a424e6c60e5f7b13c6b2d08d665b710aa
4
- data.tar.gz: fa9662898e142adf41b13d3445540ecebd98f81033431f644d86d17c348d5437
3
+ metadata.gz: 0e5e3a351d526478381b634465400c2cfe21d90594e186d01f777f191d359d75
4
+ data.tar.gz: 8c2013900c68f207e78356aec170259a6ec7af0b7452fdd8c6c5f7f65a795b0a
5
5
  SHA512:
6
- metadata.gz: 72419315fd701a8343e571768ff8a631c8fba5a54c89df33d8aa7e59058fb88d8d3a2396aed7cc38a64852817973c883080d0c13984a8a29b6592ae2edda6fcf
7
- data.tar.gz: 75ab580aed864a8d938e4b6662312c6a7b01a20dea5b35cec30fcecbfba02735d3a15807cb789906b0cfc9f183f89de4de842fa1a866de67bc56203c6c8dd381
6
+ metadata.gz: 11b1eca1c3a40f9da9caef0db97fd410508cf7d46838e3dd40ba03bdad26e0e323749f7d65a48d9d852880c5a4aedcfac601263303e3c5f76f2ce6abc54b040d
7
+ data.tar.gz: 86f01a64692ecec354a4c55a5d74c0b8455e0721366fbdd0d5b1ef58fc08c3661f8e4aad0616a207723382400030de21b049a4ef6070b785c7177d21fa1dce5c
@@ -8,92 +8,6 @@ module FromClauseTranslate
8
8
  I18n.available_locales.map { |locale| [locale, {}] }
9
9
  ]
10
10
  end
11
-
12
- module ClassMethods
13
- def translates *columns
14
- options = columns.extract_options!
15
- filter_translated_columns(columns).each do |column|
16
- self::TRANSLATED[column] = true
17
- I18n.available_locales.each do |locale|
18
- self::TRANSLATED_SELECTION[locale][column] =
19
- "#{column}_#{locale} AS #{column}"
20
- end
21
- define_translated_column_methods column
22
- end
23
- translates_plurals options[:plurals]
24
- end
25
-
26
- def translates_plurals plurals
27
- return unless plurals
28
-
29
- plurals.each do |plural|
30
- plural = plural.to_sym
31
- self::TRANSLATED[plural] = true
32
- column = plural.to_s.singularize.to_sym
33
- selection = I18n.available_locales.map do |locale|
34
- "#{column}_#{locale}"
35
- end.join ','
36
- self::TRANSLATED_PLURALS[plural] = selection
37
- I18n.available_locales.each do |locale|
38
- self::TRANSLATED_SELECTION[locale][plural] = selection
39
- end
40
- end
41
- end
42
-
43
- def translates? column
44
- self::TRANSLATED[column.to_sym]
45
- end
46
-
47
- def translated *columns
48
- scope = current_scope || all
49
- scope.add_translated_columns columns
50
- scope
51
- end
52
-
53
- private
54
-
55
- def filter_translated_columns columns
56
- columns.keep_if do |column|
57
- !translates?(column) &&
58
- column_names.include?("#{column}_#{I18n.locale}")
59
- end
60
- end
61
-
62
- def define_translated_column_methods col
63
- col = col.to_s
64
- dashed = "#{col}_"
65
- define_translated_column_getter_and_setter col, dashed
66
- define_translated_column_changing_methods col, dashed
67
- end
68
-
69
- def define_translated_column_getter_and_setter col, dashed
70
- define_method col do
71
- @attributes.key?(col) ? self[col] : send("#{dashed}#{I18n.locale}")
72
- end
73
-
74
- define_method col + '=' do |val|
75
- send "#{dashed}#{I18n.locale}=", val
76
- end
77
- end
78
-
79
- def define_translated_column_changing_methods col, dashed
80
- define_method 'saved_change_to_' + col do
81
- send "saved_change_to_#{dashed}#{I18n.locale}"
82
- end
83
-
84
- %w[changed? before_last_save change_to_be_saved in_database].each do |suf|
85
- define_method dashed + suf do
86
- send "#{dashed}#{I18n.locale}_#{suf}"
87
- end
88
- end
89
-
90
- %w[saved_change_to will_save_change_to].each do |prefix|
91
- define_method "#{prefix}_#{col}?" do
92
- send "#{prefix}_#{dashed}#{I18n.locale}?"
93
- end
94
- end
95
- end
96
- end
97
11
  end
98
12
 
99
13
  require 'from_clause_translate/active_record_relation'
@@ -0,0 +1,105 @@
1
+ module ClassMethods
2
+ include Fallbacks
3
+
4
+ def translates *columns
5
+ options = columns.extract_options!
6
+ filter_translated_columns(
7
+ map_translated_columns(columns)
8
+ ).each do |hash|
9
+ column = hash[:column]
10
+ self::TRANSLATED[column] = true
11
+ define_translated_selection column, hash
12
+ define_translated_column_methods column
13
+ end
14
+ translates_plurals options[:plurals]
15
+ end
16
+
17
+ def translates_plurals plurals
18
+ return unless plurals
19
+
20
+ plurals.each do |plural|
21
+ plural = plural.to_sym
22
+ self::TRANSLATED[plural] = true
23
+ column = plural.to_s.singularize.to_sym
24
+ selection = I18n.available_locales.map do |locale|
25
+ "#{column}_#{locale}"
26
+ end.join ','
27
+ self::TRANSLATED_PLURALS[plural] = selection
28
+ I18n.available_locales.each do |locale|
29
+ self::TRANSLATED_SELECTION[locale][plural] = selection
30
+ end
31
+ end
32
+ end
33
+
34
+ def translates? column
35
+ self::TRANSLATED[column.to_sym]
36
+ end
37
+
38
+ def translated *columns
39
+ scope = current_scope || all
40
+ scope.add_translated_columns columns
41
+ scope
42
+ end
43
+
44
+ private
45
+
46
+ def map_translated_columns columns
47
+ columns.map! do |column|
48
+ hash = column.is_a?(Hash) ? column : {column: column}
49
+ translated_fallbacks_set hash
50
+ hash
51
+ end
52
+ end
53
+
54
+ def filter_translated_columns columns
55
+ columns.keep_if do |hash|
56
+ !translates?(hash[:column]) &&
57
+ column_names.include?("#{hash[:column]}_#{I18n.locale}")
58
+ end
59
+ end
60
+
61
+ def define_translated_selection column, hash
62
+ fallback = hash[:fallback]
63
+ I18n.available_locales.each do |locale|
64
+ selection = "#{column}_#{locale}"
65
+ selection = translated_fallbacks_wrap selection, fallback[locale]
66
+ selection = translated_fallbacks_wrap selection, fallback[:_]
67
+ self::TRANSLATED_SELECTION[locale][column] = "#{selection} AS #{column}"
68
+ end
69
+ end
70
+
71
+ def define_translated_column_methods col
72
+ col = col.to_s
73
+ dashed = "#{col}_"
74
+ define_translated_column_getter_and_setter col, dashed
75
+ define_translated_column_changing_methods col, dashed
76
+ end
77
+
78
+ def define_translated_column_getter_and_setter col, dashed
79
+ define_method col do
80
+ @attributes.key?(col) ? self[col] : send("#{dashed}#{I18n.locale}")
81
+ end
82
+
83
+ define_method col + '=' do |val|
84
+ send "#{dashed}#{I18n.locale}=", val
85
+ end
86
+ end
87
+
88
+ def define_translated_column_changing_methods col, dashed
89
+ define_method 'saved_change_to_' + col do
90
+ send "saved_change_to_#{dashed}#{I18n.locale}"
91
+ end
92
+
93
+ %w[changed? before_last_save change_to_be_saved in_database].each do |suf|
94
+ define_method dashed + suf do
95
+ send "#{dashed}#{I18n.locale}_#{suf}"
96
+ end
97
+ end
98
+
99
+ %w[saved_change_to will_save_change_to].each do |prefix|
100
+ define_method "#{prefix}_#{col}?" do
101
+ send "#{prefix}_#{dashed}#{I18n.locale}?"
102
+ end
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,32 @@
1
+ module Fallbacks
2
+ private
3
+
4
+ def translated_fallbacks_set hash
5
+ fallback = hash[:fallback]
6
+ return hash[:fallback] = {} unless hash[:fallback]
7
+ return if fallback.is_a? Hash
8
+
9
+ hash[:fallback] = {_: fallback}
10
+ end
11
+
12
+ def translated_fallbacks_wrap selection, fallbacks
13
+ return selection unless fallbacks
14
+
15
+ if fallbacks.is_a?(String) || fallbacks.is_a?(Symbol)
16
+ translated_selection_fallback_wrap selection, fallbacks
17
+ else
18
+ fallbacks.each do |fallback|
19
+ selection = translated_selection_fallback_wrap selection, fallback
20
+ end
21
+ selection
22
+ end
23
+ end
24
+
25
+ def translated_selection_fallback_wrap selection, fallback
26
+ if fallback.is_a? String
27
+ "COALESCE(#{selection}, #{fallback})"
28
+ else
29
+ "COALESCE(#{selection}, #{table_name}.#{fallback})"
30
+ end
31
+ end
32
+ end
@@ -1,3 +1,3 @@
1
1
  module FromClauseTranslate
2
- VERSION = '0.1.3'.freeze
2
+ VERSION = '0.1.4'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: from_clause_translate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roman Kushin
@@ -35,6 +35,8 @@ files:
35
35
  - README.md
36
36
  - lib/from_clause_translate.rb
37
37
  - lib/from_clause_translate/active_record_relation.rb
38
+ - lib/from_clause_translate/class_methods.rb
39
+ - lib/from_clause_translate/class_methods/fallbacks.rb
38
40
  - lib/from_clause_translate/translated_columns_builder.rb
39
41
  - lib/from_clause_translate/version.rb
40
42
  homepage: