Skip to content

Commit

Permalink
Maintenance: Update dependency rubocop to v1.63.0
Browse files Browse the repository at this point in the history
This commit simplifies iterating over Arrays by leveraging advanced Ruby enumarators methods. Namely #map and #each_with_object.
It also cleans up some date range queries by using #where(date: Range) which is transformed into SQL BETWEEEN operator.
  • Loading branch information
renovatebot authored and mantas committed Apr 9, 2024
1 parent 1c65ced commit b08d6c9
Show file tree
Hide file tree
Showing 29 changed files with 189 additions and 302 deletions.
1 change: 1 addition & 0 deletions .rubocop/default.yml
Expand Up @@ -10,6 +10,7 @@ require:
- rubocop-performance
- rubocop-rails
- rubocop-rspec
- rubocop-rspec_rails
- ../config/initializers/inflections.rb
- ./rubocop_zammad.rb

Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Expand Up @@ -535,7 +535,7 @@ GEM
rspec-core (> 3.3)
rspec-support (3.13.1)
rszr (1.5.0)
rubocop (1.62.1)
rubocop (1.63.0)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
Expand Down
10 changes: 2 additions & 8 deletions app/controllers/activity_stream_controller.rb
Expand Up @@ -8,10 +8,7 @@ def show
activity_stream = current_user.activity_stream(params[:limit])

if response_expand?
list = []
activity_stream.each do |item|
list.push item.attributes_with_association_names
end
list = activity_stream.map(&:attributes_with_association_names)
render json: list, status: :ok
return
end
Expand All @@ -30,10 +27,7 @@ def show
return
end

all = []
activity_stream.each do |item|
all.push item.attributes_with_association_ids
end
all = activity_stream.map(&:attributes_with_association_ids)
render json: all, status: :ok
end
end
10 changes: 2 additions & 8 deletions app/controllers/application_controller/renders_models.rb
Expand Up @@ -121,10 +121,7 @@ def model_index_render(object, params)
generic_objects = object.reorder(Arel.sql(order_sql)).offset(pagination.offset).limit(pagination.limit)

if response_expand?
list = []
generic_objects.each do |generic_object|
list.push generic_object.attributes_with_association_names
end
list = generic_objects.map(&:attributes_with_association_names)
render json: list, status: :ok
return
end
Expand All @@ -144,10 +141,7 @@ def model_index_render(object, params)
return
end

generic_objects_with_associations = []
generic_objects.each do |item|
generic_objects_with_associations.push item.attributes_with_association_ids
end
generic_objects_with_associations = generic_objects.map(&:attributes_with_association_ids)
model_index_render_result(generic_objects_with_associations)
end

Expand Down
10 changes: 2 additions & 8 deletions app/controllers/online_notifications_controller.rb
Expand Up @@ -54,10 +54,7 @@ def index
online_notifications = OnlineNotification.list(current_user)

if response_expand?
list = []
online_notifications.each do |item|
list.push item.attributes_with_association_names
end
list = online_notifications.map(&:attributes_with_association_names)
render json: list, status: :ok
return
end
Expand All @@ -76,10 +73,7 @@ def index
return
end

all = []
online_notifications.each do |item|
all.push item.attributes_with_association_ids
end
all = online_notifications.map(&:attributes_with_association_ids)
render json: all, status: :ok
end

Expand Down
10 changes: 2 additions & 8 deletions app/controllers/organizations_controller.rb
Expand Up @@ -200,10 +200,7 @@ def search
organization_all = Organization.search(query_params)

if response_expand?
list = []
organization_all.each do |organization|
list.push organization.attributes_with_association_names
end
list = organization_all.map(&:attributes_with_association_names)
render json: list, status: :ok
return
end
Expand Down Expand Up @@ -237,10 +234,7 @@ def search
return
end

list = []
organization_all.each do |organization|
list.push organization.attributes_with_association_ids
end
list = organization_all.map(&:attributes_with_association_ids)
render json: list, status: :ok
end

Expand Down
10 changes: 2 additions & 8 deletions app/controllers/recent_view_controller.rb
Expand Up @@ -22,10 +22,7 @@ def index
recent_viewed = RecentView.list(current_user, 10)

