Skip to content

Commit

Permalink
Fix error on searching or sorting by ActiveStorage field
Browse files Browse the repository at this point in the history
Fixes #3678
  • Loading branch information
mshibuya committed May 19, 2024
1 parent 4e9366f commit dba6c4b
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rails_admin/config/fields/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def sort_column
sortable
elsif sortable.is_a?(Hash) # just join sortable hash, don't do anything smart
"#{sortable.keys.first}.#{sortable.values.first}"
elsif association # use column on target table
elsif association? # use column on target table
"#{associated_model_config.abstract_model.quoted_table_name}.#{abstract_model.quote_column_name(sortable)}"
else # use described column in the field conf.
"#{abstract_model.quoted_table_name}.#{abstract_model.quote_column_name(sortable)}"
Expand Down
8 changes: 8 additions & 0 deletions lib/rails_admin/config/fields/types/active_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ class ActiveStorage < RailsAdmin::Config::Fields::Types::FileUpload
)
end

register_instance_option :searchable do
false
end

register_instance_option :sortable do
false
end

def resource_url(thumb = false)
return nil unless value

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ def resource_url(thumb = false)
direct? && {data: {direct_upload_url: bindings[:view].main_app.rails_direct_uploads_url}} || {},
)
end

register_instance_option :searchable do
false
end

register_instance_option :sortable do
false
end
end
end
end
Expand Down
20 changes: 20 additions & 0 deletions spec/controllers/rails_admin/main_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,26 @@ def get(action, params)
end
end

context 'with a virtual field' do
before do
RailsAdmin.config('Player') do
base do
field :virtual do
sortable :name
end
end

list do
sort_by :virtual
end
end
end

it 'returns the query referenced in the sortable' do
expect(controller.send(:get_sort_hash, RailsAdmin.config(Player))).to match(sort: /["`]?players["`]?\.["`]?name["`]?/, sort_reverse: true)
end
end

it 'works with belongs_to associations with label method virtual' do
controller.params = {sort: 'parent_category', model_name: 'categories'}
expect(controller.send(:get_sort_hash, RailsAdmin.config(Category))).to eq(sort: 'categories.parent_category_id', sort_reverse: true)
Expand Down
12 changes: 12 additions & 0 deletions spec/rails_admin/config/fields/types/active_storage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,17 @@
end
end
end

describe '#searchable' do
it 'is false' do
expect(field.searchable).to be false
end
end

describe '#sortable' do
it 'is false' do
expect(field.sortable).to be false
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,17 @@
end
end
end

describe '#searchable' do
it 'is false' do
expect(field.searchable).to be false
end
end

describe '#sortable' do
it 'is false' do
expect(field.sortable).to be false
end
end
end
end

0 comments on commit dba6c4b

Please sign in to comment.