Skip to content

Commit

Permalink
Add categories model and tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
erickm32 committed Mar 1, 2017
1 parent 359c9f9 commit e4b1b55
Show file tree
Hide file tree
Showing 16 changed files with 235 additions and 3 deletions.
40 changes: 40 additions & 0 deletions .rake_tasks~
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
about
app:template
app:update
assets:clean[keep]
assets:clobber
assets:environment
assets:precompile
cache_digests:dependencies
cache_digests:nested_dependencies
db:create
db:drop
db:environment:set
db:fixtures:load
db:migrate
db:migrate:status
db:rollback
db:schema:cache:clear
db:schema:cache:dump
db:schema:dump
db:schema:load
db:seed
db:setup
db:structure:dump
db:structure:load
db:version
dev:cache
initializers
log:clear
middleware
notes
notes:custom
restart
routes
secret
stats
test
test:db
time:zones[country_or_offset]
tmp:clear
tmp:create
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ gem 'bootstrap-sass', '~> 3.3.6'
gem 'bcrypt', '~> 3.1.7'
gem 'will_paginate', '~> 3.0.7'
gem 'bootstrap-will_paginate', '~> 0.0.10'
gem 'rails-controller-testing'
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ GEM
bundler (>= 1.3.0, < 2.0)
railties (= 5.0.1)
sprockets-rails (>= 2.0.0)
rails-controller-testing (1.0.1)
actionpack (~> 5.x)
actionview (~> 5.x)
activesupport (~> 5.x)
rails-dom-testing (2.0.2)
activesupport (>= 4.2.0, < 6.0)
nokogiri (~> 1.6)
Expand Down Expand Up @@ -180,6 +184,7 @@ DEPENDENCIES
pg
puma (~> 3.0)
rails (~> 5.0.1)
rails-controller-testing
rails_12factor
sass-rails (~> 5.0)
spring
Expand Down
27 changes: 27 additions & 0 deletions app/controllers/categories_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class CategoriesController < ApplicationController
def index
@categories = Category.paginate(page: params[:page], per_page: 5)
end

def new
@category = Category.new
end

def create
@category = Category.new(category_params)
if @category.save
flash[:success] = "Category created successfully."
redirect_to categories_path
else
render 'new'
end
end

def show
end

private
def category_params
params.require(:category).permit(:name)
end
end
4 changes: 4 additions & 0 deletions app/models/category.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Category < ActiveRecord::Base
validates :name, presence: true, length: { minimum: 3, maximum: 25 }
validates_uniqueness_of :name
end
18 changes: 18 additions & 0 deletions app/views/categories/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<%= render 'shared/errors', obj: @category %>

<%= form_for(@category, :html => {class: "form-horizontal", role: 'form'}) do |f| %>
<div class="form-group">
<div class="control-label col-sm-2">
<%= f.label :category %>
</div>
<div class="col-sm-8">
<%= f.text_field :name, class: "form-control", placeholder: "Enter category name", autofocus: true %>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<%= f.submit "Create", class: 'btn btn-primary home-btn' %>
<%= link_to "Back to Articles List", articles_path, class: 'btn btn-secondary cancel-btn' %>
</div>
</div>
<% end %>
16 changes: 16 additions & 0 deletions app/views/categories/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<h1 align="center"> Listing all categories</h1>

<div align="center">
<% @categories.each do |category| %>
<ul class="listing">
<div class="row">
<div class="well col-md-4 col-md-offset-4">
<li class="article-title">
<%= link_to "#{category.name}", category_path(category) %>
</li>
</div>
</div>
</ul>
<% end %>
<%= will_paginate %>
</div>
3 changes: 3 additions & 0 deletions app/views/categories/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1 align="center">Create Category</h1>

<%= render 'form' %>
Empty file.
9 changes: 7 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root 'pages#home'
get 'about', to: 'pages#about'

resources :articles

get 'signup', to: 'users#new'
resources :users, except: [:new]

get 'login', to: 'sessions#new'
post 'login', to: 'sessions#create'
delete 'logout', to: 'sessions#destroy'
resources :users, except: [:new]
resources :articles

resources :categories, except: [:destroy]
end
8 changes: 8 additions & 0 deletions db/migrate/20170228143327_create_category.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CreateCategory < ActiveRecord::Migration[5.0]
def change
create_table :categories do |t|
t.string :name
t.timestamps
end
end
end
8 changes: 7 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170228021927) do
ActiveRecord::Schema.define(version: 20170228143327) do

create_table "articles", force: :cascade do |t|
t.string "title"
Expand All @@ -20,6 +20,12 @@
t.integer "user_id"
end

create_table "categories", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "users", force: :cascade do |t|
t.string "username"
t.string "email"
Expand Down
23 changes: 23 additions & 0 deletions test/controllers/categories_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'test_helper'

class CategoriesControllerTest < ActionController::TestCase

def setup
@category = Category.create(name: "sports")
end

test 'should get category index' do
get :index
assert_response :success
end

test 'should get category new' do
get :new
assert_response :success
end

test 'should get category show' do
get :show, params: { id: @category.id }
assert_response :success
end
end
26 changes: 26 additions & 0 deletions test/integration/create_categories_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'test_helper'

class CreateCategoriesTest < ActionDispatch::IntegrationTest
test "get new category test and create category" do
get new_category_path
assert_template 'categories/new'
assert_difference 'Category.count', 1 do
#post_via_redirect categories_path, params: { category: { name: "sports" } }
post categories_path, params: { category: { name: "sports" } }
follow_redirect!
end
assert_template 'categories/index'
assert_match "sports", response.body
end

test "invalid category submission results in failure" do
get new_category_path
assert_template 'categories/new'
assert_no_difference 'Category.count' do
post categories_path, params: { category: { name: "" } }
end
assert_template 'categories/new'
assert_select 'h2.panel-title'
assert_select 'div.panel-body'
end
end
16 changes: 16 additions & 0 deletions test/integration/list_categories_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'test_helper'

class ListCategoriesTest < ActionDispatch::IntegrationTest

def setup
@category = Category.create(name: "sports")
@category2 = Category.create(name: "programming")
end

test "should show categories listing" do
get categories_path
assert_template 'categories/index'
assert_select 'a[href=?]', category_path(@category), text: @category.name
assert_select 'a[href=?]', category_path(@category2), text: @category2.name
end
end
34 changes: 34 additions & 0 deletions test/models/category_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'test_helper'

class CategoryTest < ActiveSupport::TestCase

def setup
@category = Category.new(name: "sports")
end

test "category should be valid" do
assert @category.valid?
end

test 'category name should be present' do
@category.name = ""
assert_not @category.valid?
end

test 'category name should be unique' do
@category.save
category2 = Category.new(name: "sports")

assert_not category2.valid?
end

test 'category name should be too long' do
@category.name = "a" * 26
assert_not @category.valid?
end

test 'category name should be too short' do
@category.name = "aa"
assert_not @category.valid?
end
end

0 comments on commit e4b1b55

Please sign in to comment.