1class PreferRefactor < BasicEvent
 2
 3  def initialize problem, refactor, user
 4    @problem = problem
 5    @refactor = refactor
 6    @user = user
 7  end
 8
 9  def strategy
10    return failure! message: 'You must have created the problem to prefer the refactor' unless user_created_problem?
11    return failure! message: 'No refactor found' unless @refactor
12
13    @problem.refactors.each do |r|
14      r == @refactor ? r.preferred! : r.not_preferred!
15    end
16
17    unless @user.authored? @refactor
18      points = basis_points + x_factor
19      @refactor.author.award_points points
20
21      Notifier.refactor_was_preferred(@refactor, points).deliver
22    end
23
24    success! message: 'That refactor has been preferred'
25  end
26
27
28  private
29
30  def basis_points
31    # ...redacted!
32  end
33
34  def x_factor
35    # ...redacted!
36  end
37
38  def user_created_problem?
39    @user == @problem.author
40  end
41end

Here's the code for preferring a refactor on the site. It's part of a domain event using a framework I wrote (https://github.com/desmondmonster/happenings) and handles the validations, data changes, etc. This keeps the models simple (#preferred! and #not_preferred! just update an attribute) and gives me access to all the data relevant to a preferral. If, for example, I had put the Notifier in an callback on the refactor, the refactor now has to know about points and whether it was preferred by the problem's author.

posted Dec 10 '14 at 20:23

0 Refactors