diff --git a/app/models/help.rb b/app/models/help.rb index 3f014b6..3efb44e 100644 --- a/app/models/help.rb +++ b/app/models/help.rb @@ -15,4 +15,7 @@ class Help < ApplicationRecord validates :help_type, :title, :description, presence: true validates :fullname, :email, :cellphone, :province, :county, :district, :neighborhood, :address, presence: true validates :help_type, inclusion: { in: HELP_TYPES.keys.map(&:to_s) } + + has_many :volunteer_helps, dependent: :destroy + has_many :volunteers, through: :volunteer_helps end diff --git a/app/models/volunteer.rb b/app/models/volunteer.rb index 7fcbf05..69f55ef 100644 --- a/app/models/volunteer.rb +++ b/app/models/volunteer.rb @@ -7,4 +7,7 @@ class Volunteer < ApplicationRecord validates :fullname, :email, :cellphone, :province, :county, presence: true validates :email, uniqueness: { case_sensitive: false } validates :cellphone, uniqueness: true + + has_many :volunteer_helps, dependent: :destroy + has_many :helps, through: :volunteer_helps end diff --git a/app/models/volunteer_help.rb b/app/models/volunteer_help.rb new file mode 100644 index 0000000..0b28673 --- /dev/null +++ b/app/models/volunteer_help.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +class VolunteerHelp < ApplicationRecord + enum status: { pending: 0, accepted: 1, declined: 2, canceled: 3 } + + belongs_to :volunteer + belongs_to :help + + validates :volunteer_id, uniqueness: { scope: :help_id } +end diff --git a/db/migrate/20200330232054_create_volunteer_helps.rb b/db/migrate/20200330232054_create_volunteer_helps.rb new file mode 100644 index 0000000..7e73a89 --- /dev/null +++ b/db/migrate/20200330232054_create_volunteer_helps.rb @@ -0,0 +1,12 @@ +class CreateVolunteerHelps < ActiveRecord::Migration[6.0] + def change + create_table :volunteer_helps do |t| + t.references :volunteer, null: false, foreign_key: true + t.references :help, null: false, foreign_key: true + t.boolean :anonymous, defualt: false + t.integer :status + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 695aab5..02aaafd 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2020_03_30_173203) do +ActiveRecord::Schema.define(version: 2020_03_30_232054) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -46,6 +46,17 @@ t.index ["phone"], name: "index_users_on_phone", unique: true end + create_table "volunteer_helps", force: :cascade do |t| + t.bigint "volunteer_id", null: false + t.bigint "help_id", null: false + t.boolean "anonymous" + t.integer "status" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["help_id"], name: "index_volunteer_helps_on_help_id" + t.index ["volunteer_id"], name: "index_volunteer_helps_on_volunteer_id" + end + create_table "volunteers", force: :cascade do |t| t.string "fullname" t.string "email" @@ -60,4 +71,6 @@ t.index ["email"], name: "index_volunteers_on_email", unique: true end + add_foreign_key "volunteer_helps", "helps" + add_foreign_key "volunteer_helps", "volunteers" end diff --git a/spec/factories/volunteer_helps.rb b/spec/factories/volunteer_helps.rb new file mode 100644 index 0000000..f018764 --- /dev/null +++ b/spec/factories/volunteer_helps.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :volunteer_help do + volunteer { create(:volunteer) } + help { create(:help) } + anonymous { false } + status { :pending } + + trait :anonymous do + anonymous { true } + end + + trait :accepted do + status { :accepted } + end + + trait :declined do + status { :declined } + end + + trait :canceled do + status { :canceled } + end + end +end diff --git a/spec/models/help_spec.rb b/spec/models/help_spec.rb index ec216ab..19fc9ab 100644 --- a/spec/models/help_spec.rb +++ b/spec/models/help_spec.rb @@ -21,4 +21,9 @@ it { is_expected.to validate_presence_of(:address) } it { is_expected.to validate_inclusion_of(:help_type).in_array(Help::HELP_TYPES.keys.map(&:to_s)) } end + + describe 'associations' do + it { is_expected.to have_many(:volunteer_helps).dependent(:destroy) } + it { is_expected.to have_many(:volunteers).through(:volunteer_helps) } + end end diff --git a/spec/models/volunteer_help_spec.rb b/spec/models/volunteer_help_spec.rb new file mode 100644 index 0000000..010672d --- /dev/null +++ b/spec/models/volunteer_help_spec.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe VolunteerHelp, type: :model do + describe 'macros' do + it { is_expected.to define_enum_for(:status).with_values(pending: 0, accepted: 1, declined: 2, canceled: 3) } + end + + describe 'associations' do + it { is_expected.to belong_to(:volunteer) } + it { is_expected.to belong_to(:help) } + end + + describe 'validations' do + before { create(:volunteer_help) } + + it { is_expected.to validate_uniqueness_of(:volunteer_id).scoped_to(:help_id) } + end +end diff --git a/spec/models/volunteer_spec.rb b/spec/models/volunteer_spec.rb index 39d04e5..814de0c 100644 --- a/spec/models/volunteer_spec.rb +++ b/spec/models/volunteer_spec.rb @@ -18,4 +18,9 @@ it { is_expected.to validate_uniqueness_of(:email).case_insensitive } it { is_expected.to validate_uniqueness_of(:cellphone) } end + + describe 'associations' do + it { is_expected.to have_many(:volunteer_helps).dependent(:destroy) } + it { is_expected.to have_many(:helps).through(:volunteer_helps) } + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ceed5ad..d9d5182 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -89,7 +89,7 @@ # # order dependency and want to debug it, you can fix the order by providing # # the seed, which is printed after each run. # # --seed 1234 - # config.order = :random + config.order = :random # # # Seed global randomization in this process using the `--seed` CLI option. # # Setting this allows you to use `--seed` to deterministically reproduce