Skip to content

Commit

Permalink
Merge pull request #20 from psantos10/5_relationship_volunteer_and_help
Browse files Browse the repository at this point in the history
#5 - Relationship between Volunteer and Help Model
  • Loading branch information
psantos10 committed Mar 31, 2020
2 parents a2da6d8 + c0fe1ea commit 17dd89e
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 2 deletions.
3 changes: 3 additions & 0 deletions app/models/help.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions app/models/volunteer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 10 additions & 0 deletions app/models/volunteer_help.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions db/migrate/20200330232054_create_volunteer_helps.rb
Original file line number Diff line number Diff line change
@@ -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
15 changes: 14 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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
26 changes: 26 additions & 0 deletions spec/factories/volunteer_helps.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions spec/models/help_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
20 changes: 20 additions & 0 deletions spec/models/volunteer_help_spec.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions spec/models/volunteer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 17dd89e

Please sign in to comment.