Skip to content

Commit

Permalink
filter facets for state and years. #269
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed May 11, 2019
1 parent aa75fac commit afd1bff
Showing 1 changed file with 44 additions and 4 deletions.
48 changes: 44 additions & 4 deletions app/graphql/types/prefix_connection_with_total_count_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,61 @@ class PrefixConnectionWithTotalCountType < GraphQL::Types::Relay::BaseConnection
edge_type(PrefixEdgeType)

field :total_count, Integer, null: false
field :states, [FacetType], null: false
field :years, [FacetType], null: false

def total_count
object.nodes.size
end

def states
args = self.object.arguments

if object.parent.class.name == "Provider"
collection = object.parent.provider_prefixes.joins(:prefix)

if args[:state].present?
[{ id: args[:state],
title: args[:state].underscore.humanize,
count: collection.count }]
else
[{ id: "withoutClient",
title: "Without client",
count: collection.state("without-client").count },
{ id: "withClient",
title: "With client",
count: collection.state("with-client").count }]
end
else
[]
end
end

def years
args = self.object.arguments

if object.parent.class.name == "Provider"
collection = object.parent.provider_prefixes.joins(:prefix)
years = collection.where.not(prefixes: nil).order("YEAR(allocator_prefixes.created_at) DESC").group("YEAR(allocator_prefixes.created_at)").count
years.map { |k,v| { id: k.to_s, title: k.to_s, count: v } }

if args[:year].present?
[{ id: args[:year],
title: args[:year],
count: collection.where('YEAR(allocator_prefixes.created_at) = ?', args[:year]).count }]
else
years = collection.where.not(prefixes: nil).order("YEAR(allocator_prefixes.created_at) DESC").group("YEAR(allocator_prefixes.created_at)").count
years.map { |k,v| { id: k.to_s, title: k.to_s, count: v } }
end
elsif object.parent.class.name == "Client"
collection = object.parent.client_prefixes.joins(:prefix)
years = collection.where.not(prefixes: nil).order("YEAR(datacentre_prefixes.created_at) DESC").group("YEAR(datacentre_prefixes.created_at)").count
years.map { |k,v| { id: k.to_s, title: k.to_s, count: v } }

if args[:year].present?
[{ id: args[:year],
title: args[:year],
count: collection.where('YEAR(datacentre_prefixes.created_at) = ?', args[:year]).count }]
else
years = collection.where.not(prefixes: nil).order("YEAR(datacentre_prefixes.created_at) DESC").group("YEAR(datacentre_prefixes.created_at)").count
years.map { |k,v| { id: k.to_s, title: k.to_s, count: v } }
end
else
{}
end
Expand Down

0 comments on commit afd1bff

Please sign in to comment.