here come the tags

This commit is contained in:
Roscoe 2025-09-10 03:46:23 +01:00
commit 29368bfdb9
Signed by: RoscoeDaWah
SSH key fingerprint: SHA256:Hqn452XQ1ETzUt/FthJu6+OFkS4NBxCv5VQSEvuk7CE
16 changed files with 155 additions and 2 deletions

View file

@ -15,6 +15,13 @@ class ClipsController < ApplicationController
def create
@clip = Clip.new(clip_params)
if @clip.save
params[:clip][:tag_ids].each do |tag_id|
unless tag_id.empty?
tag = Tag.find(tag_id)
@clip.tags << tag
end
end
redirect_to @clip
else
render :new, status: :unprocessable_entity
@ -26,6 +33,23 @@ class ClipsController < ApplicationController
def update
if @clip.update(clip_params)
newtags = []
params[:clip][:tag_ids].each do |tag_id|
unless tag_id.empty?
tag = Tag.find(tag_id)
newtags.push(tag)
unless @clip.tags.include? tag
@clip.tags << tag
end
end
end
@clip.tags.each do |tag|
unless newtags.include? tag
@clip.tags.delete tag
end
end
redirect_to @clip
else
render :edit, status: :unprocessable_entity

View file

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

View file

@ -1,5 +1,7 @@
class Clip < ApplicationRecord
has_one_attached :video
belongs_to :category
has_many :tag_to_clips
has_many :tags, through: :tag_to_clips
validates :title, presence: true
end

4
app/models/tag.rb Normal file
View file

@ -0,0 +1,4 @@
class Tag < ApplicationRecord
has_many :tag_to_clips
has_many :clips, through: :tag_to_clips
end

View file

@ -0,0 +1,4 @@
class TagToClip < ApplicationRecord
belongs_to :clip
belongs_to :tag
end

View file

@ -4,6 +4,7 @@
<%= form.text_field :title %>
<%= form.file_field :video, :accept => 'video/quicktime,video/mp4' %>
<%= form.select :category_id, Category.all.collect{ |t| [ t.name, t.id ] }%>
<%= collection_select(:clip, :tag_ids, Tag.all, :id, :name, {}, { :multiple => true } )%>
</div>
<div>

View file

@ -2,6 +2,7 @@
<h1><%= @clip.title %></h1>
<% end %>
<b>Category:</b> <%= link_to @clip.category.name, @clip.category %><br>
<b>Tags:</b> <%= @clip.tags.map(&:name).join(', ') %><br>
<% if @clip.video.attached? %>
<%= video_tag @clip.video, :controls => true%>
<% end %>

View file

@ -1,4 +1,5 @@
Rails.application.routes.draw do
resources :tag_to_clips
resource :session
resources :passwords, param: :token
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

View file

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

View file

@ -0,0 +1,10 @@
class CreateTagToClips < ActiveRecord::Migration[8.0]
def change
create_table :tag_to_clips do |t|
t.references :clip, null: false, foreign_key: true
t.references :tag, null: false, foreign_key: true
t.timestamps
end
end
end

19
db/schema.rb generated
View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[8.0].define(version: 2025_09_09_013509) do
ActiveRecord::Schema[8.0].define(version: 2025_09_10_022600) do
create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
@ -62,6 +62,21 @@ ActiveRecord::Schema[8.0].define(version: 2025_09_09_013509) do
t.index ["user_id"], name: "index_sessions_on_user_id"
end
create_table "tag_to_clips", force: :cascade do |t|
t.integer "clip_id", null: false
t.integer "tag_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["clip_id"], name: "index_tag_to_clips_on_clip_id"
t.index ["tag_id"], name: "index_tag_to_clips_on_tag_id"
end
create_table "tags", 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 "email_address", null: false
t.string "password_digest", null: false
@ -74,4 +89,6 @@ ActiveRecord::Schema[8.0].define(version: 2025_09_09_013509) do
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 "tag_to_clips", "clips"
add_foreign_key "tag_to_clips", "tags"
end

View file

@ -0,0 +1,48 @@
require "test_helper"
class TagToClipsControllerTest < ActionDispatch::IntegrationTest
setup do
@tag_to_clip = tag_to_clips(:one)
end
test "should get index" do
get tag_to_clips_url
assert_response :success
end
test "should get new" do
get new_tag_to_clip_url
assert_response :success
end
test "should create tag_to_clip" do
assert_difference("TagToClip.count") do
post tag_to_clips_url, params: { tag_to_clip: { clip_id: @tag_to_clip.clip_id, tag_id: @tag_to_clip.tag_id } }
end
assert_redirected_to tag_to_clip_url(TagToClip.last)
end
test "should show tag_to_clip" do
get tag_to_clip_url(@tag_to_clip)
assert_response :success
end
test "should get edit" do
get edit_tag_to_clip_url(@tag_to_clip)
assert_response :success
end
test "should update tag_to_clip" do
patch tag_to_clip_url(@tag_to_clip), params: { tag_to_clip: { clip_id: @tag_to_clip.clip_id, tag_id: @tag_to_clip.tag_id } }
assert_redirected_to tag_to_clip_url(@tag_to_clip)
end
test "should destroy tag_to_clip" do
assert_difference("TagToClip.count", -1) do
delete tag_to_clip_url(@tag_to_clip)
end
assert_redirected_to tag_to_clips_url
end
end

9
test/fixtures/tag_to_clips.yml vendored Normal file
View file

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

7
test/fixtures/tags.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/models/tag_test.rb Normal file
View file

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

View file

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