standardapi 6.0.0.32 → 7.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +80 -58
  3. data/lib/standard_api/access_control_list.rb +148 -0
  4. data/lib/standard_api/controller.rb +116 -27
  5. data/lib/standard_api/helpers.rb +17 -10
  6. data/lib/standard_api/includes.rb +9 -0
  7. data/lib/standard_api/middleware/query_encoding.rb +3 -3
  8. data/lib/standard_api/middleware.rb +5 -0
  9. data/lib/standard_api/railtie.rb +30 -2
  10. data/lib/standard_api/route_helpers.rb +64 -14
  11. data/lib/standard_api/test_case/calculate_tests.rb +15 -8
  12. data/lib/standard_api/test_case/create_tests.rb +7 -9
  13. data/lib/standard_api/test_case/destroy_tests.rb +19 -7
  14. data/lib/standard_api/test_case/index_tests.rb +10 -6
  15. data/lib/standard_api/test_case/schema_tests.rb +7 -1
  16. data/lib/standard_api/test_case/show_tests.rb +8 -7
  17. data/lib/standard_api/test_case/update_tests.rb +15 -15
  18. data/lib/standard_api/test_case.rb +13 -3
  19. data/lib/standard_api/version.rb +1 -1
  20. data/lib/standard_api/views/application/_record.json.jbuilder +18 -17
  21. data/lib/standard_api/views/application/_record.streamer +40 -37
  22. data/lib/standard_api/views/application/_schema.json.jbuilder +20 -8
  23. data/lib/standard_api/views/application/_schema.streamer +22 -8
  24. data/lib/standard_api/views/application/new.streamer +1 -1
  25. data/lib/standard_api.rb +5 -0
  26. data/test/standard_api/caching_test.rb +14 -4
  27. data/test/standard_api/controller/include_test.rb +107 -0
  28. data/test/standard_api/controller/subresource_test.rb +157 -0
  29. data/test/standard_api/helpers_test.rb +34 -17
  30. data/test/standard_api/nested_attributes/belongs_to_test.rb +71 -0
  31. data/test/standard_api/nested_attributes/has_and_belongs_to_many_test.rb +70 -0
  32. data/test/standard_api/nested_attributes/has_many_test.rb +85 -0
  33. data/test/standard_api/nested_attributes/has_one_test.rb +71 -0
  34. data/test/standard_api/route_helpers_test.rb +56 -0
  35. data/test/standard_api/standard_api_test.rb +182 -44
  36. data/test/standard_api/test_app/app/controllers/acl/account_acl.rb +15 -0
  37. data/test/standard_api/test_app/app/controllers/acl/camera_acl.rb +7 -0
  38. data/test/standard_api/test_app/app/controllers/acl/photo_acl.rb +13 -0
  39. data/test/standard_api/test_app/app/controllers/acl/property_acl.rb +33 -0
  40. data/test/standard_api/test_app/app/controllers/acl/reference_acl.rb +7 -0
  41. data/test/standard_api/test_app/controllers.rb +28 -43
  42. data/test/standard_api/test_app/models.rb +76 -7
  43. data/test/standard_api/test_app/test/factories.rb +7 -3
  44. data/test/standard_api/test_app/views/photos/_photo.json.jbuilder +1 -0
  45. data/test/standard_api/test_app/views/photos/_photo.streamer +2 -1
  46. data/test/standard_api/test_app/views/sessions/create.json.jbuilder +1 -0
  47. data/test/standard_api/test_app/views/sessions/create.streamer +3 -0
  48. data/test/standard_api/test_app.rb +12 -1
  49. data/test/standard_api/test_helper.rb +21 -0
  50. metadata +59 -16
@@ -1,17 +1,22 @@
1
1
  # = Models
2
2
 
3
3
  class Account < ActiveRecord::Base
4
- has_many :photos
4
+ has_many :photos, -> { order(:created_at) }
5
5
  belongs_to :property
