Commit 10c58f7
Changed files (4)
app
controllers
models
spec
controllers
factories
app/controllers/items_controller.rb
@@ -1,5 +1,7 @@
class ItemsController < ApplicationController
def index
+ current_user = User.find(session[:user_id])
+ @items = current_user.items
end
def show
app/models/user.rb
@@ -1,5 +1,6 @@
class User < ActiveRecord::Base
has_secure_password
+ has_many :items
USERNAME_REGEX=/\A[-a-z0-9_.]*\z/i
validates :username, presence: true, format: { with: USERNAME_REGEX }, uniqueness: true
spec/controllers/items_controller_spec.rb
@@ -2,4 +2,22 @@ require 'rails_helper'
RSpec.describe ItemsController, type: :controller do
+ context "when logged in" do
+ let(:user) { create(:user) }
+
+ before :each do
+ session[:user_id] = user.id
+ end
+
+ describe "#index" do
+ let(:other_user) { create(:user) }
+ let!(:my_item) { create(:item, user: user) }
+ let!(:their_item) { create(:item, user: other_user) }
+
+ it 'loads all your items' do
+ get :index
+ expect(assigns(:items)).to match_array([my_item])
+ end
+ end
+ end
end
spec/factories/users.rb
@@ -1,7 +1,7 @@
FactoryGirl.define do
factory :user do
- username Faker::Internet.user_name
- email Faker::Internet.email
+ username { Faker::Internet.user_name }
+ email { Faker::Internet.email }
password "password"
password_confirmation "password"
terms_and_conditions "1"