Skip to content

Commit

Permalink
Fixed various rubocop warnings on Metrics/CyclomaticComplexity and Me…
Browse files Browse the repository at this point in the history
…trics/PerceivedComplexity
  • Loading branch information
nerakdon committed Mar 18, 2017
1 parent 8074f3f commit a7e8e65
Show file tree
Hide file tree
Showing 13 changed files with 277 additions and 306 deletions.
18 changes: 5 additions & 13 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,34 +1,26 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2017-03-17 19:15:55 -0600 using RuboCop version 0.47.1.
# on 2017-03-18 14:43:37 -0600 using RuboCop version 0.47.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 32
# Offense count: 31
Metrics/AbcSize:
Max: 64

# Offense count: 3
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 230
Max: 228

# Offense count: 9
Metrics/CyclomaticComplexity:
Max: 13

# Offense count: 41
# Offense count: 39
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 67
Max: 53

# Offense count: 6
# Configuration parameters: CountComments.
Metrics/ModuleLength:
Max: 596

# Offense count: 9
Metrics/PerceivedComplexity:
Max: 14
59 changes: 31 additions & 28 deletions app/controllers/concerns/message_train_support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,34 +66,7 @@ def load_collective
.slug_columns[collective_table.to_sym] || :slug
@collective = collective_model.find_by!(slug_column => collective_id)

unless @collective.allows_receiving_by?(
@box_user
) || @collective.allows_sending_by?(
@box_user
)
flash[:error] = :access_to_that_box_denied.l
redirect_to main_app.root_url
return
end

case @division
when :in, :ignored
unless @collective.allows_receiving_by? @box_user
flash[:error] = :access_to_that_box_denied.l
redirect_to message_train.collective_box_url(
@collective.path_part,
:sent
)
end
when :sent, :drafts
unless @collective.allows_sending_by? @box_user
flash[:error] = :access_to_that_box_denied.l
redirect_to message_train.collective_box_url(
@collective.path_part,
:in
)
end
end
authorize_collective(@collective, @division)
end

def load_box
Expand Down Expand Up @@ -144,4 +117,34 @@ def respond_to_marking
end
end
end

def authorize_collective(collective, division)
unless collective.allows_access_by?(@box_user)
flash[:error] = :access_to_that_box_denied.l
redirect_to main_app.root_url
return false
end

case division
when :in, :ignored
unless collective.allows_receiving_by? @box_user
flash[:error] = :access_to_that_box_denied.l
redirect_to message_train.collective_box_url(
collective.path_part,
:sent
)
return false
end
when :sent, :drafts
unless collective.allows_sending_by? @box_user
flash[:error] = :access_to_that_box_denied.l
redirect_to message_train.collective_box_url(
collective.path_part,
:in
)
return false
end
end
true
end
end
62 changes: 34 additions & 28 deletions app/controllers/message_train/unsubscribes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,40 @@ def index
# POST /unsubscribes
def create
if params[:all]
@from = nil
@unsubscribe = @box_user.unsubscribe_from(@from)
if @unsubscribe.errors.empty?
flash[:notice] = :unsubscribe_from_all_message.l
else
flash[:error] = @unsubscribe.errors.full_messages.to_sentence
end
redirect_to message_train.unsubscribes_url
unsubscribe_from_all
return
end
unsub = unsubscribe_params
model = unsub[:from_type].constantize

@from = model.find(unsub[:from_id])
if @from == @box_user || @from.allows_receiving_by?(@box_user)
@unsubscribe = @box_user.unsubscribe_from(@from)
if @unsubscribe.errors.empty?
if @from == @box_user
flash[:notice] = :unsubscribe_message.l
else
name_column = MessageTrain.configuration.name_columns[
@from.class.table_name.to_sym
]
collective_name = @from.send(name_column)
collective_type = @from.class.name
flash[:notice] = :collective_unsubscribe_message.l(
collective_name: collective_name,
collective_type: collective_type
)
end
else
flash[:error] = @unsubscribe.errors.full_messages.to_sentence
end
else
unless @from == @box_user || @from.allows_receiving_by?(@box_user)
flash[:error] = :you_are_not_in_that_collective_type.l(
collective_type: model.name
)
raise ActiveRecord::RecordNotFound
end

@unsubscribe = @box_user.unsubscribe_from(@from)
unless @unsubscribe.errors.empty?
flash[:error] = @unsubscribe.errors.full_messages.to_sentence
redirect_to message_train.unsubscribes_url
return
end

