Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

port to redmine 2.1.x #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions app/controllers/doodle_answers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,34 @@ class DoodleAnswersController < ApplicationController
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}
#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)
@res.answers = Array.new(@doodle.options.size) {|index| (params[:answers] || []).include?(index.to_s)}
@res.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)
@res.answers = Array.new(@doodle.options.size) {|index| (params[:answers] || []).include?(index.to_s)}
@res.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)
@res = @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
@res = DoodleAnswers.find(params[:id], :include => [:author, {:doodle => :project}])
puts @res
@doodle = @res.doodle
@project = @doodle.project
end

Expand Down
17 changes: 11 additions & 6 deletions app/controllers/doodles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class DoodlesController < ApplicationController
before_filter :find_doodle, :only => [:show, :destroy, :update, :lock, :edit]
before_filter :authorize

verify :method => :post, :only => [:lock], :redirect_to => { :action => :show }
#verify :method => :post, :only => [:lock], :redirect_to => { :action => :show }

helper :watchers
include WatchersHelper
Expand All @@ -15,21 +15,23 @@ def index
end

def new
@doodle = Doodle.new()
end

def edit
end

def show
@doodle = Doodle.find(params[:id])
@author = @doodle.author
@responses = @doodle.responses
@winners = @doodle.winning_columns
# Give the current user an empty answer if she hasn't answered yet and the doodle is active
if @doodle.active? && User.current.allowed_to?(:answer_doodles, @project)
@response = @responses.find_by_author_id(User.current.id)
@response ||= DoodleAnswers.new :author => User.current
@response.answers ||= Array.new(@doodle.options.size, false)
@responses = @responses | [ @response ]
@res = @responses.find_by_author_id(User.current.id)
@res ||= DoodleAnswers.new :author => User.current
@res.answers = Array.new(@doodle.options.size, false) if @res.answers.empty? || @res.answers.nil?
@responses = @responses | [ @res ]
end
# Code later needed for comments
#@comments = @doodle.comments
Expand All @@ -54,6 +56,8 @@ def create
end

def update
expiry_date = Date.strptime(params[:doodle][:expiry_date], t("date.formats.default"))
params[:doodle][:expiry_date] = params[:doodle][:expiry_date].empty? ? '' : expiry_date.strftime("%d.%m.%Y")
@doodle.attributes = params[:doodle]
if @doodle.save
flash[:notice] = l(:doodle_update_successful)
Expand All @@ -65,8 +69,9 @@ def update
end

def lock
@doodle = Doodle.find(params[:id])
@doodle.update_attribute :locked, params[:locked]
redirect_to :action => 'show', :id => @doodle
redirect_to :action => 'show', :id => @doodle.id
end

def preview
Expand Down
25 changes: 20 additions & 5 deletions app/models/doodle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,37 @@ class Doodle < ActiveRecord::Base
acts_as_activity_provider :find_options => {:include => [:project, :author]},
:author_key => :author_id

validates_presence_of :title, :options
validates :title, :options, :presence => true

before_validation :sanitize_options

after_create :add_author_as_watcher, :send_mails

def results
@results ||= responses.empty? ? Array.new(options.length, 0) : responses.map(&:answers).transpose.map { |x| x.select { |v| v }.length }
old_answers = responses.map(&:answers)[0]
new_answers = responses.map(&:answers)[1]
answers = nil
if( !(old_answers.nil?) && !(new_answers.nil?) && !(old_answers.length == new_answers.length))
temp = Array.new(new_answers.length, false)
temp2 = old_answers.length < new_answers.length ? old_answers : new_answers
temp2.each_with_index do |el,i|
temp[i] = old_answers[i]
end
answers = [temp,new_answers]
responses.map(&:answers)[0] = temp
else
answers = responses.map(&:answers)
end
@results ||= responses.empty? ? Array.new(options.length, 0) : answers.transpose.map { |x| x.select { |v| v }.length }
# @results ||= responses.empty? ? Array.new(options.length, 0) : responses.map(&:answers).transpose.map { |x| x.select { |v| v }.length }
end

def active?
!locked? && (expiry_date.nil? ? true : DateTime.now < expiry_date)
end

