Tapes are now Clips, also they have categories

This commit is contained in:
Roscoe 2025-09-10 03:08:27 +01:00
commit c8732b6332
Signed by: RoscoeDaWah
SSH key fingerprint: SHA256:Hqn452XQ1ETzUt/FthJu6+OFkS4NBxCv5VQSEvuk7CE
41 changed files with 347 additions and 148 deletions

2
.gitignore vendored
View file

@ -32,3 +32,5 @@
# Ignore master key for decrypting credentials and more. # Ignore master key for decrypting credentials and more.
/config/master.key /config/master.key
**/.DS_Store

View file

@ -0,0 +1,49 @@
class CategoriesController < ApplicationController
allow_unauthenticated_access only: %i[ index show ]
before_action :set_category, only: %i[ show edit update destroy ]
def index
@categories = Category.all
end
def show
@clips = Clip.where("category_id = ?", params[:id])
end
def new
@category = Category.new
end
def create
@category = Category.new(category_params)
if @category.save
redirect_to @category
else
render :new, status: :unprocessable_entity
end
end
def edit
end
def update
if @category.update(category_params)
redirect_to @category
else
render :edit, status: :unprocessable_entity
end
end
def destroy
@category.destroy
redirect_to categories_path
end
private
def set_category
@category = Category.find(params[:id])
end
def category_params
params.expect(category: [ :name ])
end
end

View file

@ -0,0 +1,48 @@
class ClipsController < ApplicationController
allow_unauthenticated_access only: %i[ index show ]
before_action :set_clip, only: %i[ show edit update destroy ]
def index
@clips = Clip.all
end
def show
end
def new
@clip = Clip.new
end
def create
@clip = Clip.new(clip_params)
if @clip.save
redirect_to @clip
else
render :new, status: :unprocessable_entity
end
end
def edit
end
def update
if @clip.update(clip_params)
redirect_to @clip
else
render :edit, status: :unprocessable_entity
end
end
def destroy
@clip.destroy
redirect_to clips_path
end
private
def set_clip
@clip = Clip.find(params[:id])
end
def clip_params
params.expect(clip: [ :title, :video, :category_id ])
end
end

View file

@ -1,5 +1,5 @@
class HomeController < ApplicationController class HomeController < ApplicationController
def index def index
@tapes = Tape.order("updated_at DESC").limit(10) @clips = Clip.order("updated_at DESC").limit(10)
end end
end end

View file

@ -1,48 +0,0 @@
class TapesController < ApplicationController
allow_unauthenticated_access only: %i[ index show ]
before_action :set_tape, only: %i[ show edit update destroy ]
def index
@tapes = Tape.all
end
def show
end
def new
@tape = Tape.new
end
def create
@tape = Tape.new(tape_params)
if @tape.save
redirect_to @tape
else
render :new, status: :unprocessable_entity
end
end
def edit
end
def update
if @tape.update(tape_params)
redirect_to @tape
else
render :edit, status: :unprocessable_entity
end
end
def destroy
@tape.destroy
redirect_to tapes_path
end
private
def set_tape
@tape = Tape.find(params[:id])
end
def tape_params
params.expect(tape: [ :title, :video ])
end
end

View file

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

View file

@ -0,0 +1,9 @@
module ClipsHelper
def seconds_to_time(seconds)
if seconds >= 3600 then
Time.at(seconds.round).utc.strftime("%H:%M:%S") #=> "01:00:00"
else
Time.at(seconds.round).utc.strftime("%M:%S") #=> "01:00:00"
end
end
end

View file

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

3
app/models/category.rb Normal file
View file

@ -0,0 +1,3 @@
class Category < ApplicationRecord
has_many :clips
end

View file

@ -1,4 +1,5 @@
class Tape < ApplicationRecord class Clip < ApplicationRecord
has_one_attached :video has_one_attached :video
belongs_to :category
validates :title, presence: true validates :title, presence: true
end end

View file

@ -0,0 +1,10 @@
<%= form_with model: category do |form| %>
<div>
<%= form.label :name %>
<%= form.text_field :name %>
</div>
<div>
<%= form.submit %>
</div>
<% end %>

View file

@ -0,0 +1,4 @@
<h1>Edit category</h1>
<%= render "form", category: @category %>
<%= link_to "Cancel", @category %>

View file

@ -0,0 +1,9 @@
<h1>Categories</h1>
<%= link_to "New category", new_category_path if authenticated? %>
<ul id="categories">
<% @categories.each do |category| %>
<li><%= link_to category.name, category %></li>
<% end %>
</ul>

View file

@ -0,0 +1,4 @@
<h1>New Category</h1>
<%= render "form", category: @category %>
<%= link_to "Cancel", categories_path %>

View file

