openstax_utilities 4.1.0 → 4.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. checksums.yaml +5 -5
  2. data/app/routines/openstax/utilities/limit_and_paginate_relation.rb +85 -0
  3. data/app/routines/openstax/utilities/order_relation.rb +105 -0
  4. data/app/routines/openstax/utilities/search_and_organize_relation.rb +130 -0
  5. data/app/routines/openstax/utilities/search_relation.rb +94 -0
  6. data/lib/openstax/utilities/access_policy.rb +4 -3
  7. data/lib/openstax/utilities/assets.rb +29 -0
  8. data/lib/openstax/utilities/assets/manifest.rb +62 -0
  9. data/lib/openstax/utilities/version.rb +1 -1
  10. data/lib/openstax_utilities.rb +5 -5
  11. data/spec/cassettes/OpenStax_Utilities_Assets/loading_remote_manifest/uses_remote_json.yml +353 -0
  12. data/spec/dummy/app/access_policies/dummier_access_policy.rb +10 -0
  13. data/spec/dummy/app/assets/config/manifest.js +3 -0
  14. data/spec/dummy/config/application.rb +6 -11
  15. data/spec/dummy/config/environments/test.rb +1 -1
  16. data/spec/dummy/config/initializers/search_users.rb +26 -0
  17. data/spec/dummy/config/secrets.yml +2 -5
  18. data/spec/dummy/db/test.sqlite3 +0 -0
  19. data/spec/dummy/log/test.log +32144 -0
  20. data/spec/dummy/tmp/cache/C09/760/6da7b2a29da9cb0f80ef102c7effb91fab3374db +0 -0
  21. data/spec/factories/user.rb +1 -1
  22. data/spec/lib/openstax/utilities/access_policy_spec.rb +16 -15
  23. data/spec/lib/openstax/utilities/assets_spec.rb +40 -0
  24. data/spec/rails_helper.rb +1 -2
  25. data/spec/routines/openstax/utilities/limit_and_paginate_relation_spec.rb +72 -0
  26. data/spec/routines/openstax/utilities/order_relation_spec.rb +55 -0
  27. data/spec/routines/openstax/utilities/{abstract_keyword_search_routine_spec.rb → search_and_organize_relation_spec.rb} +54 -47
  28. data/spec/routines/openstax/utilities/search_relation_spec.rb +81 -0
  29. data/spec/vcr_helper.rb +18 -0
  30. metadata +123 -44
  31. data/app/handlers/openstax/utilities/keyword_search_handler.rb +0 -95
  32. data/app/routines/openstax/utilities/abstract_keyword_search_routine.rb +0 -158
  33. data/spec/dummy/app/routines/search_users.rb +0 -21
  34. data/spec/handlers/openstax/utilities/keyword_search_handler_spec.rb +0 -126
@@ -1,21 +0,0 @@
1
- # Dummy routine for testing the general keyword search
2
-
3
- class SearchUsers < OpenStax::Utilities::AbstractKeywordSearchRoutine
4
- self.search_proc = lambda { |with|
5
- with.keyword :username do |names|
6
- snames = to_string_array(names, append_wildcard: true)
7
- @items = @items.where{username.like_any snames}
8
- end
9
-
10
- with.keyword :first_name do |names|
11
- snames = to_string_array(names, append_wildcard: true)
12
- @items = @items.where{name.like_any snames}
13
- end
14
-
15
- with.keyword :last_name do |names|
16
- snames = to_string_array(names, append_wildcard: true).collect{|name| "% #{name}"}
17
- @items = @items.where{name.like_any snames}
18
- end
19
- }
20
- self.sortable_fields_map = {'name' => :name, 'created_at' => :created_at, 'id' => :id}
21
- end
@@ -1,126 +0,0 @@
1
- require 'rails_helper'
2
-
3
- module OpenStax
4
- module Utilities
5
- describe KeywordSearchHandler do
6
-
7
- options = {
8
- caller: FactoryGirl.create(:user),
9
- search_routine: SearchUsers,
10
- search_relation: User.unscoped,
11
- max_items: 10,
12
- min_characters: 3
13
- }
14
-
15
- let!(:john_doe) { FactoryGirl.create :user, name: "John Doe",
16
- username: "doejohn",
17
- email: "john@doe.com" }
18
-
19
- let!(:jane_doe) { FactoryGirl.create :user, name: "Jane Doe",
20
- username: "doejane",
21
- email: "jane@doe.com" }
22
-
23
- let!(:jack_doe) { FactoryGirl.create :user, name: "Jack Doe",
24
- username: "doejack",
25
- email: "jack@doe.com" }
26
-
27
- before(:each) do
28
- 100.times do
29
- FactoryGirl.create(:user)
30
- end
31
-
32
- DummyAccessPolicy.last_action = nil
33
- DummyAccessPolicy.last_requestor = nil
34
- DummyAccessPolicy.last_resource = nil
35
- end
36
-
37
- it "passes its params to the search routine and sets the total_count output" do
38
- outputs = KeywordSearchHandler.call(options.merge(
39
- params: {q: 'username:dOe'})).outputs
40
- total_count = outputs[:total_count]
41
- items = outputs[:items]
42
- expect(DummyAccessPolicy.last_action).to eq :search
43
- expect(DummyAccessPolicy.last_requestor).to eq options[:caller]
44
- expect(DummyAccessPolicy.last_resource).to eq User
45
- expect(total_count).to eq items.count
46
- expect(items).to include(john_doe)
47
- expect(items).to include(jane_doe)
48
- expect(items).to include(jack_doe)
49
- john_index = items.index(john_doe)
50
- jane_index = items.index(jane_doe)
51
- jack_index = items.index(jack_doe)
52
- expect(jane_index).to be > john_index
53
- expect(jack_index).to be > jane_index
54
- items.each do |item|
55
- expect(item.username).to match(/\Adoe[\w]*\z/i)
56
- end
57
-
58
- DummyAccessPolicy.last_action = nil
59
- DummyAccessPolicy.last_requestor = nil
60
- DummyAccessPolicy.last_resource = nil
61
- outputs = KeywordSearchHandler.call(options.merge(
62
- params: {order_by: 'cReAtEd_At DeSc, iD dEsC',
63
- q: 'username:DoE'})).outputs
64
- total_count = outputs[:total_count]
65
- items = outputs[:items]
66
- expect(DummyAccessPolicy.last_action).to eq :search
67
- expect(DummyAccessPolicy.last_requestor).to eq options[:caller]
68
- expect(DummyAccessPolicy.last_resource).to eq User
69
- expect(total_count).to eq items.count
70
- expect(items).to include(john_doe)
71
- expect(items).to include(jane_doe)
72
- expect(items).to include(jack_doe)
73
- john_index = items.index(john_doe)
74
- jane_index = items.index(jane_doe)
75
- jack_index = items.index(jack_doe)
76
- expect(jane_index).to be < john_index
77
- expect(jack_index).to be < jane_index
78
- items.each do |item|
79
- expect(item.username).to match(/\Adoe[\w]*\z/i)
80
- end
81
- end
82
-
83
- it "errors out if no query is provided" do
84
- routine = KeywordSearchHandler.call(options.merge(params: {}))
85
- outputs = routine.outputs
86
- errors = routine.errors
87
- expect(DummyAccessPolicy.last_action).to eq :search
88
- expect(DummyAccessPolicy.last_requestor).to eq options[:caller]
89
- expect(DummyAccessPolicy.last_resource).to eq User
90
- expect(outputs).to be_empty
91
- expect(errors).not_to be_empty
92
- expect(errors.first.code).to eq :query_blank
93
- end
94
-
95
- it "errors out if the query is too short" do
96
- routine = KeywordSearchHandler.call(options.merge(params: {q: 'a'}))
97
- outputs = routine.outputs
98
- errors = routine.errors
99
- expect(DummyAccessPolicy.last_action).to eq :search
100
- expect(DummyAccessPolicy.last_requestor).to eq options[:caller]
101
- expect(DummyAccessPolicy.last_resource).to eq User
102
- expect(outputs).to be_empty
103
- expect(errors).not_to be_empty
104
- expect(errors.first.code).to eq :query_too_short
105
- end
106
-
107
- it "errors out if too many items match" do
108
- routine = KeywordSearchHandler.call(options.merge(
109
- params: {
110
- q: 'username:a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,0,1,2,3,4,5,6,7,8,9,-,_'
111
- }))
112
- outputs = routine.outputs
113
- errors = routine.errors
114
- expect(DummyAccessPolicy.last_action).to eq :search
115
- expect(DummyAccessPolicy.last_requestor).to eq options[:caller]
116
- expect(DummyAccessPolicy.last_resource).to eq User
117
- expect(outputs).not_to be_empty
118
- expect(outputs[:total_count]).to eq User.count
119
- expect(outputs[:items]).to be_nil
120
- expect(errors).not_to be_empty
121
- expect(errors.first.code).to eq :too_many_items
122
- end
123
-
124
- end
125
- end
126
- end