Skip to content

Commit

Permalink
some more updates
Browse files Browse the repository at this point in the history
  • Loading branch information
lodewiges committed Jan 2, 2025
1 parent ebe8b23 commit 0322554
Show file tree
Hide file tree
Showing 80 changed files with 178 additions and 170 deletions.
7 changes: 5 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ require:
- rubocop-performance

AllCops:
TargetRailsVersion: 6.0
TargetRubyVersion: 3.0
TargetRailsVersion: 7.0
TargetRubyVersion: 3.1
Exclude:
- 'bin/**/*'
- 'db/schema.rb'
Expand Down Expand Up @@ -105,5 +105,8 @@ Naming/MemoizedInstanceVariableName:
RSpec/FilePath:
Enabled: false

RSpec/SpecFilePathFormat:
Enabled: false

Rails/BulkChangeTable:
Enabled: false
6 changes: 3 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ gem 'browser', '~> 5.3.1'
gem 'devise', '~>4.9.2'
gem 'devise-i18n', '~>1.11.0'
gem 'factory_bot_rails', '~> 6.2.0'
gem 'faker', '~> 3.2.0'
gem 'faker', '~> 3.5.1'
gem 'font-awesome-rails', '~> 4.7.0.8'
gem 'http', '~> 5.1.1'
gem 'jbuilder', '~> 2.11.5'
Expand Down Expand Up @@ -74,10 +74,10 @@ end

