Skip to content

Commit

Permalink
Add explanations and links on polls
Browse files Browse the repository at this point in the history
  • Loading branch information
nono committed Jul 23, 2012
1 parent 12290d4 commit 54a24d0
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 16 deletions.
14 changes: 8 additions & 6 deletions app/models/poll.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ class Poll < Content
:inverse_of => :poll
accepts_nested_attributes_for :answers, :allow_destroy => true, :reject_if => :all_blank

attr_accessible :title, :answers_attributes
sanitize_attr :title
attr_accessible :title, :wiki_explanations, :answers_attributes

validates :title, :presence => { :message => "La question est obligatoire" }

wikify_attr :explanations

scope :draft, where(:state => "draft")
scope :published, where(:state => "published")
scope :archived, where(:state => "archived")
Expand All @@ -45,10 +46,11 @@ def create_node(attrs={})
### Search ####

mapping do
indexes :id, :index => :not_analyzed
indexes :created_at, :type => 'date', :include_in_all => false
indexes :title, :analyzer => 'french', :boost => 10
indexes :answers, :analyzer => 'french', :as => proc { answers.pluck(:answer).join("\n") }
indexes :id, :index => :not_analyzed
indexes :created_at, :type => 'date', :include_in_all => false
indexes :title, :analyzer => 'french', :boost => 10
indexes :explanations, :analyzer => 'french', :boost => 5
indexes :answers, :analyzer => 'french', :as => proc { answers.pluck(:answer).join("\n") }
end

### Workflow ###
Expand Down
5 changes: 4 additions & 1 deletion app/models/poll_answer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class PollAnswer < ActiveRecord::Base
acts_as_list :scope => :poll

attr_accessible :answer
sanitize_attr :answer

validates :answer, :presence => { :message => "La description de la réponse ne peut pas être vide" }

Expand All @@ -32,4 +31,8 @@ def vote(ip)
self.class.increment_counter(:votes, self.id)
poll.vote(ip)
end

def formatted
linkify answer
end
end
3 changes: 3 additions & 0 deletions app/views/moderation/polls/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
%p
= form.label :title, "Question"
= form.text_field :title, :autocomplete => 'off', :required => 'required', :spellcheck => 'true'
%p
= form.label :wiki_explanations, "Texte descriptif"
= form.text_area :wiki_explanations, :spellcheck => 'true', :class => 'markItUp'
= form.fields_for :answers do |aform|
%p
= aform.text_field :answer
Expand Down
5 changes: 4 additions & 1 deletion app/views/moderation/polls/_poll.html.haml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
= article_for poll do |c|
- c.title = "#{link_to "Sondage", polls_path, :class => "topic"} #{link_to poll.title, moderation_poll_path(:id => poll)}".html_safe
- c.body = render("polls/answers_current", :poll => poll, :id_suffix => "")
- c.body = capture do
- if poll.explanations
.explanations= poll.explanations
= render("polls/answers_current", :poll => poll, :id_suffix => "")
- if current_account && current_account.can_update?(@poll)
- c.actions = link_to("Modifier", edit_moderation_poll_path(:id => @poll), :class => 'action')
2 changes: 1 addition & 1 deletion app/views/polls/_answers_archived.html.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
%ul.poll.archived
= list_of poll.answers do |answer|
#{answer.answer} :
#{answer.formatted} :
.result(style="width: #{answer.percent}%;")
= answer.votes
(#{answer.percent} %)
Expand Down
2 changes: 1 addition & 1 deletion app/views/polls/_answers_current.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
- poll.answers.each_with_index do |answer,index|
%li
= radio_button_tag :position, answer.position, false, :id => "position_#{index}#{id_suffix}"
= label_tag "position_#{index}#{id_suffix}", answer.answer
= label_tag "position_#{index}#{id_suffix}", answer.formatted
%p.total_votes
Total :
= pluralize poll.total_votes, "vote"
Expand Down
3 changes: 3 additions & 0 deletions app/views/polls/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
%p
= form.label :title, "Question", :maxlength => 128
= form.text_field :title, :autocomplete => 'off', :required => 'required', :spellcheck => 'true'
%p
= form.label :wiki_explanations, "Texte descriptif"
= form.text_area :wiki_explanations, :spellcheck => 'true', :class => 'markItUp'
= form.fields_for :answers do |aform|
%p
= aform.text_field :answer, :maxlength => 128
Expand Down
5 changes: 4 additions & 1 deletion app/views/polls/_poll.html.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
= article_for poll do |c|
- c.title = "#{link_to "Sondage", "/sondages", :class => "topic"} #{link_to poll.title, poll}".html_safe
- c.body = render("polls/answers_#{poll_current_or_archived poll}", :poll => poll, :id_suffix => "")
- c.body = capture do
- if poll.explanations
.explanations= poll.explanations
= render("polls/answers_current", :poll => poll, :id_suffix => "")
- c.actions = capture do
- if current_account && current_account.can_update?(poll)
= link_to("Modifier", edit_moderation_poll_path(poll), :class => 'action')
Expand Down
5 changes: 4 additions & 1 deletion app/views/polls/_preview.html.haml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
= article_for preview do |c|
- c.title = "#{link_to "Sondage", "/sondages", :class => "topic"} #{link_to preview.title, "#"}".html_safe
- c.body = render("answers_current", :poll => preview, :id_suffix => "")
- c.body = capture do
- if preview.explanations
.explanations= preview.explanations
= render("answers_current", :poll => preview, :id_suffix => "")
6 changes: 6 additions & 0 deletions db/migrate/20120723203008_add_explanations_to_polls.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddExplanationsToPolls < ActiveRecord::Migration
def change
add_column :polls, :wiki_explanations, :text
add_column :polls, :explanations, :text
end
end
10 changes: 6 additions & 4 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20120718221412) do
ActiveRecord::Schema.define(:version => 20120723203008) do

create_table "access_grants", :force => true do |t|
t.integer "account_id"
Expand Down Expand Up @@ -254,11 +254,13 @@
add_index "poll_answers", ["poll_id", "position"], :name => "index_poll_answers_on_poll_id_and_position"

create_table "polls", :force => true do |t|
t.string "state", :limit => 10, :default => "draft", :null => false
t.string "title", :limit => 128, :null => false
t.string "cached_slug", :limit => 128
t.string "state", :limit => 10, :default => "draft", :null => false
t.string "title", :limit => 128, :null => false
t.string "cached_slug", :limit => 128
t.datetime "created_at"
t.datetime "updated_at"
t.text "wiki_explanations"
t.text "explanations"
end

add_index "polls", ["cached_slug"], :name => "index_polls_on_cached_slug"
Expand Down
4 changes: 4 additions & 0 deletions lib/ar_base_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@ def self.wikify_attr(attr)
def wikify(txt)
LFMarkdown.render(txt)
end

def linkify(txt)
ERB::Util.h(txt).gsub(/\[([^\]]*)\]\(([^)]*)\)/, '<a href="\2">\1</a>').html_safe
end
end

0 comments on commit 54a24d0

Please sign in to comment.