Skip to content
Browse files

restify the doodle answers

  • Loading branch information...
1 parent dde01fe commit 35c62d533345d9a5a52a8debb851d5ad2bfffb8e @thegcat committed Nov 7, 2010
View
47 app/controllers/doodle_answers_controller.rb
@@ -0,0 +1,47 @@
+class DoodleAnswersController < ApplicationController
+ unloadable
+
+ before_filter :find_doodle, :only => [:create]
+ before_filter :find_doodle_answer, :only => [:update]
+ before_filter :authorize, :is_doodle_active?
+
+ verify :method => :post, :only => [:create], :redirect_to => {:controller => 'doodles', :action => 'show', :id => :doodle_id}
+
+ def create
+ @response.answers = Array.new(@doodle.options.size) {|index| (params[:answers] || []).include?(index.to_s)}
+ @response.save ? flash[:notice] = l(:doodle_answer_create_successful) : flash[:warning] = l(:doodle_answer_create_unsuccessful)
+ redirect_to :controller => 'doodles', :action => 'show', :id => @doodle
+ end
+
+ def update
+ @response.answers = Array.new(@doodle.options.size) {|index| (params[:answers] || []).include?(index.to_s)}
+ @response.save ? flash[:notice] = l(:doodle_answer_update_successful) : flash[:warning] = l(:doodle_answer_update_unsuccessful)
+ redirect_to :controller => 'doodles', :action => 'show', :id => @doodle
+ end
+
+ private
+
+ def find_doodle
+ @doodle = Doodle.find(params[:doodle_id], :include => [:project, :responses])
+ @response = @doodle.responses.new(:author => User.current)
+ @project = @doodle.project
+ rescue ActiveRecord::RecordNotFound
+ render_404
+ end
+
+ def find_doodle_answer
+ @response = DoodleAnswers.find(params[:id], :include => [:author, {:doodle => :project}])
+ puts @response
+ @doodle = @response.doodle
+ @project = @doodle.project
+ end
+
+ def is_doodle_active?
+ unless @doodle.active?
+ flash[:error] = l(:doodle_inactive)
+ redirect_to :controller => 'doodles', :action => 'show', :id => @doodle
+ return false
+ end
+ true
+ end
+end
View
26 app/controllers/doodles_controller.rb
@@ -1,11 +1,11 @@
class DoodlesController < ApplicationController
unloadable
- before_filter :find_project, :except => [:show, :destroy, :update, :lock, :edit, :answer]
- before_filter :find_doodle, :only => [:show, :destroy, :update, :lock, :edit, :answer]
+ before_filter :find_project, :except => [:show, :destroy, :update, :lock, :edit]
+ before_filter :find_doodle, :only => [:show, :destroy, :update, :lock, :edit]
before_filter :authorize
- verify :method => :post, :only => [:lock, :answer], :redirect_to => { :action => :show }
+ verify :method => :post, :only => [:lock], :redirect_to => { :action => :show }
helper :watchers
include WatchersHelper
@@ -62,26 +62,6 @@ def update
end
end
- def answer
- unless @doodle.active?
- flash[:error] = l(:doodle_inactive)
- redirect_to :action => 'show', :id => @doodle
- return
- end
- @user = User.current
- params[:answers] ||= []
- @answers = Array.new(@doodle.options.size) { |index| params[:answers].include?(index.to_s) }
- @response = @doodle.responses.find_or_initialize_by_author_id(@user.id)
- @response.answers = @answers
- if @response.save
- flash[:notice] = l(:doodle_update_successful)
- redirect_to :action => 'show', :id => @doodle
- else
- flash[:warning] = l(:doodle_update_unsuccessful)
- redirect_to :action => 'show', :id => @doodle
- end
- end
-
def lock
@doodle.update_attribute :locked, params[:locked]
redirect_to :action => 'show', :id => @doodle
View
6 app/views/doodles/_doodle.html.erb
@@ -1,3 +1,4 @@
+<div class="doodle">
<table class="list doodle">
<col span="2" />
<% @doodle.options.each_index do |i| %>
@@ -17,7 +18,7 @@
<td class="gravatar"><%= avatar(response.author, :size => "24") %></td><td class="answeree"><%=h response.author.name %></td>
<% response.answers_with_css_classes.each_with_index do |answer_with_css,i| %>
<% answer, css = *answer_with_css %>
- <td class="<%= css %>"><%= check_box_tag "answers[]", i, answer, :disabled => (!(@doodle.active?) || !(response.author == User.current)), :onclick => "$(this).up('.answer').toggleClassName('yes').toggleClassName('no')" %></td>
+ <td class="<%= css %>"><%= check_box_tag "answers[]", i, answer, :disabled => (!@doodle.active? || !@response || !(response.author == User.current)), :onclick => "$(this).up('.answer').toggleClassName('yes').toggleClassName('no')" %></td>
<% end %>
</tr>
<% end %>
@@ -28,4 +29,5 @@
<% end %>
</tr>
</tbody>
-</table>
+</table>
+</div>
View
7 app/views/doodles/_doodle_create_answer.html.erb
@@ -0,0 +1,7 @@
+<% form_tag({:controller => 'doodle_answers', :action => 'create', :doodle_id => @doodle}, {:method => :post}) do %>
+
+<%= render :partial => 'doodle' %>
+
+<%= submit_tag l(:button_save) %>
+
+<% end %>
View
7 app/views/doodles/_doodle_update_answer.html.erb
@@ -0,0 +1,7 @@
+<% form_tag({:controller => 'doodle_answers', :action => 'update', :id => @response}, {:method => :put}) do %>
+
+<%= render :partial => 'doodle' %>
+
+<%= submit_tag l(:button_update) %>
+
+<% end %>
View
8 app/views/doodles/show.html.erb
@@ -44,11 +44,11 @@
<%= textilizable(@doodle.description) %>
</div>
</div>
-<div class="doodle">
-<% form_tag({:action => 'answer', :id => @doodle}, {:method => :post}) do %>
+
+<% unless @response %>
<%= render :partial => 'doodle' %>
-</div>
-<%= submit_tag l(:button_save) if @doodle.active? && User.current.allowed_to?(:answer_doodles, @project) %>
+<% else %>
+<%= render :partial => @response.new_record? ? 'doodle_create_answer' : 'doodle_update_answer' %>
<% end %>
<% html_title "Doodle #{@doodle.id}: #{@doodle.title}" -%>
View
4 config/locales/en.yml
@@ -3,6 +3,10 @@ en:
doodle_inactive: "Doodle is inactive"
doodle_update_successful: "Doodle successfully updated"
doodle_update_unsuccessful: "Failure updating doodle"
+ doodle_answer_create_successful: "Doodle successfully answered"
+ doodle_answer_create_unsuccessful: "Failure answering doodle"
+ doodle_answer_update_successful: "Answer sucessfully updated"
+ doodle_answer_update_unsuccessful: "Failure updating answer"
label_doodle: "Doodle"
label_doodle_new: "New doodle"
label_doodle_plural: "Doodles"
View
5 config/routes.rb
@@ -10,6 +10,9 @@
doodles_routes.connect "doodles/:id", :conditions => { :method => :delete }, :action => 'destroy', :id => /\d+/
doodles_routes.connect "doodles/:id/edit", :conditions => { :method => :get }, :action => 'edit', :id => /\d+/
doodles_routes.connect "doodles/:id/lock", :conditions => { :method => :post }, :action => 'lock', :id => /\d+/
- doodles_routes.connect "doodles/:id/answer", :conditions => { :method => :post }, :action => 'answer', :id => /\d+/
+ end
+ map.with_options :controller => 'doodle_answers' do |doodle_answers_routes|
+ doodle_answers_routes.connect "doodles/:doodle_id/doodle_answers", :conditions => { :method => :post }, :action => 'create'
+ doodle_answers_routes.connect "doodle_answers/:id", :conditions => { :method => :put }, :action => 'update', :id => /\d+/
end
end
View
4 init.rb
@@ -16,12 +16,12 @@
name 'Redmine Doodles plugin'
author 'Felix Schäfer'
description 'Per project doodles'
- version '0.5'
+ version 'trunk'
project_module :doodles do
permission :manage_doodles, {:doodles => [:lock, :edit, :update]}, :require => :member
permission :create_doodles, {:doodles => [:new, :create, :preview]}, :require => :member
- permission :answer_doodles, {:doodles => [:answer]}, :require => :loggedin
+ permission :answer_doodles, {:doodle_answers => [:create, :update]}, :require => :loggedin
permission :view_doodles, {:doodles => [:index, :show]}
end

0 comments on commit 35c62d5

Please sign in to comment.
Something went wrong with that request. Please try again.