Add VotationType model
This commit is contained in:
committed by
Senén Rodero Rodríguez
parent
d2cc110678
commit
d1c1aa6691
12
app/models/concerns/questionable.rb
Normal file
12
app/models/concerns/questionable.rb
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
module Questionable
|
||||||
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
|
included do
|
||||||
|
has_one :votation_type, as: :questionable, dependent: :destroy
|
||||||
|
delegate :max_votes, :multiple?, :vote_type, to: :votation_type, allow_nil: true
|
||||||
|
end
|
||||||
|
|
||||||
|
def unique?
|
||||||
|
votation_type.nil? || votation_type.unique?
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
class Poll::Question < ApplicationRecord
|
class Poll::Question < ApplicationRecord
|
||||||
include Measurable
|
include Measurable
|
||||||
include Searchable
|
include Searchable
|
||||||
|
include Questionable
|
||||||
|
|
||||||
acts_as_paranoid column: :hidden_at
|
acts_as_paranoid column: :hidden_at
|
||||||
include ActsAsParanoidAliases
|
include ActsAsParanoidAliases
|
||||||
|
|||||||
17
app/models/votation_type.rb
Normal file
17
app/models/votation_type.rb
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
class VotationType < ApplicationRecord
|
||||||
|
belongs_to :questionable, polymorphic: true
|
||||||
|
|
||||||
|
QUESTIONABLE_TYPES = %w[Poll::Question].freeze
|
||||||
|
|
||||||
|
enum vote_type: %w[unique multiple]
|
||||||
|
|
||||||
|
validates :questionable, presence: true
|
||||||
|
validates :questionable_type, inclusion: { in: ->(*) { QUESTIONABLE_TYPES }}
|
||||||
|
validates :max_votes, presence: true, if: :max_votes_required?
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def max_votes_required?
|
||||||
|
multiple?
|
||||||
|
end
|
||||||
|
end
|
||||||
12
db/migrate/20220323233643_add_votation_types.rb
Normal file
12
db/migrate/20220323233643_add_votation_types.rb
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
class AddVotationTypes < ActiveRecord::Migration[5.2]
|
||||||
|
def change
|
||||||
|
create_table :votation_types do |t|
|
||||||
|
t.integer :questionable_id
|
||||||
|
t.string :questionable_type
|
||||||
|
t.integer :vote_type
|
||||||
|
t.integer :max_votes
|
||||||
|
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1698,6 +1698,15 @@ ActiveRecord::Schema.define(version: 2022_09_15_154808) do
|
|||||||
t.index ["user_id"], name: "index_visits_on_user_id"
|
t.index ["user_id"], name: "index_visits_on_user_id"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
create_table "votation_types", force: :cascade do |t|
|
||||||
|
t.integer "questionable_id"
|
||||||
|
t.string "questionable_type"
|
||||||
|
t.integer "vote_type"
|
||||||
|
t.integer "max_votes"
|
||||||
|
t.datetime "created_at", null: false
|
||||||
|
t.datetime "updated_at", null: false
|
||||||
|
end
|
||||||
|
|
||||||
create_table "votes", id: :serial, force: :cascade do |t|
|
create_table "votes", id: :serial, force: :cascade do |t|
|
||||||
t.string "votable_type"
|
t.string "votable_type"
|
||||||
t.integer "votable_id"
|
t.integer "votable_id"
|
||||||
|
|||||||
14
spec/factories/votation_type.rb
Normal file
14
spec/factories/votation_type.rb
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
FactoryBot.define do
|
||||||
|
factory :votation_type do
|
||||||
|
factory :votation_type_unique do
|
||||||
|
vote_type { "unique" }
|
||||||
|
end
|
||||||
|
|
||||||
|
factory :votation_type_multiple do
|
||||||
|
vote_type { "multiple" }
|
||||||
|
max_votes { 3 }
|
||||||
|
end
|
||||||
|
|
||||||
|
association :questionable, factory: :poll_question
|
||||||
|
end
|
||||||
|
end
|
||||||
35
spec/models/votation_type_spec.rb
Normal file
35
spec/models/votation_type_spec.rb
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
require "rails_helper"
|
||||||
|
|
||||||
|
describe VotationType do
|
||||||
|
let(:vote_types) { %i[votation_type_unique votation_type_multiple] }
|
||||||
|
let(:votation_type) { build(vote_types.sample) }
|
||||||
|
|
||||||
|
it "is valid" do
|
||||||
|
expect(votation_type).to be_valid
|
||||||
|
end
|
||||||
|
|
||||||
|
it "is not valid without questionable" do
|
||||||
|
votation_type.questionable = nil
|
||||||
|
|
||||||
|
expect(votation_type).not_to be_valid
|
||||||
|
end
|
||||||
|
|
||||||
|
it "is not valid when questionable_type is not allowed" do
|
||||||
|
votation_type.questionable_type = Poll::Answer
|
||||||
|
|
||||||
|
expect(votation_type).not_to be_valid
|
||||||
|
expect(votation_type.errors[:questionable_type]).to include "is not included in the list"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "is not valid when max_votes is undefined for multiple votation_type" do
|
||||||
|
votation_type.max_votes = nil
|
||||||
|
votation_type.vote_type = "unique"
|
||||||
|
|
||||||
|
expect(votation_type).to be_valid
|
||||||
|
|
||||||
|
votation_type.vote_type = "multiple"
|
||||||
|
|
||||||
|
expect(votation_type).not_to be_valid
|
||||||
|
expect(votation_type.errors[:max_votes]).to include "can't be blank"
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user