Skip to content

Commit

Permalink
Merge pull request #275 from nirebu/nirebu/274-n+1-in-admin-line-item…
Browse files Browse the repository at this point in the history
…-filtering
  • Loading branch information
kennyadsl authored Jan 3, 2023
2 parents b006ed1 + b1da743 commit 4e7bddd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
6 changes: 3 additions & 3 deletions app/models/solidus_subscriptions/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ class Subscription < ApplicationRecord
end)

# Scope for finding subscription with a specific item
scope :with_line_item, (lambda do |id|
joins(:line_items).where(line_items: { id: id })
scope :with_subscribable, (lambda do |id|
joins(line_items: :subscribable).where(spree_variants: { id: id })
end)

def self.ransackable_scopes(_auth_object = nil)
[:in_processing_state, :with_line_item]
[:in_processing_state, :with_subscribable]
end

def self.processing_states
Expand Down
8 changes: 4 additions & 4 deletions app/views/spree/admin/subscriptions/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@
</div>
<div class="field-block col-3">
<div class="field">
<%= label_tag :q_with_line_item_eq, SolidusSubscriptions::Subscription.human_attribute_name(:with_line_item) %>
<%= label_tag :q_with_subscribable_eq, SolidusSubscriptions::Subscription.human_attribute_name(:with_subscribable) %>

<%=
f.select(
:with_line_item,
options_for_select(::SolidusSubscriptions::LineItem.joins(:subscribable).map { |item| [ item.subscribable.name, item.id ]}, params.dig(:q, :with_line_item)),
:with_subscribable,
options_for_select(::Spree::Variant.includes(:product).where(id: ::SolidusSubscriptions::LineItem.distinct.pluck(:subscribable_id)).map { |subscribable| [ subscribable.name, subscribable.id ]}, params.dig(:q, :with_subscribable)),
{ include_blank: true },
class: 'select2 fullwidth'
)
Expand Down Expand Up @@ -137,7 +137,7 @@
<td><%= subscription.interval.inspect %></td>
<td><%= render 'state_pill', subscription: subscription %></td>
<td><%= render 'processing_state_pill', subscription: subscription %></td>
<td><%= subscription.line_items.map { |line_item| line_item.subscribable&.name }.join(", ") %></td>
<td><%= subscription.line_items.includes(subscribable: :product).map { |line_item| line_item.subscribable&.name }.join(", ") %></td>
<td class="actions">
<% if subscription.state_events.include?(:cancel) %>
<%=
Expand Down
16 changes: 10 additions & 6 deletions spec/models/solidus_subscriptions/subscription_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1262,19 +1262,23 @@
describe '.ransackable_scopes' do
subject { described_class.ransackable_scopes }

it { is_expected.to match_array [:in_processing_state, :with_line_item] }
it { is_expected.to match_array [:in_processing_state, :with_subscribable] }
end

describe '.with_line_item' do
describe '.with_subscribable' do
let(:subscription) do
create :subscription, :with_line_item
end
let(:other_subscription) do
create :subscription, :with_line_item
end

it 'can find subscription with line item' do
line_item_id = subscription.line_items.first.id
found_subscription = ::SolidusSubscriptions::Subscription.with_line_item(line_item_id).first
it 'can find subscription with line items of the provided subscribable' do
subscribable = subscription.line_items.first.subscribable
other_subscribable = other_subscription.line_items.first.subscribable

expect(found_subscription.id).to eql(subscription.id)
expect(described_class.with_subscribable(subscribable)).to match_array([subscription])
expect(described_class.with_subscribable(other_subscribable)).to match_array([other_subscription])
end
end

Expand Down

0 comments on commit 4e7bddd

Please sign in to comment.