6
+ belongs_to :subject, polymorphic: true
6
7
  end
7
8
 
8
9
  class Photo < ActiveRecord::Base
9
- belongs_to :account, :counter_cache => true
10
+ belongs_to :account, counter_cache: true
10
11
  has_and_belongs_to_many :properties
12
+ has_one :camera
11
13
  end
12
14
 
13
15
  class Document < ActiveRecord::Base
14
16
  attr_accessor :file
17
+
18
+ enum level: { public: 0, secret: 1 }, _suffix: true
19
+ enum rating: { poor: 0, ok: 1, good: 2 }
15
20
  end
16
21
 
17
22
  class Pdf < Document
@@ -24,6 +29,7 @@ class Property < ActiveRecord::Base
24
29
  has_one :document_attachments, class_name: "Attachment", as: :record, inverse_of: :record
25
30
  has_one :document, through: "document_attachments"
26
31
 
32
+
27
33
  validates :name, presence: true
28
34
  accepts_nested_attributes_for :photos
29
35
 
@@ -32,8 +38,36 @@ class Property < ActiveRecord::Base
32
38
  end
33
39
  end
34
40
 
41
+ class LSNType < ActiveRecord::Type::Value
42
+
43
+ def type
44
+ :lsn
45
+ end
46
+
47
+ def cast_value(value)
48
+ case value
49
+ when Integer
50
+ [value].pack('N')
51
+ else
52
+ value&.to_s&.b
53
+ end
54
+ end
55
+
56
+ def serialize(value)
57
+ PG::TextEncoder::Bytea.new.encode(value)
58
+ end
59
+
60
+ def deserialize(value)
61
+ return nil if value.nil?
62
+ PG::TextDecoder::Bytea.new.decode(value).unpack1('N')
63
+ end
64
+
65
+ end
66
+
35
67
  class Reference < ActiveRecord::Base
36
68
  belongs_to :subject, polymorphic: true
69
+
70
+ attribute :custom_binary, LSNType.new
37
71
  end
38
72
 
39
73
  class Document < ActiveRecord::Base
@@ -45,7 +79,16 @@ class Attachment < ActiveRecord::Base
45
79
  belongs_to :document
46
80
  end
47
81
 
48
- # = Migration
82
+ class Camera < ActiveRecord::Base
83
+ end
84
+
85
+ class UuidModel < ActiveRecord::Base
86
+ end
87
+
88
+ # = Create/recreate database and migration
89
+ task = ActiveRecord::Tasks::PostgreSQLDatabaseTasks.new(ActiveRecord::Base.connection_db_config)
90
+ task.drop
91
+ task.create
49
92
 
50
93
  class CreateModelTables < ActiveRecord::Migration[6.0]
51
94
 
@@ -59,13 +102,23 @@ class CreateModelTables < ActiveRecord::Migration[6.0]
59
102
  create_table "accounts", force: :cascade do |t|
60
103
  t.string 'name', limit: 255
61
104
  t.integer 'property_id'
105
+ t.integer "subject_id"
106
+ t.string "subject_type"
107
+ t.datetime "property_cached_at"
108
+ t.datetime "subject_cached_at"
62
109
  t.integer 'photos_count', null: false, default: 0
110
+ t.datetime "created_at", null: false
111
+ end
112
+
113
+ create_table "landlords", force: :cascade do |t|
114
+ t.string "name"
63
115
  end
64
116
 
65
117
  create_table "photos", force: :cascade do |t|
66
118
  t.integer "account_id"
67
119
  t.integer "property_id"
68
120
  t.string "format", limit: 255
121
+ t.datetime "created_at", null: false
69
122
  end
70
123
 
71
124
  create_table "properties", force: :cascade do |t|
@@ -81,29 +134,45 @@ class CreateModelTables < ActiveRecord::Migration[6.0]
81
134
  create_table "references", force: :cascade do |t|
82
135
  t.integer "subject_id"