@ -0,0 +1,42 @@
<% cache @category do %>
<h1><%= @category.name %></h1>
<% end %>
<table id="clips" border="1">
<tr>
<th>Preview</th>
<th>Title</th>
<th>Duration</th>
<th>Category</th>
<th>Updated at</th>
<th>Created at</th>
</tr>
<% @clips.each do |clip| %>
<tr>
<td>
<%= link_to (image_tag url_for(clip.video.preview(resize_to_limit: [160, 120]).processed)), clip %>
</td>
<td>
<%= link_to clip.title, clip %>
</td>
<td>
<%= seconds_to_time(clip.video.metadata["duration"]) %>
</td>
<td>
<%= clip.category.name %>
</td>
<td>
<%= clip.updated_at %>
</td>
<td>
<%= clip.created_at %>
</td>
</tr>
<% end %>
</table>
<%= link_to "Back", categories_path%>
<% if authenticated? %>
<%= link_to "Edit", edit_category_path(@category) %>
<%= button_to "Delete", @category, method: :delete, data: { turbo_confirm: "Are you sure?" } %>
<% end %>

View file

@ -1,8 +1,9 @@
<%= form_with model: tape do |form| %> <%= form_with model: clip do |form| %>
<div> <div>
<%= form.label :title %> <%= form.label :title %>
<%= form.text_field :title %> <%= form.text_field :title %>
<%= form.file_field :video, :accept => 'video/quicktime,video/mp4' %> <%= form.file_field :video, :accept => 'video/quicktime,video/mp4' %>
<%= form.select :category_id, Category.all.collect{ |t| [ t.name, t.id ] }%>
</div> </div>
<div> <div>

View file

@ -0,0 +1,4 @@
<h1>Edit clip</h1>
<%= render "form", clip: @clip %>
<%= link_to "Cancel", @clip %>

View file

@ -0,0 +1,36 @@
<h1>Clips</h1>
<%= link_to "New clip", new_clip_path if authenticated? %>
<table id="clips" border="1">
<tr>
<th>Preview</th>
<th>Title</th>
<th>Duration</th>
<th>Category</th>
<th>Updated at</th>
<th>Created at</th>
</tr>
<% @clips.each do |clip| %>
<tr>
<td>
<%= link_to (image_tag url_for(clip.video.preview(resize_to_limit: [160, 120]).processed)), clip %>
</td>
<td>
<%= link_to clip.title, clip %>
</td>
<td>
<%= seconds_to_time(clip.video.metadata["duration"]) %>
</td>
<td>
<%= clip.category.name %>
</td>
<td>
<%= clip.updated_at %>
</td>
<td>
<%= clip.created_at %>
</td>
</tr>
<% end %>
</table>

View file

@ -0,0 +1,4 @@
<h1>New Clip</h1>
<%= render "form", clip: @clip %>
<%= link_to "Cancel", clips_path %>

View file

@ -0,0 +1,12 @@
<% cache @clip do %>
<h1><%= @clip.title %></h1>
<% end %>
<b>Category:</b> <%= link_to @clip.category.name, @clip.category %><br>
<% if @clip.video.attached? %>
<%= video_tag @clip.video, :controls => true%>
<% end %>
<%= link_to "Back", clips_path%>
<% if authenticated? %>
<%= link_to "Edit", edit_clip_path(@clip) %>
<%= button_to "Delete", @clip, method: :delete, data: { turbo_confirm: "Are you sure?" } %>
<% end %>

View file

@ -1,8 +1,8 @@
<h2>Recently Updated</h2> <h2>Recently Updated</h2>
<ul id="tapes"> <ul id="clips">
<% @tapes.each do |tape| %> <% @clips.each do |clip| %>
<li> <li>
<%= link_to tape.title, tape %> <%= link_to clip.title, clip %>
</li> </li>
<% end %> <% end %>
</ul> </ul>

View file

@ -17,6 +17,12 @@
<link rel="icon" href="/icon.svg" type="image/svg+xml"> <link rel="icon" href="/icon.svg" type="image/svg+xml">
<link rel="apple-touch-icon" href="/icon.png"> <link rel="apple-touch-icon" href="/icon.png">
<style>
html {
color-scheme: dark;
}
</style>
<%# Includes all stylesheet files in app/assets/stylesheets %> <%# Includes all stylesheet files in app/assets/stylesheets %>
<%= stylesheet_link_tag :app %> <%= stylesheet_link_tag :app %>
</head> </head>
@ -26,7 +32,8 @@
<%= link_to "Home", root_path %> <%= link_to "Home", root_path %>
<%= button_to "Log out", session_path, method: :delete if authenticated? %> <%= button_to "Log out", session_path, method: :delete if authenticated? %>
<%= link_to "Login", new_session_path unless authenticated? %> <%= link_to "Login", new_session_path unless authenticated? %>
<%= link_to "Tapes", tapes_path %> <%= link_to "Clips", clips_path %>
<%= link_to "Categories", categories_path %>
</nav> </nav>
<main> <main>
<%= yield %> <%= yield %>

View file

