Skip to content

Commit

Permalink
Fix syntax (w/Rubocop) in (Rails) models
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Coles <alex@alexbcoles.com>
  • Loading branch information
myabc committed Jun 30, 2015
1 parent ec1bb39 commit df51b6e
Show file tree
Hide file tree
Showing 35 changed files with 176 additions and 189 deletions.
6 changes: 2 additions & 4 deletions app/models/activity/changeset_activity_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ def event_description(event, _activity)

def event_datetime(event, _activity)
committed_on = event['committed_on']
committed_date = committed_on.is_a?(String) ? DateTime.parse(committed_on)
: committed_on
committed_date = committed_on.is_a?(String) ? DateTime.parse(committed_on) : committed_on
end

def event_path(event, _activity)
Expand All @@ -91,8 +90,7 @@ def repositories_table
def format_revision(event)
repository_class = event['repository_type'].constantize

repository_class.respond_to?(:format_revision) ? repository_class.format_revision(event['revision'])
: event['revision']
repository_class.respond_to?(:format_revision) ? repository_class.format_revision(event['revision']) : event['revision']
end

def split_comment(comments)
Expand Down
3 changes: 1 addition & 2 deletions app/models/activity/time_entry_activity_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ def event_query_projection(activity)
protected

def event_title(event, _activity)
time_entry_object_name = event['work_package_id'].blank? ? event['project_name']
: work_package_title(event)
time_entry_object_name = event['work_package_id'].blank? ? event['project_name'] : work_package_title(event)
"#{l_hours(event['time_entry_hours'])} (#{time_entry_object_name})"
end

Expand Down
10 changes: 5 additions & 5 deletions app/models/changeset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ def author
def repository_encoding
if repository.present?
repository.repo_log_encoding
else
nil

This comment has been minimized.

Copy link
@NobodysNightmare

NobodysNightmare Jul 7, 2015

Collaborator

I actually like explicit return statements.

If you asked me I would even write return in most of the cases... This might be my view alone, not sure...

This comment has been minimized.

Copy link
@myabc

myabc Jul 7, 2015

Author Owner

@NobodysNightmare then go write Python ;-)

I've probably become too accustomed to Ruby's implicitness – but this particular example just seems like extra noise. I can understand that some people might find it more readable.

end
end

Expand Down Expand Up @@ -138,11 +136,13 @@ def scan_comment_for_work_package_ids
referenced_work_packages = []