83
136
  t.string "subject_type", limit: 255
137
+ t.binary "sha"
138
+ t.binary "custom_binary"
84
139
  t.string "key"
85
140
  t.string "value"
86
141
  end
87
-
142
+
88
143
  create_table "photos_properties", force: :cascade do |t|
89
144
  t.integer "photo_id"
90
145
  t.integer "property_id"
146
+ t.index ["photo_id", "property_id"], unique: true
91
147
  end
92
-
148
+
93
149
  create_table "landlords_properties", force: :cascade do |t|
94
150
  t.integer "landlord_id"
95
151
  t.integer "property_id"
96
152
  end
97
153
 
98
154
  create_table "documents", force: :cascade do |t|
155
+ t.integer 'level', limit: 2, null: false, default: 0
156
+ t.integer 'rating', limit: 2
99
157
  t.string 'type'
100
158
  end
101
-
102
- create_table "attachments", force: :cascade do |t|
159
+
160
+ create_table "cameras", force: :cascade do |t|
161
+ t.integer 'photo_id'
162
+ t.string 'make'
163
+ end
164
+
165
+ create_table "attachments", force: :cascade do |t|
103
166
  t.string 'record_type'
104
167
  t.integer 'record_id'
105
168
  t.integer 'document_id'
106
169
  end
170
+
171
+ create_table "uuid_models", id: :uuid, force: :cascade do |t|
172
+ t.string 'title', default: 'recruit'
173
+ t.string 'name', default: -> { 'round(random() * 1000)' }
174
+ end
175
+
107
176
  end
108
177
 
109
178
  end
@@ -1,4 +1,5 @@
1
1
  FactoryBot.define do
2
+
2
3
  factory :account do
3
4
  name { Faker::Name.name }
4
5
 
@@ -7,7 +8,7 @@ FactoryBot.define do
7
8
  name { nil }
8
9
  end
9
10
  end
10
-
11
+
11
12
  factory :landlord do
12
13
  name { Faker::Name.name }
13
14
  end
@@ -17,12 +18,12 @@ FactoryBot.define do
17
18
  end
18
19
 
19
20
  factory :document do
20
- file { fixture_file_upload(Rails.root + 'test/fixtures/photo.png', 'image/png') }
21
+ file { Rack::Test::UploadedFile.new(File.join(Rails.root, 'test/fixtures/photo.png'), 'image/png') }
21
22
  end
22
23
 
23
24
  factory :pdf do
24
25
  type { 'Pdf' }
25
- file { fixture_file_upload(Rails.root + 'test/fixtures/photo.png', 'image/png') }
26
+ file { Rack::Test::UploadedFile.new(File.join(Rails.root, 'test/fixtures/photo.png'), 'image/png') }
26
27
  end
27
28
 
28
29
  factory :reference do
@@ -47,4 +48,7 @@ FactoryBot.define do
47
48
  end
48
49
  end
49
50
 
51
+ factory :camera do
52
+ make { ['Sony', 'Nokia', 'Canon', 'Leica'].sample }
53
+ end
50
54
  end
@@ -2,6 +2,7 @@ json.set! :id, photo.id
2
2
  json.set! :account_id, photo.account_id
3
3
  json.set! :property_id, photo.property_id
4
4
  json.set! :format, photo.format
5
+ json.set! :created_at, photo.created_at
5
6
  json.set! :template, 'photos/_photo'
6
7
 
7
8
  if includes[:account]
@@ -3,6 +3,7 @@ json.object! do
3
3
  json.set! :account_id, photo.account_id
4
4
  json.set! :property_id, photo.property_id
5
5
  json.set! :format, photo.format
6
+ json.set! :created_at, photo.created_at
6
7
  json.set! :template, 'photos/_photo'
7
8
 
8
9
  if includes[:account]
@@ -10,7 +11,7 @@ json.object! do
10
11
  if photo.account
11
12
  json.partial! 'application/record', record: photo.account, includes: includes[:account]