if response_expand?
list = []
recent_viewed.each do |item|
list.push item.attributes_with_association_names
end
list = recent_viewed.map(&:attributes_with_association_names)
render json: list, status: :ok
return
end
Expand All @@ -44,10 +41,7 @@ def index
return
end

all = []
recent_viewed.each do |item|
all.push item.attributes_with_association_ids
end
all = recent_viewed.map(&:attributes_with_association_ids)
render json: all, status: :ok
end

Expand Down
10 changes: 2 additions & 8 deletions app/controllers/tickets_controller.rb
Expand Up @@ -20,10 +20,7 @@ def index
.limit(pagination.limit)

if response_expand?
list = []
tickets.each do |ticket|
list.push ticket.attributes_with_association_names
end
list = tickets.map(&:attributes_with_association_names)
render json: list, status: :ok
return
end
Expand Down Expand Up @@ -484,10 +481,7 @@ def search
)

if response_expand?
list = []
tickets.each do |ticket|
list.push ticket.attributes_with_association_names
end
list = tickets.map(&:attributes_with_association_names)
render json: list, status: :ok
return
end
Expand Down
164 changes: 76 additions & 88 deletions app/controllers/time_accountings_controller.rb
Expand Up @@ -25,17 +25,15 @@ def destroy
end

def by_activity

year = params[:year] || Time.zone.now.year
year = params[:year] || Time.zone.now.year
month = params[:month] || Time.zone.now.month

start_periode = Time.zone.parse("#{year}-#{month}-01")
end_periode = start_periode.end_of_month
start_period = Time.zone.parse("#{year}-#{month}-01")
end_period = start_period.end_of_month

records = []
Ticket::TimeAccounting.where('created_at >= ? AND created_at <= ?', start_periode, end_periode).pluck(:ticket_id, :ticket_article_id, :time_unit, :type_id, :created_by_id, :created_at).each do |record|
records.push record
end
records = Ticket::TimeAccounting
.where(created_at: (start_period..end_period))
.pluck(:ticket_id, :ticket_article_id, :time_unit, :type_id, :created_by_id, :created_at)

customers = {}
organizations = {}
Expand Down Expand Up @@ -142,23 +140,24 @@ def by_activity
end

def by_ticket

year = params[:year] || Time.zone.now.year
year = params[:year] || Time.zone.now.year
month = params[:month] || Time.zone.now.month

start_periode = Time.zone.parse("#{year}-#{month}-01")
end_periode = start_periode.end_of_month

time_unit = {}
Ticket::TimeAccounting.where('created_at >= ? AND created_at <= ?', start_periode, end_periode).pluck(:ticket_id, :time_unit, :created_by_id).each do |record|
if !time_unit[record[0]]
time_unit[record[0]] = {
time_unit: 0,
agent_id: record[2],
}
start_period = Time.zone.parse("#{year}-#{month}-01")
end_period = start_period.end_of_month

time_unit = Ticket::TimeAccounting
.where(created_at: (start_period..end_period))
.pluck(:ticket_id, :time_unit, :created_by_id)
.each_with_object({}) do |record, memo|
if !memo[record[0]]
memo[record[0]] = {
time_unit: 0,
agent_id: record[2],
}
end
memo[record[0]][:time_unit] += record[1]
end
time_unit[record[0]][:time_unit] += record[1]
end

if !params[:download]
customers = {}
Expand Down Expand Up @@ -217,41 +216,37 @@ def by_ticket
end

def by_customer

year = params[:year] || Time.zone.now.year
year = params[:year] || Time.zone.now.year
month = params[:month] || Time.zone.now.month

start_periode = Time.zone.parse("#{year}-#{month}-01")
end_periode = start_periode.end_of_month

time_unit = {}
Ticket::TimeAccounting.where('created_at >= ? AND created_at <= ?', start_periode, end_periode).pluck(:ticket_id, :time_unit, :created_by_id).each do |record|
time_unit[record[0]] ||= {
time_unit: 0,
agent_id: record[2],
}
time_unit[record[0]][:time_unit] += record[1]
end
start_period = Time.zone.parse("#{year}-#{month}-01")
end_period = start_period.end_of_month

