@ -0,0 +1,3 @@ | |||
// Place all the styles related to the category controller here. | |||
// They will automatically be included in application.css. | |||
// You can use Sass (SCSS) here: http://sass-lang.com/ |
@ -0,0 +1,50 @@ | |||
class CategoryController < ApplicationController | |||
before_action :auth_user, only: [:manage, :create, :delete, :update] | |||
def get | |||
@blog = Blog.find_by_urlName(params[:blog_name]) | |||
if @blog.nil? | |||
render file: "#{Rails.root}/public/404.html" , status: :not_found | |||
return | |||
end | |||
@cat = @blog.categories.find_by_path(params[:cat_name]) | |||
if @cat.nil? | |||
render file: "#{Rails.root}/public/404.html" , status: :not_found | |||
return | |||
end | |||
@posts = @cat.posts | |||
end | |||
def manage | |||
@blog = get_user_blog | |||
@cats = @blog.categories.order(id: :asc) | |||
@category = Category.new | |||
end | |||
def create | |||
@category = Category.new | |||
@blog = get_user_blog | |||
c = @blog.categories.new(params[:category].permit(:name)) | |||
unless c.save | |||
flash.now[:alerts] = c.errors.full_messages | |||
@category = c | |||
end | |||
@cats = @blog.categories.all | |||
render :manage | |||
end | |||
def delete | |||
@category = Category.new | |||
@blog = get_user_blog | |||
@cats = @blog.categories | |||
c = @cats.find_by_path(params[:cat_name]) | |||
c.destroy unless c.nil? | |||
render :manage | |||
end | |||
def update | |||
blog = get_user_blog | |||
c = blog.categories.find_by_path(params[:cat_name]) | |||
c.update(params[:category].permit(:name)) | |||
redirect_to manage_category_path | |||
end | |||
end |
@ -0,0 +1,2 @@ | |||
module CategoryHelper | |||
end |
@ -0,0 +1,22 @@ | |||
class Category < ApplicationRecord | |||
belongs_to :blog | |||
has_and_belongs_to_many :posts, -> { distinct } | |||
validates_uniqueness_of :name, uniqueness: { scope: :blog_id } | |||
validates_uniqueness_of :path, uniqueness: { scope: :blog_id } | |||
validates_length_of :name, in: 3..30 | |||
before_validation :set_path | |||
def set_path | |||
self.path = self.name.parameterize | |||
end | |||
def to_param | |||
{ | |||
blog_name: self.blog.urlName, | |||
cat_name: self.path | |||
} | |||
end | |||
end |
@ -0,0 +1,5 @@ | |||
<h1>Posts Tagged Under "<%= @cat.name %>"</h1> | |||
<hr> | |||
<% @posts.each do |p| %> | |||
<%= render p %> | |||
<% end %> |
@ -0,0 +1,31 @@ | |||
<h2 style="margin-bottom: 0px;"> | |||
<%= @blog.title %>'s Categories | |||
</h2> | |||
<br> | |||
<% #if @pages.length > 0 %> | |||
<table style="margin-top: 15px;"> | |||
<tr> | |||
<th>Name</th> | |||
</tr> | |||
<tr> | |||
<%= form_for @category, url: manage_category_path do |f| %> | |||
<td><%= f.text_field :name %></td> | |||
<td><%= f.submit "Add", class: "btn btn-sm btn-primary" %></td> | |||
<% end %> | |||
</tr> | |||
<% @cats.each do |c| %> | |||
<tr> | |||
<%= form_for c, url: update_category_path(c.to_param) do |f| %> | |||
<td><%= f.text_field :name %></td> | |||
<td><%= f.submit "Update", class: "btn btn-sm btn-primary" %></td> | |||
<% end %> | |||
<td> | |||
<%= link_to 'Delete', | |||
delete_category_path(c.to_param), | |||
method: :delete, | |||
data: {confirm: "Are you sure?"}, | |||
class: "btn btn-sm btn-danger" %> | |||
</td> | |||
</tr> | |||
<% end %> | |||
</table> |
@ -0,0 +1,17 @@ | |||
class CreateCategories < ActiveRecord::Migration[6.0] | |||
def change | |||
create_table :categories do |t| | |||
t.string :name, index: true, null: false | |||
t.string :path, index: true, null: false | |||
t.references :blog, null: false, foreign_key: true, index: true | |||
t.index [:name, :blog_id], unique: true | |||
t.index [:path, :blog_id], unique: true | |||
t.timestamps | |||
end | |||
create_join_table :categories, :posts do |t| | |||
t.index :category_id | |||
t.index :post_id | |||
end | |||
end | |||
end |
@ -0,0 +1,7 @@ | |||
require 'test_helper' | |||
class CategoryControllerTest < ActionDispatch::IntegrationTest | |||
# test "the truth" do | |||
# assert true | |||
# end | |||
end |
@ -0,0 +1,9 @@ | |||
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html | |||
one: | |||
name: MyString | |||
blog: one | |||
two: | |||
name: MyString | |||
blog: two |
@ -0,0 +1,7 @@ | |||
require 'test_helper' | |||
class CategoryTest < ActiveSupport::TestCase | |||
# test "the truth" do | |||
# assert true | |||
# end | |||
end |