diff --git a/.rubocop_manual_todo.yml b/.rubocop_manual_todo.yml index b9cbfff18b..f68559cd67 100644 --- a/.rubocop_manual_todo.yml +++ b/.rubocop_manual_todo.yml @@ -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 diff --git a/app/controllers/admin/enterprises_controller.rb b/app/controllers/admin/enterprises_controller.rb index f1b4887f6a..ac2762cbf0 100644 --- a/app/controllers/admin/enterprises_controller.rb +++ b/app/controllers/admin/enterprises_controller.rb @@ -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 diff --git a/app/controllers/spree/admin/payment_methods_controller.rb b/app/controllers/spree/admin/payment_methods_controller.rb index 72408fef26..acd8de697b 100644 --- a/app/controllers/spree/admin/payment_methods_controller.rb +++ b/app/controllers/spree/admin/payment_methods_controller.rb @@ -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 diff --git a/app/controllers/spree/admin/shipping_methods_controller.rb b/app/controllers/spree/admin/shipping_methods_controller.rb index 14f7654177..cace3feea4 100644 --- a/app/controllers/spree/admin/shipping_methods_controller.rb +++ b/app/controllers/spree/admin/shipping_methods_controller.rb @@ -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 diff --git a/app/helpers/checkout_helper.rb b/app/helpers/checkout_helper.rb index c87abd0afb..a8d7c78264 100644 --- a/app/helpers/checkout_helper.rb +++ b/app/helpers/checkout_helper.rb @@ -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 diff --git a/app/helpers/injection_helper.rb b/app/helpers/injection_helper.rb index 17c122f0a7..7addd06135 100644 --- a/app/helpers/injection_helper.rb +++ b/app/helpers/injection_helper.rb @@ -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 } diff --git a/app/mailers/producer_mailer.rb b/app/mailers/producer_mailer.rb index 48f3c6a34d..5c61f2d0c8 100644 --- a/app/mailers/producer_mailer.rb +++ b/app/mailers/producer_mailer.rb @@ -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) diff --git a/app/models/calculator/default_tax.rb b/app/models/calculator/default_tax.rb index de888c9283..5293dfd1cb 100644 --- a/app/models/calculator/default_tax.rb +++ b/app/models/calculator/default_tax.rb @@ -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) } diff --git a/app/models/calculator/flat_percent_per_item.rb b/app/models/calculator/flat_percent_per_item.rb index ff6e222e89..b1b6a851a0 100644 --- a/app/models/calculator/flat_percent_per_item.rb +++ b/app/models/calculator/flat_percent_per_item.rb @@ -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 diff --git a/app/models/calculator/weight.rb b/app/models/calculator/weight.rb index f4f451b785..8f060bb826 100644 --- a/app/models/calculator/weight.rb +++ b/app/models/calculator/weight.rb @@ -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 diff --git a/app/models/model_set.rb b/app/models/model_set.rb index 1d5d7dbda3..8a599b4214 100644 --- a/app/models/model_set.rb +++ b/app/models/model_set.rb @@ -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 diff --git a/app/models/spree/line_item.rb b/app/models/spree/line_item.rb index 61aa00eb06..cbe411fdeb 100644 --- a/app/models/spree/line_item.rb +++ b/app/models/spree/line_item.rb @@ -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 diff --git a/app/models/spree/stock/quantifier.rb b/app/models/spree/stock/quantifier.rb index 21f1750ca0..01e9b77cb1 100644 --- a/app/models/spree/stock/quantifier.rb +++ b/app/models/spree/stock/quantifier.rb @@ -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 }) diff --git a/app/views/spree/orders/_bought.html.haml b/app/views/spree/orders/_bought.html.haml index f7030b31a8..e9cee63e7c 100644 --- a/app/views/spree/orders/_bought.html.haml +++ b/app/views/spree/orders/_bought.html.haml @@ -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) diff --git a/lib/open_food_network/available_payment_method_filter.rb b/lib/open_food_network/available_payment_method_filter.rb index 2e9af09bf8..d42d4ae51b 100644 --- a/lib/open_food_network/available_payment_method_filter.rb +++ b/lib/open_food_network/available_payment_method_filter.rb @@ -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 diff --git a/lib/open_food_network/packing_report.rb b/lib/open_food_network/packing_report.rb index 88a3582083..34c1e0e730 100644 --- a/lib/open_food_network/packing_report.rb +++ b/lib/open_food_network/packing_report.rb @@ -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 diff --git a/lib/open_food_network/tag_rule_applicator.rb b/lib/open_food_network/tag_rule_applicator.rb index fa5f1b6c49..0c01f5dd18 100644 --- a/lib/open_food_network/tag_rule_applicator.rb +++ b/lib/open_food_network/tag_rule_applicator.rb @@ -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) } diff --git a/lib/open_food_network/user_balance_calculator.rb b/lib/open_food_network/user_balance_calculator.rb index 5ce083e10f..98af1600f9 100644 --- a/lib/open_food_network/user_balance_calculator.rb +++ b/lib/open_food_network/user_balance_calculator.rb @@ -6,7 +6,7 @@ module OpenFoodNetwork end def balance - -completed_orders.sum(&:outstanding_balance) + -completed_orders.to_a.sum(&:outstanding_balance) end private diff --git a/lib/open_food_network/xero_invoices_report.rb b/lib/open_food_network/xero_invoices_report.rb index 2d6bc80f71..90a43e54c8 100644 --- a/lib/open_food_network/xero_invoices_report.rb +++ b/lib/open_food_network/xero_invoices_report.rb @@ -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) diff --git a/spec/features/admin/bulk_product_update_spec.rb b/spec/features/admin/bulk_product_update_spec.rb index cc4601385b..f0226d1a61 100644 --- a/spec/features/admin/bulk_product_update_spec.rb +++ b/spec/features/admin/bulk_product_update_spec.rb @@ -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 diff --git a/spec/helpers/injection_helper_spec.rb b/spec/helpers/injection_helper_spec.rb index b502b9b737..e7e0c396b1 100644 --- a/spec/helpers/injection_helper_spec.rb +++ b/spec/helpers/injection_helper_spec.rb @@ -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 diff --git a/spec/lib/open_food_network/tag_rule_applicator_spec.rb b/spec/lib/open_food_network/tag_rule_applicator_spec.rb index 3f9186dc16..50d8bfe32d 100644 --- a/spec/lib/open_food_network/tag_rule_applicator_spec.rb +++ b/spec/lib/open_food_network/tag_rule_applicator_spec.rb @@ -1,4 +1,5 @@ require 'open_food_network/tag_rule_applicator' +require 'spec_helper' module OpenFoodNetwork describe TagRuleApplicator do diff --git a/spec/models/spree/line_item_spec.rb b/spec/models/spree/line_item_spec.rb index b73c6b104f..a5552bc729 100644 --- a/spec/models/spree/line_item_spec.rb +++ b/spec/models/spree/line_item_spec.rb @@ -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