Skip to content

Instantly share code, notes, and snippets.

@ShepFc3
Created June 7, 2011 23:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShepFc3/1013446 to your computer and use it in GitHub Desktop.
Save ShepFc3/1013446 to your computer and use it in GitHub Desktop.
@user_groups = {}
if params[:filter] && params[:filter][:dynamic]
group_ids = feed_search.dynamic_usertags.collect { |e| e.gsub('group_id:','') }
group_ids.each do |group_id|
group = Group.find group_id
@user_groups[group.primary_filter.name.downcase] = group if group
end
end
# Refactor
@user_groups = {}
if params[:filter] && params[:filter][:dynamic]
primary_filter_groups = current_user.groups.with_primary_filter
params[:filter][:dynamic].each do |dyn|
group = primary_filter_groups.select{ |g| g.primary_filter.filter_type == dyn }
@user_groups[group.primary_filter.filter_type] = group if group
end
end
@ShepFc3
Copy link
Author

ShepFc3 commented Jun 8, 2011

Here's my final refactor. Moved the query by filter type to a named scope.

if params[:filter] && params[:filter][:dynamic]
@user_groups = Hash[*current_user.groups.with_primary_filter.by_filter_type(
params[:filter][:dynamic]
).collect{ |group|
[group.primary_filter.filter_type, group]
}.compact.flatten]
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment