Skip to content

Instantly share code, notes, and snippets.

@josefrichter
Created June 26, 2010 13:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josefrichter/454061 to your computer and use it in GitHub Desktop.
Save josefrichter/454061 to your computer and use it in GitHub Desktop.
class Feature
include Mongoid::Document
field :body, :type => String
embedded_in :suggestion, :inverse_of => :features
has_many_related :votes, :as => :votable
end
# shows a suggestion with all its features and allows voting on both
<p id="notice"><%= notice %></p>
<div class="suggestion">
<div class="voting">
<h3>
<span class="plus_votes">+<%= @suggestion.votes.find_all{|vote| vote.value == true}.count %></span> /
<span class="minus_votes">-<%= @suggestion.votes.find_all{|vote| vote.value == false}.count %></span>
</h3>
your vote?
<%= form_for([@suggestion, @suggestion.votes.build]) do |f| %>
<%= f.hidden_field(:value,:value => 1) %>
<%#= f.submit "+" %>
<button><%= image_tag("plus.png") %> agree</button>
<% end %>
<%= form_for([@suggestion, @suggestion.votes.build]) do |f| %>
<%= f.hidden_field(:value,:value => 0) %>
<%#= f.submit "-" %>
<button><%= image_tag("minus.png") %> disagree</button>
<% end %>
</div><!-- .voting -->
<div class="body">
<h3><%= @suggestion.title %></h3>
<p><%= @suggestion.body %></p>
<h4>Features</h4>
<% @features.each do |feature| %>
<div class="feature">
<div class="voting">
<span class="plus_votes">+<%= feature.votes.find_all{|vote| vote.value == true}.count %></span> /
<span class="minus_votes">-<%= feature.votes.find_all{|vote| vote.value == false}.count %></span>
<%= form_for([@suggestion, feature, feature.votes.build]) do |f| %>
<%= f.hidden_field(:value,:value => 1) %>
<%#= f.submit "+" %>
<button><%= image_tag("plus.png") %> agree</button>
<% end %>
<%= form_for([@suggestion, feature, feature.votes.build]) do |f| %>
<%= f.hidden_field(:value,:value => 0) %>
<%#= f.submit "-" %>
<button><%= image_tag("minus.png") %> disagree</button>
<% end %>
</div><!-- .voting -->
<div class="body">
<%= feature.body %>
</div><!-- .body -->
<%= link_to "Edit", edit_suggestion_feature_path(@suggestion, feature) %> |
<%= link_to "Delete", suggestion_feature_path(@suggestion,feature), :confirm => 'Are you sure?', :method => :delete %>
<!-- TODO edit and delete for admin only -->
</div><!-- .feature -->
<% end %>
<hr class="cleaner" />
<h4>Add a feature</h4>
<%= form_for([@suggestion, @suggestion.features.build]) do |f| %>
<%= f.text_area :body %><br />
<%= f.submit "Add a feature" %>
<% end %>
</div><!-- .body -->
</div><!-- .suggestion -->
<hr class="cleaner" />
<%= link_to 'Edit', edit_suggestion_path(@suggestion) %> |
<%= link_to 'Back', suggestions_path %>
class Suggestion
include Mongoid::Document
field :title, :type => String
field :body, :type => String
field :slug, :type => String
has_many_related :votes, :as => :votable
embeds_many :features
validates_presence_of :title, :body
validates_uniqueness_of :title, :slug
def to_param
slug
end
end
class Vote
include Mongoid::Document
field :value, :type => Boolean
belongs_to_related :suggestion
belongs_to_related :feature
# This way each Vote has both suggestion_id and feature_id field
end
# just create method:
def create
if params[:feature_id].nil? # casting vote on Feature or Suggestion? - ugly implementation of polymorphism...
# cast vote on Suggestion
suggestion = Suggestion.where(:slug => params[:suggestion_id]).first
params[:suggestion_id] = suggestion.id # replace slug with id
@vote = Vote.new(params[:vote])
suggestion.votes << @vote
else
# cast vote on Feature
suggestion = Suggestion.where(:slug => params[:suggestion_id]).first
feature = suggestion.features.find(params[:feature_id])
@vote = Vote.new(params[:vote])
feature.votes << @vote
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment