Change all model scopes without a callable object to use a proc

This commit is contained in:
luisramos0
2019-05-16 17:26:10 +02:00
parent 6a0925539f
commit ffbd79d3dd
14 changed files with 62 additions and 49 deletions

View File

@@ -84,9 +84,9 @@ class Enterprise < ActiveRecord::Base
after_rollback :restore_permalink
scope :by_name, order('name')
scope :visible, where(visible: true)
scope :activated, where("sells != 'unspecified'")
scope :by_name, -> { order('name') }
scope :visible, -> { where(visible: true) }
scope :activated, -> { where("sells != 'unspecified'") }
scope :ready_for_checkout, lambda {
joins(:shipping_methods).
joins(:payment_methods).
@@ -103,9 +103,9 @@ class Enterprise < ActiveRecord::Base
where("TRUE")
end
}
scope :is_primary_producer, where(is_primary_producer: true)
scope :is_distributor, where('sells != ?', 'none')
scope :is_hub, where(sells: 'any')
scope :is_primary_producer, -> { where(is_primary_producer: true) }
scope :is_distributor, -> { where('sells != ?', 'none') }
scope :is_hub, -> { where(sells: 'any') }
scope :supplying_variant_in, lambda { |variants|
joins(supplied_products: :variants_including_master).
where('spree_variants.id IN (?)', variants).
@@ -113,21 +113,21 @@ class Enterprise < ActiveRecord::Base
}
scope :with_order_cycles_as_supplier_outer,
joins("LEFT OUTER JOIN exchanges ON (exchanges.sender_id = enterprises.id AND exchanges.incoming = 't')").
joins('LEFT OUTER JOIN order_cycles ON (order_cycles.id = exchanges.order_cycle_id)')
-> { joins("LEFT OUTER JOIN exchanges ON (exchanges.sender_id = enterprises.id AND exchanges.incoming = 't')").
joins('LEFT OUTER JOIN order_cycles ON (order_cycles.id = exchanges.order_cycle_id)') }
scope :with_order_cycles_as_distributor_outer,
joins("LEFT OUTER JOIN exchanges ON (exchanges.receiver_id = enterprises.id AND exchanges.incoming = 'f')").
joins('LEFT OUTER JOIN order_cycles ON (order_cycles.id = exchanges.order_cycle_id)')
-> { joins("LEFT OUTER JOIN exchanges ON (exchanges.receiver_id = enterprises.id AND exchanges.incoming = 'f')").
joins('LEFT OUTER JOIN order_cycles ON (order_cycles.id = exchanges.order_cycle_id)') }
scope :with_order_cycles_outer,
joins("LEFT OUTER JOIN exchanges ON (exchanges.receiver_id = enterprises.id OR exchanges.sender_id = enterprises.id)").
joins('LEFT OUTER JOIN order_cycles ON (order_cycles.id = exchanges.order_cycle_id)')
-> { joins("LEFT OUTER JOIN exchanges ON (exchanges.receiver_id = enterprises.id OR exchanges.sender_id = enterprises.id)").
joins('LEFT OUTER JOIN order_cycles ON (order_cycles.id = exchanges.order_cycle_id)') }
scope :with_order_cycles_and_exchange_variants_outer,
with_order_cycles_as_distributor_outer.
-> { with_order_cycles_as_distributor_outer.
joins('LEFT OUTER JOIN exchange_variants ON (exchange_variants.exchange_id = exchanges.id)').
joins('LEFT OUTER JOIN spree_variants ON (spree_variants.id = exchange_variants.variant_id)')
joins('LEFT OUTER JOIN spree_variants ON (spree_variants.id = exchange_variants.variant_id)') }
scope :distributors_with_active_order_cycles, lambda {
with_order_cycles_as_distributor_outer.

View File

@@ -47,8 +47,8 @@ class EnterpriseGroup < ActiveRecord::Base
supports_s3 :logo
supports_s3 :promo_image
scope :by_position, order('position ASC')
scope :on_front_page, where(on_front_page: true)
scope :by_position, -> { order('position ASC') }
scope :on_front_page, -> { where(on_front_page: true) }
scope :managed_by, lambda { |user|
if user.has_spree_role?('admin')
scoped

View File

@@ -9,9 +9,10 @@ class EnterpriseRelationship < ActiveRecord::Base
after_save :update_permissions_of_child_variant_overrides
before_destroy :revoke_all_child_variant_overrides
scope :with_enterprises,
joins('LEFT JOIN enterprises AS parent_enterprises ON parent_enterprises.id = enterprise_relationships.parent_id').
scope :with_enterprises, -> {
joins('LEFT JOIN enterprises AS parent_enterprises ON parent_enterprises.id = enterprise_relationships.parent_id').
joins('LEFT JOIN enterprises AS child_enterprises ON child_enterprises.id = enterprise_relationships.child_id')
}
scope :involving_enterprises, ->(enterprises) {
where('parent_id IN (?) OR child_id IN (?)', enterprises, enterprises)
@@ -25,7 +26,7 @@ class EnterpriseRelationship < ActiveRecord::Base
where('enterprise_relationship_permissions.name = ?', permission)
}
scope :by_name, with_enterprises.order('child_enterprises.name, parent_enterprises.name')
scope :by_name, -> { with_enterprises.order('child_enterprises.name, parent_enterprises.name') }
# Load an array of the relatives of each enterprise (ie. any enterprise related to it in
# either direction). This array is split into distributors and producers, and has the format:

View File

@@ -5,5 +5,5 @@ class EnterpriseRole < ActiveRecord::Base
validates :user_id, :enterprise_id, presence: true
validates :enterprise_id, uniqueness: { scope: :user_id, message: I18n.t(:enterprise_role_uniqueness_error) }
scope :by_user_email, joins(:user).order('spree_users.email ASC')
scope :by_user_email, -> { joins(:user).order('spree_users.email ASC') }
end

View File

@@ -20,8 +20,8 @@ class Exchange < ActiveRecord::Base
accepts_nested_attributes_for :variants
scope :in_order_cycle, lambda { |order_cycle| where(order_cycle_id: order_cycle) }
scope :incoming, where(incoming: true)
scope :outgoing, where(incoming: false)
scope :incoming, -> { where(incoming: true) }
scope :outgoing, -> { where(incoming: false) }
scope :from_enterprise, lambda { |enterprise| where(sender_id: enterprise) }
scope :to_enterprise, lambda { |enterprise| where(receiver_id: enterprise) }
scope :from_enterprises, lambda { |enterprises| where('exchanges.sender_id IN (?)', enterprises) }
@@ -31,15 +31,17 @@ class Exchange < ActiveRecord::Base
scope :with_variant, lambda { |variant| joins(:exchange_variants).where('exchange_variants.variant_id = ?', variant) }
scope :with_any_variant, lambda { |variants| joins(:exchange_variants).where('exchange_variants.variant_id IN (?)', variants).select('DISTINCT exchanges.*') }
scope :with_product, lambda { |product| joins(:exchange_variants).where('exchange_variants.variant_id IN (?)', product.variants_including_master) }
scope :by_enterprise_name, joins('INNER JOIN enterprises AS sender ON (sender.id = exchanges.sender_id)').
scope :by_enterprise_name, -> { joins('INNER JOIN enterprises AS sender ON (sender.id = exchanges.sender_id)').
joins('INNER JOIN enterprises AS receiver ON (receiver.id = exchanges.receiver_id)').
order("CASE WHEN exchanges.incoming='t' THEN sender.name ELSE receiver.name END")
order("CASE WHEN exchanges.incoming='t' THEN sender.name ELSE receiver.name END") }
# Exchanges on order cycles that are dated and are upcoming or open are cached
scope :cachable, outgoing.
scope :cachable, -> {
outgoing.
joins(:order_cycle).
merge(OrderCycle.dated).
merge(OrderCycle.not_closed)
}
scope :managed_by, lambda { |user|
if user.has_spree_role?('admin')

View File

@@ -11,8 +11,8 @@ class InventoryItem < ActiveRecord::Base
validates :variant_id, presence: true
validates :visible, inclusion: { in: [true, false], message: I18n.t(:inventory_item_visibility_error) }
scope :visible, where(visible: true)
scope :hidden, where(visible: false)
scope :visible, -> { where(visible: true) }
scope :hidden, -> { where(visible: false) }
after_save :refresh_products_cache

View File

@@ -33,8 +33,8 @@ class OrderCycle < ActiveRecord::Base
scope :upcoming, lambda { where('order_cycles.orders_open_at > ?', Time.zone.now) }
scope :not_closed, lambda { where('order_cycles.orders_close_at > ? OR order_cycles.orders_close_at IS NULL', Time.zone.now) }
scope :closed, lambda { where('order_cycles.orders_close_at < ?', Time.zone.now).order("order_cycles.orders_close_at DESC") }
scope :undated, where('order_cycles.orders_open_at IS NULL OR orders_close_at IS NULL')
scope :dated, where('orders_open_at IS NOT NULL AND orders_close_at IS NOT NULL')
scope :undated, -> { where('order_cycles.orders_open_at IS NULL OR orders_close_at IS NULL') }
scope :dated, -> { where('orders_open_at IS NOT NULL AND orders_close_at IS NOT NULL') }
scope :soonest_closing, lambda { active.order('order_cycles.orders_close_at ASC') }
# TODO This method returns all the closed orders. So maybe we can replace it with :recently_closed.
@@ -42,7 +42,7 @@ class OrderCycle < ActiveRecord::Base
scope :soonest_opening, lambda { upcoming.order('order_cycles.orders_open_at ASC') }
scope :by_name, order('name')
scope :by_name, -> { order('name') }
scope :with_distributor, lambda { |distributor|
joins(:exchanges).merge(Exchange.outgoing).merge(Exchange.to_enterprise(distributor))

View File

@@ -10,13 +10,13 @@ module Spree
has_one :metadata, class_name: 'AdjustmentMetadata'
belongs_to :tax_rate, foreign_key: 'originator_id', conditions: "spree_adjustments.originator_type = 'Spree::TaxRate'"
scope :enterprise_fee, where(originator_type: 'EnterpriseFee')
scope :admin, where(source_type: nil, originator_type: nil)
scope :included_tax, where(originator_type: 'Spree::TaxRate', adjustable_type: 'Spree::LineItem')
scope :enterprise_fee, -> { where(originator_type: 'EnterpriseFee') }
scope :admin, -> { where(source_type: nil, originator_type: nil) }
scope :included_tax, -> { where(originator_type: 'Spree::TaxRate', adjustable_type: 'Spree::LineItem') }
scope :with_tax, where('spree_adjustments.included_tax > 0')
scope :without_tax, where('spree_adjustments.included_tax = 0')
scope :payment_fee, where(originator_type: 'Spree::PaymentMethod')
scope :with_tax, -> { where('spree_adjustments.included_tax > 0') }
scope :without_tax, -> { where('spree_adjustments.included_tax = 0') }
scope :payment_fee, -> { where(originator_type: 'Spree::PaymentMethod') }
attr_accessible :included_tax

View File

@@ -36,8 +36,10 @@ Spree::LineItem.class_eval do
}
# Find line items that are from order sorted by variant name and unit value
scope :sorted_by_name_and_unit_value, joins(variant: :product).
scope :sorted_by_name_and_unit_value, -> {
joins(variant: :product).
reorder('lower(spree_products.name) asc, lower(spree_variants.display_name) asc, spree_variants.unit_value asc')
}
scope :from_order_cycle, lambda { |order_cycle|
joins(order: :order_cycle).
@@ -53,13 +55,17 @@ Spree::LineItem.class_eval do
where('spree_products.supplier_id IN (?)', enterprises)
}
scope :with_tax, joins(:adjustments).
scope :with_tax, -> {
joins(:adjustments).
where('spree_adjustments.originator_type = ?', 'Spree::TaxRate').
select('DISTINCT spree_line_items.*')
}
# Line items without a Spree::TaxRate-originated adjustment
scope :without_tax, joins("LEFT OUTER JOIN spree_adjustments ON (spree_adjustments.adjustable_id=spree_line_items.id AND spree_adjustments.adjustable_type = 'Spree::LineItem' AND spree_adjustments.originator_type='Spree::TaxRate')").
scope :without_tax, -> {
joins("LEFT OUTER JOIN spree_adjustments ON (spree_adjustments.adjustable_id=spree_line_items.id AND spree_adjustments.adjustable_type = 'Spree::LineItem' AND spree_adjustments.originator_type='Spree::TaxRate')").
where('spree_adjustments.id IS NULL')
}
def cap_quantity_at_stock!
scoper.scope(variant)

View File

@@ -35,9 +35,9 @@ Spree::PaymentMethod.class_eval do
where('enterprises.id = ?', distributor)
}
scope :for_subscriptions, where(type: Subscription::ALLOWED_PAYMENT_METHOD_TYPES)
scope :for_subscriptions, -> { where(type: Subscription::ALLOWED_PAYMENT_METHOD_TYPES) }
scope :by_name, order('spree_payment_methods.name ASC')
scope :by_name, -> { order('spree_payment_methods.name ASC') }
# Rewrite Spree's ruby-land class method as a scope
scope :available, lambda { |display_on = 'both'|

View File

@@ -41,12 +41,16 @@ Spree::Product.class_eval do
after_save :refresh_products_cache
# -- Joins
scope :with_order_cycles_outer, joins('LEFT OUTER JOIN spree_variants AS o_spree_variants ON (o_spree_variants.product_id = spree_products.id)').
scope :with_order_cycles_outer, -> {
joins('LEFT OUTER JOIN spree_variants AS o_spree_variants ON (o_spree_variants.product_id = spree_products.id)').
joins('LEFT OUTER JOIN exchange_variants AS o_exchange_variants ON (o_exchange_variants.variant_id = o_spree_variants.id)').
joins('LEFT OUTER JOIN exchanges AS o_exchanges ON (o_exchanges.id = o_exchange_variants.exchange_id)').
joins('LEFT OUTER JOIN order_cycles AS o_order_cycles ON (o_order_cycles.id = o_exchanges.order_cycle_id)')
}
scope :with_order_cycles_inner, joins(variants_including_master: { exchanges: :order_cycle })
scope :with_order_cycles_inner, -> {
joins(variants_including_master: { exchanges: :order_cycle })
}
scope :visible_for, lambda { |enterprise|
joins('LEFT OUTER JOIN spree_variants AS o_spree_variants ON (o_spree_variants.product_id = spree_products.id)').
@@ -93,8 +97,8 @@ Spree::Product.class_eval do
where('order_cycles.id IS NOT NULL')
}
scope :by_producer, joins(:supplier).order('enterprises.name')
scope :by_name, order('name')
scope :by_producer, -> { joins(:supplier).order('enterprises.name') }
scope :by_name, -> { order('name') }
scope :managed_by, lambda { |user|
if user.has_spree_role?('admin')

View File

@@ -29,7 +29,7 @@ Spree::ShippingMethod.class_eval do
where('enterprises.id = ?', distributor)
}
scope :by_name, order('spree_shipping_methods.name ASC')
scope :by_name, -> { order('spree_shipping_methods.name ASC') }
# Return the services (pickup, delivery) that different distributors provide, in the format:
# {distributor_id => {pickup: true, delivery: false}, ...}

View File

@@ -33,9 +33,9 @@ Spree::Variant.class_eval do
after_save :refresh_products_cache
around_destroy :destruction
scope :with_order_cycles_inner, joins(exchanges: :order_cycle)
scope :with_order_cycles_inner, -> { joins(exchanges: :order_cycle) }
scope :not_master, where(is_master: false)
scope :not_master, -> { where(is_master: false) }
scope :in_order_cycle, lambda { |order_cycle|
with_order_cycles_inner.
merge(Exchange.outgoing).

View File

@@ -1,6 +1,6 @@
class EnsureShippingMethodsHaveDistributors < ActiveRecord::Migration
class Enterprise < ActiveRecord::Base
scope :is_distributor, where(is_distributor: true)
scope :is_distributor, -> { where(is_distributor: true) }
end
def up