Browse files

I don't remember

  • Loading branch information...
1 parent e656b7a commit 90a08108c30979d602dfa89edb6266ea1ea456ac @douglasdoro committed Oct 1, 2011
View
16 app/controllers/choices_controller.rb
@@ -0,0 +1,16 @@
+# encoding: utf-8
+
+class ChoicesController < ApplicationController
+ before_filter :authenticate_user!
+
+ def create
+ @choice = Choice.new params[:choice]
+ @choice.user_id = current_user
+
+ if @choice.save
+ redirect_to :back, :notice => "Pesquisa respondida com sucesso."
+ else
+ redirect_to :back, :alert => @choice.errors # "Não foi possível gravar sua resposta. Por favor, tente novamente."
+ end
+ end
+end
View
6 app/controllers/surveys_controller.rb
@@ -1,8 +1,12 @@
+# encoding: utf-8
+
class SurveysController < ApplicationController
before_filter :authenticate_user!, :except => [:index, :show]
# OPTIMIZE
def index
+ @choice = Choice.new
+
if current_user
if params[:filter]
return @surveys = Survey.try(params[:filter], current_user)
@@ -28,7 +32,7 @@ def new
def create
@survey = Survey.new(params[:survey])
- @survey.user_id = current_user
+ @survey.user_id = current_user.id
if @survey.save
redirect_to @survey, :notice => "Pesquisa criada com sucesso."
else
View
2 app/helpers/choices_helper.rb
@@ -0,0 +1,2 @@
+module ChoicesHelper
+end
View
5 app/models/answer.rb
@@ -1,5 +1,10 @@
class Answer < ActiveRecord::Base
belongs_to :question
+ has_many :choices
+
+ def percentage
+ self.choices.empty? ? 0 : (self.choices.size.to_f / self.question.total.to_f * 100)
+ end
end
View
21 app/models/choice.rb
@@ -1,6 +1,25 @@
+# encoding: utf-8
+
class Choice < ActiveRecord::Base
belongs_to :user
- belongs_to :question
+ belongs_to :answer
+
+ validates :user, :presence => true
+ validates :answer, :presence => true
+
+ # validates_uniqueness_of :user_id, :scope => :answer_id , :message => "Voce já respondeu essa pergunta!"
+ validate :if_responded
+ private
+
+ def if_responded
+ if self.answer_id.present? and self.user_id.present?
+ answer = Answer.find self.answer_id
+ user = User.find self.user_id
+
+ errors.add("choice", "Você já respondeu essa pergunta!") if answer.question.already_responded?(user)
+ end
+ end
+
end
View
8 app/models/question.rb
@@ -6,4 +6,12 @@ class Question < ActiveRecord::Base
accepts_nested_attributes_for :answers, :allow_destroy => true, :reject_if => lambda { |at| at[:content].blank? }
+ def already_responded?(user)
+ (user.choices.map(&:answer_id) & self.answer_ids).any?
+ end
+
+ def total
+ self.answers.map(&:choice_ids).flatten.compact.count
+ end
+
end
View
7 app/models/survey.rb
@@ -2,9 +2,8 @@ class Survey < ActiveRecord::Base
belongs_to :user
has_many :questions, :dependent => :destroy
-
- has_many :watches
- has_many :users, :through => :watches
+ has_many :watches, :dependent => :destroy
+ has_many :users, :through => :watches
accepts_nested_attributes_for :questions, :allow_destroy => true, :reject_if => lambda { |at| at[:content].blank? }
@@ -14,7 +13,7 @@ class Survey < ActiveRecord::Base
scope :published, where(:published => true)
def self.watching(user)
- includes(:questions).where(:id => user.watches).published
+ includes(:questions).where(:id => user.watches.map(&:survey_id)).published
end
def self.my(user)
View
28 app/views/surveys/index.html.erb
@@ -3,6 +3,32 @@
<h3><%= link_to survey.name.titleize , survey %></h3>
<small><%= survey.description.titleize %></small>
<%= render 'survey_links', :survey => survey %>
+ <% survey.questions.each do |question| %>
+ <ul>
+ <li>
+ <%= question.id %><strong><%= question.content %></strong>(<%= pluralize(question.total, "Respostas") %>)
+ </li>
+ <% if current_user and not question.already_responded?(current_user) %>
+ <%= form_for @choice do |f| %>
+ <% question.answers.each do |answer| %>
+ <li><label><%= f.radio_button :answer_id, answer.id %></label><%= answer.content %></li>
+ <% end %>
+ <%= f.submit "Responder" %>
+ <% end %>
+ <% else %>
+ <% question.answers.each do |answer| %>
+ <li>
+ <span style="display: block;">
+ <%= answer.content %>
+ </span>
+ <span class="bar">
+ <span style="width:<%= sprintf("%2.f%", answer.percentage) %>;"></span>
+ </span>
+ <span class="percent"><%= sprintf("%2.f%", answer.percentage) %></span>
+ </li>
+ <% end %>
+ <% end %>
+ </ul>
+ <% end %>
</div>
<% end %>
-
View
14 config/initializers/inflections.rb
@@ -1,10 +1,4 @@
-# Be sure to restart your server when you modify this file.
-
-# Add new inflection rules using the following format
-# (all these examples are active by default):
-# ActiveSupport::Inflector.inflections do |inflect|
-# inflect.plural /^(ox)$/i, '\1en'
-# inflect.singular /^(ox)en/i, '\1'
-# inflect.irregular 'person', 'people'
-# inflect.uncountable %w( fish sheep )
-# end
+ActiveSupport::Inflector.inflections do |inflect|
+ inflect.irregular "pergunta", "perguntas"
+ inflect.irregular "resposta", "respostas"
+end
View
2 config/routes.rb
@@ -12,5 +12,7 @@
put 'toggle_watch'
end
end
+
+ resources :choices, :only => [:create]
end
View
2 lib/tasks/populate.rake
@@ -39,7 +39,7 @@ namespace :db do
# Fill questions table
Question.populate 1..2 do |question|
question.survey_id = survey.id
- question.content = Populator.words(5..8).titleize
+ question.content = Populator.words(5..8).titleize << "?"
# Fill Answers table
Answer.populate 1..4 do |answer|
View
10 public/stylesheets/site.css
@@ -33,4 +33,12 @@ body, p, ul, td { font-family: verdana, arial, helvetica, sans-serif; font-size
#menu { padding: 5px; }
#menu ul { float: right;}
#menu ul li { float: left;}
-#menu ul li a { margin-left: 10px; }
+#menu ul li a { margin-left: 10px; }
+
+.bar, .bar span { border: 1px solid blue; display: inline-block; width: 200px; height: 10px;" }
+.bar span { border:none; background: red; display: block; }
+.percent { display: inline-block; }
+
+
+
+
View
5 spec/controllers/choices_controller_spec.rb
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe ChoicesController do
+
+end
View
15 spec/helpers/choices_helper_spec.rb
@@ -0,0 +1,15 @@
+require 'spec_helper'
+
+# Specs in this file have access to a helper object that includes
+# the ChoicesHelper. For example:
+#
+# describe ChoicesHelper do
+# describe "string concat" do
+# it "concats two strings with spaces" do
+# helper.concat_strings("this","that").should == "this that"
+# end
+# end
+# end
+describe ChoicesHelper do
+ pending "add some examples to (or delete) #{__FILE__}"
+end

0 comments on commit 90a0810

Please sign in to comment.