avo 3.7.2 → 3.7.3

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: 7b71babedf80b76c301a1e76767953f9a514e25921a2016cacb12c859422db15
4
- data.tar.gz: 1ae761cdfb7201bac56c687c04b857b88520fb4f6e317203dda499b38649388e
3
+ metadata.gz: 14cb5c508e7408422bb0551c9a82571ea3467965cb91b9aa47488ebd8632a9b3
4
+ data.tar.gz: 0eb34a3785aeaaf47679d5ac189a5a8ac0ff8feb0758d217f1a5e9bc38ae17ca
5
5
  SHA512:
6
- metadata.gz: e1f4e74eb8d83cc4f2986a42036bd14b5323a86ab9377c8a0946d2071cf2a252f6745bcbe1e6e320f162dd957f20d86e112357350d3f39672829955575435ff9
7
- data.tar.gz: 1ecfd5ebf8db53a389d1a8b1fcb96599cee34a63c951e28429e512c47460c99d6b48576bfda754bf8540e65b9da651c019dd7dbdcd17032d05edf6268f835f1c
6
+ metadata.gz: db7afe8be4b3a1967a8ada77d55eb58ab29f617d1a9394ed6e7801aae6e1be357682c6a1ddc54e9a170eaf478046bb0d18fc4f2b592a8e740d1abb2665dc0583
7
+ data.tar.gz: 658a22d98d8fd0f24d1ce61df6b11802f5e68b8d4e630b3df41b7a5742c6d7a74b46f02bcfb5bdc65673783188f0f278c5f86b826f09482d6115a6b6fc58f6f5
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- avo (3.7.2)
4
+ avo (3.7.3)
5
5
  actionview (>= 6.1)
6
6
  active_link_to
7
7
  activerecord (>= 6.1)
@@ -12,7 +12,7 @@ class Avo::BaseComponent < ViewComponent::Base
12
12
  # Use the @parent_resource to fetch the field using the @reflection name.
13
13
  def field
14
14
  reflection_name = params[:related_name]&.to_sym || @reflection.name
15
- @parent_resource.get_field_definitions.find { |f| f.id == reflection_name }
15
+ @parent_resource.get_field(reflection_name)
16
16
  rescue
17
17
  nil
18
18
  end
@@ -110,7 +110,7 @@ module Avo
110
110
  end
111
111
 
112
112
  def set_reflection_field
113
- @field = @resource.get_field_definitions.find { |f| f.id == @related_resource_name.to_sym }
113
+ @field = @resource.get_field(@related_resource_name.to_sym)
114
114
  @field.hydrate(resource: @resource, record: @record, view: :new)
115
115
  rescue
116
116
  end
@@ -34,21 +34,7 @@ module Avo
34
34
  @query = @query.includes(*@resource.includes)
35
35
  end
36
36
 
37
- # Sort the items
38
- if @index_params[:sort_by].present?
39
- unless @index_params[:sort_by].eql? :created_at
40
- @query = @query.unscope(:order)
41
- end
42
-
43
- # Check if the sortable field option is actually a proc and we need to do a custom sort
44
- field_id = @index_params[:sort_by].to_sym
45
- field = @resource.get_field_definitions.find { |field| field.id == field_id }
46
- @query = if field&.sortable.is_a?(Proc)
47
- Avo::ExecutionContext.new(target: field.sortable, query: @query, direction: @index_params[:sort_direction]).handle
48
- else
49
- @query.order("#{@resource.model_class.table_name}.#{@index_params[:sort_by]} #{@index_params[:sort_direction]}")
50
- end
51
- end
37
+ apply_sorting
52
38
 
53
39
  # Apply filters to the current query
54
40
  filters_to_be_applied.each do |filter_class, filter_value|
@@ -600,5 +586,33 @@ module Avo
600
586
  def apply_pagination
601
587
  @pagy, @records = @resource.apply_pagination(index_params: @index_params, query: pagy_query)
602
588
  end
589
+
590
+ def apply_sorting
591
+ return if @index_params[:sort_by].nil?
592
+
593
+ sort_by = @index_params[:sort_by].to_sym
594
+ if sort_by != :created_at
595
+ @query = @query.unscope(:order)
596
+ end
597
+
598
+ # Verify that sort_by param actually is bonded to a field.
599
+ field = @resource.get_field(sort_by)
600
+
601
+ # Check if the sortable field option is a proc and if there is a need to do a custom sort
602
+ @query = if field.present? && field.sortable.is_a?(Proc)
603
+ Avo::ExecutionContext.new(target: field.sortable, query: @query, direction: sanitized_sort_direction).handle
604
+ # Sanitize sort_by param by checking if have bounded field.
605
+ elsif field.present? && sanitized_sort_direction
606
+ @query.order("#{@resource.model_class.table_name}.#{sort_by} #{sanitized_sort_direction}")
607
+ # Transform Model to ActiveRecord::Relation because Avo expects one.
608
+ else
609
+ @query.where("1=1")
610
+ end
611
+ end
612
+
613
+ # Sanitize sort_direction param
614
+ def sanitized_sort_direction
615
+ @sanitized_sort_direction ||= @index_params[:sort_direction].presence_in(["asc", "desc"])
616
+ end
603
617
  end
604
618
  end
@@ -173,9 +173,7 @@ module Avo
173
173
  user: _current_user
174
174
  )
175
175
 
176
- reflection_resource.detect_fields.get_field_definitions.find do |field|
177
- field.id.to_s == params[:via_association_id]
178
- end
176
+ reflection_resource.detect_fields.get_field(params[:via_association_id])
179
177
  end
180
178
 
181
179
  def fetch_parent
data/lib/avo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Avo
2
- VERSION = "3.7.2" unless const_defined?(:VERSION)
2
+ VERSION = "3.7.3" unless const_defined?(:VERSION)
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: avo
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.7.2
4
+ version: 3.7.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adrian Marin
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2024-05-08 00:00:00.000000000 Z
13
+ date: 2024-05-09 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord