Skip to content

Commit 7dde259

Browse files
committed
Fix error on searching or sorting by ActiveStorage field
1 parent 5d1582d commit 7dde259

File tree

6 files changed

+61
-1
lines changed

6 files changed

+61
-1
lines changed

lib/rails_admin/config/fields/base.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def sort_column
6767
sortable
6868
elsif sortable.is_a?(Hash) # just join sortable hash, don't do anything smart
6969
"#{sortable.keys.first}.#{sortable.values.first}"
70-
elsif association # use column on target table
70+
elsif association? # use column on target table
7171
"#{associated_model_config.abstract_model.quoted_table_name}.#{abstract_model.quote_column_name(sortable)}"
7272
else # use described column in the field conf.
7373
"#{abstract_model.quoted_table_name}.#{abstract_model.quote_column_name(sortable)}"

lib/rails_admin/config/fields/types/active_storage.rb

+8
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ class ActiveStorage < RailsAdmin::Config::Fields::Types::FileUpload
4040
)
4141
end
4242

43+
register_instance_option :searchable do
44+
false
45+
end
46+
47+
register_instance_option :sortable do
48+
false
49+
end
50+
4351
def resource_url(thumb = false)
4452
return nil unless value
4553

lib/rails_admin/config/fields/types/multiple_active_storage.rb

+8
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ def resource_url(thumb = false)
7070
direct? && {data: {direct_upload_url: bindings[:view].main_app.rails_direct_uploads_url}} || {},
7171
)
7272
end
73+
74+
register_instance_option :searchable do
75+
false
76+
end
77+
78+
register_instance_option :sortable do
79+
false
80+
end
7381
end
7482
end
7583
end

spec/controllers/rails_admin/main_controller_spec.rb

+20
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,26 @@ def get(action, params)
6565
end
6666
end
6767

68+
context 'with a virtual field' do
69+
before do
70+
RailsAdmin.config('Player') do
71+
base do
72+
field :virtual do
73+
sortable :name
74+
end
75+
end
76+
77+
list do
78+
sort_by :virtual
79+
end
80+
end
81+
end
82+
83+
it 'returns the query referenced in the sortable' do
84+
expect(controller.send(:get_sort_hash, RailsAdmin.config(Player))).to match(sort: /["`]?players["`]?\.["`]?name["`]?/, sort_reverse: true)
85+
end
86+
end
87+
6888
it 'works with belongs_to associations with label method virtual' do
6989
controller.params = {sort: 'parent_category', model_name: 'categories'}
7090
expect(controller.send(:get_sort_hash, RailsAdmin.config(Category))).to eq(sort: 'categories.parent_category_id', sort_reverse: true)

spec/rails_admin/config/fields/types/active_storage_spec.rb

+12
Original file line numberDiff line numberDiff line change
@@ -123,5 +123,17 @@
123123
end
124124
end
125125
end
126+
127+
describe '#searchable' do
128+
it 'is false' do
129+
expect(field.searchable).to be false
130+
end
131+
end
132+
133+
describe '#sortable' do
134+
it 'is false' do
135+
expect(field.sortable).to be false
136+
end
137+
end
126138
end
127139
end

spec/rails_admin/config/fields/types/multiple_active_storage_spec.rb

+12
Original file line numberDiff line numberDiff line change
@@ -127,5 +127,17 @@
127127
end
128128
end
129129
end
130+
131+
describe '#searchable' do
132+
it 'is false' do
133+
expect(field.searchable).to be false
134+
end
135+
end
136+
137+
describe '#sortable' do
138+
it 'is false' do
139+
expect(field.sortable).to be false
140+
end
141+
end
130142
end
131143
end

0 commit comments

Comments
 (0)