More code climate style fixes

This commit is contained in:
Jyri-Petteri Paloposki 2020-10-10 13:58:13 +03:00
parent d8acf60049
commit 67a426a2e9
28 changed files with 157 additions and 172 deletions

View file

@ -1,7 +1,7 @@
class Dependency < ApplicationRecord
# touch to make sure todo caches for predecessor and successor are invalidated
belongs_to :predecessor, :foreign_key => 'predecessor_id', :class_name => 'Todo', :touch => true
belongs_to :successor, :foreign_key => 'successor_id', :class_name => 'Todo', :touch => true
belongs_to :successor, :foreign_key => 'successor_id', :class_name => 'Todo', :touch => true
validate :check_circular_dependencies

View file

@ -26,7 +26,7 @@ class MessageGateway < ActionMailer::Base
attachment = todo.attachments.build
# create temp file
tmp = Tempfile.new(['attachment', '.eml'], {universal_newline: true})
tmp = Tempfile.new(['attachment', '.eml'], { universal_newline: true })
tmp.write email.raw_source.gsub(/\r/, "")
# add temp file to attachment. paperclip will copy the file to the right location
@ -74,13 +74,13 @@ class MessageGateway < ActionMailer::Base
end
def get_receiving_user_from_mail_header(email)
user = get_receiving_user_from_sms_email( get_address(email) )
user = get_receiving_user_from_sms_email(get_address(email))
Rails.logger.info(user.nil? ? "User unknown": "Email belongs to #{user.login}")
return user
end
def get_address(email)
return SITE_CONFIG['email_dispatch'] == 'to' ? email.to[0] : email.from[0]
return SITE_CONFIG['email_dispatch'] == 'to' ? email.to[0] : email.from[0]
end
def get_receiving_user_from_sms_email(address)
@ -91,7 +91,7 @@ class MessageGateway < ActionMailer::Base
end
def check_sender_is_in_mailmap(user, email)
if user.present? and !sender_is_in_mailmap?(user,email)
if user.present? && !sender_is_in_mailmap?(user, email)
Rails.logger.warn "#{email.from[0]} not found in mailmap for #{user.login}"
return false
end
@ -122,7 +122,7 @@ class MessageGateway < ActionMailer::Base
parts = get_all_parts(email.parts)
# remove all parts that are not text/plain
parts.reject{|part| !part.content_type.start_with?("text/plain") }
parts.reject { |part| !part.content_type.start_with?("text/plain") }
return parts.count > 0 ? parts[0].decoded.strip : ""
end

View file

@ -49,7 +49,7 @@ class Project < ApplicationRecord
end
def set_last_reviewed_now
self.last_reviewed = Time.now
self.last_reviewed = Time.zone.now
end
def set_completed_at_date
@ -105,7 +105,7 @@ class Project < ApplicationRecord
return !self.todos.deferred_or_blocked.exists? && !self.todos.active.exists?
end
def shortened_name(length=40)
def shortened_name(length = 40)
name.truncate(length, :omission => "...").html_safe
end

View file

@ -44,9 +44,13 @@ module RecurringTodos
def xth(x)
xth_day = [
I18n.t('todos.recurrence.pattern.first'),I18n.t('todos.recurrence.pattern.second'),I18n.t('todos.recurrence.pattern.third'),
I18n.t('todos.recurrence.pattern.fourth'),I18n.t('todos.recurrence.pattern.last')]
x.nil? ? '??' : xth_day[x-1]
I18n.t('todos.recurrence.pattern.first'),
I18n.t('todos.recurrence.pattern.second'),
I18n.t('todos.recurrence.pattern.third'),
I18n.t('todos.recurrence.pattern.fourth'),
I18n.t('todos.recurrence.pattern.last'),
]
x.nil? ? '??' : xth_day[x - 1]
end
def day_of_week_as_text(day)
@ -170,7 +174,7 @@ module RecurringTodos
# takes start_from and previous into account.
# offset needs to be 1.day for daily patterns or the start will be the
# same day as the previous
def determine_start(previous, offset=0.day)
def determine_start(previous, offset = 0.day)
start = self.start_from || NullTime.new
if previous
# check if the start_from date is later than previous. If so, use

