Skip to content

Commit

Permalink
Create User#next_on_room_activities
Browse files Browse the repository at this point in the history
  • Loading branch information
hmlON committed Jul 19, 2017
1 parent 4321751 commit 31f780c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Rails:

Metrics/LineLength:
Max: 120
Exclude:
- 'spec/**/*'

Metrics/AbcSize:
Max: 20
Expand Down
6 changes: 6 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ class User < ApplicationRecord

default_scope { order(:id) }

delegate :room_activities, to: :room

devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable, omniauth_providers: [:google_oauth2, :vkontakte]

validates :name, presence: true

def next_on_room_activities
room_activities.select { |room_activity| room_activity.next_on_user == self }
end

def join_room(room)
room.users << self
room.room_actions.each do |room_action|
Expand Down
10 changes: 10 additions & 0 deletions spec/factories/rooms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
factory :room do
name { Faker::Hipster.word }

trait :with_activities do
after :create do |room|
users = create_list :user, 2, room: room
room_activity = create(:room_activity, room: room)
users.each.with_index(1) do |user, i|
create_list(:activity, i, room_activity: room_activity, user: user)
end
end
end

trait :with_old_setup do
after :build do |room|
room.actions << create_list(:action, 3)
Expand Down
23 changes: 19 additions & 4 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
RSpec.describe User, type: :model do
subject { create(:user) }
let(:user) { create :user }
let(:room) { create :room }
subject { user }

describe '#next_on_room_activities' do
let(:another_user) { create :user, room: room }
let(:room_activity_with_less_activities) { create :room_activity, room: room }
let(:room_activity_with_more_activities) { create :room_activity, room: room }
let!(:less_activities_1) { create_list(:activity, 1, room_activity: room_activity_with_less_activities, user: user) }
let!(:less_activities_2) { create_list(:activity, 2, room_activity: room_activity_with_less_activities, user: another_user) }
let!(:more_activities_1) { create_list(:activity, 2, room_activity: room_activity_with_more_activities, user: user) }
let!(:more_activities_2) { create_list(:activity, 1, room_activity: room_activity_with_more_activities, user: another_user) }

before { room.users << user }

it 'returns room activities which he completed least times compared to his roommates' do
expect(subject.next_on_room_activities.to_a).to eq [room_activity_with_less_activities]
end
end

describe '#room?' do
it 'returns true when user has room' do
room = create(:room)
room.users << subject

expect(subject.room?).to eq true
end

it 'returns false when user has no room' do
subject.room = nil

expect(subject.room?).to eq false
end
end
Expand Down

0 comments on commit 31f780c

Please sign in to comment.