After starting a bounty, you must wait 1 day before awarding it
[shapado:shapado.git] / app / controllers / bounty_controller.rb
1 class BountyController < ApplicationController
2   before_filter :login_required
3   before_filter :find_question
4
5   def start
6     if @question.bounty && @question.bounty.active
7       flash[:notice] = "this question has an active bounty" # TODO: i18n
8       redirect_to question_path(@question)
9       return
10     end
11
12     if Time.now - @question.created_at < 2.days
13       flash[:notice] = "you should wait 2 days before offering a bounty on this question" # TODO: i18n
14       redirect_to question_path(@question)
15       return
16     end
17
18     config = current_user.config_for(current_group)
19
20     if config.reputation < 75
21       flash[:notice] = "you don't have enough reputation to create a bounty on this question" # TODO: i18n
22       redirect_to question_path(@question)
23       return
24     end
25
26     @question.build_bounty(params[:bounty])
27     @question.bounty.created_by = current_user
28     @question.bounty.started_at = Time.now
29     @question.bounty.ends_at = Time.now + 1.week
30
31     if !@question.bounty.valid?
32       flash[:notice] = @question.bounty.errors.full_messages.join(" ")
33       redirect_to question_path(@question)
34       return
35     end
36
37     @question.override(:bounty => @question.bounty.raw_attributes) # FIXME: buggy mongoid assocs
38
39     current_user.update_reputation(:start_bounty, current_group, -@question.bounty.reputation)
40
41     redirect_to question_path(@question)
42   end
43
44   def close
45     if @question.bounty.ends_at < Time.now
46       flash[:notice] = "the bounty has expired"
47       @question.bounty.reward(current_group)
48       redirect_to question_path(@question)
49       return
50     end
51
52     if (Time.now - @question.bounty.started_at) < 1.day
53       flash[:error] = "you must wait #{distance_of_time_in_words(Time.now, @question.bounty.started_at)} before awarding this bounty." # TODO: i18n
54       redirect_to question_path(@question)
55       return
56     end
57
58     @answer = @question.answers.where(:_id => params[:answer_id]).first
59     @question.bounty.reward(current_group, @answer)
60
61     redirect_to question_path(@question)
62   end
63
64   protected
65   def find_question
66     @question = Question.minimal.by_slug(params[:id])
67   end
68 end