def winning_columns
@winning_columns ||= self.results.max == 0 ? [] : self.results.enum_with_index.collect {|v,i| i if v == self.results.max}.compact
@winning_columns ||= self.results.max == 0 ? [] : self.results.each_with_index.collect {|v,i| i if v == self.results.max}.compact
end

def previewfy
Expand Down Expand Up @@ -61,8 +76,8 @@ def sanitize_options
end

def send_mails
Mailer.deliver_doodle_added(self)
Mailer.deliver_doodle_added_with_answer_request(self)
#Mailer.deliver_doodle_added(self)
#Mailer.deliver_doodle_added_with_answer_request(self)
end

def add_author_as_watcher
Expand Down
3 changes: 2 additions & 1 deletion app/models/doodle_answers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class DoodleAnswers < ActiveRecord::Base
belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
has_many :edits, :class_name => 'DoodleAnswersEdits', :dependent => :delete_all

validates_presence_of :answers
validates :answers,:presence => true

after_save :create_edit

Expand All @@ -18,6 +18,7 @@ def answers_with_css_classes
def css_classes
return @css_classes unless @css_classes.nil?
@css_classes = []

self.answers.each_with_index do |answer,i|
css = "answer"
css << (answer ? " yes" : " no")
Expand Down
2 changes: 1 addition & 1 deletion app/models/doodle_answers_edits.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ def created_on
private

def send_mails
Mailer.deliver_doodle_answered(self)
#Mailer.deliver_doodle_answered(self)
end
end
54 changes: 39 additions & 15 deletions app/views/doodles/_doodle.html.erb
Original file line number Diff line number Diff line change
@@ -1,33 +1,57 @@
<div class="doodle">
<table class="list doodle">
<fieldset>
<div class="list doodle">
<table cellspacing="0" cellpadding="4">
<col span="2" />
<% @doodle.options.each_index do |i| %>
<col <%= "class=\"winner\"" if @winners.include?(i) %>/>
<col <%= 'class="winner"' if @winners.include?(i) %>/>
<% end %>
<thead>
<tr>
<td colspan="2"> </td>
<td> </td>
<% @doodle.options.each_with_index do |option,i| %>
<th<%= " class=\"winner\"" if @winners.include?(i) %>><%=h option %></th>
<th<%= ' class="winner"' if @winners.include?(i) %> align="center"><%=h option %></th>
<% end %>
</tr>
</thead>
<tbody>
<% @responses.each do |response| %>
<tr class="<%= cycle('odd', 'even') %>">
<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 => ([email protected]? || !@response || !(response.author == User.current)), :onclick => "$(this).up('.answer').toggleClassName('yes').toggleClassName('no')" %></td>

<% @responses.each do |res| %>
<tr class="<%= cycle('odd', 'even') %><%= ' current' if res.author == User.current %>">
<!--td class="gravatar"><%= avatar(res.author, :size => "24") %></td-->
<td class="answeree"><%= h res.author.name %></td>
<% p res.answers_with_css_classes %>
<% res.answers_with_css_classes.each_with_index do |answer_with_css,i| %>
<% unless @doodle.options[i].nil? %>
<% answer, css = *answer_with_css %>
<td class="<%= css %>" align="center"><%= check_box_tag "answers[]", i, answer, :disabled => ([email protected]? || !@res || !(res.author == User.current)), :onclick => "$(this).closest('.answer').toggleClass('yes').toggleClass('no')" %></td>
<% end %>
<% end %>
<% diff = @doodle.options.length - res.answers_with_css_classes.length %>
<% if diff > 0 %>
<% i = res.answers_with_css_classes.length %>
<% diff.times do %>
<% i += 1 %>
<td align="center"><%= check_box_tag "answers[]", i, nil, :disabled => ([email protected]? || !@res || !(res.author == User.current)), :onclick => "$(this).closest('.answer').toggleClass('yes').toggleClass('no')" %></td>
<% end %>
<% end %>
</tr>
<% end %>
<tr class="<%= cycle('odd', 'even') %> results">
<td class="gravatar"> </td> <td class="answeree"><%= l(:label_result_plural)%></td>
<% @doodle.results.each_with_index do |res,i| %>
<td<%= " class=\"winner\"" if @winners.include?(i) %>><%= res %></td>
<!--td class="gravatar"> </td-->
<td class="answeree"><%= l(:label_result_plural)%></td>
<% @doodle.results.each_with_index do |res,i| %>
<% unless @doodle.options[i].nil? %>
<td class="<%= "winner" if @winners.include?(i) %>" align="center"><%= res %></td>
<% end %>
<% end %>
<% diff = @doodle.options.length - @doodle.results.length %>
<% if diff > 0 %>
<% diff.times do %>
<td align="center">0</td>
<% end %>
<% end %>
</tr>
</tbody>
</table>
</div>
</div>
</fieldset>
2 changes: 1 addition & 1 deletion app/views/doodles/_doodle_create_answer.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<% form_tag({:controller => 'doodle_answers', :action => 'create', :doodle_id => @doodle}, {:method => :post}) do %>
<%= form_tag({:controller => 'doodle_answers', :action => 'create', :doodle_id => @doodle.id}, {:method => :post}) do %>

