From feb20e9ab5e171c186ffd232bb78b2f79e8bc95b Mon Sep 17 00:00:00 2001 From: Jake Barnes Date: Sat, 23 Mar 2024 15:42:30 +1100 Subject: [PATCH] Add tags to items --- app/controllers/videos_controller.rb | 2 +- app/models/item.rb | 1 + app/serializers/video_serializer.rb | 4 ++++ db/migrate/20240323040259_add_tags_to_items.rb | 5 +++++ db/structure.sql | 6 ++++-- 5 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20240323040259_add_tags_to_items.rb diff --git a/app/controllers/videos_controller.rb b/app/controllers/videos_controller.rb index 246ed93..b788687 100644 --- a/app/controllers/videos_controller.rb +++ b/app/controllers/videos_controller.rb @@ -39,7 +39,7 @@ def update authorize! :update, Item item = Item.find_by!(video_id: params[:id], user: current_user) authorize! :update, item - attributes = ActiveModelSerializers::Deserialization.jsonapi_parse(params, only: [:state, :progress]) + attributes = ActiveModelSerializers::Deserialization.jsonapi_parse(params, only: [:state, :progress, :tags]) if item.update(attributes) head :no_content else diff --git a/app/models/item.rb b/app/models/item.rb index 36723b7..1ff25cb 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -9,6 +9,7 @@ # updated_at :datetime not null # user_id :integer not null # progress :integer default(0), not null +# tags :text default([]), not null, is an Array # # Indexes # diff --git a/app/serializers/video_serializer.rb b/app/serializers/video_serializer.rb index 621585e..59cc150 100644 --- a/app/serializers/video_serializer.rb +++ b/app/serializers/video_serializer.rb @@ -9,5 +9,9 @@ class VideoSerializer < ActiveModel::Serializer Item.find_by(video: object, user: scope).progress end + attribute :tags do + Item.find_by(video: object, user: scope).tags + end + belongs_to :channel end diff --git a/db/migrate/20240323040259_add_tags_to_items.rb b/db/migrate/20240323040259_add_tags_to_items.rb new file mode 100644 index 0000000..131d424 --- /dev/null +++ b/db/migrate/20240323040259_add_tags_to_items.rb @@ -0,0 +1,5 @@ +class AddTagsToItems < ActiveRecord::Migration[6.1] + def change + add_column :items, :tags, :text, array: true, null: false, default: [] + end +end diff --git a/db/structure.sql b/db/structure.sql index 01b07a8..081f577 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -89,7 +89,8 @@ CREATE TABLE public.items ( created_at timestamp without time zone NOT NULL, updated_at timestamp without time zone NOT NULL, user_id integer NOT NULL, - progress integer DEFAULT 0 NOT NULL + progress integer DEFAULT 0 NOT NULL, + tags text[] DEFAULT '{}'::text[] NOT NULL ); @@ -457,6 +458,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20220501124525'), ('20220529012334'), ('20220912113632'), -('20240301110411'); +('20240301110411'), +('20240323040259');