Commit 3950b8e8c7528d5db79ca9b249d8a5e0ac7a02ab
Exists in
master
and in
4 other branches
Merge pull request #1680 from robbkidd/reduce_note_observer_complexity
Reduce complexity: replace case statement with method lookup.
Showing
2 changed files
with
6 additions
and
11 deletions
Show diff stats
app/observers/note_observer.rb
| ... | ... | @@ -16,16 +16,11 @@ class NoteObserver < ActiveRecord::Observer |
| 16 | 16 | protected |
| 17 | 17 | |
| 18 | 18 | def notify_team_of_new_note(note) |
| 19 | - team_without_note_author(note).map do |u| | |
| 20 | - case note.noteable_type | |
| 21 | - when "Commit"; Notify.note_commit_email(u.id, note.id).deliver | |
| 22 | - when "Issue"; Notify.note_issue_email(u.id, note.id).deliver | |
| 23 | - when "Wiki"; Notify.note_wiki_email(u.id, note.id).deliver | |
| 24 | - when "MergeRequest"; Notify.note_merge_request_email(u.id, note.id).deliver | |
| 25 | - when "Wall"; Notify.note_wall_email(u.id, note.id).deliver | |
| 26 | - when "Snippet"; true # no notifications for snippets? | |
| 27 | - else | |
| 28 | - true | |
| 19 | + notify_method = 'note_' + note.noteable_type.underscore + '_email' | |
| 20 | + | |
| 21 | + if Notify.respond_to? notify_method | |
| 22 | + team_without_note_author(note).map do |u| | |
| 23 | + Notify.send(notify_method.to_sym, u.id, note.id).deliver | |
| 29 | 24 | end |
| 30 | 25 | end |
| 31 | 26 | end | ... | ... |
spec/observers/note_observer_spec.rb
| ... | ... | @@ -90,7 +90,7 @@ describe NoteObserver do |
| 90 | 90 | it 'does nothing for a new note on a snippet' do |
| 91 | 91 | note.stub(:noteable_type).and_return('Snippet') |
| 92 | 92 | |
| 93 | - subject.send(:notify_team_of_new_note, note).should == [true, true] | |
| 93 | + subject.send(:notify_team_of_new_note, note).should be_nil | |
| 94 | 94 | end |
| 95 | 95 | end |
| 96 | 96 | ... | ... |