Skip to content

Instantly share code, notes, and snippets.

@jeena
Created December 4, 2012 15:51
Show Gist options
  • Save jeena/4205374 to your computer and use it in GitHub Desktop.
Save jeena/4205374 to your computer and use it in GitHub Desktop.
class StatisticsController < ApplicationController
layout "statistics"
def search
params.merge!({q: {id_eq: 0}}) if params[:q].blank? #so we avoid displaying all the elements at the first load...
@search = OrganizationalUnit.search(params[:q])
if @search.result.length > 0
@organizational_unit_paginated = @search.result(distinct: true).page(params[:page])
@organizational_units = OrganizationalUnit.items_list(@organizational_unit_paginated.map(&:id))
@stats = {}
result1 = @search.result.group_by(&:organizational_unit_kind_id).reject{|k,v| k == :nil || k == nil}
ok = OrganizationalUnitKind.where{id.in(result1.keys)}.all
@stats[:organizational_unit_kinds] = result1.inject({}){|h,(k,v)| h.merge({ok.select{|o| o.id == k.to_i}.try(:first).try(:name) => v})}
result1 = @search.result.group_by(&:brand_id).reject{|k,v| k == :nil || k == nil}
ok = Brand.where{id.in(result1.keys)}.all
@stats[:brands] = result1.inject({}){|h,(k,v)| h.merge({ok.select{|o| o.id == k.to_i}.try(:first).try(:name) => v})}
end
end
def overview
@organizational_unit_kinds = OrganizationalUnitKind.includes([:brands,:categories, :organizational_units]).all
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment