chore: rubocop

chore: fix api test conventions

chore: rubocop -A spec/

chore: more rubocop -A

fix failing test

rubocop fixes

removes helper methods that are in my opinion dead code

more rubocop fixes

rubocop -a --auto-gen-config
This commit is contained in:
Philipp Rothmann 2023-05-12 13:01:12 +02:00 committed by Philipp Rothmann
parent f6fb804bbe
commit fb2b4d8a8a
331 changed files with 4263 additions and 4507 deletions

View file

@ -2,29 +2,29 @@ class Order < ApplicationRecord
attr_accessor :ignore_warnings, :transport_distribution
# Associations
has_many :order_articles, :dependent => :destroy
has_many :articles, :through => :order_articles
has_many :group_orders, :dependent => :destroy
has_many :ordergroups, :through => :group_orders
has_many :users_ordered, :through => :ordergroups, :source => :users
has_many :comments, -> { order('created_at') }, :class_name => "OrderComment"
has_many :order_articles, dependent: :destroy
has_many :articles, through: :order_articles
has_many :group_orders, dependent: :destroy
has_many :ordergroups, through: :group_orders
has_many :users_ordered, through: :ordergroups, source: :users
has_many :comments, -> { order('created_at') }, class_name: 'OrderComment'
has_many :stock_changes
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'
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'
enum end_action: { no_end_action: 0, auto_close: 1, auto_close_and_send: 2, auto_close_and_send_min_quantity: 3 }
enum transport_distribution: [:skip, :ordergroup, :price, :articles]
enum transport_distribution: { skip: 0, ordergroup: 1, price: 2, articles: 3 }
# Validations
validates_presence_of :starts
validates :starts, presence: true
validate :starts_before_ends, :include_articles
validate :keep_ordered_articles
before_validation :distribute_transport
# Callbacks
after_save :save_order_articles, :update_price_of_group_orders!
before_validation :distribute_transport
# Finders
scope :started, -> { where('starts <= ?', Time.now) }
@ -49,12 +49,12 @@ class Order < ApplicationRecord
include DateTimeAttributeValidate
date_time_attribute :starts, :boxfill, :ends
def self.ransackable_attributes(auth_object = nil)
%w(id state supplier_id starts boxfill ends pickup)
def self.ransackable_attributes(_auth_object = nil)
%w[id state supplier_id starts boxfill ends pickup]
end
def self.ransackable_associations(auth_object = nil)
%w(supplier articles order_articles)
def self.ransackable_associations(_auth_object = nil)
%w[supplier articles order_articles]
end
def stockit?
@ -70,9 +70,9 @@ class Order < ApplicationRecord
# make sure to include those articles which are no longer available
# but which have already been ordered in this stock order
StockArticle.available.includes(:article_category)
.order('article_categories.name', 'articles.name').reject { |a|
.order('article_categories.name', 'articles.name').reject do |a|
a.quantity_available <= 0 && !a.ordered_in_order?(self)
}.group_by { |a| a.article_category.name }
end.group_by { |a| a.article_category.name }
else
supplier.articles.available.group_by { |a| a.article_category.name }
end
@ -87,9 +87,7 @@ class Order < ApplicationRecord
end
# Save ids, and create/delete order_articles after successfully saved the order
def article_ids=(ids)
@article_ids = ids
end
attr_writer :article_ids
def article_ids
@article_ids ||= order_articles.map { |a| a.article_id.to_s }
@ -101,19 +99,19 @@ class Order < ApplicationRecord
end
def open?
state == "open"
state == 'open'
end
def finished?
state == "finished" || state == "received"
state == 'finished' || state == 'received'
end
def received?
state == "received"
state == 'received'
end
def closed?
state == "closed"
state == 'closed'
end
def boxfill?
@ -134,11 +132,18 @@ class Order < ApplicationRecord
self.starts ||= Time.now
if FoodsoftConfig[:order_schedule]
# try to be smart when picking a reference day
last = (DateTime.parse(FoodsoftConfig[:order_schedule][:initial]) rescue nil)
last = begin
DateTime.parse(FoodsoftConfig[:order_schedule][:initial])
rescue StandardError
nil
end
last ||= Order.finished.reorder(:starts).first.try(:starts)
last ||= self.starts
# adjust boxfill and end date
self.boxfill ||= FoodsoftDateUtil.next_occurrence last, self.starts, FoodsoftConfig[:order_schedule][:boxfill] if is_boxfill_useful?
if is_boxfill_useful?
self.boxfill ||= FoodsoftDateUtil.next_occurrence last, self.starts,
FoodsoftConfig[:order_schedule][:boxfill]
end
self.ends ||= FoodsoftDateUtil.next_occurrence last, self.starts, FoodsoftConfig[:order_schedule][:ends]
end
self
@ -149,7 +154,7 @@ class Order < ApplicationRecord
def self.ordergroup_group_orders_map(ordergroup)
orders = includes(:supplier)
group_orders = GroupOrder.where(ordergroup_id: ordergroup.id, order_id: orders.map(&:id))
group_orders_hash = Hash[group_orders.collect { |go| [go.order_id, go] }]
group_orders_hash = group_orders.index_by { |go| go.order_id }
orders.map do |order|
{
order: order,
@ -160,11 +165,11 @@ class Order < ApplicationRecord
# search GroupOrder of given Ordergroup
def group_order(ordergroup)
group_orders.where(:ordergroup_id => ordergroup.id).first
group_orders.where(ordergroup_id: ordergroup.id).first
end
def stock_group_order
group_orders.where(:ordergroup_id => nil).first
group_orders.where(ordergroup_id: nil).first
end
# Returns OrderArticles in a nested Array, grouped by category and ordered by article name.
@ -172,7 +177,7 @@ class Order < ApplicationRecord
# e.g: [["drugs",[teethpaste, toiletpaper]], ["fruits" => [apple, banana, lemon]]]
def articles_grouped_by_category
@articles_grouped_by_category ||= order_articles
.includes([:article_price, :group_order_articles, :article => :article_category])
.includes([:article_price, :group_order_articles, { article: :article_category }])
.order('articles.name')
.group_by { |a| a.article.article_category.name }
.sort { |a, b| a[0] <=> b[0] }
@ -189,10 +194,10 @@ class Order < ApplicationRecord
# FIXME: Consider order.foodcoop_result
def profit(options = {})
markup = options[:without_markup] || false
if invoice
groups_sum = markup ? sum(:groups_without_markup) : sum(:groups)
groups_sum - invoice.net_amount
end
return unless invoice
groups_sum = markup ? sum(:groups_without_markup) : sum(:groups)
groups_sum - invoice.net_amount
end
# Returns the all round price of a finished order
@ -202,7 +207,7 @@ class Order < ApplicationRecord
# :fc, guess what...
def sum(type = :gross)
total = 0
if type == :net || type == :gross || type == :fc
if %i[net gross fc].include?(type)
for oa in order_articles.ordered.includes(:article, :article_price)
quantity = oa.units * oa.price.unit_quantity
case type
@ -214,8 +219,8 @@ class Order < ApplicationRecord
total += quantity * oa.price.fc_price
end
end
elsif type == :groups || type == :groups_without_markup
for go in group_orders.includes(group_order_articles: { order_article: [:article, :article_price] })
elsif %i[groups groups_without_markup].include?(type)
for go in group_orders.includes(group_order_articles: { order_article: %i[article article_price] })
for goa in go.group_order_articles
case type
when :groups
@ -232,36 +237,36 @@ class Order < ApplicationRecord
# Finishes this order. This will set the order state to "finish" and the end property to the current time.
# Ignored if the order is already finished.
def finish!(user)
unless finished?
Order.transaction do
# set new order state (needed by notify_order_finished)
update!(state: 'finished', ends: Time.now, updated_by: user)
return if finished?
# Update order_articles. Save the current article_price to keep price consistency
# Also save results for each group_order_result
# Clean up
order_articles.includes(:article).each do |oa|
oa.update_attribute(:article_price, oa.article.article_prices.first)
oa.group_order_articles.each do |goa|
goa.save_results!
# Delete no longer required order-history (group_order_article_quantities) and
# TODO: Do we need articles, which aren't ordered? (units_to_order == 0 ?)
# A: Yes, we do - for redistributing articles when the number of articles
# delivered changes, and for statistics on popular articles. Records
# with both tolerance and quantity zero can be deleted.
# goa.group_order_article_quantities.clear
end
Order.transaction do
# set new order state (needed by notify_order_finished)
update!(state: 'finished', ends: Time.now, updated_by: user)
# Update order_articles. Save the current article_price to keep price consistency
# Also save results for each group_order_result
# Clean up
order_articles.includes(:article).find_each do |oa|
oa.update_attribute(:article_price, oa.article.article_prices.first)
oa.group_order_articles.each do |goa|
goa.save_results!
# Delete no longer required order-history (group_order_article_quantities) and
# TODO: Do we need articles, which aren't ordered? (units_to_order == 0 ?)
# A: Yes, we do - for redistributing articles when the number of articles
# delivered changes, and for statistics on popular articles. Records
# with both tolerance and quantity zero can be deleted.
# goa.group_order_article_quantities.clear
end
# Update GroupOrder prices
group_orders.each(&:update_price!)
# Stats
ordergroups.each(&:update_stats!)
# Notifications
NotifyFinishedOrderJob.perform_later(self)
end
# Update GroupOrder prices
group_orders.each(&:update_price!)
# Stats
ordergroups.each(&:update_stats!)
# Notifications
NotifyFinishedOrderJob.perform_later(self)
end
end
@ -277,11 +282,11 @@ class Order < ApplicationRecord
if stockit? # Decreases the quantity of stock_articles
for oa in order_articles.includes(:article)
oa.update_results! # Update units_to_order of order_article
stock_changes.create! :stock_article => oa.article, :quantity => oa.units_to_order * -1
stock_changes.create! stock_article: oa.article, quantity: oa.units_to_order * -1
end
end
self.update!(state: 'closed', updated_by: user, foodcoop_result: profit)
update!(state: 'closed', updated_by: user, foodcoop_result: profit)
end
end
@ -289,7 +294,10 @@ class Order < ApplicationRecord
def close_direct!(user)
raise I18n.t('orders.model.error_closed') if closed?
comments.create(user: user, text: I18n.t('orders.model.close_direct_message')) unless FoodsoftConfig[:charge_members_manually]
unless FoodsoftConfig[:charge_members_manually]
comments.create(user: user,
text: I18n.t('orders.model.close_direct_message'))
end
update!(state: 'closed', updated_by: user)
end
@ -313,13 +321,12 @@ class Order < ApplicationRecord
end
def self.finish_ended!
orders = Order.where.not(end_action: Order.end_actions[:no_end_action]).where(state: 'open').where('ends <= ?', DateTime.now)
orders = Order.where.not(end_action: Order.end_actions[:no_end_action]).where(state: 'open').where('ends <= ?',
DateTime.now)
orders.each do |order|
begin
order.do_end_action!
rescue => error
ExceptionNotifier.notify_exception(error, data: { foodcoop: FoodsoftConfig.scope, order_id: order.id })
end
order.do_end_action!
rescue StandardError => e
ExceptionNotifier.notify_exception(e, data: { foodcoop: FoodsoftConfig.scope, order_id: order.id })
end
end
@ -329,7 +336,10 @@ class Order < ApplicationRecord
delta = Rails.env.test? ? 1 : 0 # since Rails 4.2 tests appear to have time differences, with this validation failing
errors.add(:ends, I18n.t('orders.model.error_starts_before_ends')) if ends && starts && ends <= (starts - delta)
errors.add(:ends, I18n.t('orders.model.error_boxfill_before_ends')) if ends && boxfill && ends <= (boxfill - delta)
errors.add(:boxfill, I18n.t('orders.model.error_starts_before_boxfill')) if boxfill && starts && boxfill <= (starts - delta)
return unless boxfill && starts && boxfill <= (starts - delta)
errors.add(:boxfill,
I18n.t('orders.model.error_starts_before_boxfill'))
end
def include_articles
@ -340,17 +350,17 @@ class Order < ApplicationRecord
chosen_order_articles = order_articles.where(article_id: article_ids)
to_be_removed = order_articles - chosen_order_articles
to_be_removed_but_ordered = to_be_removed.select { |a| a.quantity > 0 || a.tolerance > 0 }
unless to_be_removed_but_ordered.empty? || ignore_warnings
errors.add(:articles, I18n.t(stockit? ? 'orders.model.warning_ordered_stock' : 'orders.model.warning_ordered'))
@erroneous_article_ids = to_be_removed_but_ordered.map { |a| a.article_id }
end
return if to_be_removed_but_ordered.empty? || ignore_warnings
errors.add(:articles, I18n.t(stockit? ? 'orders.model.warning_ordered_stock' : 'orders.model.warning_ordered'))
@erroneous_article_ids = to_be_removed_but_ordered.map { |a| a.article_id }
end
def save_order_articles
# fetch selected articles
articles_list = Article.find(article_ids)
# create new order_articles
(articles_list - articles).each { |article| order_articles.create(:article => article) }
(articles_list - articles).each { |article| order_articles.create(article: article) }
# delete old order_articles
articles.reject { |article| articles_list.include?(article) }.each do |article|
order_articles.detect { |order_article| order_article.article_id == article.id }.destroy
@ -363,17 +373,17 @@ class Order < ApplicationRecord
return unless group_orders.any?
case transport_distribution.try(&:to_i)
when Order.transport_distributions[:ordergroup] then
when Order.transport_distributions[:ordergroup]
amount = transport / group_orders.size
group_orders.each do |go|
go.transport = amount.ceil(2)
end
when Order.transport_distributions[:price] then
when Order.transport_distributions[:price]
amount = transport / group_orders.sum(:price)
group_orders.each do |go|
go.transport = (amount * go.price).ceil(2)
end
when Order.transport_distributions[:articles] then
when Order.transport_distributions[:articles]
amount = transport / group_orders.includes(:group_order_articles).sum(:result)
group_orders.each do |go|
go.transport = (amount * go.group_order_articles.sum(:result)).ceil(2)
@ -389,7 +399,7 @@ class Order < ApplicationRecord
def charge_group_orders!(user, transaction_type = nil)
note = transaction_note
group_orders.includes(:ordergroup).each do |group_order|
group_orders.includes(:ordergroup).find_each do |group_order|
if group_order.ordergroup
price = group_order.total * -1 # decrease! account balance
group_order.ordergroup.add_financial_transaction!(price, note, user, transaction_type, nil, group_order)