Module: HasHelpers::Search::SearchUtils

Defined in:
app/lib/has_helpers/search/search_utils.rb

Constant Summary collapse

DEFAULT_QUERY_OPTIONS =
{ page: 1, per_page: 15 }.freeze

Class Method Summary collapse

Class Method Details

.base_query_options(kwarg_options, user) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/lib/has_helpers/search/search_utils.rb', line 39

def base_query_options(kwarg_options, user)
  DEFAULT_QUERY_OPTIONS.merge(
    kwarg_options.slice(:with)
  ).
    tap do |options|
    # `with` may come from the GraphQL JSON scalar with string keys —
    # symbolize them so the organization_id sanitization below sees them.
    options[:with] = (kwarg_options[:with] || {}).symbolize_keys

    if options[:with].has_key?(:organization_id)
      given_org_id = options[:with][:organization_id].to_i
      options[:with].delete(:organization_id) unless given_org_id == 0 || given_org_id == user.organization_id
    end

    unless options[:with][:organization_id]
      options[:with][:organization_id] = user.organization_id
    end
  end
end

.global_resources(_user, application) ⇒ Object



23
24
25
26
27
28
29
# File 'app/lib/has_helpers/search/search_utils.rb', line 23

def global_resources(_user, application)
  application.resources.
    select { |r| r.resource_type == ::HasHelpers::ResourceType::BASE }.
    select do |r|
    r.klass.search_index rescue nil
  end
end

.providerObject



31
32
33
34
35
36
37
# File 'app/lib/has_helpers/search/search_utils.rb', line 31

def provider
  if ::HasHelpers::Feature.active?(:sql_search)
    ::HasHelpers::Query::Sql
  else
    ::HasHelpers::Query::Elasticsearch
  end
end

.resources(user, application, resources) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/lib/has_helpers/search/search_utils.rb', line 9

def resources(user, application, resources)
  result = Array(resources).map(&:strip)

  if result.empty?
    global_resources(user, application).map(&:name)
  else
    resource_indexes = []
    resources.each do |resource|
      resource_indexes << resource
    end
    resource_indexes
  end
end

.valid_resource?(resource, application) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'app/lib/has_helpers/search/search_utils.rb', line 59

def valid_resource?(resource, application)
  application.resources.any? { |r| r.name == resource }
end

.validate_resource!(resource, application) ⇒ Object



63
64
65
66
67
68
69
70
# File 'app/lib/has_helpers/search/search_utils.rb', line 63

def validate_resource!(resource, application)
  resource = resource.sub(/^::/, "")
  unless valid_resource?(resource, application) || valid_resource?(resource.safe_constantize, application)
    {
      status: 400 # BAD_REQUEST
    }
  end
end