if @from == @box_user
flash[:notice] = :unsubscribe_message.l
else
name_column = MessageTrain.configuration.name_columns[
@from.class.table_name.to_sym
]
collective_name = @from.send(name_column)
collective_type = @from.class.name
flash[:notice] = :collective_unsubscribe_message.l(
collective_name: collective_name,
collective_type: collective_type
)
end
redirect_to message_train.unsubscribes_url
end

Expand Down Expand Up @@ -84,5 +80,15 @@ def destroy
def unsubscribe_params
params.require(:unsubscribe).permit(:from_type, :from_id)
end

def unsubscribe_from_all
@unsubscribe = @box_user.unsubscribe_from(nil)
if @unsubscribe.errors.empty?
flash[:notice] = :unsubscribe_from_all_message.l
else
flash[:error] = @unsubscribe.errors.full_messages.to_sentence
end
redirect_to message_train.unsubscribes_url
end
end
end
59 changes: 35 additions & 24 deletions app/helpers/message_train/conversations_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,10 @@ def conversation_senders(conversation)

def conversation_class(box, conversation)
css_classes = []

css_classes << if conversation.includes_unread_for?(@box_user)
'unread'
else
'read'
end

conversation.includes_drafts_by?(@box_user) && css_classes << 'draft'

!conversation.includes_undeleted_for?(@box_user) && css_classes << 'hide'

if box.division == :trash
!conversation.includes_trashed_for?(@box_user) &&
css_classes << 'hide'
else
!conversation.includes_untrashed_for?(@box_user) &&
css_classes << 'hide'
if box.division == :ignored
!conversation.participant_ignored?(@box_user) && css_classes << 'hide'
else
conversation.participant_ignored?(@box_user) && css_classes << 'hide'
end
end
css_classes.uniq.join(' ')
css_classes << conversation_css_for_read_state(conversation)
css_classes << conversation_css_for_draft_state(conversation)
css_classes << conversation_css_for_hide_state(box, conversation)
css_classes.join(' ')
end

def conversation_trashed_toggle(conversation, collective = nil)
Expand Down Expand Up @@ -85,5 +65,36 @@ def conversation_toggle(conv, icon, mark, method, title, options = {})
)
end
# rubocop:enable Metrics/ParameterLists

# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/PerceivedComplexity
# This really is this complex.
def conversation_css_for_hide_state(box, conversation)
return 'hide' unless conversation.includes_undeleted_for?(@box_user)
return 'hide' if box.division == :trash &&
!conversation.includes_trashed_for?(@box_user)
return 'hide' if box.division != :trash &&
!conversation.includes_untrashed_for?(@box_user)

if box.division == :ignored
'hide' unless conversation.participant_ignored?(@box_user)
elsif conversation.participant_ignored?(@box_user)
'hide'
end
end
# rubocop:enable Metrics/PerceivedComplexity
# rubocop:enable Metrics/CyclomaticComplexity

def conversation_css_for_draft_state(conversation)
'draft' if conversation.includes_drafts_by?(@box_user)
end

def conversation_css_for_read_state(conversation)
if conversation.includes_unread_for?(@box_user)
'unread'
else
'read'
end
end
end
end
37 changes: 23 additions & 14 deletions app/helpers/message_train/messages_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,9 @@ module MessageTrain
module MessagesHelper
def message_class(box, message)
css_classes = []

css_classes << if message.is_unread_for?(@box_user)
'unread panel-info'
else
'read'
end

message.draft && css_classes << 'draft'

if box.division == :trash
!message.is_trashed_for?(@box_user) && css_classes << 'hide'
else
!message.is_untrashed_for?(@box_user) && css_classes << 'hide'
end
css_classes << message_css_for_read_state(message)
css_classes << message_css_for_draft_state(message)
css_classes << message_css_for_hide_state(box, message)
css_classes.join(' ')
end

Expand Down Expand Up @@ -63,5 +52,25 @@ def message_toggle(message, icon, mark_to_set, title, options = {})
}
)
end

def message_css_for_hide_state(box, message)
if box.division == :trash
'hide' unless message.is_trashed_for?(@box_user)
else
'hide' unless message.is_untrashed_for?(@box_user)
end
end

def message_css_for_draft_state(message)
'draft' if message.draft
end

def message_css_for_read_state(message)
if message.is_unread_for?(@box_user)
'unread panel-info'
else
'read'
end
end
end
end

0 comments on commit a7e8e65

Please sign in to comment.