This file contains 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
SERVICE_GRADE_CODE_OPTIONS = [ | |
['Any', []], | |
['E01-E04', ['E01', 'E02', 'E03', 'E04']], | |
['E05-E07', ['E05', 'E06', 'E07']], | |
['E08-E09', ['E08', 'E09']], | |
['O01-O03', ['O01', 'O02', 'O03']], | |
['O04-O10', ['O04', 'O05', 'O06', 'O07', 'O08', 'O09', 'O10']], | |
['WO1-WO5', ['WO1', 'WO2', 'WO3', 'WO4', 'WO5']], | |
['Other', []] | |
] |
Then we render that in our page with typical code:
This file contains 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
<label for="service_grade_code">Rank:</label> | |
<%= select_tag 'service_grade_code', | |
options_for_select EvacuationsController::SERVICE_GRADE_CODE_OPTIONS.map(&:first) %> |
Back in the controller, when the form is submitted, the following code sets up the conditions for a find call:
This file contains 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
unless params[:service_grade_code].blank? || params[:service_grade_code] == 'Any' | |
if params[:service_grade_code] == 'Other' | |
conditions << ['merged_movements.service_grade_code NOT IN (?)', | |
SERVICE_GRADE_CODE_OPTIONS.inject([]) {|m, sgco| m + sgco.last}] | |
elsif codes = SERVICE_GRADE_CODE_OPTIONS.detect {|sgco| sgco.first == params[:service_grade_code]}.last | |
conditions << ['merged_movements.service_grade_code IN (?)', codes] | |
end | |
end |
The only somewhat interesting bit is how the conditions for "Other" are set up with an inject from the valid options defined in the SERVICE_GRADE_CODE_OPTIONS constant.