-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementing a button for Presence in the 2de kamer (#449)
* intial commit * Remove a mistake from permissions.rb * added migration file * Fixed lint
- Loading branch information
Showing
18 changed files
with
265 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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,2 @@ | ||
class V1::StudyRoomPresencesController < V1::ApplicationController | ||
end |
This file contains 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,20 @@ | ||
class StudyRoomPresence < ApplicationRecord | ||
belongs_to :user | ||
|
||
validates :start_time, presence: true | ||
validates :end_time, presence: true | ||
validates_datetime :end_time, after: :start_time | ||
validates :status, inclusion: { in: %w[chilling studying banaan] } | ||
|
||
scope :current, (lambda { | ||
where('start_time <= :current_time AND end_time >= :current_time', | ||
current_time: Time.current) | ||
}) | ||
scope :future, (lambda { | ||
where('start_time >= :current_time', current_time: Time.current) | ||
}) | ||
scope :current_and_future, (lambda { | ||
where('(start_time <= :current_time AND end_time >= :current_time) | ||
or (start_time >= :current_time)', current_time: Time.current) | ||
}) | ||
end |
This file contains 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 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,2 @@ | ||
class StudyRoomPresencePolicy < ApplicationPolicy | ||
end |
This file contains 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,17 @@ | ||
class V1::StudyRoomPresenceResource < V1::ApplicationResource | ||
attributes :start_time, :end_time, :status | ||
|
||
has_one :user, always_include_linkage_data: true | ||
|
||
filter :current, apply: ->(records, _value, _options) { records.current } | ||
filter :future, apply: ->(records, _value, _options) { records.future } | ||
filter :current_and_future, apply: ->(records, _value, _options) { records.current_and_future } | ||
|
||
before_create do | ||
@model.user_id = current_user.id | ||
end | ||
|
||
def self.creatable_fields(_context) | ||
%i[start_time end_time status] | ||
end | ||
end |
This file contains 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 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,17 @@ | ||
class CreateStudyRoomPresence < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :study_room_presences do |t| | ||
t.datetime :start_time, null: false | ||
t.datetime :end_time, null: false | ||
t.text :status, null: false | ||
t.integer :user_id, null: false | ||
t.datetime :deleted_at | ||
t.timestamps | ||
end | ||
end | ||
|
||
Permission.create(name: 'study_room_presence.create') | ||
Permission.create(name: 'study_room_presence.read') | ||
Permission.create(name: 'study_room_presence.update') | ||
Permission.create(name: 'study_room_presence.destroy') | ||
end |
This file contains 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 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 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 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 @@ | ||
FactoryBot.define do | ||
factory :study_room_presence do | ||
start_time { Faker::Time.between(from: 3.days.ago, to: 5.days.ago) } | ||
end_time { Faker::Time.between(from: 10.days.from_now, to: 5.days.from_now) } | ||
status { %w[chilling studying banaan].sample } | ||
user | ||
|
||
trait(:future) { start_time { Faker::Time.between(from: 1.day.from_now, to: 4.days.from_now) } } | ||
trait(:history) { end_time { Faker::Time.between(from: 2.days.ago, to: 1.day.ago) } } | ||
end | ||
end |
This file contains 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 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,112 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe StudyRoomPresence, type: :model do | ||
subject(:study_room_presence) { build_stubbed(:study_room_presence) } | ||
|
||
describe '#valid?' do | ||
it { expect(study_room_presence).to be_valid } | ||
|
||
context 'when without start time' do | ||
subject(:study_room_presence) do | ||
build_stubbed(:study_room_presence, start_time: nil) | ||
end | ||
|
||
it { expect(study_room_presence).not_to be_valid } | ||
end | ||
|
||
context 'when without end time' do | ||
subject(:study_room_presence) do | ||
build_stubbed(:study_room_presence, end_time: nil) | ||
end | ||
|
||
it { expect(study_room_presence).not_to be_valid } | ||
end | ||
|
||
context 'when end time before start time' do | ||
subject(:study_room_presence) do | ||
build_stubbed(:study_room_presence, | ||
end_time: 1.day.ago, start_time: 1.day.from_now) | ||
end | ||
|
||
it { expect(study_room_presence).not_to be_valid } | ||
end | ||
|
||
context 'when without status' do | ||
subject(:study_room_presence) { build_stubbed(:study_room_presence, status: nil) } | ||
|
||
it { expect(study_room_presence).not_to be_valid } | ||
end | ||
|
||
context 'when without a user' do | ||
subject(:study_room_presence) { build_stubbed(:study_room_presence, user: nil) } | ||
|
||
it { expect(study_room_presence).not_to be_valid } | ||
end | ||
end | ||
|
||
describe '#current' do | ||
context 'when started in the past and ended in the future' do | ||
before { create(:study_room_presence) } | ||
|
||
it { expect(described_class.current.count).to eq 1 } | ||
end | ||
|
||
context 'when not yet started' do | ||
before { create(:study_room_presence, start_time: 1.minute.from_now) } | ||
|
||
it { expect(described_class.current.count).to eq 0 } | ||
end | ||
|
||
context 'when just ended' do | ||
before { create(:study_room_presence, end_time: 1.second.ago) } | ||
|
||
it { expect(described_class.current.count).to eq 0 } | ||
end | ||
end | ||
|
||
describe '#future' do | ||
context 'when started in the past and ended in the future' do | ||
before { create(:study_room_presence) } | ||
|
||
it { expect(described_class.future.count).to eq 0 } | ||
end | ||
|
||
context 'when not yet started' do | ||
before { create(:study_room_presence, start_time: 1.minute.from_now) } | ||
|
||
it { expect(described_class.future.count).to eq 1 } | ||
end | ||
|
||
context 'when just ended' do | ||
before { create(:study_room_presence, end_time: 1.second.ago) } | ||
|
||
it { expect(described_class.future.count).to eq 0 } | ||
end | ||
end | ||
|
||
describe '#current_and_future' do | ||
context 'when started in the past and ended in the future' do | ||
before { create(:study_room_presence) } | ||
|
||
it { expect(described_class.current_and_future.count).to eq 1 } | ||
end | ||
|
||
context 'when not yet started' do | ||
before { create(:study_room_presence, start_time: 1.minute.from_now) } | ||
|
||
it { expect(described_class.current_and_future.count).to eq 1 } | ||
end | ||
|
||
context 'when just ended' do | ||
before { create(:study_room_presence, end_time: 1.second.ago) } | ||
|
||
it { expect(described_class.current_and_future.count).to eq 0 } | ||
end | ||
|
||
context 'when starting in the future' do | ||
before { create(:study_room_presence, start_time: 1.second.from_now) } | ||
|
||
it { expect(described_class.current_and_future.count).to eq 1 } | ||
end | ||
end | ||
end |
12 changes: 12 additions & 0 deletions
12
spec/requests/v1/study_room_presence_controller/create_spec.rb
This file contains 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,12 @@ | ||
require 'rails_helper' | ||
|
||
describe V1::StudyRoomPresencesController do | ||
describe 'POST /study_room_presences/:id', version: 1 do | ||
it_behaves_like 'a creatable and permissible model' do | ||
let(:record) { build(:study_room_presence) } | ||
let(:record_url) { '/v1/study_room_presences' } | ||
let(:record_permission) { 'study_room_presence.create' } | ||
let(:invalid_attributes) { { status: '' } } | ||
end | ||
end | ||
end |
11 changes: 11 additions & 0 deletions
11
spec/requests/v1/study_room_presence_controller/destroy_spec.rb
This file contains 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' | ||
|
||
describe V1::StudyRoomPresencesController do | ||
describe 'DELETE /study_room_presences/:id', version: 1 do | ||
it_behaves_like 'a destroyable and permissible model' do | ||
let(:record) { create(:study_room_presence) } | ||
let(:record_url) { "/v1/study_room_presences/#{record.id}" } | ||
let(:record_permission) { 'study_room_presence.destroy' } | ||
end | ||
end | ||
end |
13 changes: 13 additions & 0 deletions
13
spec/requests/v1/study_room_presence_controller/index_spec.rb
This file contains 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,13 @@ | ||
require 'rails_helper' | ||
|
||
describe V1::StudyRoomPresencesController do | ||
describe 'GET /study_room_presences', version: 1 do | ||
let(:records) { create_list(:study_room_presence, 3) } | ||
let(:record_url) { '/v1/study_room_presences' } | ||
let(:record_permission) { 'study_room_presence.read' } | ||
let(:request) { get(record_url) } | ||
|
||
it_behaves_like 'a permissible model' | ||
it_behaves_like 'an indexable model' | ||
end | ||
end |
12 changes: 12 additions & 0 deletions
12
spec/requests/v1/study_room_presence_controller/show_spec.rb
This file contains 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,12 @@ | ||
require 'rails_helper' | ||
|
||
describe V1::StudyRoomPresencesController do | ||
describe 'GET /study_room_presences/:id', version: 1 do | ||
it_behaves_like 'a permissible model' do | ||
let(:record) { create(:study_room_presence) } | ||
let(:record_url) { "/v1/study_room_presences/#{record.id}" } | ||
let(:record_permission) { 'study_room_presence.read' } | ||
let(:valid_request) { get(record_url) } | ||
end | ||
end | ||
end |
12 changes: 12 additions & 0 deletions
12
spec/requests/v1/study_room_presence_controller/update_spec.rb
This file contains 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,12 @@ | ||
require 'rails_helper' | ||
|
||
describe V1::StudyRoomPresencesController do | ||
describe 'PUT /study_room_presences/:id', version: 1 do | ||
it_behaves_like 'an updatable and permissible model' do | ||
let(:record) { create(:study_room_presence) } | ||
let(:record_url) { "/v1/study_room_presences/#{record.id}" } | ||
let(:record_permission) { 'study_room_presence.update' } | ||
let(:invalid_attributes) { { status: '' } } | ||
end | ||
end | ||
end |