<%= render :partial => 'doodle' %>

Expand Down
4 changes: 2 additions & 2 deletions app/views/doodles/_doodle_update_answer.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<% form_tag({:controller => 'doodle_answers', :action => 'update', :id => @response}, {:method => :put}) do %>
<%= form_tag({:controller => 'doodle_answers', :action => 'update', :id => @res}, {:method => :put}) do %>

<%= render :partial => 'doodle' %>

<%= submit_tag l(:button_update) %>
<%= submit_tag l(:label_doodle_answer) %>

<% end %>
10 changes: 5 additions & 5 deletions app/views/doodles/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

<div class="box tabular">
<p><%= f.text_field :title, :required => true, :size => 60 %></p>
<p><%= f.text_field :expiry_date, :size => 10 %><%= calendar_for('doodle_expiry_date') %></p>
<p><%= f.text_area :description, :cols => 60, :rows => 8, :class => 'wiki-edit' %>
<% if @doodle.new_record? %>
<p><%= f.text_field :expiry_date, :value => format_date(@doodle[:expiry_date]) , :size => 10 %><%= calendar_for('doodle_expiry_date') %></p>
<p><%= f.text_area :description, :cols => 60, :rows => 8, :class => 'wiki-edit' %></p>
<%# if @doodle.new_record? %>
<div id="options">
<%= render :partial => 'options', :locals => {:options => @doodle.options} %>
</div>
<p><%= link_to_function l(:label_add_options) do |page|
<p><%= link_to_function l(:label_add_options),nil do |page|
page.insert_html :bottom, :options, :partial => 'options', :locals => {:options => nil}
end %></p>
<p><label><%= l(:label_doodle_should_answer) %></label>
<% @project.users.sort.each do |user| -%>
<label class="floating"><%= check_box_tag 'doodle[should_answer_ids][]', user.id %> <%=h user %></label>
<% end -%>
<%# end -%>
</p>
<% end %>
</div>
Expand Down
6 changes: 4 additions & 2 deletions app/views/doodles/_options.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<% (options.nil? ? Array.new(5, "") : (options + Array.new(options.length < 5 ? (5 - options.length) : 0,""))).each do |option| %>
<% counter = 0;
(options.nil? ? Array.new(5, "") : (options + Array.new(options.length < 5 ? (5 - options.length) : 0,""))).each do |option| %>
<div class="option">
<p><%= label "doodle_options_", nil, l(:field_option) %> <%= text_field_tag "doodle[options][]", option, :size => 60 %> <%= link_to_function l(:button_delete), "$(this).up('.option').remove()" %></p>
<% counter += 1 %>
<p><%= label "doodle_options_", nil, l(:field_option) %> <%= text_field_tag "doodle[options][]", option, :size => 60, :id => "doodle_options_#{counter}" %><%= calendar_for('doodle_options_'+counter.to_s()) %> <%= link_to_function l(:button_delete), "$(this).up('.option').remove()" %></p>
</div>
<% end %>
56 changes: 32 additions & 24 deletions app/views/doodles/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,38 +1,46 @@
<div class="contextual">
<%= link_to_if_authorized(l(:label_doodle_new),
{:controller => 'doodles', :action => 'new', :project_id => @project},
:class => 'icon icon-add',
:onclick => 'Element.show("add-doodle"); Form.Element.focus("doodle_title"); return false;') if @project %>
</div>
<% content_for :sidebar_right do %>
<% if @project && User.current.allowed_to?({:controller => 'doodles', :action => 'new'}, @project) %>
<%= link_to l(:label_doodle_new),
{:controller => 'doodles', :action => 'new', :project_id => @project},
:class => 'icon icon-add',
:onclick => '$("#add-doodle").show(); $("#doodle_title").focus(); $("#preview").hide(); return false;' %>
<% end %>
<%end%>

