tramway-api 1.8.3 → 1.8.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 151b3c808fba04666fdb2cccbd5368d45c86c6539878c52e1c9fa93905a3eb85
4
- data.tar.gz: 8eeaf9cde77e40c5f170c3c1d242c88025003d10176947e943ae59dd10520b25
3
+ metadata.gz: '099a8be3907e271356fd1862d50e6b45f91a611baf481dc719a2fbf74d0f2277'
4
+ data.tar.gz: 3b1a738428b4f0c147b4a4b3911fd80f84e9c3cfce6deed23eb7b66e2999947d
5
5
  SHA512:
6
- metadata.gz: 989b0c66ec542b84a55f803f676b0778b78ccd012ced6579ae858c0aaa31f0d345f9ff4ea737a64e393735a37ffa6285fe153ceb862b0b1dc692a4f1f2999ee3
7
- data.tar.gz: ac301bb8e6fc9a7dc5e8ae7a2100703977cc7d9d58a4f2c384bdb02130ed30b02b214b1c682874b4b32fdce38f33017faadfa89352b8cb4081fad439305c3d23
6
+ metadata.gz: 2c4467c3a491b5a6154e4d67e302d412444f0dfc96087a8778e303350635b8928ac3820e137b5cde37adcd886b561706e27af3d5c543240901cbccc8ab7b148c
7
+ data.tar.gz: 583afa7ea8e70fbda137a4bd701cfb37369fdc4944114347dd889911ffb6f1da3c3377ecf3497931d41cd2f9fbc52fd1596ca95c097acdf46eda6ba3bf2d740c
data/README.md CHANGED
@@ -88,14 +88,28 @@ rails g model user email:text password_digest:text username:text state:text uuid
88
88
  Enable extension in your database:
89
89
 
90
90
  *db/migrate/enable_extension.rb*
91
- ```
92
- def change
93
- enable_extension 'uuid-ossp'
91
+ ```ruby
92
+ class EnableExtensionUUIDOSSP < ActiveRecord::Migration
93
+ def change
94
+ enable_extension 'uuid-ossp'
95
+ end
94
96
  end
95
97
  ```
96
98
 
99
+ ### You can choose a method, which will be using as public ID method
100
+
101
+ By default, it's a `uuid` method
102
+
103
+ To choose your own public ID method, just add this line to:
104
+
105
+ *config/initializers/tramway.rb*
106
+
107
+ ```ruby
108
+ Tramway::Api.id_methods_of(User => :id)
109
+ ```
110
+ If you want to use `uuid` by default, please, add it to your models
97
111
 
98
- Add generating uuid by default to every model, that is accessible by API
112
+ #### Add generating uuid by default to every model, that is accessible by API
99
113
 
100
114
  *db/migrate/add_uuid_to_some_model.rb*
101
115
 
@@ -371,15 +385,89 @@ You have your records in JSON API spec.
371
385
 
372
386
  ### Create
373
387
 
374
- Production ready
388
+ *config/initializers/tramway.rb*
375
389
 
376
- Docs coming soon
390
+ ```ruby
391
+ ::Tramway::Api.set_available_models({ YourModel => [ :create ] }, project: :your_project_name })
392
+ ```
393
+
394
+ *app/forms/your_model_form.rb*
395
+
396
+ ```ruby
397
+ class YourModelForm < Tramway::Core::ApplicationForm
398
+ properties :attribute1, :attribute2, :name
399
+
400
+ association :another_association_model
401
+
402
+ def name=(value)
403
+ model.first_name = value.split(' ')[0]
404
+ model.first_name = value.split(' ')[1]
405
+ end
406
+ end
407
+ ```
408
+
409
+ Now you can your request. It has such structure
410
+
411
+ **POST** `/api/v1/records?model=YourModel`
412
+
413
+ ```
414
+ Params Structure
415
+ {
416
+ data: {
417
+ attributes: {
418
+ attribute1: 'some value',
419
+ attribute2: 'some value',
420
+ name: 'some full name',
421
+ another_association_model: {
422
+ # here a list of attributes, which you described in AnotherAssociationModelForm
423
+ }
424
+ }
425
+ }
426
+ }
427
+ ```
377
428
 
378
429
  ### Update
379
430
 
380
- Production ready
431
+ *config/initializers/tramway.rb*
381
432
 
382
- Docs coming soon
433
+ ```ruby
434
+ ::Tramway::Api.set_available_models({ YourModel => [ :update ] }, project: :your_project_name })
435
+ ```
436
+
437
+ *app/forms/your_model_form.rb*
438
+
439
+ ```ruby
440
+ class YourModelForm < Tramway::Core::ApplicationForm
441
+ properties :attribute1, :attribute2, :name
442
+
443
+ association :another_association_model
444
+
445
+ def name=(value)
446
+ model.first_name = value.split(' ')[0]
447
+ model.first_name = value.split(' ')[1]
448
+ end
449
+ end
450
+ ```
451
+
452
+ Now you can your request. It has such structure
453
+
454
+ **PATCH** `/api/v1/records/#{object_id}?model=YourModel`
455
+
456
+ ```
457
+ Params Structure
458
+ {
459
+ data: {
460
+ attributes: {
461
+ attribute1: 'some value',
462
+ attribute2: 'some value',
463
+ name: 'some full name',
464
+ another_association_model: {
465
+ # here a list of attributes, which you described in AnotherAssociationModelForm
466
+ }
467
+ }
468
+ }
469
+ }
470
+ ```
383
471
 
384
472
  ### Show
385
473
 
@@ -15,7 +15,7 @@ module Tramway
15
15
  def snake_case(params)
16
16
  hash = {}
17
17
  params.each do |attribute, value|
18
- key = UUID.validate(attribute) ? attribute : attribute.to_s.gsub('-', '_')
18
+ key = ::UUID.validate(attribute) ? attribute : attribute.to_s.gsub('-', '_')
19
19
  hash.merge! key => value
20
20
  end
21
21
  hash
@@ -24,13 +24,21 @@ module Tramway
24
24
  private
25
25
 
26
26
  def record
27
- id_method = Tramway::Api.id_method_of(model: model_class) || :uuid
28
- @record = model_class.find_by! id_method => params[:id] if params[:id].present?
27
+ if params[:key].present?
28
+ if ids_methods_of(model: model_class).include? params[:key]
29
+ @record = model_class.find_by! params[:key] => params[:id] if params[:id].present?
30
+ end
31
+ else
32
+ default_id_method = Tramway::Api.default_id_method_of(model: model_class) || :uuid
33
+ @record = model_class.find_by! default_id_method => params[:id] if params[:id].present?
34
+ end
29
35
  end
30
36
 
31
37
  def records
32
- collection = model_class.active.order(id: :desc).send params[:scope] || :all
33
- collection = collection.full_text_search params[:search] if params[:search]
38
+ active_records = model_class.respond_to?(:active) ? model_class.active : model_class.all
39
+ collection = active_records.order(id: :desc).send params[:scope] || :all
40
+ collection = collection.page(params[:page]).per(params[:per]) if params[:page].present?
41
+ collection = collection.full_text_search params[:search] if params[:search].present?
34
42
  collection
35
43
  end
36
44
 
@@ -6,7 +6,7 @@ class Tramway::Api::V1::ApplicationSerializer < ActiveModel::Serializer
6
6
  attribute :id
7
7
 
8
8
  def id
9
- id_method = Tramway::Api.id_method_of(model: object.class) || :uuid
9
+ id_method = Tramway::Api.default_id_method_of(model: object.class) || :uuid
10
10
  object.send(id_method)
11
11
  end
12
12
 
@@ -91,8 +91,12 @@ module Tramway
91
91
  end)
92
92
  end
93
93
 
94
- def id_method_of(model:)
95
- @@id_methods[model.to_s]
94
+ def other_id_methods_of(model:)
95
+ @@id_methods[model.to_s][:other]
96
+ end
97
+
98
+ def default_id_method_of(model:)
99
+ @@id_methods[model.to_s][:default]
96
100
  end
97
101
  end
98
102
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Tramway
4
4
  module Api
5
- VERSION = '1.8.3'
5
+ VERSION = '1.8.6.1'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tramway-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.3
4
+ version: 1.8.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Kalashnikov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-30 00:00:00.000000000 Z
11
+ date: 2020-07-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_model_serializers