-
-
Notifications
You must be signed in to change notification settings - Fork 532
Introduce Judge model #1146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Introduce Judge model #1146
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| class JudgesController < ApplicationController | ||
| before_action :authenticate_user!, :must_be_admin | ||
| before_action :set_judge, except: [:new, :create] | ||
|
|
||
| def new | ||
| @judge = Judge.new | ||
| end | ||
|
|
||
| def create | ||
| @judge = Judge.new(judge_params) | ||
|
|
||
| respond_to do |format| | ||
| if @judge.save | ||
| format.html { redirect_to edit_casa_org_path(current_organization), notice: "Judge was successfully created." } | ||
| else | ||
| format.html { render :new } | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def edit | ||
| end | ||
|
|
||
| def update | ||
| if @judge.update(judge_params) | ||
| redirect_to edit_casa_org_path(current_organization), notice: "Judge was successfully updated." | ||
| else | ||
| render :edit | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def set_judge | ||
| @judge = Judge.find(params[:id]) | ||
| end | ||
|
|
||
| def judge_params | ||
| params.require(:judge).permit(:name, :active).merge( | ||
| casa_org: current_organization | ||
| ) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| class Judge < ApplicationRecord | ||
| has_paper_trail | ||
|
|
||
| belongs_to :casa_org | ||
|
|
||
| validates :name, presence: true, uniqueness: {scope: %i[casa_org]} | ||
|
|
||
| scope :for_organization, ->(org) { where(casa_org: org) } | ||
| scope :active, -> { where(active: true) } | ||
| end | ||
|
|
||
| # == Schema Information | ||
| # | ||
| # Table name: judges | ||
| # | ||
| # id :bigint not null, primary key | ||
| # active :boolean default(TRUE) | ||
| # name :string | ||
| # created_at :datetime not null | ||
| # updated_at :datetime not null | ||
| # casa_org_id :bigint not null | ||
| # | ||
| # Indexes | ||
| # | ||
| # index_judges_on_casa_org_id (casa_org_id) | ||
| # | ||
| # Foreign Keys | ||
| # | ||
| # fk_rails_... (casa_org_id => casa_orgs.id) | ||
| # |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,7 @@ | |
| <thead> | ||
| <tr> | ||
| <th>Name</th> | ||
| <th>Active</th> | ||
| <th>Active?</th> | ||
| <th>Actions</th> | ||
| </tr> | ||
| </thead> | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <div class="row"> | ||
| <div class="col-sm-12 dashboard-table-header pt-2 pb-2"> | ||
| <h3>Judge</h3> | ||
| <%= link_to "New Judge", new_judge_path, class: "btn btn-primary" %> | ||
| </div> | ||
| </div> | ||
|
|
||
| <table class="table table-striped table-bordered"> | ||
| <thead> | ||
| <tr> | ||
| <th>Name</th> | ||
| <th>Active?</th> | ||
| <th>Actions</th> | ||
| </tr> | ||
| </thead> | ||
|
|
||
| <tbody> | ||
| <% @judges.each do |judge| %> | ||
| <tr id="judge-<%= judge.id %>"> | ||
| <td scope="row"> | ||
| <%= judge.name %> | ||
| </td> | ||
| <td scope="row"> | ||
| <%= judge.active ? "Yes" : "No" %> | ||
| </td> | ||
| <td> | ||
| <%= link_to "Edit", edit_judge_path(judge) %> | ||
| </td> | ||
| </tr> | ||
| <% end %> | ||
| </tbody> | ||
| </table> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <%= link_to 'Back', :back %> | ||
|
|
||
| <h1><%= title %></h1> | ||
|
|
||
| <div class="card card-container"> | ||
| <div class="card-body"> | ||
| <%= form_with(model: judge, local: true) do |form| %> | ||
| <%= render "/shared/error_messages", resource: judge %> | ||
| <div class="field form-group"> | ||
| <%= form.label :name %> | ||
| <%= form.text_field :name, class: "form-control" %> | ||
| </div> | ||
| <div class="field form-group"> | ||
| <%= form.check_box :active %> | ||
| <%= form.label :active %> | ||
| </div> | ||
| <div class="actions"> | ||
| <%= form.submit "Submit", class: "btn btn-primary" %> | ||
| </div> | ||
| <% end %> | ||
| </div> | ||
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| <%= render partial: "form", locals: { title: "Edit Judge", judge: @judge } %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| <%= render partial: "form", locals: { title: "New Judge", judge: @judge } %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| class CreateJudges < ActiveRecord::Migration[6.0] | ||
| def change | ||
| create_table :judges do |t| | ||
| t.references :casa_org, null: false, foreign_key: true | ||
|
|
||
| t.timestamps | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| class AddActiveToJudge < ActiveRecord::Migration[6.0] | ||
| def change | ||
| add_column :judges, :active, :boolean, default: true | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| class AddNameToJudge < ActiveRecord::Migration[6.0] | ||
| def change | ||
| add_column :judges, :name, :string | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| FactoryBot.define do | ||
| factory :judge do | ||
| casa_org { create(:casa_org) } | ||
| name { Faker::Name.name } | ||
| active { true } | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| require "rails_helper" | ||
|
|
||
| RSpec.describe Judge, type: :model do | ||
| it { is_expected.to belong_to(:casa_org) } | ||
| it { is_expected.to validate_presence_of(:name) } | ||
|
littleforest marked this conversation as resolved.
|
||
|
|
||
| it "has a valid factory" do | ||
| judge = build(:judge) | ||
| expect(judge.valid?).to be true | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| require "rails_helper" | ||
|
|
||
| RSpec.describe "/judges", type: :request do | ||
| describe "GET /judges/new" do | ||
| context "logged in as admin user" do | ||
| it "can successfully access a judge create page" do | ||
| sign_in_as_admin | ||
|
|
||
| get new_judge_path | ||
|
|
||
| expect(response).to be_successful | ||
| end | ||
| end | ||
|
|
||
| context "logged in as a non-admin user" do | ||
| it "cannot access a judge create page" do | ||
| sign_in_as_volunteer | ||
|
|
||
| get new_judge_path | ||
|
|
||
| expect(response).to redirect_to root_path | ||
| expect(response.request.flash[:notice]).to eq "Sorry, you are not authorized to perform this action." | ||
| end | ||
| end | ||
|
|
||
| context "unauthenticated request" do | ||
| it "cannot access a judge create page" do | ||
| get new_judge_path | ||
|
|
||
| expect(response).to redirect_to new_user_session_path | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe "POST /judges" do | ||
| let(:params) { {judge: {name: "Joe Judge", active: true}} } | ||
| context "logged in as admin user" do | ||
| it "can successfully create a judge" do | ||
| casa_org = create(:casa_org) | ||
| sign_in create(:casa_admin, casa_org: casa_org) | ||
|
|
||
| expect { | ||
| post judges_path, params: params | ||
| }.to change(Judge, :count).by(1) | ||
|
|
||
| judge = Judge.last | ||
|
|
||
| expect(judge.name).to eql "Joe Judge" | ||
| expect(judge.casa_org).to eql casa_org | ||
| expect(judge.active).to be_truthy | ||
| expect(response).to redirect_to edit_casa_org_path(casa_org) | ||
| expect(response.request.flash[:notice]).to eq "Judge was successfully created." | ||
| end | ||
| end | ||
|
|
||
| context "logged in as a non-admin user" do | ||
| it "cannot create a judge" do | ||
| sign_in_as_volunteer | ||
|
|
||
| post judges_path, params: params | ||
|
|
||
| expect(response).to redirect_to root_path | ||
| expect(response.request.flash[:notice]).to eq "Sorry, you are not authorized to perform this action." | ||
| end | ||
| end | ||
|
|
||
| context "unauthenticated request" do | ||
| it "cannot create a judge" do | ||
| post judges_path, params: params | ||
|
|
||
| expect(response).to redirect_to new_user_session_path | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe "GET /judges/:id/edit" do | ||
| let(:judge) { create(:judge) } | ||
|
|
||
| context "logged in as admin user" do | ||
| it "can successfully access a judge edit page" do | ||
| sign_in_as_admin | ||
|
|
||
| get edit_judge_path(judge) | ||
|
|
||
| expect(response).to be_successful | ||
| end | ||
| end | ||
|
|
||
| context "logged in as a non-admin user" do | ||
| it "cannot access a judge edit page" do | ||
| sign_in_as_volunteer | ||
|
|
||
| get edit_judge_path(judge) | ||
|
|
||
| expect(response).to redirect_to root_path | ||
| expect(response.request.flash[:notice]).to eq "Sorry, you are not authorized to perform this action." | ||
| end | ||
| end | ||
|
|
||
| context "unauthenticated request" do | ||
| it "cannot access a judge edit page" do | ||
| get edit_judge_path(judge) | ||
|
|
||
| expect(response).to redirect_to new_user_session_path | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe "PUT /judges/:id" do | ||
| let(:judge) { create(:judge) } | ||
| let(:params) { {judge: {name: "New Name", judge_id: judge.id, active: false}} } | ||
| context "logged in as admin user" do | ||
| it "can successfully update a judge" do | ||
| casa_org = create(:casa_org) | ||
| sign_in create(:casa_admin, casa_org: casa_org) | ||
|
|
||
| put judge_path(judge), params: params | ||
|
|
||
| judge.reload | ||
| expect(judge.name).to eq "New Name" | ||
| expect(judge.active).to be_falsey | ||
|
|
||
| expect(response).to redirect_to edit_casa_org_path(casa_org) | ||
| expect(response.request.flash[:notice]).to eq "Judge was successfully updated." | ||
| end | ||
| end | ||
|
|
||
| context "logged in as a non-admin user" do | ||
| it "cannot update a judge" do | ||
| sign_in_as_volunteer | ||
|
|
||
| put judge_path(judge), params: params | ||
|
|
||
| expect(response).to redirect_to root_path | ||
| expect(response.request.flash[:notice]).to eq "Sorry, you are not authorized to perform this action." | ||
| end | ||
| end | ||
|
|
||
| context "unauthenticated request" do | ||
| it "cannot update a judge" do | ||
| put judge_path(judge), params: params | ||
|
|
||
| expect(response).to redirect_to new_user_session_path | ||
| end | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is fine, but for future simplicity, you can just write it like:
And it will automatically create the
casa_orgassociation.