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

@ -4,19 +4,19 @@ class User < ApplicationRecord
include CustomFields
# TODO: acts_as_paraniod ??
has_many :memberships, :dependent => :destroy
has_many :groups, :through => :memberships
has_many :memberships, dependent: :destroy
has_many :groups, through: :memberships
# has_one :ordergroup, :through => :memberships, :source => :group, :class_name => "Ordergroup"
def ordergroup
Ordergroup.joins(:memberships).where(memberships: { user_id: self.id }).first
Ordergroup.joins(:memberships).where(memberships: { user_id: id }).first
end
has_many :workgroups, :through => :memberships, :source => :group, :class_name => "Workgroup"
has_many :assignments, :dependent => :destroy
has_many :tasks, :through => :assignments
has_many :send_messages, :class_name => "Message", :foreign_key => "sender_id"
has_many :created_orders, :class_name => 'Order', :foreign_key => 'created_by_user_id', :dependent => :nullify
has_many :mail_delivery_status, :class_name => 'MailDeliveryStatus', :foreign_key => 'email', :primary_key => 'email'
has_many :workgroups, through: :memberships, source: :group, class_name: 'Workgroup'
has_many :assignments, dependent: :destroy
has_many :tasks, through: :assignments
has_many :send_messages, class_name: 'Message', foreign_key: 'sender_id'
has_many :created_orders, class_name: 'Order', foreign_key: 'created_by_user_id', dependent: :nullify
has_many :mail_delivery_status, class_name: 'MailDeliveryStatus', foreign_key: 'email', primary_key: 'email'
attr_accessor :create_ordergroup, :password, :send_welcome_mail, :settings_attributes
@ -26,22 +26,22 @@ class User < ApplicationRecord
# makes the current_user (logged-in-user) available in models
cattr_accessor :current_user
validates_presence_of :email
validates_presence_of :password, :on => :create
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
validates_uniqueness_of :email, :case_sensitive => false
validates_presence_of :first_name # for simple_form validations
validates_length_of :first_name, :in => 2..50
validates_confirmation_of :password
validates_length_of :password, :in => 5..50, :allow_blank => true
validates :email, presence: true
validates :password, presence: { on: :create }
validates :email, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i }
validates :email, uniqueness: { case_sensitive: false }
validates :first_name, presence: true # for simple_form validations
validates :first_name, length: { in: 2..50 }
validates :password, confirmation: true
validates :password, length: { in: 5..50, allow_blank: true }
# allow nick to be nil depending on foodcoop config
# TODO Rails 4 may have a more beautiful way
# http://stackoverflow.com/questions/19845910/conditional-allow-nil-part-of-validation
validates_length_of :nick, :in => 2..25, :allow_nil => true, :unless => Proc.new { FoodsoftConfig[:use_nick] }
validates_length_of :nick, :in => 2..25, :allow_nil => false, :if => Proc.new { FoodsoftConfig[:use_nick] }
validates_uniqueness_of :nick, :case_sensitive => false, :allow_nil => true # allow_nil in length validation
validates_format_of :iban, :with => /\A[A-Z]{2}[0-9]{2}[0-9A-Z]{,30}\z/, :allow_blank => true
validates_uniqueness_of :iban, :case_sensitive => false, :allow_blank => true
validates :nick, length: { in: 2..25, allow_nil: true, unless: proc { FoodsoftConfig[:use_nick] } }
validates :nick, length: { in: 2..25, allow_nil: false, if: proc { FoodsoftConfig[:use_nick] } }
validates :nick, uniqueness: { case_sensitive: false, allow_nil: true } # allow_nil in length validation
validates :iban, format: { with: /\A[A-Z]{2}[0-9]{2}[0-9A-Z]{,30}\z/, allow_blank: true }
validates :iban, uniqueness: { case_sensitive: false, allow_blank: true }
before_validation :set_password
after_initialize do
@ -58,17 +58,19 @@ class User < ApplicationRecord
end
after_save do
settings_attributes.each do |key, value|
value.each do |k, v|
case v
when '1'
value[k] = true
when '0'
value[k] = false
if settings_attributes
settings_attributes.each do |key, value|
value.each do |k, v|
case v
when '1'
value[k] = true
when '0'
value[k] = false
end
end
settings.merge!(key, value)
end
self.settings.merge!(key, value)
end if settings_attributes
end
if ActiveModel::Type::Boolean.new.cast(create_ordergroup)
og = Ordergroup.new({ name: name })
@ -103,7 +105,7 @@ class User < ApplicationRecord
match_name = q.split.map do |a|
users[:first_name].matches("%#{a}%").or users[:last_name].matches("%#{a}%")
end.reduce(:and)
User.where(match_nick.or match_name)
User.where(match_nick.or(match_name))
end
def locale
@ -111,7 +113,7 @@ class User < ApplicationRecord
end
def name
[first_name, last_name].join(" ")
[first_name, last_name].join(' ')
end
def receive_email?
@ -120,22 +122,24 @@ class User < ApplicationRecord
# Sets the user's password. It will be stored encrypted along with a random salt.
def set_password
unless password.blank?
salt = [Array.new(6) { rand(256).chr }.join].pack("m").chomp
self.password_hash, self.password_salt = Digest::SHA1.hexdigest(password + salt), salt
end
return if password.blank?
salt = [Array.new(6) { rand(256).chr }.join].pack('m').chomp
self.password_hash = Digest::SHA1.hexdigest(password + salt)
self.password_salt = salt
end
# Returns true if the password argument matches the user's password.
def has_password(password)
Digest::SHA1.hexdigest(password + self.password_salt) == self.password_hash
Digest::SHA1.hexdigest(password + password_salt) == password_hash
end
# Returns a random password.
def new_random_password(size = 6)
c = %w(b c d f g h j k l m n p qu r s t v w x z ch cr fr nd ng nk nt ph pr rd sh sl sp st th tr)
v = %w(a e i o u y)
f, r = true, ''
c = %w[b c d f g h j k l m n p qu r s t v w x z ch cr fr nd ng nk nt ph pr rd sh sl sp st th tr]
v = %w[a e i o u y]
f = true
r = ''
(size * 2).times do
r << (f ? c[rand * c.size] : v[rand * v.size])
f = !f
@ -198,12 +202,12 @@ class User < ApplicationRecord
# returns true if user is a member of a given group
def member_of?(group)
group.users.exists?(self.id)
group.users.exists?(id)
end
# Returns an array with the users groups (but without the Ordergroups -> because tpye=>"")
def member_of_groups()
self.groups.where(type: '')
def member_of_groups
groups.where(type: '')
end
def deleted?
@ -220,11 +224,9 @@ class User < ApplicationRecord
def self.authenticate(login, password)
user = find_by_nick(login) || find_by_email(login)
if user && password && user.has_password(password)
user
else
nil
end
return unless user && password && user.has_password(password)
user
end
def self.custom_fields
@ -248,29 +250,29 @@ class User < ApplicationRecord
def token_attributes
# would be sensible to match ApplicationController#show_user
# this should not be part of the model anyway
{ :id => id, :name => "#{display} (#{ordergroup.try(:name)})" }
{ id: id, name: "#{display} (#{ordergroup.try(:name)})" }
end
def self.sort_by_param(param)
param ||= "name"
param ||= 'name'
sort_param_map = {
"nick" => "nick",
"nick_reverse" => "nick DESC",
"name" => "first_name, last_name",
"name_reverse" => "first_name DESC, last_name DESC",
"email" => "users.email",
"email_reverse" => "users.email DESC",
"phone" => "phone",
"phone_reverse" => "phone DESC",
"last_activity" => "last_activity",
"last_activity_reverse" => "last_activity DESC",
"ordergroup" => "IFNULL(groups.type, '') <> 'Ordergroup', groups.name",
"ordergroup_reverse" => "IFNULL(groups.type, '') <> 'Ordergroup', groups.name DESC"
'nick' => 'nick',
'nick_reverse' => 'nick DESC',
'name' => 'first_name, last_name',
'name_reverse' => 'first_name DESC, last_name DESC',
'email' => 'users.email',
'email_reverse' => 'users.email DESC',
'phone' => 'phone',
'phone_reverse' => 'phone DESC',
'last_activity' => 'last_activity',
'last_activity_reverse' => 'last_activity DESC',
'ordergroup' => "IFNULL(groups.type, '') <> 'Ordergroup', groups.name",
'ordergroup_reverse' => "IFNULL(groups.type, '') <> 'Ordergroup', groups.name DESC"
}
# Never pass user input data to Arel.sql() because of SQL Injection vulnerabilities.
# This case here is okay, as param is mapped to the actual order string.
self.eager_load(:groups).order(Arel.sql(sort_param_map[param])) # eager_load is like left_join but without duplicates
eager_load(:groups).order(Arel.sql(sort_param_map[param])) # eager_load is like left_join but without duplicates
end
end