Class: OpenStax::Accounts::SearchLocalAccounts

Inherits:
Object
  • Object
show all
Defined in:
app/routines/openstax/accounts/search_local_accounts.rb

Constant Summary collapse

ACCOUNTS =
OpenStax::Accounts::Account.arel_table
SORTABLE_FIELDS =
{
  'username' => :username,
  'first_name' => :first_name,
  'last_name' => :last_name,
  'full_name' => :full_name,
  'id' => :openstax_uid,
  'created_at' => :created_at
}

Instance Method Summary collapse

Instance Method Details

#exec(*args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'app/routines/openstax/accounts/search_local_accounts.rb', line 21

def exec(*args)
  params = args.last.is_a?(Hash) ? args.pop : {}
  params[:q] ||= args[0]
  params[:ob] ||= args[1]
  params[:pp] ||= args[2]
  params[:p] ||= args[3]

  run(:search, relation: OpenStax::Accounts::Account.unscoped,
               sortable_fields: SORTABLE_FIELDS,
               params: params) do |with|

    with.default_keyword :any

    with.keyword :username do |names|
      names.each do |name|
        sanitized_names = to_string_array(name, append_wildcard: true)
        next @items = @items.none if sanitized_names.empty?

        @items = @items.where(ACCOUNTS[:username].matches_any(sanitized_names))
      end
    end

    with.keyword :first_name do |names|
      names.each do |name|
        sanitized_names = to_string_array(name, append_wildcard: true)
        next @items = @items.none if sanitized_names.empty?

        @items = @items.where(ACCOUNTS[:first_name].matches_any(sanitized_names))
      end
    end

    with.keyword :last_name do |names|
      names.each do |name|
        sanitized_names = to_string_array(name, append_wildcard: true)
        next @items = @items.none if sanitized_names.empty?

        @items = @items.where(ACCOUNTS[:last_name].matches_any(sanitized_names))
      end
    end

    with.keyword :full_name do |names|
      names.each do |name|
        sanitized_names = to_string_array(name, append_wildcard: true)
        next @items = @items.none if sanitized_names.empty?

        @items = @items.where(ACCOUNTS[:full_name].matches_any(sanitized_names))
      end
    end

    with.keyword :name do |names|
      names.each do |name|
        sanitized_names = to_string_array(name, append_wildcard: true)
        next @items = @items.none if sanitized_names.empty?

        @items = @items.where(
          ACCOUNTS[:username].matches_any(sanitized_names)
            .or(ACCOUNTS[:first_name].matches_any(sanitized_names))
            .or(ACCOUNTS[:last_name].matches_any(sanitized_names))
            .or(ACCOUNTS[:full_name].matches_any(sanitized_names))
        )
      end
    end

    with.keyword :id do |ids|
      ids.each do |id|
        sanitized_ids = to_string_array(id)
        next @items = @items.none if sanitized_ids.empty?
        @items = @items.where(ACCOUNTS[:openstax_uid].eq_any(sanitized_ids))
      end
    end

    with.keyword :uuid do |uuids|
      uuids.each do |uuid|
        sanitized_uuids = to_string_array(uuid)
        next @items = @items.none if sanitized_uuids.empty?
        @items = @items.where(ACCOUNTS[:uuid].eq_any(sanitized_uuids))
      end
    end

    with.keyword :support_identifier do |support_identifiers|
      support_identifiers.each do |support_identifier|
        sanitized_identifiers = to_string_array(support_identifier)
        next @items = @items.none if sanitized_identifiers.empty?
        @items = @items.where(ACCOUNTS[:support_identifier].eq_any(sanitized_identifiers))
      end
    end

    with.keyword :any do |terms|
      terms.each do |term|
        sanitized_names = to_string_array(term, append_wildcard: true)
        sanitized_ids = to_string_array(term)
        next @items = @items.none if sanitized_names.empty? || sanitized_ids.empty?

        @items = @items.where(
          ACCOUNTS[:username].matches_any(sanitized_names)
            .or(ACCOUNTS[:first_name].matches_any(sanitized_names))
            .or(ACCOUNTS[:last_name].matches_any(sanitized_names))
            .or(ACCOUNTS[:full_name].matches_any(sanitized_names))
            .or(ACCOUNTS[:openstax_uid].eq_any(sanitized_ids))
        )
      end
    end

  end
end