customers = {}
time_unit.each do |ticket_id, local_time_unit|
ticket = Ticket.lookup(id: ticket_id)
next if !ticket
results = Ticket::TimeAccounting
.where(created_at: (start_period..end_period))
.pluck(:ticket_id, :time_unit, :created_by_id)
.each_with_object({}) do |record, memo|
memo[record[0]] ||= {
time_unit: 0,
agent_id: record[2],
}
memo[record[0]][:time_unit] += record[1]
end
.each_with_object({}) do |(ticket_id, local_time_unit), memo|
ticket = Ticket.lookup(id: ticket_id)
next if !ticket

customers[ticket.customer_id] ||= {}
customers[ticket.customer_id][ticket.organization_id] ||= {
customer: User.lookup(id: ticket.customer_id).attributes,
organization: Organization.lookup(id: ticket.organization_id)&.attributes,
time_unit: 0,
}
customers[ticket.customer_id][ticket.organization_id][:time_unit] += local_time_unit[:time_unit]
end
results = []
customers.each_value do |organizations|
organizations.each_value do |content|
results.push content
memo[ticket.customer_id] ||= {}
memo[ticket.customer_id][ticket.organization_id] ||= {
customer: User.lookup(id: ticket.customer_id).attributes,
organization: Organization.lookup(id: ticket.organization_id)&.attributes,
time_unit: 0,
}
memo[ticket.customer_id][ticket.organization_id][:time_unit] += local_time_unit[:time_unit]
end
end
.values
.map(&:values)
.flatten

if params[:download]
header = [
Expand All @@ -269,15 +264,13 @@ def by_customer
data_type: 'float'
}
]
records = []
results.each do |row|
records = results.map do |row|
customer_name = User.find(row[:customer]['id']).fullname
organization_name = ''
if row[:organization].present?
organization_name = row[:organization]['name']
end
result_row = [customer_name, organization_name, row[:time_unit]]
records.push result_row
[customer_name, organization_name, row[:time_unit]]
end

excel = ExcelSheet.new(
Expand All @@ -301,38 +294,34 @@ def by_customer
end

def by_organization

year = params[:year] || Time.zone.now.year
year = params[:year] || Time.zone.now.year
month = params[:month] || Time.zone.now.month

start_periode = Time.zone.parse("#{year}-#{month}-01")
end_periode = start_periode.end_of_month
start_period = Time.zone.parse("#{year}-#{month}-01")
end_period = start_period.end_of_month

time_unit = {}
Ticket::TimeAccounting.where('created_at >= ? AND created_at <= ?', start_periode, end_periode).pluck(:ticket_id, :time_unit, :created_by_id).each do |record|
time_unit[record[0]] ||= {
time_unit: 0,
agent_id: record[2],
}
time_unit[record[0]][:time_unit] += record[1]
end

organizations = {}
time_unit.each do |ticket_id, local_time_unit|
ticket = Ticket.lookup(id: ticket_id)
next if !ticket
next if !ticket.organization_id
results = Ticket::TimeAccounting
.where(created_at: (start_period..end_period))
.pluck(:ticket_id, :time_unit, :created_by_id)
.each_with_object({}) do |record, memo|
memo[record[0]] ||= {
time_unit: 0,
agent_id: record[2],
}
memo[record[0]][:time_unit] += record[1]
end
.each_with_object({}) do |(ticket_id, local_time_unit), memo|
ticket = Ticket.lookup(id: ticket_id)
next if !ticket
next if !ticket.organization_id

organizations[ticket.organization_id] ||= {
organization: Organization.lookup(id: ticket.organization_id).attributes,
time_unit: 0,
}
organizations[ticket.organization_id][:time_unit] += local_time_unit[:time_unit]
end
results = []
organizations.each_value do |content|
results.push content
end
memo[ticket.organization_id] ||= {
organization: Organization.lookup(id: ticket.organization_id).attributes,
time_unit: 0,
}
memo[ticket.organization_id][:time_unit] += local_time_unit[:time_unit]
end
.values

if params[:download]
header = [
Expand All @@ -346,14 +335,13 @@ def by_organization
data_type: 'float',
}
]
records = []
results.each do |row|

records = results.map do |row|
organization_name = ''
if row[:organization].present?
organization_name = row[:organization]['name']
end
result_row = [organization_name, row[:time_unit]]
records.push result_row
[organization_name, row[:time_unit]]
end

excel = ExcelSheet.new(
Expand Down

0 comments on commit b08d6c9

Please sign in to comment.