Added new column most_voted to Poll::Question::Answers

This commit is contained in:
María Checa
2017-10-19 12:17:20 +02:00
parent 68341e5a76
commit 6e680c187f
3 changed files with 58 additions and 10 deletions

View File

@@ -13,6 +13,7 @@ class Poll::Question::Answer < ActiveRecord::Base
validates :given_order, presence: true, uniqueness: { scope: :question_id }
before_validation :set_order, on: :create
before_save :most_voted
def description
super.try :html_safe
@@ -36,13 +37,15 @@ class Poll::Question::Answer < ActiveRecord::Base
Poll::Answer.where(question_id: question, answer: title).count
end
def is_winner?
def most_voted
answers = question.question_answers
.map { |a| Poll::Answer.where(question_id: a.question, answer: a.title).count }
!answers.any?{ |a| a > total_votes }
most_voted = !answers.any?{ |a| a > total_votes }
self.update_attributes(most_voted: most_voted)
end
def total_votes_percentage
((total_votes*100) / question.answers_total_votes).round(2)
((total_votes*100) / question.answers_total_votes).round(2) rescue 0
end
end

View File

@@ -0,0 +1,5 @@
class AddMostVotedToPollQuestionAnswer < ActiveRecord::Migration
def change
add_column :poll_question_answers, :most_voted, :boolean
end
end

View File

@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20171010143623) do
ActiveRecord::Schema.define(version: 20171019095042) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -486,6 +486,10 @@ ActiveRecord::Schema.define(version: 20171010143623) do
t.boolean "draft_publication_enabled", default: false
t.boolean "result_publication_enabled", default: false
t.boolean "published", default: true
t.date "proposals_phase_start_date"
t.date "proposals_phase_end_date"
t.boolean "proposals_phase_enabled"
t.text "proposals_description"
end
add_index "legislation_processes", ["allegations_end_date"], name: "index_legislation_processes_on_allegations_end_date", using: :btree
@@ -498,6 +502,36 @@ ActiveRecord::Schema.define(version: 20171010143623) do
add_index "legislation_processes", ["result_publication_date"], name: "index_legislation_processes_on_result_publication_date", using: :btree
add_index "legislation_processes", ["start_date"], name: "index_legislation_processes_on_start_date", using: :btree
create_table "legislation_proposals", force: :cascade do |t|
t.integer "legislation_process_id"
t.string "title", limit: 80
t.text "description"
t.string "question"
t.string "external_url"
t.integer "author_id"
t.datetime "hidden_at"
t.integer "flags_count", default: 0
t.datetime "ignored_flag_at"
t.integer "cached_votes_up", default: 0
t.integer "comments_count", default: 0
t.datetime "confirmed_hide_at"
t.integer "hot_score", limit: 8, default: 0
t.integer "confidence_score", default: 0
t.string "responsible_name", limit: 60
t.text "summary"
t.string "video_url"
t.tsvector "tsv"
t.integer "geozone_id"
t.datetime "retired_at"
t.string "retired_reason"
t.text "retired_explanation"
t.integer "community_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "legislation_proposals", ["legislation_process_id"], name: "index_legislation_proposals_on_legislation_process_id", using: :btree
create_table "legislation_question_options", force: :cascade do |t|
t.integer "legislation_question_id"
t.string "value"
@@ -666,6 +700,7 @@ ActiveRecord::Schema.define(version: 20171010143623) do
t.text "description"
t.integer "question_id"
t.integer "given_order", default: 1
t.boolean "most_voted"
end
add_index "poll_question_answers", ["question_id"], name: "index_poll_question_answers_on_question_id", using: :btree
@@ -919,16 +954,20 @@ ActiveRecord::Schema.define(version: 20171010143623) do
add_index "taggings", ["taggable_id", "taggable_type", "context"], name: "index_taggings_on_taggable_id_and_taggable_type_and_context", using: :btree
create_table "tags", force: :cascade do |t|
t.string "name", limit: 40
t.integer "taggings_count", default: 0
t.integer "debates_count", default: 0
t.integer "proposals_count", default: 0
t.integer "spending_proposals_count", default: 0
t.string "name", limit: 40
t.integer "taggings_count", default: 0
t.integer "debates_count", default: 0
t.integer "proposals_count", default: 0
t.integer "spending_proposals_count", default: 0
t.string "kind"
t.integer "budget/investments_count", default: 0
t.integer "budget/investments_count", default: 0
t.integer "legislation/proposals_count", default: 0
t.integer "legislation/processes_count", default: 0
end
add_index "tags", ["debates_count"], name: "index_tags_on_debates_count", using: :btree
add_index "tags", ["legislation/processes_count"], name: "index_tags_on_legislation/processes_count", using: :btree
add_index "tags", ["legislation/proposals_count"], name: "index_tags_on_legislation/proposals_count", using: :btree
add_index "tags", ["name"], name: "index_tags_on_name", unique: true, using: :btree
add_index "tags", ["proposals_count"], name: "index_tags_on_proposals_count", using: :btree
add_index "tags", ["spending_proposals_count"], name: "index_tags_on_spending_proposals_count", using: :btree
@@ -1106,6 +1145,7 @@ ActiveRecord::Schema.define(version: 20171010143623) do
add_foreign_key "identities", "users"
add_foreign_key "images", "users"
add_foreign_key "legislation_draft_versions", "legislation_processes"
add_foreign_key "legislation_proposals", "legislation_processes"
add_foreign_key "locks", "users"
add_foreign_key "managers", "users"
add_foreign_key "moderators", "users"