Skip to content

Commit

Permalink
Ransack allowlists for Spree::Review
Browse files Browse the repository at this point in the history
We filter admin views for reviews with the ransack gem. Since version 4,
Ransack mandates every filtered model to explicitly specify which
attributes and associations should be "ransackable". This PR adds those
allowlists.
  • Loading branch information
mamhoff committed Jan 25, 2024
1 parent 39e77b7 commit f85e177
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
17 changes: 17 additions & 0 deletions app/models/spree/review.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@ class Spree::Review < ApplicationRecord
scope :not_approved, -> { where(approved: false) }
scope :default_approval_filter, -> { Spree::Reviews::Config[:include_unapproved_reviews] ? all : approved }

def self.ransackable_attributes(*)
[
"approved",
"name",
"review",
"title"
]
end

def self.ransackable_associations(*)
[
"feedback_reviews",
"product",
"user"
]
end

def feedback_stars
return 0 if feedback_reviews.size <= 0

Expand Down
12 changes: 12 additions & 0 deletions spec/models/review_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,18 @@
end
end

describe '.ransackable_attributes' do
subject { described_class.ransackable_attributes }

it { is_expected.to contain_exactly("approved", "name", "review", "title") }
end

describe '.ransackable_associations' do
subject { described_class.ransackable_associations }

it { is_expected.to contain_exactly("feedback_reviews", "product", "user") }
end

describe '#recalculate_product_rating' do
let(:product) { create(:product) }
let!(:review) { create(:review, product: product) }
Expand Down

0 comments on commit f85e177

Please sign in to comment.