Browse Source

Added categories

pull/44/head
Stephen 6 months ago
parent
commit
eb368065f0
21 changed files with 264 additions and 3 deletions
  1. +2
    -0
      README.md
  2. +3
    -0
      app/assets/stylesheets/category.scss
  3. +10
    -0
      app/controllers/application_controller.rb
  4. +50
    -0
      app/controllers/category_controller.rb
  5. +31
    -0
      app/controllers/post_controller.rb
  6. +2
    -0
      app/helpers/category_helper.rb
  7. +1
    -0
      app/models/blog.rb
  8. +22
    -0
      app/models/category.rb
  9. +1
    -0
      app/models/post.rb
  10. +5
    -0
      app/views/category/get.html.erb
  11. +31
    -0
      app/views/category/manage.html.erb
  12. +12
    -0
      app/views/post/edit.html.erb
  13. +12
    -0
      app/views/post/new.html.erb
  14. +11
    -1
      app/views/posts/_post.html.erb
  15. +1
    -0
      app/views/user/dashboard.html.erb
  16. +8
    -1
      config/routes.rb
  17. +17
    -0
      db/migrate/20200322145832_create_categories.rb
  18. +22
    -1
      db/schema.rb
  19. +7
    -0
      test/controllers/category_controller_test.rb
  20. +9
    -0
      test/fixtures/categories.yml
  21. +7
    -0
      test/models/category_test.rb

+ 2
- 0
README.md View File

@@ -32,6 +32,8 @@ Pressr is a multi-user, multi-blog website written in Ruby on Rails. Although it

* Draft posts/pages

* Categories

## Configuration

Database settings are configured in config/database.yml. It defaults to Postgres running on localhost.


+ 3
- 0
app/assets/stylesheets/category.scss View File

@@ -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/

+ 10
- 0
app/controllers/application_controller.rb View File

@@ -99,6 +99,16 @@ html
)
end

def auth_user
if current_user.nil?
flash[:alert] = "Please login first."
redirect_to login_path
elsif get_user_blog.nil? || !get_user_blog.can_edit(current_user)
flash[:alert] = "You don't have access to that page!"
redirect_back fallback_location: root_path
end
end

private

def default_bg_color


+ 50
- 0
app/controllers/category_controller.rb View File

@@ -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

+ 31
- 0
app/controllers/post_controller.rb View File

@@ -66,6 +66,10 @@ class PostController < ApplicationController
return
end
@post = Post.create
@cats = {}
@blog.categories.each {|c| @cats[c.path] = {name: c.name, val: false}}
#For editing only
#@post.categories.each {|c| @cats[c.path] = true}
end

def create
@@ -79,6 +83,15 @@ class PostController < ApplicationController
post = blog.posts.create(title: params[:post][:title],
body: params[:post][:body],
is_draft: params[:post][:is_draft] || false)
cats = params[:categories]
cats.each do |k, v|
c = blog.categories.find_by_path(k)
unless c.nil?
if v == 1
post.categories << c
end
end
end
redirect_to edit_post_path(post_id: post.id)
end

@@ -88,6 +101,11 @@ class PostController < ApplicationController
render file: "#{Rails.root}/public/403.html" , status: :forbidden
return
end

@cats = {}
@post.blog.categories.each {|c| @cats[c.path] = {name: c.name, value: false}}
@post.categories.each {|c| @cats[c.path] = {name: c.name, value: true}}
@post_or_update = @post.nil? ? "Post" : "Update"
end

@@ -100,6 +118,19 @@ class PostController < ApplicationController
end
@post.update(post_params)
@post.save

cats = params[:categories]
cats.each do |k, v|
c = @post.blog.categories.find_by_path(k)
unless c.nil?
if v.to_i == 1
@post.categories << c
else
@post.categories.delete c
end
end
end
redirect_to edit_post_path
end



+ 2
- 0
app/helpers/category_helper.rb View File

@@ -0,0 +1,2 @@
module CategoryHelper
end

+ 1
- 0
app/models/blog.rb View File

@@ -5,6 +5,7 @@ class Blog < ApplicationRecord
has_many :posts
belongs_to :user
has_many :pages
has_many :categories

preload_counts :posts



+ 22
- 0
app/models/category.rb View File

@@ -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

+ 1
- 0
app/models/post.rb View File

@@ -8,6 +8,7 @@ class Post < ApplicationRecord
belongs_to :blog
has_many :comments
has_and_belongs_to_many :categories, -> { distinct }

preload_counts :comments



+ 5
- 0
app/views/category/get.html.erb View File

@@ -0,0 +1,5 @@
<h1>Posts Tagged Under "<%= @cat.name %>"</h1>
<hr>
<% @posts.each do |p| %>
<%= render p %>
<% end %>

+ 31
- 0
app/views/category/manage.html.erb View File

@@ -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>

+ 12
- 0
app/views/post/edit.html.erb View File

@@ -31,6 +31,18 @@
</tr>
</table>
<hr>
<table style="padding: 15px;">
<th>Categories</th>
<%= form_for :categories do |f| %>
<% @cats.each do |k, v| %>
<tr>
<td><%= v[:name] %></td>
<td><%= f.check_box k, checked: v[:value] %></td>
</tr>
<% end %>
<% end %>
</table>
<hr>
Posted On
<%= f.date_field :created_at %>
</div>


