Last active
October 13, 2020 16:01
-
-
Save alexshapalov/a7db4d959f1889eaf728542fd4f89874 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example of refactoring long method. | |
def self.get_troubled_reasons(where_cond, session_country) | |
reasons = (1..18).map { |e| "TR#{e.to_s.rjust(3, '0')}" } | |
query = reasons.map do |reason| | |
"SUM(CASE WHEN reason_code LIKE '%#{reason}%' THEN 1 ELSE 0 END) AS #{reason}" | |
end.join(',') | |
TroubledOrder.joins(:order).select(query).where(orders: { country_code: session_country }).where(where_cond).first | |
end | |
# def self.get_troubled_reasons(where_cond, session_country) | |
# troubled_reason = TroubledOrder.joins(:order).select( | |
# "SUM(CASE WHEN reason_code LIKE '%TR001%' THEN 1 ELSE 0 END) AS TR001, | |
# SUM(CASE WHEN reason_code LIKE '%TR001%' THEN 1 ELSE 0 END) AS TR002, | |
# SUM(CASE WHEN reason_code LIKE '%TR001%' THEN 1 ELSE 0 END) AS TR003, | |
# SUM(CASE WHEN reason_code LIKE '%TR001%' THEN 1 ELSE 0 END) AS TR004, | |
# SUM(CASE WHEN reason_code LIKE '%TR001%' THEN 1 ELSE 0 END) AS TR005, | |
# SUM(CASE WHEN reason_code LIKE '%TR001%' THEN 1 ELSE 0 END) AS TR006, | |
# SUM(CASE WHEN reason_code LIKE '%TR001%' THEN 1 ELSE 0 END) AS TR007, | |
# SUM(CASE WHEN reason_code LIKE '%TR001%' THEN 1 ELSE 0 END) AS TR008, | |
# SUM(CASE WHEN reason_code LIKE '%TR001%' THEN 1 ELSE 0 END) AS TR009, | |
# SUM(CASE WHEN reason_code LIKE '%TR001%' THEN 1 ELSE 0 END) AS TR010, | |
# SUM(CASE WHEN reason_code LIKE '%TR001%' THEN 1 ELSE 0 END) AS TR011, | |
# SUM(CASE WHEN reason_code LIKE '%TR001%' THEN 1 ELSE 0 END) AS TR012, | |
# SUM(CASE WHEN reason_code LIKE '%TR001%' THEN 1 ELSE 0 END) AS TR013, | |
# SUM(CASE WHEN reason_code LIKE '%TR001%' THEN 1 ELSE 0 END) AS TR014, | |
# SUM(CASE WHEN reason_code LIKE '%TR001%' THEN 1 ELSE 0 END) AS TR015, | |
# SUM(CASE WHEN reason_code LIKE '%TR001%' THEN 1 ELSE 0 END) AS TR016, | |
# SUM(CASE WHEN reason_code LIKE '%TR001%' THEN 1 ELSE 0 END) AS TR017, | |
# SUM(CASE WHEN reason_code LIKE '%TR001%' THEN 1 ELSE 0 END) AS TR018" | |
# ).where("orders.country_code = '#{session_country}'").where(where_cond).first | |
# troubled_reason | |
# end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment