Skip to content

Commit

Permalink
Support filtering has_one associations
Browse files Browse the repository at this point in the history
  • Loading branch information
mshibuya committed Aug 14, 2022
1 parent a1c4c67 commit 9657774
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rails_admin/adapters/mongoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def perform_search_on_associated_collection(field_name, conditions)
case target_association.type
when :belongs_to, :has_and_belongs_to_many
[{target_association.foreign_key.to_s => {'$in' => model.where('$or' => conditions).all.collect { |r| r.send(target_association.primary_key) }}}]
when :has_many
when :has_many, :has_one
[{target_association.primary_key.to_s => {'$in' => model.where('$or' => conditions).all.collect { |r| r.send(target_association.foreign_key) }}}]
end
end
Expand Down
16 changes: 16 additions & 0 deletions spec/integration/actions/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,22 @@
is_expected.to have_no_content(@players[3].name)
end

it 'allows to filter on has_one relationships' do
@draft = FactoryBot.create(:draft, player: @players[1], college: 'University of Alabama')
RailsAdmin.config Player do
list do
field :name
field :draft do
searchable :college
end
end
end

visit index_path(model_name: 'player', f: {draft: {'1' => {v: 'Alabama'}}})
is_expected.to have_content(@players[1].name)
is_expected.to have_css('tbody .name_field', count: 1)
end

it 'allows to disable search on attributes' do
RailsAdmin.config Player do
list do
Expand Down
18 changes: 18 additions & 0 deletions spec/integration/widgets/filter_box_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,22 @@
expect(find('[name^="f[time_field]"][name$="[v][]"]', match: :first, visible: false).value).to eq '2000-01-01T14:00:00'
end
end

describe 'for has_one association field' do
before do
RailsAdmin.config Player do
field :draft do
searchable :college
end
end
end

it 'is filterable' do
visit index_path(model_name: 'player')
click_link 'Add filter'
click_link 'Draft'
expect(page).to have_css '[name^="f[draft]"][name$="[o]"]'
expect(page).to have_css '[name^="f[draft]"][name$="[v]"]'
end
end
end
1 change: 1 addition & 0 deletions src/rails_admin/filter-box.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ import flatpickr from "flatpickr";
case "string":
case "text":
case "belongs_to_association":
case "has_one_association":
control = $(
'<select class="switch-additional-fieldsets form-control form-control-sm"></select>'
)
Expand Down

0 comments on commit 9657774

Please sign in to comment.