Class: Hyrax::CustomQueries::FindCountBy

Inherits:
Object
  • Object
show all
Defined in:
app/services/hyrax/custom_queries/find_count_by.rb

Overview

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query_service:) ⇒ FindCountBy

Returns a new instance of FindCountBy.



11
12
13
# File 'app/services/hyrax/custom_queries/find_count_by.rb', line 11

def initialize(query_service:)
  @query_service = query_service
end

Instance Attribute Details

#query_serviceObject (readonly)

Returns the value of attribute query_service.



15
16
17
# File 'app/services/hyrax/custom_queries/find_count_by.rb', line 15

def query_service
  @query_service
end

Class Method Details

.queriesObject



7
8
9
# File 'app/services/hyrax/custom_queries/find_count_by.rb', line 7

def self.queries
  [:find_count_by]
end

Instance Method Details

#find_count_by(hash = {}, models: nil) ⇒ Object

Note:

this is an unoptimized default implementation of this custom query. it’s Hyrax’s policy to provide such implementations of custom queries in use for cross-compatibility of Valkyrie query services. it’s advisable to provide an optimized query for the specific adapter.

Parameters:

  • hash (Hash) (defaults to: {})

    the hash representation of the query



26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/services/hyrax/custom_queries/find_count_by.rb', line 26

def find_count_by(hash = {}, models: nil)
  return nil if models.empty? && hash.blank?

  internal_array = ["{ #{hash.map { |k, v| "\"#{k}\": #{v}" }.join(', ')} }"] if hash.present?
  if models.empty?
    query_service.orm_class.count_by_sql(([find_count_by_properties_query] + internal_array))
  elsif hash.blank?
    query_service.orm_class.count_by_sql([find_count_by_models_query] + [models])
  else
    query_service.orm_class.count_by_sql(([find_count_by_properties_and_models_query] + internal_array + [models]))
  end
end

#find_count_by_models_queryObject



47
48
49
50
51
52
# File 'app/services/hyrax/custom_queries/find_count_by.rb', line 47

def find_count_by_models_query
  <<-SQL
    SELECT count(*) FROM orm_resources
    WHERE internal_resource IN (?);
  SQL
end

#find_count_by_properties_and_models_queryObject



39
40
41
42
43
44
45
# File 'app/services/hyrax/custom_queries/find_count_by.rb', line 39

def find_count_by_properties_and_models_query
  <<-SQL
    SELECT count(*) FROM orm_resources
    WHERE metadata @> ?
    AND internal_resource IN (?);
  SQL
end

#find_count_by_properties_queryObject



54
55
56
57
58
59
# File 'app/services/hyrax/custom_queries/find_count_by.rb', line 54

def find_count_by_properties_query
  <<-SQL
    SELECT count(*) FROM orm_resources
    WHERE metadata @> ?;
  SQL
end