+ 12
- 0
app/views/post/new.html.erb View File

@@ -26,6 +26,18 @@
<td><%= f.radio_button :is_draft, false %></td>
</tr>
</table>
<hr>
<table style="padding: 15px;">
<th>Categories</th>
<%= form_for :categories do |f| %>
<% @cats.each do |k, v| %>
<tr>
<td><%= v[:name] %></td>
<td><%= f.check_box k %></td>
</tr>
<% end %>
<% end %>
</table>
</div>
</div>
<% end %>

+ 11
- 1
app/views/posts/_post.html.erb View File

@@ -30,11 +30,21 @@
<!-- Limit body to 1000 chars -->
<%= text %>
<div style="clear: both"></div>
<%= post.comments_count %> comments
<small style="float: right">
Categories:
<% if post.categories.size > 0 %>
<%= (post.categories.map {|c| link_to(c.name, category_path(c.to_param))}).join(", ").html_safe %>
<% else %>
Uncategorized
<% end %>
</small>
<% if !blog_name.nil? %>
<br>
<div style="float: right">
<%= link_to 'Read more...', post_path(post_id: post.id, blog_name: blog_name) %>
</div>
<br>
<% end %>
<%= post.comments_count %> comments
</div>
<hr>

+ 1
- 0
app/views/user/dashboard.html.erb View File

@@ -18,6 +18,7 @@
<td><%= link_to 'New post', new_post_path(blog_name: b.urlName) %></td>
<td><%= link_to 'Edit blog', edit_blog_path(blog_name: b.urlName) %></td>
<td><%= link_to 'Manage pages', manage_page_path(blog_name: b.urlName) %></td>
<td><%= link_to 'Manage categories', manage_category_path(blog_name: b.urlName) %></td>
</tr>
<% end %>
</table>


+ 8
- 1
config/routes.rb View File

@@ -37,7 +37,7 @@ Rails.application.routes.draw do
post '/dashboard/i/create' => 'user#upload_image', as: :upload_image
post '/dashboard/v/create' => 'user#upload_video', as: :upload_video

# Page stuff
# Pages
get '/b/:blog_name/p/:id/:path' => 'page#get', as: :page
get '/b/:blog_name/p/:id/:path/edit' => 'page#edit', as: :edit_page
patch '/b/:blog_name/p/:id/:path/edit' => 'page#update'
@@ -47,6 +47,13 @@ Rails.application.routes.draw do
post '/b/:blog_name/pages/new' => 'page#create'
post '/b/:blog_name/pages/:p/priority/:dir' => 'page#priority', as: :page_priority

# Categories
get '/b/:blog_name/c/:cat_name' => 'category#get', as: :category
get '/b/:blog_name/categories' => 'category#manage', as: :manage_category
post '/b/:blog_name/categories' => 'category#create'
delete '/b/:blog_name/c/:cat_name' => 'category#delete', as: :delete_category
patch '/b/:blog_name/c/:cat_name' => 'category#update', as: :update_category

# Comments
post '/b/:blog_name/post/:post_id/comment' => 'comment#create', as: :comment
delete '/comment/:id' => 'comment#delete', as: :delete_comment


+ 17
- 0
db/migrate/20200322145832_create_categories.rb View File

@@ -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

+ 22
- 1
db/schema.rb View File

@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2020_02_04_151117) do
ActiveRecord::Schema.define(version: 2020_03_22_145832) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -30,6 +30,26 @@ ActiveRecord::Schema.define(version: 2020_02_04_151117) do
t.index ["user_id"], name: "index_blogs_on_user_id"
end

create_table "categories", force: :cascade do |t|
t.string "name", null: false
t.string "path", null: false
t.bigint "blog_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["blog_id"], name: "index_categories_on_blog_id"
t.index ["name", "blog_id"], name: "index_categories_on_name_and_blog_id", unique: true
t.index ["name"], name: "index_categories_on_name"
t.index ["path", "blog_id"], name: "index_categories_on_path_and_blog_id", unique: true
t.index ["path"], name: "index_categories_on_path"
end

create_table "categories_posts", id: false, force: :cascade do |t|
t.bigint "category_id", null: false
t.bigint "post_id", null: false
t.index ["category_id"], name: "index_categories_posts_on_category_id"
t.index ["post_id"], name: "index_categories_posts_on_post_id"
end

create_table "comments", force: :cascade do |t|
t.integer "likes"
t.integer "dislikes"
@@ -111,5 +131,6 @@ ActiveRecord::Schema.define(version: 2020_02_04_151117) do
t.index ["user_id"], name: "index_video_meta_on_user_id"
end

add_foreign_key "categories", "blogs"
add_foreign_key "pages", "blogs"
end

+ 7
- 0
test/controllers/category_controller_test.rb View File

@@ -0,0 +1,7 @@
require 'test_helper'

class CategoryControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end

+ 9
- 0
test/fixtures/categories.yml View File

@@ -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

+ 7
- 0
test/models/category_test.rb View File

@@ -0,0 +1,7 @@
require 'test_helper'

class CategoryTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

Loading…
Cancel
Save