tramway-api 1.8 → 1.8.1

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: fdcaae068ee8dbb36b6cfd6a85c414d5356c4e25d5d9404d8340f1093e18a786
4
- data.tar.gz: 893e1421c8296743740e969d7740a536b98cca1c3f308b3b84c2959c717996c7
3
+ metadata.gz: 3065ba5d3d8a135210084aae9fdeea5291bfbc25c195bffa97e544491b7c9776
4
+ data.tar.gz: 9e604ff996584c0982dc2abd7ce7da324060d89f53e55383cf2dc24caa47ed03
5
5
  SHA512:
6
- metadata.gz: 68691b7551fb533604c7aafb6cb87d0b6770940c6da3d053800b55c6aeb0d191d8c377fe0a00581fb5dcd20206fa0483a1b47205403c9f781da88a72a1161bb7
7
- data.tar.gz: b7988845eb7752b6e1f82d3956d52f2d13f419a221ee4aa7f1a4e0bea984417d61b84446b32d4b12cae68ffdf27b28ba0db16d4ff28901b6cc00171ce27d386a
6
+ metadata.gz: cd85298817523d5f697e9c4074f45bc258f004f8d07865ea2297248d62ba42327f20a2fbd9dde83d2d1d714769e8df0cfd6207da65309085ad804a3f4861de14
7
+ data.tar.gz: f101fd0601e24a0b96488daadad96d5dd8d8db666aa52d839e403444b4478f9d33325e8ef926b06a7a83277036b12478716ca2fff5b924706a50ac824252e2b9
@@ -3,13 +3,13 @@
3
3
  module Tramway::Api::V1
4
4
  class RecordsController < ::Tramway::Api::V1::ApplicationController
5
5
  before_action :check_available_model_class
6
- before_action :check_available_model_action
6
+ before_action :check_available_model_action_for_record, only: [ :show, :update, :destroy ]
7
7
  before_action :authenticate_user_if_needed
8
8
 
9
9
  def index
10
- records = model_class.active.order(id: :desc).send params[:scope] || :all
11
- records = records.full_text_search params[:search] if params[:search]
12
- render json: records,
10
+ collection = available_action_for_collection
11
+
12
+ render json: collection,
13
13
  each_serializer: serializer_class,
14
14
  include: '*',
15
15
  status: :ok
@@ -63,6 +63,12 @@ module Tramway::Api::V1
63
63
  end
64
64
  end
65
65
 
66
+ def records
67
+ collection = model_class.active.order(id: :desc).send params[:scope] || :all
68
+ collection = collection.full_text_search params[:search] if params[:search]
69
+ collection
70
+ end
71
+
66
72
  def check_available_model_class
67
73
  unless model_class
68
74
  head(:unauthorized) && return unless current_user
@@ -70,23 +76,43 @@ module Tramway::Api::V1
70
76
  end
71
77
  end
72
78
 
73
- def check_available_model_action
79
+ def check_available_model_action_for_record
80
+ action_is_available = check_action
81
+ action_is_available.tap do
82
+ head(:unprocessable_entity) && return if action_is_available.is_a?(Proc) && !action_is_available.call(record, current_user)
83
+ end
84
+ end
85
+
86
+ def available_action_for_collection
87
+ action_is_available = check_action
88
+ return records if action_is_available == true
89
+ action_is_available.call records, current_user if action_is_available.is_a?(Proc)
90
+ end
91
+
92
+ def check_action
74
93
  action_is_available = checking_roles.map do |role|
75
- Tramway::Api.action_is_available?(
76
- record: record,
94
+ Tramway::Api.action_is_available(
77
95
  action: action_name.to_sym,
78
96
  project: (@application_engine || @application.name),
79
97
  role: role,
80
98
  model_name: params[:model],
81
99
  current_user: current_user
82
100
  )
83
- end.include? true
101
+ end.compact.uniq - [false]
102
+
103
+ if action_is_available.count > 1
104
+ Tramway::Error.raise_error(:tramway, :api, :api, :v1, :records_controller, :available_action_for_collection, :duplicate_actions)
105
+ end
106
+
107
+ action_is_available = action_is_available.first
84
108
 
85
- head(:unprocessable_entity) && return unless action_is_available
109
+ action_is_available.tap do
110
+ head(:unprocessable_entity) && return unless action_is_available
111
+ end
86
112
  end
87
113
 
88
114
  def authenticate_user_if_needed
89
- action_is_open = Tramway::Api.action_is_available?(
115
+ action_is_open = Tramway::Api.action_is_available(
90
116
  action: action_name.to_sym,
91
117
  project: (@application_engine || @application.name),
92
118
  model_name: params[:model]
@@ -54,7 +54,7 @@ module Tramway
54
54
  end&.flatten || []
55
55
  end
56
56
 
57
- def action_is_available?(record: nil, project:, role: :open, model_name:, action:, current_user: nil)
57
+ def action_is_available(project:, role: :open, model_name:, action:, current_user: nil)
58
58
  actions = select_actions(project: project, role: role, model_name: model_name)
59
59
  availability = actions&.select do |a|
60
60
  if a.is_a? Symbol
@@ -67,7 +67,7 @@ module Tramway
67
67
  return false unless availability.present?
68
68
  return true if availability.is_a? Symbol
69
69
 
70
- availability.values.first.call record, current_user
70
+ availability.values.first
71
71
  end
72
72
 
73
73
  def select_actions(project:, role:, model_name:)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Tramway
4
4
  module Api
5
- VERSION = '1.8'
5
+ VERSION = '1.8.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'
4
+ version: 1.8.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-03-17 00:00:00.000000000 Z
11
+ date: 2020-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_model_serializers