12
13
  else
13
- json.null!
14
+ json.value! nil
14
15
  end
15
16
  end
16
17
  end
@@ -0,0 +1 @@
1
+ json.set! 'includes', includes
@@ -0,0 +1,3 @@
1
+ json.object! do
2
+ json.set! 'includes', includes
3
+ end
@@ -18,7 +18,7 @@ class TestApplication < Rails::Application
18
18
  config.cache_classes = true
19
19
  config.action_controller.perform_caching = true
20
20
  config.cache_store = :memory_store, { size: 8.megabytes }
21
- config.action_dispatch.show_exceptions = false
21
+ config.action_dispatch.show_exceptions = :none
22
22
 
23
23
  # if defined?(FactoryBotRails)
24
24
  # config.factory_bot.definition_file_paths += [ '../factories' ]
@@ -28,6 +28,15 @@ end
28
28
  # Test Application initialization
29
29
  TestApplication.initialize!
30
30
 
31
+ # Make sure to test the right view files
32
+ ActionView::Template.unregister_template_handler :streamer, :jbuilder
33
+ case ENV["TSENCODER"]
34
+ when "turbostreamer"
35
+ ActionView::Template.register_template_handler :streamer, TurboStreamer::Handler
36
+ else
37
+ ActionView::Template.register_template_handler :jbuilder, JbuilderHandler
38
+ end
39
+
31
40
  # Test Application Models
32
41
  require 'standard_api/test_app/models'
33
42
 
@@ -44,6 +53,8 @@ Rails.application.routes.draw do
44
53
  end
45
54
 
46
55
  standard_resource :account
56
+ # standard_resources :photos, only: [ :index, :show ]
57
+
47
58
  end
48
59
 
49
60
  # Test Application Helpers
@@ -64,6 +64,15 @@ class ActiveSupport::TestCase
64
64
 
65
65
  # = Helper Methods
66
66
 
67
+ def debug
68
+ ActiveRecord::Base.logger = Logger.new(STDOUT)
69
+ $debugging = true
70
+ yield
71
+ ensure
72
+ ActiveRecord::Base.logger = nil
73
+ $debugging = false
74
+ end
75
+
67
76
  def controller_path
68
77
  if defined?(@controller)
69
78
  @controller.controller_path
@@ -76,6 +85,18 @@ class ActiveSupport::TestCase
76
85
  { :controller => controller_path, :action => action }.merge(options)
77
86
  end
78
87
 
88
+ def assert_sql(sql, &block)
89
+ queries = []
90
+ callback = -> (*, payload) do
91
+ queries << payload[:sql]
92
+ end
93
+
94
+ ActiveSupport::Notifications.subscribed(callback, "sql.active_record", &block)
95
+
96
+ assert_not_nil queries.map { |x| x.strip.gsub(/\s+/, ' ') }.
97
+ find { |x| x == sql.strip.gsub(/\s+/, ' ') }
98
+ end
99
+
79
100
  def assert_rendered(options = {}, message = nil)
80
101
  options = case options
81
102
  when NilClass, Regexp, String, Symbol
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: standardapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.0.0.32
4
+ version: 7.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Bracy
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-20 00:00:00.000000000 Z
11
+ date: 2024-04-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -16,70 +16,70 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 6.0.0
19
+ version: 7.1.3
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 6.0.0
26
+ version: 7.1.3
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activesupport
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 6.0.0
33
+ version: 7.1.3
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: 6.0.0
40
+ version: 7.1.3
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: actionpack
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: 6.0.0
47
+ version: 7.1.3
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: 6.0.0
54
+ version: 7.1.3
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: activerecord-sort
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: 6.0.0
61
+ version: 6.1.0
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: 6.0.0
68
+ version: 6.1.0
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: activerecord-filter
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ">="
74
74
  - !ruby/object:Gem::Version
