Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create_dots #1

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/components/dot_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<%= tag.turbo_frame id: dom_id(@dot) do%>
<div
data-controller="dots"
data-action="click->dots#toggle"
style="<%= style %>"
>
<%= form_tag dot_path(@dot), method: :put, data: {dots_target: 'form'} do |f|%>
</div>
<% end %>
<% end %>
22 changes: 22 additions & 0 deletions app/components/dot_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

class DotComponent < ViewComponent::Base
def initialize(dot:)
@dot = dot
end

private

def style
"margin: 4px;
border-radius: 999px;
width: 20px;
height: 20px;
background-color: #{color};
"
end

def color
@dot.enabled? ? 'green' : 'gray'
end
end
25 changes: 25 additions & 0 deletions app/controllers/dots_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class DotsController < ApplicationController
def index
@dots_by_rows = uniqe_rows.map do |row|
Dot.where(row:).limit(dot_limit)
end
end

def update
@dot = Dot.find(params[:id])
@previous_dot = Dot.find_by(row: @dot.row, enabled: true)
@dot.toggle!(:enabled)

@previous_dot.toggle!(:enabled) if @previous_dot && @previous_dot != @dot
end

private

def uniqe_rows
Dot.pluck(:row).uniq
end

def dot_limit
params[:dot_limit] || 3
end
end
11 changes: 11 additions & 0 deletions app/javascript/controllers/dots_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Controller } from "@hotwired/stimulus"

// Connects to data-controller="dots"
export default class extends Controller {
static targets = ['form'];
connect() {
}
toggle(){
Turbo.navigator.submitForm(this.formTarget)
}
}
2 changes: 2 additions & 0 deletions app/models/dot.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Dot < ApplicationRecord
end
1 change: 1 addition & 0 deletions app/views/dots/_dot.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render DotComponent.new(dot: dot) %>
3 changes: 3 additions & 0 deletions app/views/dots/_dots_select.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= form_with url: dots_path, method: :get, data: { turbo_frame: 'dots' } do |f| %>
<%= f.select :dot_limit, 1..3, {}, {onchange: 'this.form.requestSubmit()'} %>
<% end %>
11 changes: 11 additions & 0 deletions app/views/dots/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<%= render 'dots_select' %>
<%= render 'dots_select' %>

<%= turbo_frame_tag 'dots' do %>
<div style="display: flex">
<% @dots_by_rows.each do |row| %>
<%= render row %>
<br>
<% end %>
</div>
<% end %>
2 changes: 2 additions & 0 deletions app/views/dots/update.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<%= turbo_stream.update @dot %>
<%= turbo_stream.update @previous_dot if @previous_dot && @previous_dot != @dot%>
1 change: 1 addition & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ class Application < Rails::Application
#
# config.time_zone = "Central Time (US & Canada)"
# config.eager_load_paths << Rails.root.join("extras")
config.view_component.capture_compatibility_patch_enabled = true
end
end
6 changes: 2 additions & 4 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
Rails.application.routes.draw do
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

# Defines the root path route ("/")
# root "articles#index"
root 'dots#index'
resources :dots, only: %i[index update]
end
10 changes: 10 additions & 0 deletions db/migrate/20230501132924_create_dots.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateDots < ActiveRecord::Migration[7.0]
def change
create_table :dots do |t|
t.boolean :enabled, null: false, default: false
t.integer :row, null: false

t.timestamps
end
end
end
21 changes: 21 additions & 0 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.