@ -1,4 +0,0 @@
<h1>Edit tape</h1>
<%= render "form", tape: @tape %>
<%= link_to "Cancel", @tape %>

View file

@ -1,28 +0,0 @@
<h1>Tapes</h1>
<%= link_to "New tape", new_tape_path if authenticated? %>
<table id="tapes" border="1">
<tr>
<th>Preview</th>
<th>Title</th>
<th>Updated at</th>
<th>Created at</th>
</tr>
<% @tapes.each do |tape| %>
<tr>
<td>
<%= link_to (image_tag url_for(tape.video.preview(resize_to_limit: [160, 120]).processed)), tape %>
</td>
<td>
<%= link_to tape.title, tape %>
</td>
<td>
<%= tape.updated_at %>
</td>
<td>
<%= tape.created_at %>
</td>
</tr>
<% end %>
</div>

View file

@ -1,4 +0,0 @@
<h1>New tape</h1>
<%= render "form", tape: @tape %>
<%= link_to "Cancel", tapes_path %>

View file

@ -1,11 +0,0 @@
<% cache @tape do %>
<h1><%= @tape.title %></h1>
<% end %>
<% if @tape.video.attached? %>
<%= video_tag @tape.video, :controls => true%>
<% end %>
<%= link_to "Back", tapes_path%>
<% if authenticated? %>
<%= link_to "Edit", edit_tape_path(@tape) %>
<%= button_to "Delete", @tape, method: :delete, data: { turbo_confirm: "Are you sure?" } %>
<% end %>

View file

@ -14,7 +14,8 @@ Rails.application.routes.draw do
# Defines the root path route ("/") # Defines the root path route ("/")
# root "posts#index" # root "posts#index"
resources :tapes resources :clips
resources :categories
root "home#index" root "home#index"
end end

View file

@ -1,9 +0,0 @@
class CreateTapes < ActiveRecord::Migration[8.0]
def change
create_table :tapes do |t|
t.string :title
t.timestamps
end
end
end

View file

@ -0,0 +1,11 @@
class CreateClips < ActiveRecord::Migration[8.0]
def change
create_table :clips do |t|
t.string :title
t.belongs_to :category, foreign_key: true
t.timestamps
end
end
end

View file

@ -0,0 +1,9 @@
class CreateCategories < ActiveRecord::Migration[8.0]
def change
create_table :categories do |t|
t.string :name
t.timestamps
end
end
end

23
db/schema.rb generated
View file

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[8.0].define(version: 2025_09_08_020324) do ActiveRecord::Schema[8.0].define(version: 2025_09_09_013509) do
create_table "active_storage_attachments", force: :cascade do |t| create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false t.string "name", null: false
t.string "record_type", null: false t.string "record_type", null: false
@ -39,6 +39,20 @@ ActiveRecord::Schema[8.0].define(version: 2025_09_08_020324) do
t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true
end 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 "clips", force: :cascade do |t|
t.string "title"
t.integer "category_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["category_id"], name: "index_clips_on_category_id"
end
create_table "sessions", force: :cascade do |t| create_table "sessions", force: :cascade do |t|
t.integer "user_id", null: false t.integer "user_id", null: false
t.string "ip_address" t.string "ip_address"
@ -48,12 +62,6 @@ ActiveRecord::Schema[8.0].define(version: 2025_09_08_020324) do
t.index ["user_id"], name: "index_sessions_on_user_id" t.index ["user_id"], name: "index_sessions_on_user_id"
end end
create_table "tapes", force: :cascade do |t|
t.string "title"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", force: :cascade do |t| create_table "users", force: :cascade do |t|
t.string "email_address", null: false t.string "email_address", null: false
t.string "password_digest", null: false t.string "password_digest", null: false
@ -64,5 +72,6 @@ ActiveRecord::Schema[8.0].define(version: 2025_09_08_020324) do
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id" add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id" add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
add_foreign_key "clips", "categories"
add_foreign_key "sessions", "users" add_foreign_key "sessions", "users"
end end

View file

@ -0,0 +1,7 @@
require "test_helper"
class CategoriesControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end

View file

@ -1,6 +1,6 @@
require "test_helper" require "test_helper"
class TapesControllerTest < ActionDispatch::IntegrationTest class ClipsControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do # test "the truth" do
# assert true # assert true
# end # end

7
test/fixtures/categories.yml vendored Normal file
View file

@ -0,0 +1,7 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
name: MyString
two:
name: MyString

7
test/fixtures/clips.yml vendored Normal file
View file

@ -0,0 +1,7 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
title: MyString
two:
title: MyString

View file

@ -0,0 +1,7 @@
require "test_helper"
class CategoryTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View file

@ -1,6 +1,6 @@
require "test_helper" require "test_helper"
class TapeTest < ActiveSupport::TestCase class ClipTest < ActiveSupport::TestCase
# test "the truth" do # test "the truth" do
# assert true # assert true
# end # end