Class: VideoChapter
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- VideoChapter
- Defined in:
- app/models/video_chapter.rb
Overview
A single YouTube-style chapter marker for a Video. Persisted locally so we
can edit and re-push to the YouTube description without re-running AI
generation.
Constant Summary
Constants included from Schedulable
Schedulable::SIMPLE_FORM_OPTIONS
Instance Attribute Summary collapse
- #start_ms ⇒ Object readonly
- #title ⇒ Object readonly
Belongs to collapse
Class Method Summary collapse
-
.format_ms_to_hms(ms) ⇒ String?
Format a millisecond offset for display.
-
.parse_timestamp_to_ms(value) ⇒ Integer?
Parse a user-entered timestamp into milliseconds.
Instance Method Summary collapse
-
#timestamp_hms ⇒ String?
Hours-minutes-seconds string (zero-padded MM/SS), used by the CRM form.
-
#timestamp_hms=(value) ⇒ Integer?
Setter paired with #timestamp_hms for
accepts_nested_attributes_forforms.
Methods inherited from ApplicationRecord
ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation
Methods included from Schedulable
Methods included from Models::AfterCommittable
Methods included from Models::EventPublishable
Instance Attribute Details
#start_ms ⇒ Object (readonly)
14 |
# File 'app/models/video_chapter.rb', line 14 validates :start_ms, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 } |
#title ⇒ Object (readonly)
13 |
# File 'app/models/video_chapter.rb', line 13 validates :title, presence: true, length: { maximum: 100 } |
Class Method Details
.format_ms_to_hms(ms) ⇒ String?
Format a millisecond offset for display. Drops the hour part when zero.
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'app/models/video_chapter.rb', line 53 def self.format_ms_to_hms(ms) return nil if ms.nil? total_seconds = ms.to_i / 1000 hours = total_seconds / 3600 minutes = (total_seconds % 3600) / 60 seconds = total_seconds % 60 if hours.positive? format('%<h>d:%<m>02d:%<s>02d', h: hours, m: minutes, s: seconds) else format('%<m>d:%<s>02d', m: minutes, s: seconds) end end |
.parse_timestamp_to_ms(value) ⇒ Integer?
Parse a user-entered timestamp into milliseconds.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'app/models/video_chapter.rb', line 33 def self.(value) return nil if value.nil? s = value.to_s.strip return nil if s.empty? parts = s.split(':') return nil unless parts.all? { |p| p.match?(/\A\d+\z/) } nums = parts.map(&:to_i) case nums.length when 1 then nums[0] * 1000 when 2 then ((nums[0] * 60) + nums[1]) * 1000 when 3 then ((nums[0] * 3600) + (nums[1] * 60) + nums[2]) * 1000 end end |
Instance Method Details
#timestamp_hms ⇒ String?
Hours-minutes-seconds string (zero-padded MM/SS), used by the CRM form.
18 19 20 |
# File 'app/models/video_chapter.rb', line 18 def self.class.format_ms_to_hms(start_ms) end |
#timestamp_hms=(value) ⇒ Integer?
Setter paired with #timestamp_hms for accepts_nested_attributes_for forms.
Accepts "H:MM:SS", "M:SS", or a bare integer seconds string. Blank clears.
26 27 28 |
# File 'app/models/video_chapter.rb', line 26 def (value) self.start_ms = self.class.(value) end |
#video ⇒ Video
9 |
# File 'app/models/video_chapter.rb', line 9 belongs_to :video, foreign_key: :digital_asset_id, class_name: 'Video', inverse_of: :video_chapters |