bitia-rails 0.4.0 → 0.5.0

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: 24c398854bca6c786ff70c898d741a3bde2bc2d2ba95849fc526fb1dd5a5288c
4
- data.tar.gz: 4a5ec03ff2efb4ac6881ea802c8c2e7ce32b545ad5a1936e09319ffefb9bf337
3
+ metadata.gz: 0202b94e37dfe57da1c1947fe34b5caf3c87e1ae795a543467b67b6e333b87a3
4
+ data.tar.gz: 601f294f1a4f04d77dfbf6b5a7bad48f83138506099571671d75a92ff48dcb98
5
5
  SHA512:
6
- metadata.gz: b38c419c7e5362fad376fe1c6a70a368b2af21ce77243b73014556100c8fd20b88d253dd6830754683582f5a2510f0d417b95d095fed374c25ce6b476fff5a10
7
- data.tar.gz: ad1ca766fa2fceefa74e368b1e5e33174ab86070a87d626318eb2fd4d2f03b1be356412de7c5cff1c3d1b0dbe7f95a5c520b04512fb2d5622f822285c3bda5fd
6
+ metadata.gz: 9264e3db125094bc32e43e96c2fd2134b2d74796fdf92c1e4651cf8c5d0f324412671a096b74d74891b71d3ab8bb2885f6eb3e0ddbea1fed7340102a8997c914
7
+ data.tar.gz: bedb44cd7f8bb4514f673b1a6df0cc5bdc56bdd0062cb0a85282a9011d6e1e8c08e0705bcb3257d7ec4f1e91b14d8b957c2c79171794b051879118a4fa29282d
data/Gemfile CHANGED
@@ -1,6 +1,9 @@
1
1
  source 'https://rubygems.org'
2
2
  git_source(:github) { |repo| "https://github.com/#{repo}.git" }
3
3
 
4
+ # For same_site: :none :
5
+ gem 'rack', '~> 2.2.2'
6
+
4
7
  gemspec
5
8
 
6
9
  gem 'responders'
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bitia-rails (0.3.0)
4
+ bitia-rails (0.4.0)
5
5
  rails (~> 5.2.4, >= 5.2.4.3)
6
6
  responders
7
7
 
@@ -177,6 +177,7 @@ DEPENDENCIES
177
177
  factory_bot_rails
178
178
  jbuilder
179
179
  pry
180
+ rack (~> 2.2.2)
180
181
  responders
181
182
  rspec-rails
182
183
  rubocop
@@ -1,164 +1,154 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class Bitia::ApiController < ActionController::Base
4
- include ActionController::MimeResponds
3
+ module Bitia
4
+ class ApiController < ActionController::Base
5
+ include ActionController::MimeResponds
5
6
 
6
- respond_to :json
7
+ respond_to :json
7
8
 
8
- include Resourceable
9
+ include Resourceable
9
10
 
10
- before_action :purable_initialize_metavars
11
- before_action :purable_prepare_resources
11
+ before_action :purable_initialize_metavars
12
+ before_action :purable_prepare_resources
12
13
 
13
- def index
14
- all = resources.count
15
- if params.include? :offset
16
- instance_variable_set("@#{resources_name}", resources.offset(params[:offset]))
17
- end
18
- if params.include? :limit
19
- instance_variable_set("@#{resources_name}", resources.limit(Integer(params[:limit])))
20
- end
21
- @metadata[:all] = all unless @metadata[:all]
22
-
23
- render
24
- end
14
+ def index
15
+ all = resources.count
16
+ if params.include? :offset
17
+ instance_variable_set("@#{resources_name}", resources.offset(params[:offset]))
18
+ end
19
+ if params.include? :limit
20
+ instance_variable_set("@#{resources_name}", resources.limit(Integer(params[:limit])))
21
+ end
22
+ @metadata[:all] = all unless @metadata[:all]
25
23
 
26
- def new
27
- authorize resource
28
- render
29
- end
24
+ render
25
+ end
30
26
 
31
- def show
32
- authorize resource
33
- render
34
- end
27
+ def new
28
+ authorize resource
29
+ render
30
+ end
35
31
 
36
- def create
37
- pure_create_or_update
38
- render
39
- end
32
+ def show
33
+ authorize resource
34
+ render
35
+ end
40
36
 
41
- def update
42
- pure_create_or_update
43
- render
44
- end
37
+ def create
38
+ pure_create_or_update
39
+ render
40
+ end
45
41
 
46
- def destroy
47
- authorize resource
48
- resource.destroy!
42
+ def update
43
+ pure_create_or_update
44
+ render
45
+ end
49
46
 
50
- render
51
- end
47
+ def destroy
48
+ authorize resource
49
+ resource.destroy!
52
50
 
53
- def pure_filter(param)
54
- return unless params[param].present?
51
+ render
52
+ end
55
53
 
56
- instance_variable_get("@#{resources_name}").where!(param => params[param])
57
- end
54
+ def pure_filter(param)
55
+ return unless params[param].present?
58
56
 
59
- helper_method :pure_filter
57
+ instance_variable_get("@#{resources_name}").where!(param => params[param])
58
+ end
60
59
 
61
- def purable_model
62
- self.class.send(:purable_model)
63
- end
60
+ helper_method :pure_filter
64
61
 
65
- helper_method :purable_model
62
+ private
66
63
 
67
- def purable_model_chain
68
- self.class.send(:purable_model_chain)
69
- end
64
+ def purable_initialize_metavars
65
+ @resource = purable_model.name.underscore.to_sym
66
+ @metadata ||= {}
67
+ nil
68
+ end
70
69
 
71
- helper_method :purable_model
70
+ def purable_prepare_resources
71
+ @current_user = User.find(params[:user_id]) if params.include? :user_id
72
+
73
+ if params[:action] == 'index'
74
+ instance_variable_set("@#{resources_name}", purable_relation.all)
75
+ elsif params[:action] == 'show'
76
+ id = params[:id].present? ? params[:id] : purable_param_id
77
+
78
+ instance_variable_set(
79
+ "@#{resource_name}", id ? purable_relation.find(id) : purable_relation.new
80
+ )
81
+ elsif purable_resource_params.is_a?(Array)
82
+ resources = purable_resource_params.map do |params|
83
+ if params[:id].present? && params[:id] != 'self'
84
+ purable_relation.find(params[:id]).tap { |r| r.assign_attributes(params) }
85
+ else
86
+ purable_relation.new(params)
87
+ end
88
+ end
72
89
 
73
- private
90
+ instance_variable_set("@#{resources_name}", resources)
91
+ else
92
+ resource = purable_relation.new(purable_resource_params)
93
+ instance_variable_set("@#{resource_name}", resource)
94
+ end
95
+ end
74
96
 
75
- def purable_initialize_metavars
76
- @resource = purable_model.name.underscore.to_sym
77
- @metadata ||= {}
78
- nil
79
- end
97
+ def purable_param_id
98
+ purable_resource_params[:id]
99
+ rescue StandardError
100
+ nil
101
+ end
80
102
 
81
- def purable_prepare_resources
82
- @current_user = User.find(params[:user_id]) if params.include? :user_id
83
-
84
- if params[:action] == 'index'
85
- instance_variable_set("@#{resources_name}", purable_relation.all)
86
- elsif params[:action] == 'show'
87
- id = params[:id].present? ? params[:id] : purable_param_id
88
-
89
- instance_variable_set(
90
- "@#{resource_name}", id ? purable_relation.find(id) : purable_relation.new
91
- )
92
- elsif purable_resource_params.is_a?(Array)
93
- resources = purable_resource_params.map do |params|
94
- if params[:id].present? && params[:id] != 'self'
95
- purable_relation.find(params[:id]).tap { |r| r.assign_attributes(params) }
96
- else
97
- purable_relation.new(params)
103
+ def purable_relation
104
+ purable_model_chain.inject(nil) do |m, pm|
105
+ if m.nil?
106
+ pm.singularize.classify.constantize
107
+ elsif params.include?("#{m.to_s.underscore}_id")
108
+ m.find(params["#{m.to_s.underscore}_id"]).send(pm.to_sym) # TODO: uncovered
98
109
  end
