How can I rewrite this code so it’s completely dynamic, and I don’t have to use the case clause to manually list all possible values of @group?
# Grouping
@group = params[:group] if !params[:group].blank?
case @group
when 'category_id'
@ideas_grouped = @ideas.group_by { |i| i.category_id }
when 'status_id'
@ideas_grouped = @ideas.group_by { |i| i.status_id }
when 'personal_bias'
@ideas_grouped = @ideas.group_by { |i| i.personal_bias }
when 'business_value'
@ideas_grouped = @ideas.group_by { |i| i.business_value }
end
If you need no white-listing:
If you need white-listing you may call
include?first (see Amar’s answer), but to add something new, let me push it with a declarative approach (Object#whitelistis left as an exercise for the reader,maybecomes from Ick):