Upgrade to Pro — share decks privately, control downloads, hide ads and more …

自作アプリにViewComponentを導入した

Avatar for siso25 siso25
November 27, 2025

 自作アプリにViewComponentを導入した

TokyuRuby会議16の発表資料です

Avatar for siso25

siso25

November 27, 2025

More Decks by siso25

Other Decks in Technology

Transcript

  1. ViewComponent::Previewを使う StimulusコントローラーでValuesを追加 Stimulus 1 export default class extends Controller {

    2 static values = { 3 // ルーレットが回転する時間 4 spinDuration: Number, 5 } 6 7 // スタートボタンクリック時の処理 8 start() { 9 const animation = this.rotate(this.spinDuration)
  2. ViewComponent::Previewを使う spin_duration をコンポーネント使用時に設定できるようにする Component Slim 1 class RouletteComponent < ViewComponent::Base

    2 def initialize(spin_duration:) 3 @spin_duration = spin_duration 4 end 5 end 1 .relative[ 2 data-controller="rotate" 3 data-rotate-spin-duration-value=@spin_duration
  3. ViewComponent::Previewを使う •URLパラメータでアニメーションの時間を指定できるようにする •テストでは 0秒 に設定してwait 10 を削除 Preview RSpec 1

    class RouletteComponentPreview < ViewComponent::Preview 2 def with_spin_duration(spin_duration:) 3 render Roulettes::Roulette::Component.new(spin_duration) 4 end 5 end 1 it 'トークテーマ数を超えた回数ルーレットを実行したら選択状態をリセットする', js: true do 2 visit "/rails/view_components/roulette_component/with_spin_duration?spin_duration=0" 3 click_button 'スタート' 4 result_text = find "[data-rotate-target='resultText']" # wait: 10 を削除