99
110
  end
100
-
101
- instance_variable_set("@#{resources_name}", resources)
102
- else
103
- resource = purable_relation.new(purable_resource_params)
104
- instance_variable_set("@#{resource_name}", resource)
105
111
  end
106
- end
107
-
108
- def purable_param_id
109
- purable_resource_params[:id]
110
- rescue StandardError
111
- nil
112
- end
113
112
 
114
- def purable_relation
115
- purable_model_chain.inject(nil) do |m, pm|
116
- if m.nil?
117
- pm.singularize.classify.constantize
118
- elsif params.include?("#{m.to_s.underscore}_id")
119
- m.find(params["#{m.to_s.underscore}_id"]).send(pm.to_sym) # TODO: uncovered
113
+ def pure_create_or_update
114
+ if resources.present?
115
+ purable_model.transaction { resources.each(&:save!) }
116
+ else
117
+ resource.save!
120
118
  end
121
119
  end
122
- end
123
120
 
124
- def pure_create_or_update
125
- if resources.present?
126
- purable_model.transaction { resources.each(&:save!) }
127
- else
128
- resource.save!
121
+ def purable_resource_params
122
+ send :"#{resource_name}_params"
129
123
  end
130
- end
131
124
 
132
- def purable_resource_params
133
- send :"#{resource_name}_params"
134
- end
125
+ def purable_model
126
+ purable_model_chain.last.singularize.classify.constantize
127
+ end
135
128
 
136
- def self.purable_model_chain
137
- controller_path = self.controller_path.dup
129
+ def purable_model_chain
130
+ controller_path = self.class.controller_path.dup
138
131
 
139
- if defined?(controller_prefix) and controller_prefix.present?
140
- prefix_s = controller_prefix.map(&:to_s).join('/')
141
- raise StandardError, 'Bad prefix' if controller_path.index(prefix_s) != 0
132
+ if defined?(self.class.controller_prefix) and self.class.controller_prefix.present?
133
+ prefix_s = self.class.controller_prefix.map(&:to_s).join('/')
134
+ raise StandardError, 'Bad prefix' if controller_path.index(prefix_s) != 0
142
135
 
143
- controller_path.sub! "#{prefix_s}/", ''
144
- end
145
-
146
- controller_path.split('/')
147
- end
136
+ controller_path.sub! "#{prefix_s}/", ''
137
+ end
148
138
 
149
- private_class_method :purable_model_chain
139
+ controller_path.split('/')
140
+ end
150
141
 
151
- def self.purable_model
152
- purable_model_chain.last.singularize.classify.constantize
153
- end
142
+ def initialize
143
+ super
154
144
 
155
- private_class_method :purable_model
145
+ resource_accessor_name = send(:purable_model).name.underscore.to_sym
146
+ define_singleton_method(resource_accessor_name) { resource }
147
+ # rubocop:disable Style/AccessModifierDeclarations
148
+ singleton_class.class_eval { private resource_accessor_name }
149
+ # rubocop:enable Style/AccessModifierDeclarations
150
+ end
156
151
 
157
- def self.inherited(klass)
158
- resource_accessor_name = klass.send(:purable_model).name.underscore.to_sym
159
- klass.define_method(resource_accessor_name) { resource }
160
- klass.helper_method resource_accessor_name
152
+ private_class_method :inherited
161
153
  end
162
-
163
- private_class_method :inherited
164
154
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Bitia
4
- VERSION = '0.4.0'
4
+ VERSION = '0.5.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bitia-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artem Levenkov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-14 00:00:00.000000000 Z
11
+ date: 2020-07-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails