Skip to content
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

Tweak ruby version and exercise assignments #3

Merged
merged 6 commits into from
Oct 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ruby-2.7.2
ruby-2.7.4
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ruby 2.7.2
ruby 2.7.4
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby "2.7.2"
ruby "2.7.4"

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem "rails", "~> 6.1.3.1"
Expand Down
10 changes: 8 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,13 @@ GEM
method_source (1.0.0)
methodfinder (2.2.1)
mini_mime (1.0.3)
mini_portile2 (2.5.3)
minitest (5.14.4)
msgpack (1.3.3)
nio4r (2.5.7)
nokogiri (1.11.3)
mini_portile2 (~> 2.5.0)
racc (~> 1.4)
nokogiri (1.11.3-x86_64-darwin)
racc (~> 1.4)
object_shadow (1.1.1)
Expand Down Expand Up @@ -243,7 +247,9 @@ GEM
zeitwerk (2.4.2)

PLATFORMS
ruby
x86_64-darwin-19
x86_64-darwin-20

DEPENDENCIES
annotate
Expand All @@ -264,7 +270,7 @@ DEPENDENCIES
tzinfo-data

RUBY VERSION
ruby 2.7.2p137
ruby 2.7.4p191

BUNDLED WITH
2.2.2
2.2.29
12 changes: 10 additions & 2 deletions app/controllers/exercise_assignments_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class ExerciseAssignmentsController < ApplicationController
before_action :set_exercise_assignment, only: [:show, :destroy]
before_action :set_exercise_assignment, only: [:show, :destroy, :update]

def index
@exercise_assignments = ExerciseAssignment.all
Expand All @@ -21,6 +21,14 @@ def create
end
end

def update
if @exercise_assignment.update(exercise_assignment_params)
render json: @exercise_assignment
else
render json: @exercise_assignment.errors, status: :unprocessable_entity
end
end

def destroy
@exercise_assignment.destroy
end
Expand All @@ -31,6 +39,6 @@ def set_exercise_assignment
end

def exercise_assignment_params
params.require(:exercise_assignment).permit(:exercise_id, :member_id)
params.require(:exercise_assignment).permit(:exercise_id, :member_id, data: {})
end
end
1 change: 1 addition & 0 deletions app/models/exercise_assignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#
# id :bigint not null, primary key
# completed_at :datetime
# data :jsonb
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I've had to explain this a few times so this settles that bit. :)

# created_at :datetime not null
# updated_at :datetime not null
# exercise_id :string
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Rails.application.routes.draw do
resources :members
resources :exercise_assignments, except: [:edit, :update]
resources :exercise_assignments
end
5 changes: 5 additions & 0 deletions db/migrate/20211020212234_add_data_to_exercise_assignments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddDataToExerciseAssignments < ActiveRecord::Migration[6.1]
def change
add_column :exercise_assignments, :data, :jsonb, default: {}
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions spec/factories/exercise_assignments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#
# id :bigint not null, primary key
# completed_at :datetime
# data :jsonb
# created_at :datetime not null
# updated_at :datetime not null
# exercise_id :string
Expand Down
1 change: 1 addition & 0 deletions spec/models/exercise_assignment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#
# id :bigint not null, primary key
# completed_at :datetime
# data :jsonb
# created_at :datetime not null
# updated_at :datetime not null
# exercise_id :string
Expand Down
36 changes: 25 additions & 11 deletions spec/requests/exercise_assignments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,38 @@

RSpec.describe "/exercise_assignments", type: :request do
describe "GET /index" do
let(:exercise_assignments) { FactoryBot.create_list(:exercise_assignment, 3) }
let!(:exercise_assignments) { FactoryBot.create_list(:exercise_assignment, 3) }

it "renders a successful response" do
get exercise_assignments_url
get exercise_assignments_path
expect(response).to be_successful
end
end

describe "GET /show" do
let(:exercise_assignment) { FactoryBot.create(:exercise_assignment) }
let!(:exercise_assignment) { FactoryBot.create(:exercise_assignment) }

it "renders a successful response" do
get exercise_assignment_url(exercise_assignment)
get exercise_assignment_path(exercise_assignment)
expect(response).to be_successful
end
end

describe "POST /create" do
let!(:member) { FactoryBot.create(:member) }
let!(:exercise_id) { Exercise.ids.sample }
let(:member) { FactoryBot.create(:member) }
let(:exercise_id) { Exercise.ids.sample }

context "with valid parameters" do
let(:params) { {exercise_assignment: { member_id: member.id, exercise_id: exercise_id }} }

it "creates a new ExerciseAssignment" do
expect {
post exercise_assignments_url, params: params
post exercise_assignments_path, params: params
}.to change(ExerciseAssignment, :count).by(1)
end

it "renders a JSON response with the new exercise_assignment" do
post exercise_assignments_url, params: params
post exercise_assignments_path, params: params
expect(response).to have_http_status(:created)
expect(response.content_type).to match(a_string_including("application/json"))
end
Expand All @@ -44,24 +44,38 @@

it "does not create a new ExerciseAssignment" do
expect {
post exercise_assignments_url, params: params
post exercise_assignments_path, params: params
}.to change(ExerciseAssignment, :count).by(0)
end

it "renders a JSON response with errors for the new exercise_assignment" do
post exercise_assignments_url, params: params
post exercise_assignments_path, params: params
expect(response).to have_http_status(:unprocessable_entity)
expect(response.content_type).to match(a_string_including("application/json"))
end
end
end

describe "PATCH /update" do
let(:member) { FactoryBot.create(:member) }
let!(:exercise_assignment) { FactoryBot.create(:exercise_assignment, :completed, data: {"calm_level_before" => "2", "calm_level_after" => "7"}) }
let(:params) { {exercise_assignment: {data: {"calm_level_before" => "8", "calm_level_after" => "4"} }} }

it "updates an existing ExerciseAssignment" do
expect {
patch exercise_assignment_path(exercise_assignment), params: params
}.to change { exercise_assignment.reload.data }
.from({"calm_level_before" => "2", "calm_level_after" => "7"})
.to ({"calm_level_before" => "8", "calm_level_after" => "4"})
end
end

describe "DELETE /destroy" do
let!(:exercise_assignment) { FactoryBot.create(:exercise_assignment) }

it "destroys the requested exercise_assignment" do
expect {
delete exercise_assignment_url(exercise_assignment)
delete exercise_assignment_path(exercise_assignment)
}.to change(ExerciseAssignment, :count).by(-1)
end
end
Expand Down
20 changes: 10 additions & 10 deletions spec/requests/members_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
let!(:members) { FactoryBot.create_list(:member, 2) }

it "renders a successful response" do
get members_url
get members_path
expect(response).to be_successful
end
end
Expand All @@ -17,7 +17,7 @@
let!(:member) { FactoryBot.create(:member) }

it "renders a successful response" do
get member_url(member)
get member_path(member)
expect(response).to be_successful
end
end
Expand All @@ -26,25 +26,25 @@
context "with valid parameters" do
it "creates a new Member" do
expect {
post members_url, params: { member: valid_attributes }
post members_path, params: { member: valid_attributes }
}.to change(Member, :count).by(1)
end

it "renders a JSON response with the new member" do
post members_url, params: { member: valid_attributes }
post members_path, params: { member: valid_attributes }
expect(response).to have_http_status(:created)
end
end

context "with invalid parameters" do
it "does not create a new Member" do
expect {
post members_url, params: { member: invalid_attributes }
post members_path, params: { member: invalid_attributes }
}.to change(Member, :count).by(0)
end

it "renders a JSON response with errors for the new member" do
post members_url, params: { member: invalid_attributes }
post members_path, params: { member: invalid_attributes }
expect(response).to have_http_status(:unprocessable_entity)

errors = JSON.parse(response.body)
Expand All @@ -61,14 +61,14 @@

context "with valid parameters" do
it "updates the requested member" do
patch member_url(member), params: { member: valid_attributes }
patch member_path(member), params: { member: valid_attributes }

expect(member.reload.full_name).to eq("Lara Croft")
end

it "renders a JSON response with the member" do
member = Member.create! valid_attributes
patch member_url(member), params: { member: valid_attributes }
patch member_path(member), params: { member: valid_attributes }

expect(response).to have_http_status(:ok)

Expand All @@ -80,7 +80,7 @@

context "with invalid parameters" do
it "renders a JSON response with errors for the member" do
patch member_url(member), params: { member: invalid_attributes }
patch member_path(member), params: { member: invalid_attributes }

expect(response).to have_http_status(:unprocessable_entity)

Expand All @@ -98,7 +98,7 @@

it "destroys the requested member" do
expect {
delete member_url(member)
delete member_path(member)
}.to change(Member, :count).by(-1)
end
end
Expand Down