tramway-api 1.8.6.6 → 1.8.6.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c073884812f246c0b76d5367b19c2f1348dbe8539286b08027487e716e2dd959
4
- data.tar.gz: d49ede45c33a877a8deea8dcb9fb0285d1ef0d2154cd9e44109b0801e0280e4c
3
+ metadata.gz: df04e623451ee6c2e632f318925f3f0c90a3f75af5f50fdbf4dba7b44642d90e
4
+ data.tar.gz: c5c71de5a66e7821b648cb2a2c501e4df11c1f3e2745015fb8c1ed050c2a8e99
5
5
  SHA512:
6
- metadata.gz: a26de6225b11387505eac518f7d2ddec5ea0d3070007f6838e4d5192e9296793eed0f2c38af2a445286fffc0fc4f2bdb65fb449f32a6c64e4be13d2c51ce1270
7
- data.tar.gz: 33e67a11a31f45d4413d8f24a8f7a75075a38f74f3f99cb0456be838f59ccf38acf0f3cf4649cc27c6fb054e0f4349ff0b78fbf241a66204650685ab7f95d4b7
6
+ metadata.gz: 882cefd561e168aabca2c72eaff7eb5f5781f88659cc7c7f6ebc8431c90d2ff7fde947bae8f6977cf6fe392c5473a27543769eb09ebd1268cda6713422f236b3
7
+ data.tar.gz: 2906e2e3d2d85dd9712091a1651c0ab3aabb797e810443e2513a3a1d97b07b896237e2745b19256af1e308739b2876dfbc0249efd76cce5c540d10a20211b737
data/README.md CHANGED
@@ -41,7 +41,6 @@ coming soon...
41
41
  ```ruby
42
42
  gem 'active_model_serializers', '0.10.5'
43
43
  gem 'tramway-core'
44
- gem 'state_machine', github: 'seuros/state_machine'
45
44
  gem 'knock'
46
45
  gem 'audited'
47
46
  gem 'ransack'
@@ -146,7 +145,7 @@ end
146
145
 
147
146
  #### Configurate available models. Tramway will create end points according to this config
148
147
 
149
- ```
148
+ ```ruby
150
149
  ::Tramway::Api.set_available_models({
151
150
  User => [
152
151
  {
@@ -294,7 +293,6 @@ RSpec.describe 'Post generate token', type: :feature do
294
293
 
295
294
  end
296
295
  end
297
-
298
296
  ```
299
297
 
300
298
  Run `rspec` to test
@@ -340,11 +338,6 @@ this model must have field `password_digest`, because we use `bcrypt` gem for au
340
338
 
341
339
  Sets ActiveRecord models which will be used in API
342
340
 
343
- Argument is a hash. Keys are underscored models names, values are hashes with actions of available methods for every model.
344
- * `open` key means that this action will be used without authentication
345
- * `closed` key means that this action will be used with authentication
346
-
347
-
348
341
  Enabled methods:
349
342
 
350
343
  * create
@@ -360,7 +353,7 @@ Every model you've added in initializer will be able by URL `api/v1/records?mode
360
353
  Just update your initializer:
361
354
 
362
355
  ```ruby
363
- ::Tramway::Api.set_available_models user: { open: %i[create], closed: %i[update index] } # we've added index method
356
+ ::Tramway::Api.set_available_models({ User => { %i[index] })
364
357
  ```
365
358
 
366
359
  Create serializer
@@ -436,6 +429,46 @@ Params Structure
436
429
  }