group :test do
gem 'rails-controller-testing', '~> 1.0.5'
gem 'rubocop', '~> 1.50.2', require: false
gem 'rubocop', '~> 1.61', require: false
gem 'rubocop-performance'
gem 'rubocop-rails'
gem 'rubocop-rspec'
gem 'rubocop-rspec', '~> 2.25'
gem 'simplecov', require: false
gem 'simplecov-lcov', '~> 0.8.0', require: false
gem 'terminal-notifier-guard'
Expand Down
41 changes: 26 additions & 15 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ GEM
factory_bot_rails (6.2.0)
factory_bot (~> 6.2.0)
railties (>= 5.0.0)
faker (3.2.3)
faker (3.5.1)
i18n (>= 1.8.11, < 2)
faraday (2.12.2)
faraday-net_http (>= 2.0, < 3.5)
Expand Down Expand Up @@ -210,6 +210,7 @@ GEM
activerecord
kaminari-core (= 1.2.2)
kaminari-core (1.2.2)
language_server-protocol (3.17.0.3)
libv8-node (16.19.0.1-x86_64-linux)
listen (3.9.0)
rb-fsevent (~> 0.10, >= 0.10.3)
Expand Down Expand Up @@ -396,7 +397,6 @@ GEM
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 4.0)
netrc (~> 0.8)
rexml (3.4.0)
rouge (4.5.1)
rspec (3.13.0)
rspec-core (~> 3.13.0)
Expand All @@ -419,28 +419,37 @@ GEM
rspec-mocks (~> 3.13)
rspec-support (~> 3.13)
rspec-support (3.13.2)
rubocop (1.50.2)
rubocop (1.69.2)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.2.0.0)
parser (>= 3.3.0.2)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.28.0, < 2.0)
regexp_parser (>= 2.9.3, < 3.0)
rubocop-ast (>= 1.36.2, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
unicode-display_width (>= 2.4.0, < 4.0)
rubocop-ast (1.37.0)
parser (>= 3.3.1.0)
rubocop-capybara (2.21.0)
rubocop (~> 1.41)
rubocop-factory_bot (2.26.1)
rubocop (~> 1.61)
rubocop-performance (1.23.0)
rubocop (>= 1.48.1, < 2.0)
rubocop-ast (>= 1.31.1, < 2.0)
rubocop-rails (2.25.1)
rubocop-rails (2.28.0)
activesupport (>= 4.2.0)
rack (>= 1.1)
rubocop (>= 1.33.0, < 2.0)
rubocop (>= 1.52.0, < 2.0)
rubocop-ast (>= 1.31.1, < 2.0)
rubocop-rspec (3.0.0)
rubocop-rspec (2.31.0)
rubocop (~> 1.40)
rubocop-capybara (~> 2.17)
rubocop-factory_bot (~> 2.22)
rubocop-rspec_rails (~> 2.28)
rubocop-rspec_rails (2.29.1)
rubocop (~> 1.61)
ruby-progressbar (1.13.0)
rufus-scheduler (3.9.2)
fugit (~> 1.1, >= 1.11.1)
Expand Down Expand Up @@ -518,7 +527,9 @@ GEM
concurrent-ruby (~> 1.0)
uglifier (4.2.1)
execjs (>= 0.3.0, < 3)
unicode-display_width (2.6.0)
unicode-display_width (3.1.3)
unicode-emoji (~> 4.0, >= 4.0.4)
unicode-emoji (4.0.4)
uniform_notifier (1.16.0)
uri (1.0.2)
validates_timeliness (7.0.0)
Expand Down Expand Up @@ -564,7 +575,7 @@ DEPENDENCIES
devise-i18n (~> 1.11.0)
dotenv-rails (~> 2.8.1)
factory_bot_rails (~> 6.2.0)
faker (~> 3.2.0)
faker (~> 3.5.1)
font-awesome-rails (~> 4.7.0.8)
foreman
guard-livereload (~> 2.5.2)
Expand Down Expand Up @@ -598,10 +609,10 @@ DEPENDENCIES
redis-rails (~> 5.0.2)
rest-client (~> 2.1.0)
rspec-rails
rubocop (~> 1.50.2)
rubocop (~> 1.61)
rubocop-performance
rubocop-rails
rubocop-rspec
rubocop-rspec (~> 2.25)
sassc-rails (~> 2.1.2)
sentry-rails (~> 5.9)
sentry-ruby (~> 5.9)
Expand Down
44 changes: 22 additions & 22 deletions app/controllers/activities_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,28 @@ def index
@price_lists_json = PriceList.unarchived.to_json(only: %i[id name])
end

def show # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
@activity = Activity.includes(:price_list,

Check warning on line 26 in app/controllers/activities_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/activities_controller.rb#L26

Added line #L26 was not covered by tests
{ orders: [{ order_rows: :product }, :user, :created_by] }).find(params[:id])
authorize @activity

Check warning on line 28 in app/controllers/activities_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/activities_controller.rb#L28

Added line #L28 was not covered by tests

@price_list = @activity.price_list
@bartenders = @activity.bartenders
@orders = @activity.orders

Check warning on line 32 in app/controllers/activities_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/activities_controller.rb#L30-L32

Added lines #L30 - L32 were not covered by tests

@credit_mutations = @activity.credit_mutations
@credit_mutations_total = @activity.credit_mutations_total

Check warning on line 35 in app/controllers/activities_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/activities_controller.rb#L34-L35

Added lines #L34 - L35 were not covered by tests

@revenue_by_category = @activity.revenue_by_category
@revenue_with_cash = @activity.revenue_with_cash
@revenue_with_pin = @activity.revenue_with_pin
@revenue_with_credit = @activity.revenue_with_credit
@cash_total = @activity.cash_total
@revenue_total = @activity.revenue_total

Check warning on line 42 in app/controllers/activities_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/activities_controller.rb#L37-L42

Added lines #L37 - L42 were not covered by tests

@count_per_product = @activity.count_per_product

Check warning on line 44 in app/controllers/activities_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/activities_controller.rb#L44

Added line #L44 was not covered by tests
end

def create
@activity = Activity.new(permitted_attributes.merge(created_by: current_user))
authorize @activity
Expand Down Expand Up @@ -61,28 +83,6 @@ def destroy
redirect_to Activity
end

def show # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
@activity = Activity.includes(:price_list,
{ orders: [{ order_rows: :product }, :user, :created_by] }).find(params[:id])
authorize @activity

@price_list = @activity.price_list
@bartenders = @activity.bartenders
@orders = @activity.orders

@credit_mutations = @activity.credit_mutations
@credit_mutations_total = @activity.credit_mutations_total

@revenue_by_category = @activity.revenue_by_category
@revenue_with_cash = @activity.revenue_with_cash
@revenue_with_pin = @activity.revenue_with_pin
@revenue_with_credit = @activity.revenue_with_credit
@cash_total = @activity.cash_total
@revenue_total = @activity.revenue_total

@count_per_product = @activity.count_per_product
end

def order_screen # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
authorize Activity

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def allowed_filters
end

def send_insufficient_credit_mail?(user, old_credit)
return if user.nil?
return false if user.nil?

user.provider == 'amber_oauth2' && user.credit.negative? && old_credit.positive?
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/payments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class PaymentsController < ApplicationController
after_action :verify_authorized, only: %i[index create add]

def index
@payments = Payment.all.order(created_at: :desc)
@payments = Payment.order(created_at: :desc)
authorize @payments
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/price_lists_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def index
authorize PriceList

price_lists = policy_scope(PriceList.order(created_at: :desc))
products = Product.all.order(:id).includes(:product_prices)
products = Product.order(:id).includes(:product_prices)

@price_list = PriceList.new

Expand Down
4 changes: 2 additions & 2 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module ApplicationHelper
# TODO: Spec this method
# :nocov:
def nav_link(name = nil, options = nil, html_options = nil, &block)
def nav_link(name = nil, options = nil, html_options = nil, &)
options&.merge!(class: 'active') { |_, v1, v2| [v1, v2].join ' ' } if current_page?(name)

link_to(name, options, html_options, &block)
link_to(name, options, html_options, &)
end

def flash_class(level)
Expand Down
12 changes: 6 additions & 6 deletions app/models/activity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ class Activity < ApplicationRecord
validates_datetime :end_time, after: :start_time
validate :activity_not_locked

scope :upcoming, (lambda {
scope :upcoming, lambda {
where('(start_time < ? and end_time > ?) or start_time > ?', Time.zone.now,
Time.zone.now, Time.zone.now).order(:start_time, :end_time)
})
}

scope :current, (lambda {
scope :current, lambda {
where('(start_time < ? and end_time > ?)', Time.zone.now,
Time.zone.now).order(:start_time, :end_time)
})
}

scope :not_locked, (lambda {
scope :not_locked, lambda {
where('end_time >= ? AND locked_by_id IS NULL', 2.months.ago)
})
}

delegate :products, to: :price_list

Expand Down
4 changes: 2 additions & 2 deletions app/models/credit_mutation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ class CreditMutation < ApplicationRecord

validate :activity_not_locked

scope :linked_to_activity, (lambda {
scope :linked_to_activity, lambda {
where.not(activity: nil)
})
}

before_destroy -> { throw(:abort) }

Expand Down
2 changes: 1 addition & 1 deletion app/models/invoice.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Invoice < ApplicationRecord
has_secure_token

enum status: { pending: 0, sent: 1, paid: 3 }
enum :status, { pending: 0, sent: 1, paid: 3 }

belongs_to :user
belongs_to :activity
Expand Down
2 changes: 1 addition & 1 deletion app/models/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Order < ApplicationRecord
before_create :can_user_create_order?
before_destroy -> { throw(:abort) }

scope :orders_for, (->(user) { where(user: user) })
scope :orders_for, ->(user) { where(user: user) }

def can_user_create_order?
throw(:abort) unless user.nil? || user.can_order(activity)
Expand Down
4 changes: 2 additions & 2 deletions app/models/payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class << self
undef_method :open
end
# See: https://docs.mollie.com/payments/status-changes
enum status: { open: 0, pending: 1, paid: 2, failed: 3, canceled: 4, expired: 5 }
enum :status, { open: 0, pending: 1, paid: 2, failed: 3, canceled: 4, expired: 5 }
COMPLETE_STATUSES = %w[paid failed canceled expired].freeze

belongs_to :user, optional: true
Expand All @@ -18,7 +18,7 @@ class << self
validate :user_xor_invoice
validate :user_amount

scope :not_completed, (-> { where.not(status: COMPLETE_STATUSES) })
scope :not_completed, -> { where.not(status: COMPLETE_STATUSES) }

after_save :process_complete_payment!

Expand Down
2 changes: 1 addition & 1 deletion app/models/price_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class PriceList < ApplicationRecord

validates :name, presence: true

scope :unarchived, (-> { where(archived_at: nil) })
scope :unarchived, -> { where(archived_at: nil) }

def product_price_for(product)
@product_price ||= ProductPrice.includes(:product).where(price_list: self)
Expand Down
4 changes: 2 additions & 2 deletions app/models/product.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Product < ApplicationRecord
enum category: { beer: 0, low_alcohol_beer: 9, craft_beer: 6, non_alcoholic: 1, distilled: 2, whiskey: 8, wine: 3, food: 4, tobacco: 5,
donation: 7 }
enum :category, { beer: 0, low_alcohol_beer: 9, craft_beer: 6, non_alcoholic: 1, distilled: 2, whiskey: 8, wine: 3, food: 4, tobacco: 5,
donation: 7 }

has_many :product_prices, dependent: :destroy
has_many :price_lists, through: :product_prices, dependent: :restrict_with_error
Expand Down
2 changes: 1 addition & 1 deletion app/models/role.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Role < ApplicationRecord
enum role_type: { treasurer: 0, main_bartender: 1, renting_manager: 2 }
enum :role_type, { treasurer: 0, main_bartender: 1, renting_manager: 2 }

validates :role_type, :group_uid, presence: true
has_many :roles_users, class_name: 'RolesUsers', dependent: :destroy, inverse_of: :role
Expand Down
18 changes: 9 additions & 9 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ class User < ApplicationRecord # rubocop:disable Metrics/ClassLength
validates :uid, uniqueness: true, allow_blank: true
validate :no_deactivation_when_nonzero_credit

scope :in_amber, (-> { where(provider: 'amber_oauth2') })
scope :manual, (-> { where(provider: nil) })
scope :active, (-> { where(deactivated: false) })
scope :inactive, (-> { where(deactivated: true) })
scope :treasurer, (-> { joins(:roles).merge(Role.treasurer) })
scope :in_amber, -> { where(provider: 'amber_oauth2') }
scope :manual, -> { where(provider: nil) }
scope :active, -> { where(deactivated: false) }
scope :inactive, -> { where(deactivated: true) }
scope :treasurer, -> { joins(:roles).merge(Role.treasurer) }

attr_accessor :current_activity

Expand Down Expand Up @@ -53,7 +53,7 @@ def can_order(activity = nil)
if activity.nil?
!insufficient_credit
else
!insufficient_credit or activity.orders.select { |order| order.user_id == id }.any?
!insufficient_credit or activity.orders.any? { |order| order.user_id == id }
end
end

Expand Down Expand Up @@ -113,15 +113,15 @@ def self.full_name_from_attributes(first_name, last_name_prefix, last_name, nick
end

def self.calculate_credits
credits = User.all.left_outer_joins(:credit_mutations).group(:id).sum('amount')
credits = User.left_outer_joins(:credit_mutations).group(:id).sum('amount')
costs = User.calculate_spendings

credits.each_with_object({}) { |(id, credit), h| h[id] = credit - costs.fetch(id, 0) }
end

def self.calculate_spendings(from: '01-01-1970', to: Time.zone.now)
User.all.joins(:order_rows)
.where('orders.created_at >= ? AND orders.created_at < ?', from, to)
User.joins(:order_rows)
.where(orders: { created_at: from...to })
.group(:id).sum('product_count * price_per_product')
end

Expand Down
Loading

0 comments on commit 0322554

Please sign in to comment.