View file

@ -32,7 +32,7 @@ module RecurringTodos
get(:recurrence_selector) == 1 ? get(:every_other2) : 1
end
def every_xth_day(default=nil)
def every_xth_day(default = nil)
get(:every_other3) || default
end

View file

@ -2,7 +2,7 @@ module RecurringTodos
class RecurringTodosBuilder
attr_reader :builder, :project, :context, :tag_list, :user
def initialize (user, attributes)
def initialize(user, attributes)
@user = user
@attributes = Tracks::AttributeHandler.new(@user, attributes)
@ -60,7 +60,7 @@ module RecurringTodos
end
def parse_dates
%w{end_date start_from}.each {|date| @attributes.parse_date date }
%w{end_date start_from}.each { |date| @attributes.parse_date date }
end
def parse_project

View file

@ -41,7 +41,7 @@ module RecurringTodos
# we did not find anything this week, so check the nth next, starting from
# sunday
start = start + self.every_x_week.week - (start.wday()).days
start = start + self.every_x_week.week - (start.wday).days
start = find_first_day_in_this_week(start)
return start unless start == -1
@ -56,7 +56,7 @@ module RecurringTodos
return self.start_from || Time.zone.now
else
start = previous + 1.day
if start.wday() == 0
if start.wday == 0
# we went to a new week, go to the nth next week and find first match
# that week. Note that we already went into the next week, so -1
start += (every_x_week - 1).week
@ -72,8 +72,8 @@ module RecurringTodos
def find_first_day_in_this_week(start)
# check if there are any days left this week for the next todo
start.wday().upto 6 do |i|
return start + (i - start.wday()).days if on_xday(i)
start.wday.upto 6 do |i|
return start + (i - start.wday).days if on_xday(i)
end
-1
end

View file

@ -81,7 +81,7 @@ module RecurringTodos
if start.month > month || (start.month == month && start.day >= every_x_day)
# if there is no next month n and day m in this year, search in next
# year
start = Time.zone.local(start.year+1, month, 1)
start = Time.zone.local(start.year + 1, month, 1)
else
# if there is a next month n, stay in this year
start = Time.zone.local(start.year, month, 1)
@ -91,14 +91,14 @@ module RecurringTodos
def get_relative_weekday_of_month(start, month)
# if there is no next month n in this year, search in next year
the_next = start.month > month ? Time.zone.local(start.year+1, month, 1) : start
the_next = start.month > month ? Time.zone.local(start.year + 1, month, 1) : start
# get the xth day of the month
the_next = get_xth_day_of_month(self.every_xth_day, day_of_week, month, the_next.year)
# if the_next is before previous, we went back into the past, so try next
# year
the_next = get_xth_day_of_month(self.every_xth_day, day_of_week, month, start.year+1) if the_next <= start
the_next = get_xth_day_of_month(self.every_xth_day, day_of_week, month, start.year + 1) if the_next <= start
the_next
end

View file

@ -32,7 +32,7 @@ module RecurringTodos
end
def get_recurrence_selector
@selector=='yearly_every_x_day' ? 0 : 1
@selector == 'yearly_every_x_day' ? 0 : 1
end
def get_every_other2

View file