<div id="add-doodle" style="display:none;">
<h2><%= l(:label_doodle_new) %></h2>
<% labelled_tabular_form_for :doodle, @doodle, :url => { :controller => 'doodles', :action => 'create', :project_id => @project }, :html => { :id => 'doodle-form' } do |f| %>
<%= labelled_form_for :doodle, @doodle, :url => { :controller => 'doodles', :action => 'create', :project_id => @project }, :html => { :id => 'doodle-form' } do |f| %>
<%= render :partial => 'form', :locals => { :f => f } %>
<%= submit_tag l(:button_create) %>
<%= link_to_remote l(:label_preview),
<%= link_to l(:label_preview),
{ :url => { :controller => 'doodles', :action => 'preview', :project_id => @project },
:method => 'post',
:update => 'preview',
:remote => true,
:with => "Form.serialize('doodle-form')"
}, :accesskey => accesskey(:preview) %> |
<%= link_to l(:button_cancel), '#', :onclick => 'Element.hide("add-doodle")' %>
<%= link_to l(:button_cancel), '#', :onclick => '$("#add-doodle").hide()' %>
<% end if @project %>
<div id="preview" class="doodle"></div>
</div>

<h2><%= l(:label_doodle_plural)%></h2>

<% if @doodles.empty? %>
<p class="nodata"><%= l(:label_no_data) %></p>
<% else %>
<% @doodles.each do |doodle| %>
<h3><%= link_to h(doodle.title), :controller => 'doodles', :action => 'show', :id => doodle %></h3>
<p class="author"><%= authoring doodle.created_on, doodle.author %></p>
<div class="wiki">
<%= textilizable(doodle.description[0,254] + (doodle.description[254].nil? ? "" : "…")) unless doodle.description.nil? %>
</div>
<% end %>
<% end %>
<div id="preview" class="doodle">
<h2><%= l(:label_doodle_plural)%></h2>

<% if @doodles.empty? %>
<p class="nodata"><%= l(:label_no_data) %></p>
<% else %>
<% @doodles.each do |doodle| %>
<%= link_to(doodle, :class => "link_to_doodle") do %>
<fieldset>
<div class="info">
<%= l(:doodle_ends) %> <%= format_date(doodle.expiry_date) %>
</div>
<h3><%= doodle.title %></h3>
<p class="author"><%= strip_tags(authoring doodle.created_on, doodle.author) %></p>
<%= textilizable(doodle.description[0,254] + (doodle.description[254].nil? ? "" : "…")) unless doodle.description.nil? %>
</fieldset>
<% end %>
<% end %>
<% end %>
</div>

<% html_title l(:label_doodle_plural) -%>
8 changes: 6 additions & 2 deletions app/views/doodles/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<h2><%= l(:label_doodle_new) %></h2>
<% labelled_tabular_form_for :doodle, @doodle, :url => { :controller => 'doodles', :action => 'create', :project_id => @project }, :html => { :id => 'doodle-form' } do |f| %>
<%= labelled_form_for :doodle, @doodle, :url => { :controller => 'doodles', :action => 'create', :project_id => @project }, :html => { :id => 'doodle-form' } do |f| %>
<%= render :partial => 'form', :locals => { :f => f } %>
<%= submit_tag l(:button_create) %>
<%= link_to_remote l(:label_preview),
Expand All @@ -9,4 +9,8 @@
:with => "Form.serialize('doodle-form')"
}, :accesskey => accesskey(:preview) %>
<% end if @project %>
<div id="preview" class="doodle"></div>
<div id="preview" class="doodle"></div>

<% content_for :header_tags do %>
<%= stylesheet_link_tag 'calendar' %>
<% end %>
Loading