75
- version: 6.0.0.2
75
+ version: 6.1.0
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
- version: 6.0.0.2
82
+ version: 6.1.0
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: pg
85
85
  requirement: !ruby/object:Gem::Requirement
@@ -108,6 +108,34 @@ dependencies:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: oj
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: turbostreamer
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: 1.11.0
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: 1.11.0
111
139
  - !ruby/object:Gem::Dependency
112
140
  name: jbuilder
113
141
  requirement: !ruby/object:Gem::Requirement
@@ -245,11 +273,13 @@ extra_rdoc_files:
245
273
  files:
246
274
  - README.md
247
275
  - lib/standard_api.rb
276
+ - lib/standard_api/access_control_list.rb
248
277
  - lib/standard_api/active_record/connection_adapters/postgresql/schema_statements.rb
249
278
  - lib/standard_api/controller.rb
250
279
  - lib/standard_api/errors.rb
251
280
  - lib/standard_api/helpers.rb
252
281
  - lib/standard_api/includes.rb
282
+ - lib/standard_api/middleware.rb
253
283
  - lib/standard_api/middleware/query_encoding.rb
254
284
  - lib/standard_api/orders.rb
255
285
  - lib/standard_api/railtie.rb
@@ -277,11 +307,22 @@ files:
277
307
  - lib/standard_api/views/application/show.json.jbuilder
278
308
  - lib/standard_api/views/application/show.streamer
279
309
  - test/standard_api/caching_test.rb
310
+ - test/standard_api/controller/include_test.rb
311
+ - test/standard_api/controller/subresource_test.rb
280
312
  - test/standard_api/helpers_test.rb
313
+ - test/standard_api/nested_attributes/belongs_to_test.rb
314
+ - test/standard_api/nested_attributes/has_and_belongs_to_many_test.rb
315
+ - test/standard_api/nested_attributes/has_many_test.rb
316
+ - test/standard_api/nested_attributes/has_one_test.rb
281
317
  - test/standard_api/performance.rb
282
318
  - test/standard_api/route_helpers_test.rb
283
319
  - test/standard_api/standard_api_test.rb
284
320
  - test/standard_api/test_app.rb
321
+ - test/standard_api/test_app/app/controllers/acl/account_acl.rb
322
+ - test/standard_api/test_app/app/controllers/acl/camera_acl.rb
323
+ - test/standard_api/test_app/app/controllers/acl/photo_acl.rb
324
+ - test/standard_api/test_app/app/controllers/acl/property_acl.rb
325
+ - test/standard_api/test_app/app/controllers/acl/reference_acl.rb
285
326
  - test/standard_api/test_app/config/database.yml
286
327
  - test/standard_api/test_app/controllers.rb
287
328
  - test/standard_api/test_app/models.rb
@@ -294,13 +335,15 @@ files:
294
335
  - test/standard_api/test_app/views/photos/schema.json.jbuilder
295
336
  - test/standard_api/test_app/views/photos/schema.streamer
296
337
  - test/standard_api/test_app/views/properties/edit.html.erb
338
+ - test/standard_api/test_app/views/sessions/create.json.jbuilder
339
+ - test/standard_api/test_app/views/sessions/create.streamer
297
340
  - test/standard_api/test_app/views/sessions/new.html.erb
298
341
  - test/standard_api/test_helper.rb
299
342
  homepage: https://github.com/waratuman/standardapi
300
343
  licenses:
301
344
  - MIT
302
345
  metadata: {}
303
- post_install_message:
346
+ post_install_message:
304
347
  rdoc_options:
305
348
  - "--main"
306
349
  - README.md
@@ -318,8 +361,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
318
361
  - !ruby/object:Gem::Version
319
362
  version: '0'
320
363
  requirements: []
321
- rubygems_version: 3.1.2
322
- signing_key:
364
+ rubygems_version: 3.5.9
365
+ signing_key:
323
366
  specification_version: 4
324
367
  summary: StandardAPI makes it easy to expose a query interface for your Rails models
325
368
  test_files: []