437
430
  ```
438
431
 
432
+ Also, you can test it with this
433
+
434
+ *spec/factories/your_models.rb*
435
+
436
+ ```ruby
437
+ FactoryBot.define do
438
+ factory :your_model do
439
+ attribute1 { # some code which generate value for this attribute }
440
+ attribute2 { # some code which generate value for this attribute }
441
+ name { generate :name }
442
+ end
443
+ end
444
+ ```
445
+
446
+ *spec/api/your_model_spec.rb*
447
+
448
+ ```ruby
449
+ require 'rails_helper'
450
+
451
+ RSpec.describe 'Post generate token', type: :feature do
452
+ describe 'POST /api/v1/user_token' do
453
+ let(:user) { create :user, password: '123456789' }
454
+
455
+ it 'returns created status' do
456
+ post '/api/v1/user_token', params: { auth: { login: user.email, password: '123456789' } }
457
+
458
+ expect(response.status).to eq 201
459
+ end
460
+
461
+ it 'returns token' do
462
+ post '/api/v1/user_token', params: { auth: { login: user.email, password: '123456789' } }
463
+
464
+ expect(json_response[:auth_token].present?).to be_truthy
465
+ expect(json_response[:user]).to include_json({ email: user.email, uuid: user.uuid })
466
+ end
467
+
468
+ end
469
+ end
470
+ ```
471
+
439
472
  ### Update
440
473
 
441
474
  *config/initializers/tramway.rb*
@@ -58,9 +58,7 @@ module Tramway
58
58
 
59
59
  def current_user
60
60
  Tramway::Api.user_based_models.map do |user_based_model|
61
- unless user_based_model == User
62
- send("current_#{user_based_model.name.underscore}")
63
- end
61
+ send("current_#{user_based_model.name.underscore}") unless user_based_model == User
64
62
  end.compact.first
65
63
  end
66
64
  end
@@ -4,6 +4,8 @@ module Tramway
4
4
  module Api
5
5
  module V1
6
6
  class ApplicationController < ::Tramway::Api::ApplicationController
7
+ before_action :application
8
+
7
9
  def render_errors_for(model)
8
10
  render json: model, status: :unprocessable_entity, serializer: ::Tramway::Api::V1::ErrorSerializer
9
11
  end
@@ -21,6 +23,40 @@ module Tramway
21
23
  hash
22
24
  end
23
25
 
26
+ # Need to be removed
27
+
28
+ before_action :load_application
29
+
30
+ def load_application
31
+ if engine_loaded(request).present?
32
+ build_application_with_engine engine_loaded request
33
+ elsif application_class(request).present?
34
+ @application = application_class(request).camelize.constantize.first
35
+ else
36
+ @application = application_object request
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def build_application_with_engine(engine_loaded)
43
+ engine_module = "::Tramway::#{engine_loaded.camelize}".constantize
44
+ @application = "#{engine_module}::#{engine_module.application.to_s.camelize}".constantize.first
45
+ @application_engine = engine_loaded
46
+ end
47
+
48
+ def application_class(request)
49
+ Constraints::DomainConstraint.new(request.domain).application_class
50
+ end
51
+
52
+ def engine_loaded(request)
53
+ Constraints::DomainConstraint.new(request.domain).engine_loaded
54
+ end
55
+
56
+ def application_object(request)
57
+ Constraints::DomainConstraint.new(request.domain).application_object
58
+ end
59
+
24
60
  private
25
61
 
26
62
  def record
@@ -45,6 +81,7 @@ module Tramway
45
81
  def check_available_model_class
46
82
  unless model_class
47
83
  head(:unauthorized) && return unless current_user
84
+
48
85
  head(:unprocessable_entity) && return
49
86
  end
50
87
  end
@@ -69,7 +106,7 @@ module Tramway
69
106
  action_is_available = checking_roles.map do |role|
70
107
  Tramway::Api.action_is_available(
71
108
  action: action_name.to_sym,
72
- project: (@application_engine || @application.name),
109
+ project: (@application_engine || application_name),
73
110
  role: role,
74
111
  model_name: params[:model],
75
112
  current_user: current_user
@@ -90,7 +127,7 @@ module Tramway
90
127
  def authenticate_user_if_needed
91
128
  action_is_open = Tramway::Api.action_is_available(
92
129
  action: action_name.to_sym,
93
- project: (@application_engine || @application.name),
130
+ project: (@application_engine || application_name),
94
131
  model_name: params[:model]
95
132
  )
96
133
  head(:unauthorized) && return if !current_user && !action_is_open
@@ -108,14 +145,21 @@ module Tramway
108
145
 
109
146
  protected
110
147
 
111
- def model_class
148
+ def application_name
149
+ @application ||= Tramway::Core.application&.model_class&.first || Tramway::Core.application
112
150
  begin
113
- params[:model].constantize
114
- rescue ActiveSupport::Concern::MultipleIncludedBlocks => e
115
- raise "#{e}. Maybe #{params[:model]} model doesn't exists or there is naming conflicts with it"
151
+ @application.name
152
+ rescue
153
+ raise("Tramway::Api @application not initialized, Tramway::Core.application: #{::Tramway::Core.application}, model_class: #{Tramway::Core.application&.model_class }")
116
154
  end
117
155
  end
118
156
 
157
+ def model_class
158
+ params[:model].constantize
159
+ rescue ActiveSupport::Concern::MultipleIncludedBlocks => e
160
+ raise "#{e}. Maybe #{params[:model]} model doesn't exists or there is naming conflicts with it"
161
+ end
162
+
119
163
  def decorator_class(model_name = nil)
120
164
  "#{model_name || model_class}Decorator".constantize
121
165
  end
@@ -5,6 +5,7 @@ module Tramway::Api::V1
5
5
  before_action :check_available_model_class
6
6
  before_action :check_available_model_action_for_record, only: %i[show update destroy]
7
7
  before_action :authenticate_user_if_needed
8
+ before_action :application
8
9
 
9
10
  def index
10
11
  collection = available_action_for_collection
@@ -54,5 +55,11 @@ module Tramway::Api::V1
54
55
  include: '*',
55
56
  status: :no_content
56
57
  end
58
+
59
+ def application
60
+ if ::Tramway::Core.application
61
+ @application = Tramway::Core.application&.model_class&.first || Tramway::Core.application
62
+ end
63
+ end
57
64
  end
58
65
  end
data/lib/tramway/api.rb CHANGED
@@ -96,7 +96,8 @@ module Tramway
96
96
  end
97
97
 
98
98
  def default_id_method_of(model:)
99
- @@id_methods[model.to_s][:default]
99
+ @@id_methods ||= {}
100
+ @@id_methods.dig(model.to_s, :default)
100
101
  end
101
102
  end
102
103
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Tramway
4
4
  module Api
5
- VERSION = '1.8.6.6'
5
+ VERSION = '1.8.6.11'
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.6.6
4
+ version: 1.8.6.11
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-08-10 00:00:00.000000000 Z
11
+ date: 2021-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_model_serializers
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: 1.9.2
55
+ - !ruby/object:Gem::Dependency
56
+ name: uuid
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  description: Engine for api
56
70
  email:
57
71
  - kalashnikovisme@gmail.com
@@ -102,7 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
102
116
  - !ruby/object:Gem::Version
103
117
  version: '0'
104
118
  requirements: []
105
- rubygems_version: 3.1.2
119
+ rubygems_version: 3.1.4
106
120
  signing_key:
107
121
  specification_version: 4
108
122
  summary: Engine for api