-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
235 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<h1 align="center">Create Category</h1> | ||
|
||
<%= render 'form' %> |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |