tramway-api 1.8.2.3 → 1.8.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +66 -6
- data/app/controllers/tramway/api/application_controller.rb +3 -1
- data/app/controllers/tramway/api/v1/application_controller.rb +6 -7
- data/app/serializers/tramway/api/v1/application_serializer.rb +2 -1
- data/lib/tramway/api.rb +11 -0
- data/lib/tramway/api/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 151b3c808fba04666fdb2cccbd5368d45c86c6539878c52e1c9fa93905a3eb85
|
4
|
+
data.tar.gz: 8eeaf9cde77e40c5f170c3c1d242c88025003d10176947e943ae59dd10520b25
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 989b0c66ec542b84a55f803f676b0778b78ccd012ced6579ae858c0aaa31f0d345f9ff4ea737a64e393735a37ffa6285fe153ceb862b0b1dc692a4f1f2999ee3
|
7
|
+
data.tar.gz: ac301bb8e6fc9a7dc5e8ae7a2100703977cc7d9d58a4f2c384bdb02130ed30b02b214b1c682874b4b32fdce38f33017faadfa89352b8cb4081fad439305c3d23
|
data/README.md
CHANGED
@@ -44,6 +44,7 @@ coming soon...
|
|
44
44
|
gem 'state_machine', github: 'seuros/state_machine'
|
45
45
|
gem 'knock'
|
46
46
|
gem 'audited'
|
47
|
+
gem 'ransack'
|
47
48
|
```
|
48
49
|
|
49
50
|
## Usage
|
@@ -64,10 +65,24 @@ gem 'knock'
|
|
64
65
|
|
65
66
|
Run `bundle install`
|
66
67
|
|
68
|
+
### Initialize @application object
|
69
|
+
|
70
|
+
[How-to](https://github.com/Purple-Magic/tramway-core/blob/develop/README.md#every-tramway-application-need-initialized-application-object-or-if-you-create-tramway-plugin-it-should-be-application_engine-object)
|
71
|
+
|
72
|
+
*config/routes.rb*
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
Rails.application.routes.draw do
|
76
|
+
# ...
|
77
|
+
mount Tramway::Api::Engine, at: '/api'
|
78
|
+
# ...
|
79
|
+
end
|
80
|
+
```
|
81
|
+
|
67
82
|
Then generate User (you use another name, it's just an example) model
|
68
83
|
|
69
84
|
```
|
70
|
-
rails g model user email:text password_digest:text username:text state:text
|
85
|
+
rails g model user email:text password_digest:text username:text state:text uuid:uuid
|
71
86
|
```
|
72
87
|
|
73
88
|
Enable extension in your database:
|
@@ -100,11 +115,26 @@ class User < Tramway::Core::ApplicationRecord
|
|
100
115
|
end
|
101
116
|
```
|
102
117
|
|
103
|
-
Create file `config/initializers/tramway.rb`
|
118
|
+
#### Create file `config/initializers/tramway.rb`
|
119
|
+
#### If you need JWT authentication add this line to the `config/initializers/tramway.rb`
|
104
120
|
|
105
121
|
```ruby
|
106
122
|
::Tramway::Api.auth_config = { user_model: User, auth_attributes: %i[email username] }
|
107
|
-
|
123
|
+
```
|
124
|
+
|
125
|
+
#### Configurate available models. Tramway will create end points according to this config
|
126
|
+
|
127
|
+
```
|
128
|
+
::Tramway::Api.set_available_models({
|
129
|
+
User => [
|
130
|
+
{
|
131
|
+
show: lambda do |record, current_user|
|
132
|
+
record.id == current_user.id # shows only current_user profile
|
133
|
+
end
|
134
|
+
}
|
135
|
+
],
|
136
|
+
project: :your_project_name
|
137
|
+
})
|
108
138
|
```
|
109
139
|
|
110
140
|
Run `rails g tramway:core:install`
|
@@ -316,7 +346,7 @@ Create serializer
|
|
316
346
|
*app/serializers/user_serializer.rb*
|
317
347
|
|
318
348
|
```ruby
|
319
|
-
class UserSerializer < Tramway::
|
349
|
+
class UserSerializer < Tramway::Api::V1::ApplicationSerializer
|
320
350
|
attributes :username, :email
|
321
351
|
end
|
322
352
|
```
|
@@ -353,9 +383,39 @@ Docs coming soon
|
|
353
383
|
|
354
384
|
### Show
|
355
385
|
|
356
|
-
|
386
|
+
#### Description
|
357
387
|
|
358
|
-
|
388
|
+
It returns just one record, if it is not deleted.
|
389
|
+
|
390
|
+
#### Using
|
391
|
+
|
392
|
+
##### Allow method show in tramway initializer for `YourModel`
|
393
|
+
|
394
|
+
*config/initializers/tramway.rb*
|
395
|
+
|
396
|
+
```ruby
|
397
|
+
::Tramway::Api.set_available_models({ YourModel => [ :show ] }, project: :your_project_name })
|
398
|
+
```
|
399
|
+
|
400
|
+
##### Create serializer
|
401
|
+
|
402
|
+
*app/serializers/user_serializer.rb*
|
403
|
+
|
404
|
+
```ruby
|
405
|
+
class UserSerializer < Tramway::Core::ApplicationSerializer
|
406
|
+
attributes :username, :email
|
407
|
+
end
|
408
|
+
```
|
409
|
+
|
410
|
+
##### Run your server on the localhost `rails s`
|
411
|
+
##### Made this query to test new API method (for example: you can create file `bin/test_tramway.rb` with this lines):
|
412
|
+
|
413
|
+
```ruby
|
414
|
+
require 'net/http'
|
415
|
+
|
416
|
+
YourModel.create! attribute_1: 'some value', attribute_2: 'some_value'
|
417
|
+
Net::HTTP.get('localhost:3000', "/api/v1/records/#{YourModel.last.id}?model=YourModel")
|
418
|
+
```
|
359
419
|
|
360
420
|
### Destroy
|
361
421
|
|
@@ -58,7 +58,9 @@ module Tramway
|
|
58
58
|
|
59
59
|
def current_user
|
60
60
|
Tramway::Api.user_based_models.map do |user_based_model|
|
61
|
-
|
61
|
+
unless user_based_model == User
|
62
|
+
send("current_#{user_based_model.name.underscore}")
|
63
|
+
end
|
62
64
|
end.compact.first
|
63
65
|
end
|
64
66
|
end
|
@@ -24,7 +24,8 @@ module Tramway
|
|
24
24
|
private
|
25
25
|
|
26
26
|
def record
|
27
|
-
|
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?
|
28
29
|
end
|
29
30
|
|
30
31
|
def records
|
@@ -100,12 +101,10 @@ module Tramway
|
|
100
101
|
protected
|
101
102
|
|
102
103
|
def model_class
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
raise "#{e}. Maybe #{params[:model]} model doesn't exists or there is naming conflicts with it"
|
108
|
-
end
|
104
|
+
begin
|
105
|
+
params[:model].constantize
|
106
|
+
rescue ActiveSupport::Concern::MultipleIncludedBlocks => e
|
107
|
+
raise "#{e}. Maybe #{params[:model]} model doesn't exists or there is naming conflicts with it"
|
109
108
|
end
|
110
109
|
end
|
111
110
|
|
data/lib/tramway/api.rb
CHANGED
@@ -83,6 +83,17 @@ module Tramway
|
|
83
83
|
new_hash.merge! pair[0].to_s => pair[1]
|
84
84
|
end
|
85
85
|
end
|
86
|
+
|
87
|
+
def id_methods_of(options = {})
|
88
|
+
@@id_methods ||= {}
|
89
|
+
@@id_methods.merge!(options.reduce({}) do |hash, pair|
|
90
|
+
hash.merge! pair[0].to_s => pair[1]
|
91
|
+
end)
|
92
|
+
end
|
93
|
+
|
94
|
+
def id_method_of(model:)
|
95
|
+
@@id_methods[model.to_s]
|
96
|
+
end
|
86
97
|
end
|
87
98
|
end
|
88
99
|
end
|
data/lib/tramway/api/version.rb
CHANGED
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.
|
4
|
+
version: 1.8.3
|
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-
|
11
|
+
date: 2020-06-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: active_model_serializers
|