Merge pull request #5919 from luisramos0/injector_refactor_for_rails41

[Rails 4.1, works in master] Convert relations with to_a so that they work in rails 4.1
This commit is contained in:
Luis Ramos
2020-10-31 09:33:27 +00:00
committed by GitHub
23 changed files with 80 additions and 59 deletions

View File

@@ -42,7 +42,6 @@ Layout/LineLength:
- app/helpers/angular_form_helper.rb
- app/helpers/checkout_helper.rb
- app/helpers/enterprises_helper.rb
- app/helpers/injection_helper.rb
- app/helpers/markdown_helper.rb
- app/helpers/order_cycles_helper.rb
- app/helpers/spree/orders_helper.rb
@@ -88,6 +87,7 @@ Layout/LineLength:
- Gemfile
- lib/discourse/single_sign_on.rb
- lib/open_food_network/available_payment_method_filter.rb
- lib/open_food_network/bulk_coop_report.rb
- lib/open_food_network/customers_report.rb
- lib/open_food_network/enterprise_fee_applicator.rb
- lib/open_food_network/enterprise_fee_calculator.rb

View File

@@ -181,10 +181,10 @@ module Admin
enterprise_payment_methods = @enterprise.payment_methods.to_a
enterprise_shipping_methods = @enterprise.shipping_methods.to_a
# rubocop:disable Style/TernaryParentheses
@payment_methods = Spree::PaymentMethod.managed_by(spree_current_user).sort_by! do |pm|
@payment_methods = Spree::PaymentMethod.managed_by(spree_current_user).to_a.sort_by! do |pm|
[(enterprise_payment_methods.include? pm) ? 0 : 1, pm.name]
end
@shipping_methods = Spree::ShippingMethod.managed_by(spree_current_user).sort_by! do |sm|
@shipping_methods = Spree::ShippingMethod.managed_by(spree_current_user).to_a.sort_by! do |sm|
[(enterprise_shipping_methods.include? sm) ? 0 : 1, sm.name]
end
# rubocop:enable Style/TernaryParentheses

View File

@@ -127,7 +127,7 @@ module Spree
def load_hubs
# rubocop:disable Style/TernaryParentheses
@hubs = Enterprise.managed_by(spree_current_user).is_distributor.sort_by! do |d|
@hubs = Enterprise.managed_by(spree_current_user).is_distributor.to_a.sort_by! do |d|
[(@payment_method.has_distributor? d) ? 0 : 1, d.name]
end
# rubocop:enable Style/TernaryParentheses

View File

@@ -50,7 +50,7 @@ module Spree
def load_hubs
# rubocop:disable Style/TernaryParentheses
@hubs = Enterprise.managed_by(spree_current_user).is_distributor.sort_by! do |d|
@hubs = Enterprise.managed_by(spree_current_user).is_distributor.to_a.sort_by! do |d|
[(@shipping_method.has_distributor? d) ? 0 : 1, d.name]
end
# rubocop:enable Style/TernaryParentheses

View File

@@ -7,6 +7,8 @@ module CheckoutHelper
adjustments = order.adjustments.eligible
exclude = opts[:exclude] || {}
adjustments = adjustments.to_a
# Remove empty tax adjustments and (optionally) shipping fees
adjustments.reject! { |a| a.originator_type == 'Spree::TaxRate' && a.amount == 0 }
adjustments.reject! { |a| a.originator_type == 'Spree::ShippingMethod' } if exclude.include? :shipping

View File