@ -89,7 +89,7 @@ module Stats
created_count_array = Array.new(30) { |i| @actions_created_last30days.size / 30.0 }
done_count_array = Array.new(30) { |i| @actions_done_last30days.size / 30.0 }
# TODO: make the strftime i18n proof
time_labels = Array.new(30) { |i| I18n.l(Time.zone.now-i.days, :format => :stats) }
time_labels = Array.new(30) { |i| I18n.l(Time.zone.now - i.days, :format => :stats) }
return {
datasets: [
@ -103,11 +103,11 @@ module Stats
end
def completion_time_data
@actions_completion_time = @user.todos.completed.select("completed_at, created_at").reorder("completed_at DESC" )
@actions_completion_time = @user.todos.completed.select("completed_at, created_at").reorder("completed_at DESC")
# convert to array and fill in non-existing weeks with 0
@max_weeks = @actions_completion_time.last ? difference_in_weeks(@today, @actions_completion_time.last.completed_at) : 1
@actions_completed_per_week_array = convert_to_weeks_running_array(@actions_completion_time, @max_weeks+1)
@actions_completed_per_week_array = convert_to_weeks_running_array(@actions_completion_time, @max_weeks + 1)
# stop the chart after 10 weeks
@count = [10, @max_weeks].min
@ -119,7 +119,7 @@ module Stats
# get percentage done cumulative
@cum_percent_done = convert_to_cumulative_array(@actions_completion_time_array, @actions_completion_time.count(:all))
time_labels = Array.new(@count) { |i| "#{i}-#{i+1}" }
time_labels = Array.new(@count) { |i| "#{i}-#{i + 1}" }
time_labels[0] = I18n.t('stats.within_one')
time_labels[@count] = "> #{@count}"
@ -137,7 +137,7 @@ module Stats
# convert to array and fill in non-existing weeks with 0
@max_weeks = difference_in_weeks(@today, @actions_running_time.last.created_at)
@actions_running_per_week_array = convert_to_weeks_from_today_array(@actions_running_time, @max_weeks+1, :created_at)
@actions_running_per_week_array = convert_to_weeks_from_today_array(@actions_running_time, @max_weeks + 1, :created_at)
# cut off chart at 52 weeks = one year
@count = [52, @max_weeks].min
@ -147,9 +147,9 @@ module Stats
@max_actions = @actions_running_time_array.max
# get percentage done cumulative
@cum_percent_done = convert_to_cumulative_array(@actions_running_time_array, @actions_running_time.count )
@cum_percent_done = convert_to_cumulative_array(@actions_running_time_array, @actions_running_time.count)
time_labels = Array.new(@count) { |i| "#{i}-#{i+1}" }
time_labels = Array.new(@count) { |i| "#{i}-#{i + 1}" }
time_labels[0] = "< 1"
time_labels[@count] = "> #{@count}"
@ -186,9 +186,9 @@ module Stats
@max_actions = @actions_running_time_array.max
# get percentage done cumulative
@cum_percent_done = convert_to_cumulative_array(@actions_running_time_array, @actions_running_time.count )
@cum_percent_done = convert_to_cumulative_array(@actions_running_time_array, @actions_running_time.count)
time_labels = Array.new(@count) { |i| "#{i}-#{i+1}" }
time_labels = Array.new(@count) { |i| "#{i}-#{i + 1}" }
time_labels[0] = "< 1"
time_labels[@count] = "> #{@count}"
@ -211,10 +211,10 @@ module Stats
# cut off chart at 52 weeks = one year
@count = [52, @max_weeks].min
@actions_open_per_week_array = convert_to_weeks_running_from_today_array(@actions_started, @max_weeks+1)
@actions_open_per_week_array = convert_to_weeks_running_from_today_array(@actions_started, @max_weeks + 1)
@actions_open_per_week_array = cut_off_array(@actions_open_per_week_array, @count)
time_labels = Array.new(@count+1) { |i| "#{i}-#{i+1}" }
time_labels = Array.new(@count + 1) { |i| "#{i}-#{i + 1}" }
time_labels[0] = "< 1"
return {
@ -252,7 +252,7 @@ module Stats
@actions_completion_day = @user.todos.completed_after(@cut_off_month).select("completed_at")
# convert to hash to be able to fill in non-existing days
@max=0
@max = 0
@actions_creation_day_array = Array.new(7) { |i| 0 }
@actions_creation_day.each { |r| @actions_creation_day_array[r.created_at.wday] += 1 }
@ -275,11 +275,11 @@ module Stats
# convert to hash to be able to fill in non-existing days
@actions_creation_hour_array = Array.new(24) { |i| 0 }
@actions_creation_hour.each{|r| @actions_creation_hour_array[r.created_at.hour] += 1 }
@actions_creation_hour.each { |r| @actions_creation_hour_array[r.created_at.hour] += 1 }
# convert to hash to be able to fill in non-existing days
@actions_completion_hour_array = Array.new(24) { |i| 0 }
@actions_completion_hour.each{|r| @actions_completion_hour_array[r.completed_at.hour] += 1 }
@actions_completion_hour.each { |r| @actions_completion_hour_array[r.completed_at.hour] += 1 }
return {
datasets: [
@ -296,11 +296,11 @@ module Stats
# convert to hash to be able to fill in non-existing days
@actions_creation_hour_array = Array.new(24) { |i| 0 }
@actions_creation_hour.each{|r| @actions_creation_hour_array[r.created_at.hour] += 1 }
@actions_creation_hour.each { |r| @actions_creation_hour_array[r.created_at.hour] += 1 }
# convert to hash to be able to fill in non-existing days
@actions_completion_hour_array = Array.new(24) { |i| 0 }
@actions_completion_hour.each{|r| @actions_completion_hour_array[r.completed_at.hour] += 1 }
@actions_completion_hour.each { |r| @actions_completion_hour_array[r.completed_at.hour] += 1 }
return {
datasets: [
@ -393,7 +393,7 @@ module Stats
# calculate fractions
a = Array.new(array.size) {|i| array[i] * 100.0 / max}
# make cumulative
1.upto(array.size-1) { |i| a[i] += a[i - 1] }
1.upto(array.size - 1) { |i| a[i] += a[i - 1] }
return a
end
@ -412,7 +412,7 @@ module Stats
end
def three_month_avg(set, i)
(set.fetch(i) { 0 } + set.fetch(i+1) { 0 } + set.fetch(i + 2) { 0 }) / 3.0
(set.fetch(i) { 0 } + set.fetch(i + 1) { 0 } + set.fetch(i + 2) { 0 }) / 3.0
end
def set_three_month_avg(set, upper_bound)
@ -421,14 +421,14 @@ module Stats
def compute_running_avg_array(set, upper_bound)
result = set_three_month_avg(set, upper_bound)
result[upper_bound - 1] = result[upper_bound-1] * 3 if upper_bound == set.length
result[upper_bound - 2] = result[upper_bound-2] * 3 / 2 if upper_bound > 1 and upper_bound == set.length
result[upper_bound - 1] = result[upper_bound - 1] * 3 if upper_bound == set.length
result[upper_bound - 2] = result[upper_bound - 2] * 3 / 2 if upper_bound > 1 and upper_bound == set.length
result[0] = "null"
result
end # unsolved, not triggered, edge case for set.length == upper_bound + 1
def month_label(i)
I18n.t('date.month_names')[(Time.zone.now.mon - i -1 ) % 12 + 1]
I18n.t('date.month_names')[(Time.zone.now.mon - i - 1) % 12 + 1]
end
def array_of_month_labels(count)

View file

@ -32,7 +32,7 @@ module Stats
end
def counts
@counts ||= tags.map { |t| t.count }
@counts ||= tags.map(&:count)
end
end
end

View file

@ -64,8 +64,8 @@ class Todo < ApplicationRecord
where('todos.due > ? AND todos.due <= ?', start_date, end_date)
end
STARRED_TAG_NAME = "starred"
DEFAULT_INCLUDES = [:project, :context, :tags, :taggings, :pending_successors, :uncompleted_predecessors, :recurring_todo]
STARRED_TAG_NAME = "starred".freeze
DEFAULT_INCLUDES = [:project, :context, :tags, :taggings, :pending_successors, :uncompleted_predecessors, :recurring_todo].freeze
# state machine
include AASM
@ -149,7 +149,7 @@ class Todo < ApplicationRecord
end
def save_predecessors
unless @predecessor_array.nil? # Only save predecessors if they changed
unless @predecessor_array.nil? # Only save predecessors if they changed
current_array = self.predecessors
remove_array = current_array - @predecessor_array
add_array = @predecessor_array - current_array
@ -245,7 +245,7 @@ class Todo < ApplicationRecord
end
def toggle_star!
self.starred= !starred?
self.starred = !starred?
end
def starred=(starred)
@ -313,7 +313,7 @@ class Todo < ApplicationRecord
alias_method :original_context=, :context=
def context=(value)
if value.is_a? Context
self.original_context=(value)
self.original_context = (value)
else
c = Context.where(:name => value[:name]).first
c = Context.create(value) if c.nil?

View file

@ -14,8 +14,8 @@ module Todos
not_done_todos = current_user.todos.active.not_hidden
end
not_done_todos = not_done_todos.
reorder(Arel.sql("todos.due IS NULL, todos.due ASC, todos.created_at ASC"))
not_done_todos = not_done_todos
.reorder(Arel.sql("todos.due IS NULL, todos.due ASC, todos.created_at ASC"))
.includes(Todo::DEFAULT_INCLUDES)
not_done_todos = not_done_todos.limit(sanitize(params[:limit])) if params[:limit]

View file

@ -13,60 +13,69 @@ class User < ApplicationRecord
def find_by_params(params)
find(params['id'] || params['context_id']) || nil
end
def update_positions(context_ids)
context_ids.each_with_index {|id, position|
context = self.detect { |c| c.id == id.to_i }
raise I18n.t('models.user.error_context_not_associated', :context => id, :user => @user.id) if context.nil?
context.update_attribute(:position, position + 1)
}
end
context_ids.each_with_index { |id, position|
context = self.detect { |c| c.id == id.to_i }
raise I18n.t('models.user.error_context_not_associated', :context => id, :user => @user.id) if context.nil?
context.update_attribute(:position, position + 1)
}
end
end
has_many(:projects, -> { order 'projects.position ASC' }, dependent: :delete_all) do
def find_by_params(params)
find(params['id'] || params['project_id'])
end
def update_positions(project_ids)
project_ids.each_with_index {|id, position|
project_ids.each_with_index { |id, position|
project = self.find_by(id: id.to_i)
raise I18n.t('models.user.error_project_not_associated', :project => id, :user => @user.id) if project.nil?
project.update_attribute(:position, position + 1)
}
end
def projects_in_state_by_position(state)
self.select{ |p| p.state == state }.sort_by{ |p| p.position }
self.select { |p| p.state == state }.sort_by { |p| p.position }
end
def next_from(project)
self.offset_from(project, 1)
end
def previous_from(project)
self.offset_from(project, -1)
end
def offset_from(project, offset)
projects = self.projects_in_state_by_position(project.state)
position = projects.index(project)
return nil if position == 0 && offset < 0
projects.at(position + offset)
end
def cache_note_counts
project_note_counts = Note.group(:project_id).count
self.each do |project|
project.cached_note_count = project_note_counts[project.id] || 0
end
end
def alphabetize(scope_conditions = {})
projects = where(scope_conditions)
projects = projects.sort_by { |project| project.name.downcase }
self.update_positions(projects.map{ |p| p.id })
self.update_positions(projects.map(&:id))
return projects
end
def actionize(scope_conditions = {})
todos_in_project = where(scope_conditions).includes(:todos)
todos_in_project = todos_in_project.sort_by { |x| [-x.todos.active.count, -x.id] }
todos_in_project.reject{ |p| p.todos.active.count > 0 }
sorted_project_ids = todos_in_project.map { |p| p.id }
todos_in_project.reject { |p| p.todos.active.count > 0 }
sorted_project_ids = todos_in_project.map(&:id)
all_project_ids = self.map { |p| p.id }
all_project_ids = self.map(&:id)
other_project_ids = all_project_ids - sorted_project_ids
update_positions(sorted_project_ids + other_project_ids)
@ -76,9 +85,9 @@ class User < ApplicationRecord
end
has_many(:todos, -> { order 'todos.completed_at DESC, todos.created_at DESC' }, dependent: :delete_all) do
def count_by_group(g)
except(:order).group(g).count
end
def count_by_group(g)
except(:order).group(g).count
end
end
has_many :recurring_todos,
@ -89,9 +98,9 @@ class User < ApplicationRecord
-> { where('state = ?', 'deferred')
.order('show_from ASC, todos.created_at DESC') },
:class_name => 'Todo') do
def find_and_activate_ready
where('show_from <= ?', Time.current).collect { |t| t.activate! }
end
def find_and_activate_ready
where('show_from <= ?', Time.current).collect { |t| t.activate! }
end
end
has_many :tags, dependent: :delete_all
@ -157,7 +166,7 @@ class User < ApplicationRecord
"#{first_name} #{last_name}"
end
def change_password(pass,pass_confirm)
def change_password(pass, pass_confirm)
self.password = pass
self.password_confirmation = pass_confirm
save!