comments.scan(/([\s\(\[,-]|^)((#{kw_regexp})[\s:]+)?(#\d+(\s+@#{TIMELOG_RE})?([\s,;&]+#\d+(\s+@#{TIMELOG_RE})?)*)(?=[[:punct:]]|\s|<|$)/i) do |match|
action, refs = match[2], match[3]
action = match[2]
refs = match[3]
next unless action.present? || ref_keywords_any

refs.scan(/#(\d+)(\s+@#{TIMELOG_RE})?/).each do |m|
work_package, hours = find_referenced_work_package_by_id(m[0].to_i), m[2]
work_package = find_referenced_work_package_by_id(m[0].to_i)
hours = m[2]
if work_package
referenced_work_packages << work_package
fix_work_package(work_package) if fix_keywords.include?(action.to_s.downcase)
Expand Down Expand Up @@ -237,7 +237,7 @@ def log_time(work_package, hours)
work_package: work_package,
spent_on: commit_date,
comments: l(:text_time_logged_by_changeset, value: text_tag, locale: Setting.default_language)
)
)
time_entry.activity = log_time_activity unless log_time_activity.nil?

unless time_entry.save
Expand Down
30 changes: 14 additions & 16 deletions app/models/custom_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,22 +192,20 @@ def order_statement
klass = (customized_class.superclass && !(customized_class.superclass == ActiveRecord::Base)) ? customized_class.superclass : customized_class

case field_format
when 'string', 'text', 'list', 'date', 'bool'
# COALESCE is here to make sure that blank and NULL values are sorted equally
"COALESCE((SELECT cv_sort.value FROM #{CustomValue.table_name} cv_sort" +
" WHERE cv_sort.customized_type='#{klass.name}'" +
" AND cv_sort.customized_id=#{klass.table_name}.id" +
" AND cv_sort.custom_field_id=#{id} LIMIT 1), '')"
when 'int', 'float'
# Make the database cast values into numeric
# Postgresql will raise an error if a value can not be casted!
# CustomValue validations should ensure that it doesn't occur
"(SELECT CAST(cv_sort.value AS decimal(60,3)) FROM #{CustomValue.table_name} cv_sort" +
" WHERE cv_sort.customized_type='#{klass.name}'" +
" AND cv_sort.customized_id=#{klass.table_name}.id" +
" AND cv_sort.custom_field_id=#{id} AND cv_sort.value <> '' AND cv_sort.value IS NOT NULL LIMIT 1)"
else
nil
when 'string', 'text', 'list', 'date', 'bool'
# COALESCE is here to make sure that blank and NULL values are sorted equally
"COALESCE((SELECT cv_sort.value FROM #{CustomValue.table_name} cv_sort" +
" WHERE cv_sort.customized_type='#{klass.name}'" +
" AND cv_sort.customized_id=#{klass.table_name}.id" +
" AND cv_sort.custom_field_id=#{id} LIMIT 1), '')"
when 'int', 'float'
# Make the database cast values into numeric
# Postgresql will raise an error if a value can not be casted!
# CustomValue validations should ensure that it doesn't occur
"(SELECT CAST(cv_sort.value AS decimal(60,3)) FROM #{CustomValue.table_name} cv_sort" +
" WHERE cv_sort.customized_type='#{klass.name}'" +
" AND cv_sort.customized_id=#{klass.table_name}.id" +
" AND cv_sort.custom_field_id=#{id} AND cv_sort.value <> '' AND cv_sort.value IS NOT NULL LIMIT 1)"
end
end

Expand Down
5 changes: 2 additions & 3 deletions app/models/enumeration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,15 @@ def self.same_active_state?(new, previous)
def self.sort_by_ancestor_last(entries)
ancestor_relationships = entries.map { |entry| [entry, entry.ancestors] }

ancestor_relationships.sort do |one, two|
ancestor_relationships.sort { |one, two|
if one.last.include?(two.first)
-1
elsif two.last.include?(one.first)
1
else
0
end
end.map(&:first)
}.map(&:first)
end

def check_integrity
Expand All @@ -178,6 +178,5 @@ def check_integrity
# Force load the subclasses in development mode
['time_entry_activity', 'issue_priority',
'reported_project_status'].each do |enum_subclass|

require_dependency enum_subclass
end
4 changes: 2 additions & 2 deletions app/models/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ def remove_references_before_destroy
deleted_user = DeletedUser.first

WorkPackage.update_all({ assigned_to_id: deleted_user.id },
{ assigned_to_id: id })
assigned_to_id: id)

Journal::WorkPackageJournal.update_all({ assigned_to_id: deleted_user.id },
{ assigned_to_id: id })
assigned_to_id: id)
end

def uniqueness_of_groupname
Expand Down
6 changes: 2 additions & 4 deletions app/models/journal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ def project
journable.project
elsif journable.is_a? Project
journable
else
nil
end
end

Expand Down Expand Up @@ -149,11 +147,11 @@ def get_changes
normalized_data = JournalManager.normalize_newlines(data.journaled_attributes)
normalized_predecessor_data = JournalManager.normalize_newlines(predecessor.data.journaled_attributes)

normalized_data.select do |k, v|
normalized_data.select { |k, v|
# we dont record changes for changes from nil to empty strings and vice versa
pred = normalized_predecessor_data[k]
v != pred && (v.present? || pred.present?)
end.each do |k, v|
}.each do |k, v|
@changes[k] = [normalized_predecessor_data[k], v]
end
end
Expand Down
5 changes: 2 additions & 3 deletions app/models/journal_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def self.merge_reference_journals_by_id(current, predecessor, key)
all_attachable_journal_ids = current.map { |j| j[key] } | predecessor.map { |j| j[key] }

all_attachable_journal_ids.each_with_object({}) { |i, h|
h[i] = [predecessor.detect { |j| j[key] == i },
h[i] = [predecessor.detect do |j| j[key] == i end,

This comment has been minimized.

Copy link
@NobodysNightmare

NobodysNightmare Jul 7, 2015

Collaborator

{ ... }?

This comment has been minimized.

Copy link
@myabc

myabc Jul 7, 2015

Author Owner

@NobodysNightmare yep, you're right.

This comment has been minimized.

Copy link
@myabc

myabc Jul 8, 2015

Author Owner

@NobodysNightmare this seems to be a bug with rubocop. I've reported it here: rubocop/rubocop#2021

This comment has been minimized.

Copy link
@myabc

myabc Jul 8, 2015

Author Owner

@NobodysNightmare reverted change in 8fd5357

current.detect { |j| j[key] == i }]
}
end
Expand Down Expand Up @@ -213,8 +213,7 @@ def self.journaled_class(journal_type)

def self.normalize_newlines(data)
data.each_with_object({}) { |e, h|
h[e[0]] = (e[1].is_a?(String) ? e[1].gsub(/\r\n/, "\n")
: e[1])
h[e[0]] = (e[1].is_a?(String) ? e[1].gsub(/\r\n/, "\n") : e[1])
}
end

Expand Down
1 change: 0 additions & 1 deletion app/models/ldap_auth_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ def get_user_dn(login)
ldap_con.search(base: base_dn,
filter: object_filter & login_filter,
attributes: search_attributes) do |entry|

if onthefly_register?
attrs = get_user_attributes_from_ldap_entry(entry)
else
Expand Down
2 changes: 0 additions & 2 deletions app/models/legacy_journal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ def project
journaled.project
elsif journaled.is_a? Project
journaled
else
nil
end
end

Expand Down
Loading

0 comments on commit df51b6e

Please sign in to comment.