@@ -4,7 +4,7 @@ module InjectionHelper
include SerializerHelper
def inject_enterprises(enterprises = nil)
inject_json_ams(
inject_json_array(
"enterprises",
enterprises || default_enterprise_query,
Api::EnterpriseSerializer,
@@ -15,7 +15,7 @@ module InjectionHelper
def inject_groups
select_only = required_attributes EnterpriseGroup, Api::GroupListSerializer
inject_json_ams(
inject_json_array(
"groups",
EnterpriseGroup.on_front_page.by_position.select(select_only).
includes(enterprises: [:shipping_methods, { address: [:state, :country] }],
@@ -26,7 +26,7 @@ module InjectionHelper
end
def inject_enterprise_shopfront(enterprise)
inject_json_ams(
inject_json(
"shopfront",
enterprise,
Api::EnterpriseShopfrontSerializer
@@ -36,7 +36,7 @@ module InjectionHelper
def inject_enterprise_shopfront_list
select_only = required_attributes Enterprise, Api::EnterpriseShopfrontListSerializer
inject_json_ams(
inject_json_array(
"enterprises",
Enterprise.activated.visible.select(select_only).includes(address: [:state, :country]).all,
Api::EnterpriseShopfrontListSerializer
@@ -50,13 +50,13 @@ module InjectionHelper
includes(:properties, address: [:state, :country], supplied_products: :properties).
all
inject_json_ams "enterprises",
enterprises_and_relatives,
Api::EnterpriseSerializer, enterprise_injection_data
inject_json_array "enterprises",
enterprises_and_relatives,
Api::EnterpriseSerializer, enterprise_injection_data
end
def inject_group_enterprises
inject_json_ams(
inject_json_array(
"enterprises",
@group.enterprises.activated.all,
Api::EnterpriseSerializer,
@@ -65,11 +65,18 @@ module InjectionHelper
end
def inject_current_hub
inject_json_ams "currentHub", current_distributor, Api::EnterpriseSerializer, enterprise_injection_data
inject_json "currentHub",
current_distributor,
Api::EnterpriseSerializer,
enterprise_injection_data
end
def inject_current_order
inject_json_ams "currentOrder", current_order, Api::CurrentOrderSerializer, current_distributor: current_distributor, current_order_cycle: current_order_cycle
inject_json "currentOrder",
current_order,
Api::CurrentOrderSerializer,
current_distributor: current_distributor,
current_order_cycle: current_order_cycle
end
def inject_current_order_cycle
@@ -79,73 +86,78 @@ module InjectionHelper
end
def inject_available_shipping_methods
inject_json_ams "shippingMethods", available_shipping_methods,
Api::ShippingMethodSerializer, current_order: current_order
inject_json_array "shippingMethods", available_shipping_methods,
Api::ShippingMethodSerializer, current_order: current_order
end
def inject_available_payment_methods
inject_json_ams "paymentMethods", available_payment_methods,
Api::PaymentMethodSerializer, current_order: current_order
inject_json_array "paymentMethods", available_payment_methods,
Api::PaymentMethodSerializer, current_order: current_order
end
def inject_taxons
inject_json_ams "taxons", Spree::Taxon.all.to_a, Api::TaxonSerializer
inject_json_array "taxons", Spree::Taxon.all.to_a, Api::TaxonSerializer
end
def inject_properties
inject_json_ams "properties", Spree::Property.all.to_a, Api::PropertySerializer
inject_json_array "properties", Spree::Property.all.to_a, Api::PropertySerializer
end
def inject_currency_config
inject_json_ams "currencyConfig", {}, Api::CurrencyConfigSerializer
inject_json "currencyConfig", {}, Api::CurrencyConfigSerializer
end
def inject_open_street_map_config
inject_json_ams "openStreetMapConfig", {}, Api::OpenStreetMapConfigSerializer
inject_json "openStreetMapConfig", {}, Api::OpenStreetMapConfigSerializer
end
def inject_spree_api_key
render partial: "json/injection_ams", locals: { name: 'spreeApiKey', json: "'#{@spree_api_key}'" }
render partial: "json/injection_ams",
locals: { name: 'spreeApiKey', json: "'#{@spree_api_key}'" }
end
def inject_available_countries
inject_json_ams "availableCountries", available_countries, Api::CountrySerializer
inject_json_array "availableCountries", available_countries, Api::CountrySerializer
end
def inject_enterprise_attributes
render partial: "json/injection_ams", locals: { name: 'enterpriseAttributes', json: @enterprise_attributes.to_json.to_s }
render partial: "json/injection_ams",
locals: { name: 'enterpriseAttributes', json: @enterprise_attributes.to_json.to_s }
end
def inject_orders
inject_json_ams "orders", @orders.all, Api::OrderSerializer
inject_json_array "orders", @orders.all, Api::OrderSerializer
end
def inject_shops
customers = spree_current_user.customers
shops = Enterprise.where(id: @orders.pluck(:distributor_id).uniq | customers.pluck(:enterprise_id))
inject_json_ams "shops", shops.all, Api::ShopForOrdersSerializer
shops = Enterprise.where(id: @orders.pluck(:distributor_id).uniq |
customers.pluck(:enterprise_id))
inject_json_array "shops", shops.all, Api::ShopForOrdersSerializer
end
def inject_saved_credit_cards
data = spree_current_user ? spree_current_user.credit_cards.with_payment_profile.all : []
inject_json_ams "savedCreditCards", data, Api::CreditCardSerializer
inject_json_array "savedCreditCards", data, Api::CreditCardSerializer
end
def inject_current_user
inject_json_ams "user", spree_current_user, Api::UserSerializer
inject_json "user", spree_current_user, Api::UserSerializer
end
def inject_rails_flash
inject_json_ams "railsFlash", OpenStruct.new(flash.to_hash), Api::RailsFlashSerializer
inject_json "railsFlash", OpenStruct.new(flash.to_hash), Api::RailsFlashSerializer
end
def inject_json_ams(name, data, serializer, opts = {})
if data.is_a?(Array)
opts = { each_serializer: serializer }.merge(opts)
serializer = ActiveModel::ArraySerializer
end
def inject_json_array(name, data, serializer, opts = {})
opts = { each_serializer: serializer }.merge(opts)
serializer = ActiveModel::ArraySerializer
inject_json(name, data, serializer, opts)
end
def inject_json(name, data, serializer, opts = {})
serializer_instance = serializer.new(data, opts)
json = serializer_instance.to_json
render partial: "json/injection_ams", locals: { name: name, json: json }

View File

@@ -60,11 +60,11 @@ class ProducerMailer < Spree::BaseMailer
end
def total_from_line_items(line_items)
Spree::Money.new line_items.sum(&:total)
Spree::Money.new line_items.to_a.sum(&:total)
end
def tax_total_from_line_items(line_items)
Spree::Money.new line_items.sum(&:included_tax)
Spree::Money.new line_items.to_a.sum(&:included_tax)
end
# This hack makes ActiveRecord skip the default_scope (deleted_at IS NULL)

View File

@@ -49,7 +49,7 @@ module Calculator
# Finds relevant fees for each line_item,
# calculates the tax on them, and returns the total tax
def per_item_fees_total(order, calculator)
order.line_items.sum do |line_item|
order.line_items.to_a.sum do |line_item|
calculator.per_item_enterprise_fee_applicators_for(line_item.variant)
.select { |applicator| applicable_rate?(applicator, line_item) }
.sum { |applicator| applicator.enterprise_fee.compute_amount(line_item) }

View File

@@ -18,7 +18,7 @@ class Calculator::FlatPercentPerItem < Spree::Calculator
end
def compute(object)
line_items_for(object).sum do |li|
line_items_for(object).to_a.sum do |li|
unless li.price.present? && li.quantity.present?
raise ArgumentError, "object must respond to #price and #quantity"
end

View File

@@ -32,7 +32,7 @@ module Calculator
private
def total_weight(line_items)
line_items.sum do |line_item|
line_items.to_a.sum do |line_item|
line_item_weight(line_item)
end
end

View File

@@ -50,6 +50,7 @@ class ModelSet
# Remove all elements to be deleted from collection and return them
# Allows us to render @model_set.collection without deleted elements
deleted = []
@collection = collection.to_a
collection.delete_if { |e| deleted << e if @delete_if.andand.call(e.attributes) }
deleted
end

View File

@@ -192,7 +192,7 @@ module Spree
line_item_adjustments = OrderAdjustmentsFetcher.new(order).line_item_adjustments(self)
(price + line_item_adjustments.sum(&:amount) / quantity).round(2)
(price + line_item_adjustments.to_a.sum(&:amount) / quantity).round(2)
end
def single_display_amount_with_adjustments

View File

@@ -30,7 +30,7 @@ module Spree
def fetch_stock_items
# Don't re-fetch associated stock items from the DB if we've already eager-loaded them
return @variant.stock_items.to_a if @variant.stock_items.loaded?
return @variant.stock_items if @variant.stock_items.loaded?
Spree::StockItem.joins(:stock_location).
where(:variant_id => @variant, Spree::StockLocation.table_name => { active: true })

View File

@@ -3,7 +3,7 @@
%td.toggle-bought{ colspan: 2, ng: { click: 'showBought=!showBought' } }
%h5.brick
%i{ ng: { class: "{ 'ofn-i_007-caret-right': !showBought, 'ofn-i_005-caret-down': showBought}"} }
= t(:orders_bought_items_notice, count: @order.finalised_line_items.sum(&:quantity))
= t(:orders_bought_items_notice, count: @order.finalised_line_items.to_a.sum(&:quantity))
%td.text-right{ colspan: 3 }
%a.edit-finalised.button.radius.expand.small{ href: changeable_orders_link_path, ng: { class: "{secondary: !showBought, primary: showBought}" } }
= t(:orders_bought_edit_button)

View File

@@ -2,9 +2,14 @@ module OpenFoodNetwork
class AvailablePaymentMethodFilter
def filter!(payment_methods)
if stripe_enabled?
payment_methods.reject!{ |p| p.type.ends_with?("StripeConnect") && stripe_configuration_incomplete?(p) }
payment_methods.to_a.reject! do |payment_method|
payment_method.type.ends_with?("StripeConnect") &&
stripe_configuration_incomplete?(payment_method)
end
else
payment_methods.reject!{ |p| p.type.ends_with?("StripeConnect") }
payment_methods.to_a.reject! do |payment_method|
payment_method.type.ends_with?("StripeConnect")
end
end
end

View File

@@ -61,7 +61,7 @@ module OpenFoodNetwork
proc { |_line_items| "" },
proc { |_line_items| I18n.t('admin.reports.total_items') },
proc { |_line_items| "" },
proc { |line_items| line_items.sum(&:quantity) },
proc { |line_items| line_items.to_a.sum(&:quantity) },
proc { |_line_items| "" }] },
{ group_by: proc { |line_item| line_item.product.supplier },
sort_by: proc { |supplier| supplier.name } },
@@ -82,7 +82,7 @@ module OpenFoodNetwork
proc { |_line_items| "" },
proc { |_line_items| I18n.t('admin.reports.total_items') },
proc { |_line_items| "" },
proc { |line_items| line_items.sum(&:quantity) },
proc { |line_items| line_items.to_a.sum(&:quantity) },
proc { |_line_items| "" }] },
{ group_by: proc { |line_item| line_item.product },
sort_by: proc { |product| product.name } },
@@ -102,7 +102,7 @@ module OpenFoodNetwork
proc { |line_items| line_items.first.product.supplier.name },
proc { |line_items| line_items.first.product.name },
proc { |line_items| line_items.first.full_name },
proc { |line_items| line_items.sum(&:quantity) },
proc { |line_items| line_items.to_a.sum(&:quantity) },
proc { |line_items| is_temperature_controlled?(line_items.first) }]
else
[
@@ -113,7 +113,7 @@ module OpenFoodNetwork
proc { |line_items| line_items.first.order.bill_address.lastname },
proc { |line_items| line_items.first.product.name },
proc { |line_items| line_items.first.full_name },
proc { |line_items| line_items.sum(&:quantity) },
proc { |line_items| line_items.to_a.sum(&:quantity) },
proc { |line_items| is_temperature_controlled?(line_items.first) }
]
end

View File

@@ -14,7 +14,7 @@ module OpenFoodNetwork
def filter!(subject)
return unless subject.respond_to?(:any?) && subject.any?
subject.reject! do |element|
subject.to_a.reject! do |element|
if rule_class.respond_to?(:tagged_children_for)
children = rule_class.tagged_children_for(element)
children.reject! { |child| reject?(child) }

View File

@@ -6,7 +6,7 @@ module OpenFoodNetwork
end
def balance
-completed_orders.sum(&:outstanding_balance)
-completed_orders.to_a.sum(&:outstanding_balance)
end
private

View File

@@ -180,11 +180,11 @@ module OpenFoodNetwork
end
def total_untaxable_products(order)
order.line_items.without_tax.sum(&:amount)
order.line_items.without_tax.to_a.sum(&:amount)
end
def total_taxable_products(order)
order.line_items.with_tax.sum(&:amount)
order.line_items.with_tax.to_a.sum(&:amount)
end
def total_untaxable_fees(order)

View File

@@ -133,7 +133,7 @@ feature '
expect(page).to have_selector "a.view-variants", count: 1
all("a.view-variants").each(&:click)
expect(page).to have_selector "span[name='on_hand']", text: p1.variants.sum(&:on_hand).to_s
expect(page).to have_selector "span[name='on_hand']", text: p1.variants.to_a.sum(&:on_hand).to_s
expect(page).to have_field "variant_on_hand", with: "15"
expect(page).to have_field "variant_on_hand", with: "6"
end

View File

@@ -11,7 +11,7 @@ describe InjectionHelper, type: :helper do
let!(:d2o1) { create(:completed_order_with_totals, distributor: distributor2, user_id: user.id) }
it "will inject via AMS" do
expect(helper.inject_json_ams("test", [enterprise], Api::IdSerializer)).to match /#{enterprise.id}/
expect(helper.inject_json_array("test", [enterprise], Api::IdSerializer)).to match /#{enterprise.id}/
end
it "injects enterprises" do

View File

@@ -1,4 +1,5 @@
require 'open_food_network/tag_rule_applicator'
require 'spec_helper'
module OpenFoodNetwork
describe TagRuleApplicator do

View File

@@ -420,7 +420,7 @@ module Spree
allow(li).to receive(:price) { 55.55 }
allow(li).to receive_message_chain(:order, :adjustments, :loaded?)
allow(li).to receive_message_chain(:order, :adjustments, :select)
allow(li).to receive_message_chain(:order, :adjustments, :where, :sum) { 11.11 }
allow(li).to receive_message_chain(:order, :adjustments, :where, :to_a, :sum) { 11.11 }
allow(li).to receive(:quantity) { 2 }
expect(li.price_with_adjustments).to eq(61.11)
end
@@ -433,7 +433,7 @@ module Spree
allow(li).to receive(:price) { 55.55 }
allow(li).to receive_message_chain(:order, :adjustments, :loaded?)
allow(li).to receive_message_chain(:order, :adjustments, :select)
allow(li).to receive_message_chain(:order, :adjustments, :where, :sum) { 11.11 }
allow(li).to receive_message_chain(:order, :adjustments, :where, :to_a, :sum) { 11.11 }
allow(li).to receive(:quantity) { 2 }
expect(li.amount_with_adjustments).to eq(122.22)
end