Skip to content

Instantly share code, notes, and snippets.

@luckyruby
Created February 13, 2012 19:58
Show Gist options
  • Save luckyruby/1819712 to your computer and use it in GitHub Desktop.
Save luckyruby/1819712 to your computer and use it in GitHub Desktop.
def self.qualifying_cartypes(location, cartypes)
result = []
supported_cartypes = location.supported_car_types
cartypes.split(":").each do |cartype|
name_match = cartype[0,4].gsub('*','_').ljust(4,'_')
if cartype[4,1] == '*'
minimum_cartype = CarType.where("name ilike ?", "#{name_match}%").order(:position).first
supported_cartypes.each {|i| result << i.name if i.position.to_i >= minimum_cartype.position}
elsif cartype == '*'
supported_cartypes.each{|i| result << i.name}
elsif cartype[0,1] == 'A' #use pseudo cartype logic
case cartype
when 'ACAR'
result.concat CarType.where("substring(name from 2 for 1) NOT IN ('K','Y','R','H')").pluck(:name)
when 'ALLB'
result.concat CarType.where("substring(name from 2 for 1) IN ('B','C','E','N')").pluck(:name)
when 'ALLC'
result.concat CarType.where("substring(name from 2 for 1) NOT IN ('R','H','Y','K','P','Q')").pluck(:name)
when 'ALLD'
result.concat CarType.where("substring(name from 2 for 1) IN ('D','F','G','L','V','W')").pluck(:name)
when 'AELT'
result.concat CarType.where("substring(name from 1 for 1) IN ('N','H','D','J','R','G','U','W')").pluck(:name)
when 'ACPR'
result.concat CarType.where("substring(name from 2 for 1) IN ('E','N')").pluck(:name)
when 'ASPC'
result.concat CarType.where("substring(name from 1 for 1) = 'X' OR substring(name from 2 for 1) = 'X'").pluck(:name)
when 'ASPX'
result.concat CarType.all
when 'APUP'
result.concat CarType.where("substring(name from 2 for 1) IN ('Q','P')").pluck(:name)
when 'AWGN'
result.concat CarType.where("substring(name from 2 for 1) IN ('V','F','W','G')").pluck(:name)
when 'AREC'
result.concat CarType.where("substring(name from 2 for 1) IN ('R','H')").pluck(:name)
when 'ASUV'
result.concat CarType.where("substring(name from 2 for 1) = 'F'").pluck(:name)
when 'AVAN'
result.concat CarType.where("substring(name from 2 for 1) = 'V'").pluck(:name)
when 'ASIX'
result.concat CarType.where("(substring(name from 1 for 1) IN ('I','J','S','R','G','P','F','U') AND substring(name from 2 for 1) = 'V') OR (substring(name from 1 for 1) IN ('F','G','P','U') AND substring(name from 2 for 1) = 'F')").pluck(:name)
when 'ASEV'
result.concat CarType.where("(substring(name from 1 for 1) IN ('S','R','F','G','P','U') AND substring(name from 2 for 1) = 'V') OR (substring(name from 1 for 1) IN ('P','U') AND substring(name from 2 for 1) = 'F')").pluck(:name)
when 'AEIG'
result.concat CarType.where("substring(name from 1 for 1) IN ('S','R','F','G','P','U') AND substring(name from 2 for 1) = 'V'").pluck(:name)
when 'AFWD'
result.concat CarType.where("substring(name from 3 for 1) IN ('N','C','B','D')").pluck(:name)
when 'ATRV'
result.concat CarType.where("substring(name from 2 for 1) = 'J'").pluck(:name)
when 'ACGO'
result.concat CarType.where("substring(name from 2 for 1) = 'K'").pluck(:name)
when 'ALMO'
result.concat CarType.where("substring(name from 2 for 1) = 'L'").pluck(:name)
when 'ASPT'
result.concat CarType.where("substring(name from 2 for 1) = 'S'").pluck(:name)
when 'ACNV'
result.concat CarType.where("substring(name from 2 for 1) IN ('T','N')").pluck(:name)
when 'AOFR'
result.concat CarType.where("substring(name from 2 for 1) = 'Z'").pluck(:name)
when 'AMNO'
result.concat CarType.where("substring(name from 2 for 1) = 'M'").pluck(:name)
when 'AMTO'
result.concat CarType.where("substring(name from 2 for 1) = 'H'").pluck(:name)
when 'AMCY'
result.concat CarType.where("substring(name from 2 for 1) = 'Y'").pluck(:name)
when 'ACRS'
result.concat CarType.where("substring(name from 2 for 1) = 'G'").pluck(:name)
when 'AMAN'
result.concat CarType.where("substring(name from 3 for 1) IN ('M','N','C')").pluck(:name)
when 'AUTO'
result.concat CarType.where("substring(name from 3 for 1) IN ('A','B','D')").pluck(:name)
when 'AGAS'
result.concat CarType.where("substring(name from 4 for 1) IN ('N','V','R','Z')").pluck(:name)
when 'APET'
result.concat CarType.where("substring(name from 4 for 1) IN ('N','V','R','Z')").pluck(:name)
when 'ADSL'
result.concat CarType.where("substring(name from 4 for 1) IN ('D','Q')").pluck(:name)
when 'AGRN'
result.concat CarType.where("substring(name from 4 for 1) IN ('H','I','E','C','L','S','A','B','M','F','U','X')").pluck(:name)
when 'AHYB'
result.concat CarType.where("substring(name from 4 for 1) IN ('H','I')").pluck(:name)
when 'AELC'
result.concat CarType.where("substring(name from 4 for 1) IN ('E','C')").pluck(:name)
when 'AHYD'
result.concat CarType.where("substring(name from 4 for 1) IN ('A','B')").pluck(:name)
when 'AMFP'
result.concat CarType.where("substring(name from 4 for 1) IN ('M','F')").pluck(:name)
when 'ACPG'
result.concat CarType.where("substring(name from 4 for 1) IN ('L','S')").pluck(:name)
when 'AETH'
result.concat CarType.where("substring(name from 4 for 1) IN ('U','X')").pluck(:name)
end
else
conditions = ["name ilike ?"]
name_match[0] = '_' if %w(M E C I S F P L).include? cartype[0,1]
name_match[1] = '_' if %w(B C D X P Q W R H).include? cartype[1,1]
name_match[2] = '_' if %w(A M N B).include? cartype[2,1]
name_match[3] = '_' if %w(R N Q I C S B F Z X).include? cartype[3,1]
#If MV is requested allow IV to be returned (and vice versa)
if %w(MV IV).include? cartype[0,2]
name_match[0,2] = '__'
conditions << "substring(name from 1 for 2) IN ('MV','IV','NV','JV')"
end
#Elite Rules
case cartype[0,1]
when 'M' && cartype[1,1] != 'V'
conditions << "substring(name from 1 for 1) IN ('M','N')"
when 'E'
conditions << "substring(name from 1 for 1) IN ('E','H')"
when 'C'
conditions << "substring(name from 1 for 1) IN ('C','D')"
when 'I' && cartype[1,1] != 'V'
conditions << "substring(name from 1 for 1) IN ('I','J')"
when 'S'
conditions << "substring(name from 1 for 1) IN ('S','R')"
when 'F'
conditions << "substring(name from 1 for 1) IN ('F','G')"
when 'P'
conditions << "substring(name from 1 for 1) IN ('P','U')"
when 'L'
conditions << "substring(name from 1 for 1) IN ('L','W')"
end
#2nd Position Rules
case cartype[1,1]
when 'B' #If B in Type, allow B, C, E, or N.
conditions << "substring(name from 2 for 1) IN ('B','C','E','N')"
when 'C' #If C in Type, allow any car type (2nd position) to be returned except R, H, Y, or K.
conditions << "substring(name from 2 for 1) NOT IN ('R','H','Y','K')"
when 'D' #If D in Type, allow D, X, Z, W, V, L, G or F.
conditions << "substring(name from 2 for 1) IN ('D','X','Z','W','V','L','G','F')"
when 'E','N'
conditions << "substring(name from 2 for 1) IN ('E','N')"
when 'X' #If X in Type allow S, T, X, Z, E, N, G, or F.
conditions << "substring(name from 2 for 1) IN ('S','T','X','Z','E','N','G','F')"
when 'P','Q' #If either P or Q in Type allow P or Q
conditions << "substring(name from 2 for 1) IN ('P','Q')"
when 'W' #If W in Type allow V, F, W, or G
conditions << "substring(name from 2 for 1) IN ('V','F','W','G')"
when 'R','H' #If R in Type allow H and if H in type allow R.
conditions << "substring(name from 2 for 1) IN ('H','R')"
end
#3rd Position Rules
case cartype[2,1]
when 'A' #If A is requested in Trans/Drive allow only A, B or D Trans/Drive to be returned.
conditions << "substring(name from 3 for 1) IN ('A','B','D')"
when 'M' #If M is requested in Trans/Drive, allow any Trans/Drive to be returned
#no conditions
#If N or B is requested in Trans/Drive, allow the equivalent AWD to be returned.
when 'N'
conditions << "substring(name from 3 for 1) IN ('N','C')"
when 'B'
conditions << "substring(name from 3 for 1) IN ('B','D')"
end
#4th Position Rules
case cartype[3,1]
when 'R' #If R is requested in Fuel/Air, allow only A/C code in Fuel/AirCon.
conditions << "substring(name from 4 for 1) IN ('R','D','H','E','L','A','M','V','U')"
when 'N' #If N is requested in Fuel/Air, allow any air or non-air code in Fuel/AirCon.
#no conditions
#When any non-air is requested, also allow the equivalent A/C to be returned.
when 'Q'
conditions << "substring(name from 4 for 1) IN ('Q','D')"
when 'I'
conditions << "substring(name from 4 for 1) IN ('I','H')"
when 'C'
conditions << "substring(name from 4 for 1) IN ('C','E')"
when 'S'
conditions << "substring(name from 4 for 1) IN ('S','L')"
when 'B'
conditions << "substring(name from 4 for 1) IN ('B','A')"
when 'F'
conditions << "substring(name from 4 for 1) IN ('F','M')"
when 'Z'
conditions << "substring(name from 4 for 1) IN ('Z','V')"
when 'X'
conditions << "substring(name from 4 for 1) IN ('X','U')"
end
conditions = conditions.join(" AND ")
result.concat CarType.where(conditions, name_match).pluck(:name)
end
end
result.uniq!
supported_cartypes.select{|i| result.include?(i.name)} if result.present?
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment