Skip to content

Commit

Permalink
🎉 Add color and palette models
Browse files Browse the repository at this point in the history
  • Loading branch information
chuckcarpenter committed Sep 9, 2024
1 parent ca09694 commit c67f67e
Show file tree
Hide file tree
Showing 23 changed files with 252 additions and 1 deletion.
2 changes: 2 additions & 0 deletions app/controllers/colors_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class ColorsController < ApplicationController
end
51 changes: 51 additions & 0 deletions app/controllers/palettes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class PalettesController < ApplicationController
http_basic_authenticate_with name: "dhh", password: "secret", except: [ :index, :show ]

def index
@palettes = Palette.all
end

def show
@palette = Palette.find(params[:id])
end

def new
@palette = Palette.new
end

def create
@palette = Palette.new(palette_params)

if @palette.save
redirect_to @palette
else
render :new, status: :unprocessable_entity
end
end

def edit
@palette = Palette.find(params[:id])
end

def update
@palette = Palette.find(params[:id])

if @palette.update(palette_params)
redirect_to @palette
else
render :edit, status: :unprocessable_entity
end
end

def destroy
@palette = Palette.find(params[:id])
@palette.destroy

redirect_to root_path, status: :see_other
end

private
def palette_params
params.require(:palette).permit(:name, :is_color_history, :is_favorite, :is_locked)
end
end
2 changes: 2 additions & 0 deletions app/helpers/colors_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module ColorsHelper
end
2 changes: 2 additions & 0 deletions app/helpers/palettes_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module PalettesHelper
end
5 changes: 5 additions & 0 deletions app/models/color.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Color < ApplicationRecord
belongs_to :palette

# serialize :color_order, Array
end
8 changes: 8 additions & 0 deletions app/models/palette.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Palette < ApplicationRecord
has_many :colors

validates :name, presence: true
validates :is_color_history, inclusion: { in: [ true, false ] }
validates :is_favorite, inclusion: { in: [ true, false ] }
validates :is_locked, inclusion: { in: [ true, false ] }
end
22 changes: 22 additions & 0 deletions app/views/palettes/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<%= form_with model: @palette do |form| %>
<div>
<%= form.label :name %><br>
<%= form.text_field :name %>
<% @palette.errors.full_messages_for(:name).each do |message| %>
<div><%= message %></div>
<% end %>
</div>

<div>
<%= form.label :is_favorite %><br>
<%= form.collection_radio_buttons :is_favorite, [[true, 'Yes'], [false, 'No']], :first, :last %>
<% @palette.errors.full_messages_for(:is_favorite).each do |message| %>
<div><%= message %></div>
<% end %>
</div>

<div>
<%= form.submit %>
</div>
<% end %>
3 changes: 3 additions & 0 deletions app/views/palettes/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1>Edit Palette</h1>

<%= render "form", palette: @palette %>
11 changes: 11 additions & 0 deletions app/views/palettes/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<h1>Palettes</h1>

<ul>
<% @palettes.each do |palette| %>
<li>
<%= link_to palette.name, palette %>
</li>
<% end %>
</ul>

<%= link_to "New Palette", new_palette_path %>
3 changes: 3 additions & 0 deletions app/views/palettes/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1>New Palette</h1>

<%= render "form", palette: @palette %>
18 changes: 18 additions & 0 deletions app/views/palettes/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<h1><%= @palette.name %></h1>

<p><%= @palette.is_favorite %></p>

<%= link_to "Edit", edit_palette_path(@palette) %>
<%= link_to "Delete", palette_path(@palette), data: {
turbo_method: :delete,
turbo_confirm: "Are you sure?"
} %>


<h2>Colors</h2>
<% @palette.colors.each do |color| %>
<p>
<%= color.name %>
</p>
<% end %>
5 changes: 4 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
get "manifest" => "rails/pwa#manifest", as: :pwa_manifest

# Defines the root path route ("/")
# root "posts#index"
root "palettes#index"
resources :palettes do
resources :colors
end
end
14 changes: 14 additions & 0 deletions db/migrate/20240903225132_create_palettes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class CreatePalettes < ActiveRecord::Migration[7.2]
def change
create_table :palettes do |t|
t.string :name
t.integer :index
t.boolean :is_color_history, default: false
t.boolean :is_favorite, default: false
t.boolean :is_locked, default: false
t.integer :selected_color_index

t.timestamps
end
end
end
10 changes: 10 additions & 0 deletions db/migrate/20240909192444_create_colors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateColors < ActiveRecord::Migration[7.2]
def change
create_table :colors do |t|
t.string :name
t.references :palette, null: false, foreign_key: true

t.timestamps
end
end
end
8 changes: 8 additions & 0 deletions db/migrate/20240909194534_add_rgba_to_colors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class AddRgbaToColors < ActiveRecord::Migration[7.2]
def change
add_column :colors, :r, :integer
add_column :colors, :g, :integer
add_column :colors, :b, :integer
add_column :colors, :a, :integer
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddColorOrderArrayToPalettes < ActiveRecord::Migration[7.2]
def change
add_column :palettes, :color_order, :json, default: []
end
end
39 changes: 39 additions & 0 deletions db/schema.rb

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

7 changes: 7 additions & 0 deletions test/controllers/colors_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class ColorsControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end
8 changes: 8 additions & 0 deletions test/controllers/palettes_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require "test_helper"

class PalettesControllerTest < ActionDispatch::IntegrationTest
test "should get index" do
get palettes_index_url
assert_response :success
end
end
9 changes: 9 additions & 0 deletions test/fixtures/colors.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
name: MyString
palette: one

two:
name: MyString
palette: two
7 changes: 7 additions & 0 deletions test/fixtures/palettes.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
name: MyString

two:
name: MyString
7 changes: 7 additions & 0 deletions test/models/color_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class ColorTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
7 changes: 7 additions & 0 deletions test/models/palette_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class PaletteTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit c67f67e

Please sign in to comment.