Adopt Rails 5 belongs_to_required_by_default

This commit is contained in:
Patrick Gansterer 2020-08-01 02:49:15 +02:00
parent 2557645f4f
commit 44a198c7bc
23 changed files with 53 additions and 38 deletions

View File

@ -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

View File

@ -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

View File

@ -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) }

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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'

View File

@ -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

View File

@ -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

View File

@ -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 }

View File

@ -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) }

View File

@ -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

View File

@ -1,5 +1,5 @@
class Link < ApplicationRecord
belongs_to :workgroup
belongs_to :workgroup, optional: true
validates_presence_of :name, :url

View File

@ -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

View File

@ -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

View File

@ -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) }

View File

@ -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

View File

@ -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

View File

@ -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 }

View File

@ -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

View File

@ -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

View File

@ -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