forest_liana 4.2.0 → 5.0.0.pre.beta.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
  SHA1:
3
- metadata.gz: d2b30b26c3afe44df763e7bfc1bc7d0f763bf927
4
- data.tar.gz: b03f1798d20593420715c03b68ff31daf39369ee
3
+ metadata.gz: 919b3681f70c2a7265839b6f26375c066cc04657
4
+ data.tar.gz: 670e5dcb4227b380f1547262d0dda5715dc7ffae
5
5
  SHA512:
6
- metadata.gz: b875ee9e95450d54df04e4932b470c9f87a1165b0114a373828879ad5e026fd13182463741eda1bee0d5990e5be55fa8e41e35962358ee36a35b32bf9a1597cd
7
- data.tar.gz: df637e24aa4e579bc5b2d3b30b1329b5d30ed8a938665066f2c7350731ee5462de2ac23ce84f5b4ce450fe9f73139606119eb40db81560a07e843fd26ecd03ff
6
+ metadata.gz: 399a95797f3649fe6f926f76b75241c58ad88f4705e1abfede85e391b08f398fe2c1c909d6451d977c5341d5070c776f81b049383fc8d812775cc20443edb2e7
7
+ data.tar.gz: 65432f209cc2d54976e7227aca68742093385db4bcc4d0de569c6ef9b4c93757c8cb253393afdb18c8752f3b7e00ff602e768128b76bf4d1d99bfdbc7e65b9b0
@@ -137,16 +137,28 @@ module ForestLiana
137
137
  end
138
138
 
139
139
  def destroy
140
- begin
141
- checker = ForestLiana::PermissionsChecker.new(@resource, 'delete', @rendering_id)
142
- return head :forbidden unless checker.is_authorized?
140
+ checker = ForestLiana::PermissionsChecker.new(@resource, 'delete', @rendering_id)
141
+ return head :forbidden unless checker.is_authorized?
143
142
 
144
- @resource.destroy(params[:id]) if @resource.exists?(params[:id])
145
- head :no_content
146
- rescue => error
147
- FOREST_LOGGER.error "Record Destroy error: #{error}\n#{format_stacktrace(error)}"
148
- internal_server_error
149
- end
143
+ @resource.destroy(params[:id]) if @resource.exists?(params[:id])
144
+
145
+ head :no_content
146
+ rescue => error
147
+ FOREST_LOGGER.error "Record Destroy error: #{error}\n#{format_stacktrace(error)}"
148
+ internal_server_error
149
+ end
150
+
151
+ def destroy_bulk
152
+ checker = ForestLiana::PermissionsChecker.new(@resource, 'delete', @rendering_id)
153
+ return head :forbidden unless checker.is_authorized?
154
+
155
+ ids = ForestLiana::ResourcesGetter.get_ids_from_request(params)
156
+ @resource.destroy(ids) if ids&.any?
157
+
158
+ head :no_content
159
+ rescue => error
160
+ FOREST_LOGGER.error "Records Destroy error: #{error}\n#{format_stacktrace(error)}"
161
+ internal_server_error
150
162
  end
151
163
 
152
164
  private
@@ -30,7 +30,11 @@ class ForestLiana::Router
30
30
  when 'POST'
31
31
  action = 'create'
32
32
  when 'DELETE'
33
- action = 'destroy'
33
+ if params[:id]
34
+ action = 'destroy'
35
+ else
36
+ action = 'destroy_bulk'
37
+ end
34
38
  end
35
39
 
36
40
  controller.action(action.to_sym).call(env)
@@ -14,17 +14,22 @@ module ForestLiana
14
14
 
15
15
  remove_association = !@with_deletion || @association.macro == :has_and_belongs_to_many
16
16
 
17
- if remove_association
18
- if @data.is_a?(Array)
19
- @data.each do |record_deleted|
20
- associated_records.delete(@association.klass.find(record_deleted[:id]))
17
+ if @data.is_a?(Array)
18
+ record_ids = @data.map { |record| record[:id] }
19
+ elsif @data.dig('attributes').present?
20
+ record_ids = ForestLiana::ResourcesGetter.get_ids_from_request(@params)
21
+ else
22
+ record_ids = Array.new
23
+ end
24
+
25
+ if !record_ids.nil? && record_ids.any?
26
+ if remove_association
27
+ record_ids.each do |id|
28
+ associated_records.delete(@association.klass.find(id))
21
29
  end
22
30
  end
23
- end
24
31
 
25
- if @with_deletion
26
- if @data.is_a?(Array)
27
- record_ids = @data.map { |record| record[:id] }
32
+ if @with_deletion
28
33
  record_ids = record_ids.select { |record_id| @association.klass.exists?(record_id) }
29
34
  @association.klass.destroy(record_ids)
30
35
  end
@@ -19,7 +19,7 @@ module ForestLiana
19
19
  end
20
20
 
21
21
  def perform
22
- @records = @search_query_builder.perform(@records)
22
+ @records
23
23
  end
24
24
 
25
25
  def count
@@ -64,10 +64,12 @@ module ForestLiana
64
64
  end
65
65
 
66
66
  def prepare_query
67
- @records = get_resource()
68
- .find(@params[:id])
69
- .send(@params[:association_name])
70
- .eager_load(@includes)
67
+ @records = @search_query_builder.perform(
68
+ get_resource()
69
+ .find(@params[:id])
70
+ .send(@params[:association_name])
71
+ .eager_load(@includes)
72
+ )
71
73
  end
72
74
 
73
75
  def offset
@@ -19,6 +19,51 @@ module ForestLiana
19
19
  prepare_query()
20
20
  end
21
21
 
22
+ def self.get_ids_from_request(params)
23
+ attributes = params.dig('data', 'attributes')
24
+ has_body_attributes = attributes != nil
25
+ is_select_all_records_query = has_body_attributes && attributes[:all_records] == true
26
+
27
+ # NOTICE: If it is not a "select all records" query and it receives a list of ID, return list of ID.
28
+ return attributes[:ids] if (!is_select_all_records_query && attributes[:ids])
29
+
30
+ # NOTICE: If it is a "select all records" we have to perform query to build ID list.
31
+ ids = Array.new
32
+
33
+ # NOTICE: Merging all_records_subset_query into attributes preserves filters in HasManyGetter and ResourcesGetter.
34
+ attributes = attributes.merge(attributes[:all_records_subset_query].dup.to_unsafe_h)
35
+
36
+ # NOTICE: Initialize actual resources getter (could either a HasManyGetter or a ResourcesGetter).
37
+ is_related_data = attributes[:parent_collection_id] &&
38
+ attributes[:parent_collection_name] &&
39
+ attributes[:parent_association_name]
40
+ if is_related_data
41
+ parent_collection_name = attributes[:parent_collection_name]
42
+ parent_model = ForestLiana::SchemaUtils.find_model_from_collection_name(parent_collection_name)
43
+ model = parent_model.reflect_on_association(attributes[:parent_association_name].try(:to_sym))
44
+ resources_getter = ForestLiana::HasManyGetter.new(parent_model, model, attributes.merge({
45
+ collection: parent_collection_name,
46
+ id: attributes[:parent_collection_id],
47
+ association_name: attributes[:parent_association_name],
48
+ }))
49
+ else
50
+ collection_name = attributes[:collection_name]
51
+ model = ForestLiana::SchemaUtils.find_model_from_collection_name(collection_name)
52
+ resources_getter = ForestLiana::ResourcesGetter.new(model, attributes)
53
+ end
54
+
55
+ # NOTICE: build IDs list.
56
+ resources_getter.query_for_batch.find_in_batches() do |records|
57
+ ids += records.map { |record| record.id }
58
+ end
59
+
60
+ # NOTICE: remove excluded IDs.
61
+ ids_excluded = (attributes[:all_records_ids_excluded]).map { |id_excluded| id_excluded.to_s }
62
+ return ids.select { |id| !ids_excluded.include? id.to_s } if (ids_excluded && ids_excluded.any?)
63
+
64
+ return ids
65
+ end
66
+
22
67
  def perform
23
68
  @records = @records.eager_load(@includes)
24
69
  end
@@ -53,6 +53,7 @@ ForestLiana::Engine.routes.draw do
53
53
  post ':collection', to: router
54
54
  put ':collection/:id', to: router
55
55
  delete ':collection/:id', to: router
56
+ delete ':collection', to: router
56
57
 
57
58
  # Smart Actions forms value
58
59
  post 'actions/:action_name/values' => 'actions#values'
@@ -1,3 +1,3 @@
1
1
  module ForestLiana
2
- VERSION = "4.2.0"
2
+ VERSION = "5.0.0-beta.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: forest_liana
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.2.0
4
+ version: 5.0.0.pre.beta.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sandro Munda
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-01-22 00:00:00.000000000 Z
11
+ date: 2020-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -453,9 +453,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
453
453
  version: '0'
454
454
  required_rubygems_version: !ruby/object:Gem::Requirement
455
455
  requirements:
456
- - - ">="
456
+ - - ">"
457
457
  - !ruby/object:Gem::Version
458
- version: '0'
458
+ version: 1.3.1
459
459
  requirements: []
460
460
  rubyforge_project:
461
461
  rubygems_version: 2.6.14