Skip to content
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
43 changes: 43 additions & 0 deletions app/controllers/categories_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,45 @@
class CategoriesController < ApplicationController
def index
@categories = Category.all
end

def show
@category = Category.find_by(id: params[:id])
if @category.nil?
head :not_found
return
end
end

def new
@category = Category.new
end

def create
@category = Category.new( category_params )
if @category.save
redirect_to category_path(@category.id)
else
render new_category_path
end
end

def edit
@category = Category.find_by(id: params[:id] )
end

def update
@category = Category.find_by(id: params[:id] )
if @category.update( category_params )
redirect_to category_path(@category.id)
else
render new_category_path
end
end

private

def category_params
return params.require(:category).permit(:name)
end
end
2 changes: 2 additions & 0 deletions app/models/category.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
class Category < ApplicationRecord
has_and_belongs_to_many :products

validates :name, presence: true, uniqueness: true
end
Empty file.
Empty file.
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
delete "/logout", to: "users#destroy", as: "logout"

resources :products, except: :destroy
resources :categories, except: :destroy
end
43 changes: 40 additions & 3 deletions test/controllers/categories_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,44 @@
require "test_helper"

describe CategoriesController do
# it "does a thing" do
# value(1+1).must_equal 2
# end
describe "index action" do
it "gives back a successful response" do
get categories_path

must_respond_with :success
end
end

describe 'show action' do
it 'responds with a success when id given exists' do

get category_path(categories(:valid_category).id)

must_respond_with :success

end

it 'responds with a not_found when id given does not exist' do

get category_path("-500")

must_respond_with :not_found
end
end

describe "create" do
it 'creates a new category successfully and redirects the user to the category page' do
category_hash = {
category: {
name: "New Category"
}
}

expect {
post categories_path, params: category_hash
}.must_differ 'Category.count', 1

must_redirect_to category_path(Category.find_by(name: "New Category"))
end
end
end
5 changes: 5 additions & 0 deletions test/fixtures/categories.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
valid_category:
name: ExpensiveItems

valid_category2:
name: EvenPricierItems
28 changes: 25 additions & 3 deletions test/models/category_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
require "test_helper"

describe Category do
# it "does a thing" do
# value(1+1).must_equal 2
# end
describe "validations" do
it "can be valid" do
is_valid = categories(:valid_category).valid?

assert( is_valid )
end

it "is invalid if there is no name" do
invalid_category = categories(:valid_category)
invalid_category.name = ""

is_valid = invalid_category.valid?

refute( is_valid )
end

it "gives an error message if the name given is not unique" do
invalid_category = categories(:valid_category2)
invalid_category.name = "ExpensiveItems"

is_valid = invalid_category.valid?

refute( is_valid )
end
end
end