@@ -90,17 +90,16 @@ def coalesce_select_custom_value_as_string
9090 # COALESCE is here to make sure that blank and NULL values are sorted equally
9191 [
9292 <<-SQL
93- COALESCE(#{ select_custom_value_as_string } , '')
93+ COALESCE(#{ select_custom_value_as_string } , '')
9494 SQL
9595 ]
9696 end
9797
9898 def select_custom_value_as_string
9999 <<-SQL
100100 (SELECT cv_sort.value FROM #{ CustomValue . table_name } cv_sort
101- WHERE cv_sort.customized_type='#{ self . class . customized_class . name } '
102- AND cv_sort.customized_id=#{ self . class . customized_class . table_name } .id
103- AND cv_sort.custom_field_id=#{ id } LIMIT 1)
101+ WHERE #{ cv_sort_only_custom_field_condition_sql }
102+ LIMIT 1)
104103 SQL
105104 end
106105
@@ -109,8 +108,7 @@ def select_custom_option_position
109108 (SELECT co_sort.position FROM #{ CustomOption . table_name } co_sort
110109 LEFT JOIN #{ CustomValue . table_name } cv_sort
111110 ON co_sort.id = CAST(cv_sort.value AS decimal(60,3))
112- WHERE cv_sort.custom_field_id=#{ id }
113- AND cv_sort.customized_id=#{ self . class . customized_class . table_name } .id
111+ WHERE #{ cv_sort_only_custom_field_condition_sql }
114112 LIMIT 1
115113 )
116114 SQL
@@ -119,9 +117,7 @@ def select_custom_option_position
119117 def select_custom_values_as_group
120118 <<-SQL
121119 COALESCE((SELECT string_agg(cv_sort.value, '.') FROM #{ CustomValue . table_name } cv_sort
122- WHERE cv_sort.customized_type='#{ self . class . customized_class . name } '
123- AND cv_sort.customized_id=#{ self . class . customized_class . table_name } .id
124- AND cv_sort.custom_field_id=#{ id }
120+ WHERE #{ cv_sort_only_custom_field_condition_sql }
125121 AND cv_sort.value IS NOT NULL), '')
126122 SQL
127123 end
@@ -131,18 +127,14 @@ def select_custom_values_joined_options_as_group
131127 COALESCE((SELECT string_agg(co_sort.value, '.' ORDER BY co_sort.position ASC) FROM #{ CustomOption . table_name } co_sort
132128 LEFT JOIN #{ CustomValue . table_name } cv_sort
133129 ON cv_sort.value IS NOT NULL AND co_sort.id = cv_sort.value::numeric
134- WHERE cv_sort.customized_type='#{ self . class . customized_class . name } '
135- AND cv_sort.customized_id=#{ self . class . customized_class . table_name } .id
136- AND cv_sort.custom_field_id=#{ id } ), '')
130+ WHERE #{ cv_sort_only_custom_field_condition_sql } ), '')
137131 SQL
138132 end
139133
140134 def select_custom_value_as_decimal
141135 <<-SQL
142136 (SELECT CAST(cv_sort.value AS decimal(60,3)) FROM #{ CustomValue . table_name } cv_sort
143- WHERE cv_sort.customized_type='#{ self . class . customized_class . name } '
144- AND cv_sort.customized_id=#{ self . class . customized_class . table_name } .id
145- AND cv_sort.custom_field_id=#{ id }
137+ WHERE #{ cv_sort_only_custom_field_condition_sql }
146138 AND cv_sort.value <> ''
147139 AND cv_sort.value IS NOT NULL
148140 LIMIT 1)
@@ -164,4 +156,14 @@ def order_by_version_sql(column)
164156 LIMIT 1)
165157 SQL
166158 end
159+
160+ private
161+
162+ def cv_sort_only_custom_field_condition_sql
163+ <<-SQL
164+ cv_sort.customized_type='#{ self . class . customized_class . name } '
165+ AND cv_sort.customized_id=#{ self . class . customized_class . table_name } .id
166+ AND cv_sort.custom_field_id=#{ id }
167+ SQL
168+ end
167169end
0 commit comments