-
Notifications
You must be signed in to change notification settings - Fork 4
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
Implementing a button for Presence in the 2de kamer #449
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class V1::StudyRoomPresencesController < V1::ApplicationController | ||
end |
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class StudyRoomPresencePolicy < ApplicationPolicy | ||
end |
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 |
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ def create_permissions(permission_map) | |
'article' => %i[create read update destroy], | ||
'article_comment' => %i[create read update destroy], | ||
'board_room_presence' => %i[create read update destroy], | ||
'study_room_presence' => %i[create read update destroy], | ||
'book' => %i[create read update destroy], | ||
'group' => %i[create read update destroy], | ||
'membership' => %i[create read update destroy], | ||
|
@@ -65,6 +66,7 @@ def create_permissions(permission_map) | |
'article' => %i[create read], | ||
'article_comment' => %i[create read], | ||
'board_room_presence' => %i[read], | ||
'study_room_presence' => %i[create read], | ||
'book' => %i[read], | ||
'group' => %i[read], | ||
'membership' => %i[read], | ||
|
@@ -101,6 +103,7 @@ def create_permissions(permission_map) | |
'article' => %i[read], | ||
'article_comment' => %i[create read], | ||
'board_room_presence' => %i[read], | ||
'study_room_presence' => %i[read], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not so sure about this one. why do old members need to know this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it is the same type of information as board room presence, and oudleden are able to view that so that is why i made this choice, however, I do agree and this is not strictly needed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removing the permission for oudleden to view both would be the most logical thing. in my opinion. How do you think about it? |
||
'book' => [], | ||
'group' => [], | ||
'poll' => [], | ||
|
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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.
nice find