From 44a198c7bcebe3a79bee1eb612975603beafd42f Mon Sep 17 00:00:00 2001 From: Patrick Gansterer Date: Sat, 1 Aug 2020 02:49:15 +0200 Subject: [PATCH] Adopt Rails 5 belongs_to_required_by_default --- app/controllers/orders_controller.rb | 1 + app/models/bank_transaction.rb | 6 +++--- app/models/delivery.rb | 2 +- app/models/financial_transaction.rb | 8 ++++---- app/models/financial_transaction_type.rb | 2 +- app/models/group_order.rb | 4 ++-- app/models/invoice.rb | 2 +- app/models/order.rb | 4 ++-- app/models/order_article.rb | 2 +- app/models/stock_change.rb | 6 +++--- app/models/supplier.rb | 2 +- app/models/task.rb | 4 ++-- config/initializers/new_framework_defaults.rb | 3 --- plugins/links/app/models/link.rb | 2 +- plugins/messages/app/models/message.rb | 6 +++--- plugins/polls/app/models/poll_vote.rb | 2 +- plugins/printer/app/models/printer_job.rb | 2 +- spec/factories/group_order.rb | 1 + spec/factories/invoice.rb | 1 + spec/factories/order.rb | 2 ++ spec/factories/supplier.rb | 11 +++++++++++ spec/integration/supplier_spec.rb | 1 + spec/models/order_spec.rb | 17 +++++++++-------- 23 files changed, 53 insertions(+), 38 deletions(-) diff --git a/app/controllers/orders_controller.rb b/app/controllers/orders_controller.rb index 038d7c62..55108e4c 100644 --- a/app/controllers/orders_controller.rb +++ b/app/controllers/orders_controller.rb @@ -76,6 +76,7 @@ class OrdersController < ApplicationController def create @order = Order.new(params[:order]) @order.created_by = current_user + @order.updated_by = current_user if @order.save flash[:notice] = I18n.t('orders.create.notice') redirect_to @order diff --git a/app/models/bank_transaction.rb b/app/models/bank_transaction.rb index 5cb7b447..877060b8 100644 --- a/app/models/bank_transaction.rb +++ b/app/models/bank_transaction.rb @@ -18,9 +18,9 @@ class BankTransaction < ApplicationRecord # @return [Binary] Optional PNG image for e.g. scan of paper receipt. belongs_to :bank_account - belongs_to :financial_link - belongs_to :supplier, foreign_key: 'iban', primary_key: 'iban' - belongs_to :user, foreign_key: 'iban', primary_key: 'iban' + belongs_to :financial_link, optional: true + belongs_to :supplier, optional: true, foreign_key: 'iban', primary_key: 'iban' + belongs_to :user, optional: true, foreign_key: 'iban', primary_key: 'iban' validates_presence_of :date, :amount, :bank_account_id validates_numericality_of :amount diff --git a/app/models/delivery.rb b/app/models/delivery.rb index b4227317..c5bf7958 100644 --- a/app/models/delivery.rb +++ b/app/models/delivery.rb @@ -1,7 +1,7 @@ class Delivery < StockEvent belongs_to :supplier - belongs_to :invoice + belongs_to :invoice, optional: true scope :recent, -> { order('created_at DESC').limit(10) } diff --git a/app/models/financial_transaction.rb b/app/models/financial_transaction.rb index 040f5765..4979a415 100644 --- a/app/models/financial_transaction.rb +++ b/app/models/financial_transaction.rb @@ -1,12 +1,12 @@ # financial transactions are the foodcoop internal financial transactions # only ordergroups have an account balance and are happy to transfer money class FinancialTransaction < ApplicationRecord - belongs_to :ordergroup + belongs_to :ordergroup, optional: true belongs_to :user - belongs_to :financial_link + belongs_to :financial_link, optional: true belongs_to :financial_transaction_type - belongs_to :group_order - belongs_to :reverts, class_name: 'FinancialTransaction', foreign_key: 'reverts_id' + belongs_to :group_order, optional: true + belongs_to :reverts, optional: true, class_name: 'FinancialTransaction', foreign_key: 'reverts_id' has_one :reverted_by, class_name: 'FinancialTransaction', foreign_key: 'reverts_id' validates_presence_of :amount, :note, :user_id diff --git a/app/models/financial_transaction_type.rb b/app/models/financial_transaction_type.rb index c6d91a0b..62ccf3ba 100644 --- a/app/models/financial_transaction_type.rb +++ b/app/models/financial_transaction_type.rb @@ -1,6 +1,6 @@ class FinancialTransactionType < ApplicationRecord belongs_to :financial_transaction_class - belongs_to :bank_account + belongs_to :bank_account, optional: true has_many :financial_transactions, dependent: :restrict_with_exception validates :name, presence: true diff --git a/app/models/group_order.rb b/app/models/group_order.rb index b29322ce..626f3fbd 100644 --- a/app/models/group_order.rb +++ b/app/models/group_order.rb @@ -5,11 +5,11 @@ class GroupOrder < ApplicationRecord attr_accessor :group_order_articles_attributes belongs_to :order - belongs_to :ordergroup + belongs_to :ordergroup, optional: true has_many :group_order_articles, :dependent => :destroy has_many :order_articles, :through => :group_order_articles has_one :financial_transaction - belongs_to :updated_by, :class_name => "User", :foreign_key => "updated_by_user_id" + belongs_to :updated_by, optional: true, class_name: 'User', foreign_key: 'updated_by_user_id' validates_presence_of :order_id validates_numericality_of :price diff --git a/app/models/invoice.rb b/app/models/invoice.rb index c102a3ea..a5c04e97 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -3,7 +3,7 @@ class Invoice < ApplicationRecord belongs_to :supplier belongs_to :created_by, :class_name => 'User', :foreign_key => 'created_by_user_id' - belongs_to :financial_link + belongs_to :financial_link, optional: true has_many :deliveries, dependent: :nullify has_many :orders, dependent: :nullify diff --git a/app/models/order.rb b/app/models/order.rb index b0d9534e..fb8c1d69 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -11,8 +11,8 @@ class Order < ApplicationRecord has_many :users_ordered, :through => :ordergroups, :source => :users has_many :comments, -> { order('created_at') }, :class_name => "OrderComment" has_many :stock_changes - belongs_to :invoice - belongs_to :supplier + belongs_to :invoice, optional: true + belongs_to :supplier, optional: true belongs_to :updated_by, :class_name => 'User', :foreign_key => 'updated_by_user_id' belongs_to :created_by, :class_name => 'User', :foreign_key => 'created_by_user_id' diff --git a/app/models/order_article.rb b/app/models/order_article.rb index 743ead39..a238e7a0 100644 --- a/app/models/order_article.rb +++ b/app/models/order_article.rb @@ -6,7 +6,7 @@ class OrderArticle < ApplicationRecord belongs_to :order belongs_to :article - belongs_to :article_price + belongs_to :article_price, optional: true has_many :group_order_articles, :dependent => :destroy validates_presence_of :order_id, :article_id diff --git a/app/models/stock_change.rb b/app/models/stock_change.rb index 7959864b..4cbd8939 100644 --- a/app/models/stock_change.rb +++ b/app/models/stock_change.rb @@ -1,7 +1,7 @@ class StockChange < ApplicationRecord - belongs_to :delivery, foreign_key: 'stock_event_id' - belongs_to :order - belongs_to :stock_taking, foreign_key: 'stock_event_id' + belongs_to :delivery, optional: true, foreign_key: 'stock_event_id' + belongs_to :order, optional: true + belongs_to :stock_taking, optional: true, foreign_key: 'stock_event_id' belongs_to :stock_article validates_presence_of :stock_article_id, :quantity diff --git a/app/models/supplier.rb b/app/models/supplier.rb index 03abc236..6a9b3f1d 100644 --- a/app/models/supplier.rb +++ b/app/models/supplier.rb @@ -9,7 +9,7 @@ class Supplier < ApplicationRecord has_many :deliveries has_many :invoices belongs_to :supplier_category - belongs_to :shared_supplier # for the sharedLists-App + belongs_to :shared_supplier, optional: true # for the sharedLists-App validates :name, :presence => true, :length => { :in => 4..30 } validates :phone, :presence => true, :length => { :in => 8..25 } diff --git a/app/models/task.rb b/app/models/task.rb index b36da935..0bbddc66 100644 --- a/app/models/task.rb +++ b/app/models/task.rb @@ -2,8 +2,8 @@ class Task < ApplicationRecord has_many :assignments, :dependent => :destroy has_many :users, :through => :assignments - belongs_to :workgroup - belongs_to :periodic_task_group + belongs_to :workgroup, optional: true + belongs_to :periodic_task_group, optional: true belongs_to :created_by, :class_name => 'User', :foreign_key => 'created_by_user_id' scope :non_group, -> { where(workgroup_id: nil, done: false) } diff --git a/config/initializers/new_framework_defaults.rb b/config/initializers/new_framework_defaults.rb index a2bfed30..fac64e0a 100644 --- a/config/initializers/new_framework_defaults.rb +++ b/config/initializers/new_framework_defaults.rb @@ -15,6 +15,3 @@ Rails.application.config.action_controller.forgery_protection_origin_check = fal # Make Ruby 2.4 preserve the timezone of the receiver when calling `to_time`. # Previous versions had false. ActiveSupport.to_time_preserves_timezone = false - -# Require `belongs_to` associations by default. Previous versions had false. -Rails.application.config.active_record.belongs_to_required_by_default = false diff --git a/plugins/links/app/models/link.rb b/plugins/links/app/models/link.rb index 638226c8..3a8b57a9 100644 --- a/plugins/links/app/models/link.rb +++ b/plugins/links/app/models/link.rb @@ -1,5 +1,5 @@ class Link < ApplicationRecord - belongs_to :workgroup + belongs_to :workgroup, optional: true validates_presence_of :name, :url diff --git a/plugins/messages/app/models/message.rb b/plugins/messages/app/models/message.rb index 5fd18578..69b89738 100644 --- a/plugins/messages/app/models/message.rb +++ b/plugins/messages/app/models/message.rb @@ -1,9 +1,9 @@ require "base32" class Message < ApplicationRecord - belongs_to :sender, :class_name => "User", :foreign_key => "sender_id" - belongs_to :group, :class_name => "Group", :foreign_key => "group_id" - belongs_to :reply_to_message, :class_name => "Message", :foreign_key => "reply_to" + belongs_to :sender, class_name: 'User', foreign_key: 'sender_id' + belongs_to :group, optional: true, class_name: 'Group', foreign_key: 'group_id' + belongs_to :reply_to_message, optional: true, class_name: 'Message', foreign_key: 'reply_to' has_many :message_recipients, dependent: :destroy has_many :recipients, through: :message_recipients, source: :user diff --git a/plugins/polls/app/models/poll_vote.rb b/plugins/polls/app/models/poll_vote.rb index f7e6f73c..c43eb66c 100644 --- a/plugins/polls/app/models/poll_vote.rb +++ b/plugins/polls/app/models/poll_vote.rb @@ -1,6 +1,6 @@ class PollVote < ActiveRecord::Base belongs_to :poll - belongs_to :ordergroup + belongs_to :ordergroup, optional: true belongs_to :user has_many :poll_choices, dependent: :destroy end diff --git a/plugins/printer/app/models/printer_job.rb b/plugins/printer/app/models/printer_job.rb index 3f8914d9..fc1fb6ce 100644 --- a/plugins/printer/app/models/printer_job.rb +++ b/plugins/printer/app/models/printer_job.rb @@ -2,7 +2,7 @@ class PrinterJob < ActiveRecord::Base belongs_to :order belongs_to :created_by, class_name: 'User', foreign_key: 'created_by_user_id' - belongs_to :finished_by, class_name: 'User', foreign_key: 'finished_by_user_id' + belongs_to :finished_by, optional: true, class_name: 'User', foreign_key: 'finished_by_user_id' has_many :printer_job_updates scope :finished, -> { where.not(finished_at: nil) } diff --git a/spec/factories/group_order.rb b/spec/factories/group_order.rb index 30445b16..c1f0fc9d 100644 --- a/spec/factories/group_order.rb +++ b/spec/factories/group_order.rb @@ -5,6 +5,7 @@ FactoryBot.define do # requires order factory :group_order do ordergroup { create(:user, groups: [FactoryBot.create(:ordergroup)]).ordergroup } + updated_by { create :user } end end diff --git a/spec/factories/invoice.rb b/spec/factories/invoice.rb index d43af28a..b52bfea8 100644 --- a/spec/factories/invoice.rb +++ b/spec/factories/invoice.rb @@ -6,6 +6,7 @@ FactoryBot.define do supplier number { rand(1..99999) } amount { rand(0.1..26.0).round(2) } + created_by { create :user } after :create do |invoice| invoice.supplier.reload diff --git a/spec/factories/order.rb b/spec/factories/order.rb index 64d15f75..9d7f6557 100644 --- a/spec/factories/order.rb +++ b/spec/factories/order.rb @@ -6,6 +6,8 @@ FactoryBot.define do starts { Time.now } supplier { create :supplier, article_count: (article_count.nil? ? true : article_count) } article_ids { supplier.articles.map(&:id) unless supplier.nil? } + created_by { create :user } + updated_by { create :user } transient do article_count { true } diff --git a/spec/factories/supplier.rb b/spec/factories/supplier.rb index 290fbb69..c46d1345 100644 --- a/spec/factories/supplier.rb +++ b/spec/factories/supplier.rb @@ -11,6 +11,12 @@ FactoryBot.define do article_count { 0 } end + before :create do |supplier, evaluator| + next if supplier.class == SharedSupplier + next if supplier.supplier_category_id? + supplier.supplier_category = create :supplier_category + end + after :create do |supplier, evaluator| article_count = evaluator.article_count article_count = rand(1..99) if article_count == true @@ -20,4 +26,9 @@ FactoryBot.define do factory :shared_supplier, class: SharedSupplier end + factory :supplier_category do + sequence(:name) { |n| Faker::Lorem.characters(number: rand(2..12)) + " ##{n}" } + financial_transaction_class + end + end diff --git a/spec/integration/supplier_spec.rb b/spec/integration/supplier_spec.rb index 702906f2..10afb6e6 100644 --- a/spec/integration/supplier_spec.rb +++ b/spec/integration/supplier_spec.rb @@ -9,6 +9,7 @@ feature 'supplier' do before { login user } it 'can be created' do + create :supplier_category visit suppliers_path click_on I18n.t('suppliers.index.action_new') supplier = build :supplier diff --git a/spec/models/order_spec.rb b/spec/models/order_spec.rb index a15c0e84..61f1c101 100644 --- a/spec/models/order_spec.rb +++ b/spec/models/order_spec.rb @@ -1,12 +1,13 @@ require_relative '../spec_helper' describe Order do + let(:user) { create :user, groups: [create(:ordergroup)] } it 'automaticly finishes ended' do - create :order, created_by: User.first, starts: Date.yesterday, ends: 1.hour.from_now - create :order, created_by: User.first, starts: Date.yesterday, ends: 1.hour.ago - create :order, created_by: User.first, starts: Date.yesterday, ends: 1.hour.from_now, end_action: :auto_close - order = create :order, created_by: User.first, starts: Date.yesterday, ends: 1.hour.ago, end_action: :auto_close + create :order, created_by: user, starts: Date.yesterday, ends: 1.hour.from_now + create :order, created_by: user, starts: Date.yesterday, ends: 1.hour.ago + create :order, created_by: user, starts: Date.yesterday, ends: 1.hour.from_now, end_action: :auto_close + order = create :order, created_by: user, starts: Date.yesterday, ends: 1.hour.ago, end_action: :auto_close Order.finish_ended! order.reload @@ -18,7 +19,7 @@ describe Order do it 'sends mail if min_order_quantity has been reached' do create :user, groups: [create(:ordergroup)] - create :order, created_by: User.first, starts: Date.yesterday, ends: 1.hour.ago, end_action: :auto_close_and_send_min_quantity + create :order, created_by: user, starts: Date.yesterday, ends: 1.hour.ago, end_action: :auto_close_and_send_min_quantity Order.finish_ended! expect(ActionMailer::Base.deliveries.count).to eq 1 @@ -50,7 +51,7 @@ describe Order do it 'can be finished' do # TODO randomise user - order.finish!(User.first) + order.finish!(user) expect(order).to_not be_open expect(order).to be_finished expect(order).to_not be_closed @@ -58,8 +59,8 @@ describe Order do it 'can be closed' do # TODO randomise user - order.finish!(User.first) - order.close!(User.first) + order.finish!(user) + order.close!(user) expect(order).to_not be_open expect(order).to be_closed end