From c67f67e684f9d169435dd08c6da285805f7cbbe9 Mon Sep 17 00:00:00 2001 From: Chuck Carpenter Date: Mon, 9 Sep 2024 13:34:36 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=89=20Add=20color=20and=20palette=20mo?= =?UTF-8?q?dels?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/colors_controller.rb | 2 + app/controllers/palettes_controller.rb | 51 +++++++++++++++++++ app/helpers/colors_helper.rb | 2 + app/helpers/palettes_helper.rb | 2 + app/models/color.rb | 5 ++ app/models/palette.rb | 8 +++ app/views/palettes/_form.html.erb | 22 ++++++++ app/views/palettes/edit.html.erb | 3 ++ app/views/palettes/index.html.erb | 11 ++++ app/views/palettes/new.html.erb | 3 ++ app/views/palettes/show.html.erb | 18 +++++++ config/routes.rb | 5 +- db/migrate/20240903225132_create_palettes.rb | 14 +++++ db/migrate/20240909192444_create_colors.rb | 10 ++++ .../20240909194534_add_rgba_to_colors.rb | 8 +++ ...01548_add_color_order_array_to_palettes.rb | 5 ++ db/schema.rb | 39 ++++++++++++++ test/controllers/colors_controller_test.rb | 7 +++ test/controllers/palettes_controller_test.rb | 8 +++ test/fixtures/colors.yml | 9 ++++ test/fixtures/palettes.yml | 7 +++ test/models/color_test.rb | 7 +++ test/models/palette_test.rb | 7 +++ 23 files changed, 252 insertions(+), 1 deletion(-) create mode 100644 app/controllers/colors_controller.rb create mode 100644 app/controllers/palettes_controller.rb create mode 100644 app/helpers/colors_helper.rb create mode 100644 app/helpers/palettes_helper.rb create mode 100644 app/models/color.rb create mode 100644 app/models/palette.rb create mode 100644 app/views/palettes/_form.html.erb create mode 100644 app/views/palettes/edit.html.erb create mode 100644 app/views/palettes/index.html.erb create mode 100644 app/views/palettes/new.html.erb create mode 100644 app/views/palettes/show.html.erb create mode 100644 db/migrate/20240903225132_create_palettes.rb create mode 100644 db/migrate/20240909192444_create_colors.rb create mode 100644 db/migrate/20240909194534_add_rgba_to_colors.rb create mode 100644 db/migrate/20240909201548_add_color_order_array_to_palettes.rb create mode 100644 db/schema.rb create mode 100644 test/controllers/colors_controller_test.rb create mode 100644 test/controllers/palettes_controller_test.rb create mode 100644 test/fixtures/colors.yml create mode 100644 test/fixtures/palettes.yml create mode 100644 test/models/color_test.rb create mode 100644 test/models/palette_test.rb diff --git a/app/controllers/colors_controller.rb b/app/controllers/colors_controller.rb new file mode 100644 index 0000000..ebbf2c4 --- /dev/null +++ b/app/controllers/colors_controller.rb @@ -0,0 +1,2 @@ +class ColorsController < ApplicationController +end diff --git a/app/controllers/palettes_controller.rb b/app/controllers/palettes_controller.rb new file mode 100644 index 0000000..4ad6b6c --- /dev/null +++ b/app/controllers/palettes_controller.rb @@ -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 diff --git a/app/helpers/colors_helper.rb b/app/helpers/colors_helper.rb new file mode 100644 index 0000000..f74a8a7 --- /dev/null +++ b/app/helpers/colors_helper.rb @@ -0,0 +1,2 @@ +module ColorsHelper +end diff --git a/app/helpers/palettes_helper.rb b/app/helpers/palettes_helper.rb new file mode 100644 index 0000000..a5ffefb --- /dev/null +++ b/app/helpers/palettes_helper.rb @@ -0,0 +1,2 @@ +module PalettesHelper +end diff --git a/app/models/color.rb b/app/models/color.rb new file mode 100644 index 0000000..f43f552 --- /dev/null +++ b/app/models/color.rb @@ -0,0 +1,5 @@ +class Color < ApplicationRecord + belongs_to :palette + + # serialize :color_order, Array +end diff --git a/app/models/palette.rb b/app/models/palette.rb new file mode 100644 index 0000000..e1b78c1 --- /dev/null +++ b/app/models/palette.rb @@ -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 diff --git a/app/views/palettes/_form.html.erb b/app/views/palettes/_form.html.erb new file mode 100644 index 0000000..60783bc --- /dev/null +++ b/app/views/palettes/_form.html.erb @@ -0,0 +1,22 @@ +<%= form_with model: @palette do |form| %> +
+ <%= form.label :name %>
+ <%= form.text_field :name %> + <% @palette.errors.full_messages_for(:name).each do |message| %> +
<%= message %>
+ <% end %> +
+ +
+ <%= form.label :is_favorite %>
+ <%= form.collection_radio_buttons :is_favorite, [[true, 'Yes'], [false, 'No']], :first, :last %> + + <% @palette.errors.full_messages_for(:is_favorite).each do |message| %> +
<%= message %>
+ <% end %> +
+ +
+ <%= form.submit %> +
+<% end %> \ No newline at end of file diff --git a/app/views/palettes/edit.html.erb b/app/views/palettes/edit.html.erb new file mode 100644 index 0000000..28a4a58 --- /dev/null +++ b/app/views/palettes/edit.html.erb @@ -0,0 +1,3 @@ +

Edit Palette

+ +<%= render "form", palette: @palette %> \ No newline at end of file diff --git a/app/views/palettes/index.html.erb b/app/views/palettes/index.html.erb new file mode 100644 index 0000000..741a2cb --- /dev/null +++ b/app/views/palettes/index.html.erb @@ -0,0 +1,11 @@ +

Palettes

+ + + +<%= link_to "New Palette", new_palette_path %> diff --git a/app/views/palettes/new.html.erb b/app/views/palettes/new.html.erb new file mode 100644 index 0000000..568ae32 --- /dev/null +++ b/app/views/palettes/new.html.erb @@ -0,0 +1,3 @@ +

New Palette

+ +<%= render "form", palette: @palette %> \ No newline at end of file diff --git a/app/views/palettes/show.html.erb b/app/views/palettes/show.html.erb new file mode 100644 index 0000000..97e9a02 --- /dev/null +++ b/app/views/palettes/show.html.erb @@ -0,0 +1,18 @@ +

<%= @palette.name %>

+ +

<%= @palette.is_favorite %>

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

Colors

+<% @palette.colors.each do |color| %> +

+ <%= color.name %> +

+<% end %> \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 33c9639..2b21948 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/db/migrate/20240903225132_create_palettes.rb b/db/migrate/20240903225132_create_palettes.rb new file mode 100644 index 0000000..e957135 --- /dev/null +++ b/db/migrate/20240903225132_create_palettes.rb @@ -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 diff --git a/db/migrate/20240909192444_create_colors.rb b/db/migrate/20240909192444_create_colors.rb new file mode 100644 index 0000000..d89a0bb --- /dev/null +++ b/db/migrate/20240909192444_create_colors.rb @@ -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 diff --git a/db/migrate/20240909194534_add_rgba_to_colors.rb b/db/migrate/20240909194534_add_rgba_to_colors.rb new file mode 100644 index 0000000..1d29d52 --- /dev/null +++ b/db/migrate/20240909194534_add_rgba_to_colors.rb @@ -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 diff --git a/db/migrate/20240909201548_add_color_order_array_to_palettes.rb b/db/migrate/20240909201548_add_color_order_array_to_palettes.rb new file mode 100644 index 0000000..63a6558 --- /dev/null +++ b/db/migrate/20240909201548_add_color_order_array_to_palettes.rb @@ -0,0 +1,5 @@ +class AddColorOrderArrayToPalettes < ActiveRecord::Migration[7.2] + def change + add_column :palettes, :color_order, :json, default: [] + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..1f27c1a --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,39 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# This file is the source Rails uses to define your schema when running `bin/rails +# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema[7.2].define(version: 2024_09_09_201548) do + create_table "colors", force: :cascade do |t| + t.string "name" + t.integer "palette_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.integer "r" + t.integer "g" + t.integer "b" + t.integer "a" + t.index ["palette_id"], name: "index_colors_on_palette_id" + end + + create_table "palettes", force: :cascade do |t| + t.string "name" + t.integer "index" + t.boolean "is_color_history" + t.boolean "is_favorite" + t.boolean "is_locked" + t.integer "selected_color_index" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.json "color_order", default: [] + end + + add_foreign_key "colors", "palettes" +end diff --git a/test/controllers/colors_controller_test.rb b/test/controllers/colors_controller_test.rb new file mode 100644 index 0000000..f0cb4ea --- /dev/null +++ b/test/controllers/colors_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class ColorsControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end diff --git a/test/controllers/palettes_controller_test.rb b/test/controllers/palettes_controller_test.rb new file mode 100644 index 0000000..b8137f0 --- /dev/null +++ b/test/controllers/palettes_controller_test.rb @@ -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 diff --git a/test/fixtures/colors.yml b/test/fixtures/colors.yml new file mode 100644 index 0000000..508e5d3 --- /dev/null +++ b/test/fixtures/colors.yml @@ -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 diff --git a/test/fixtures/palettes.yml b/test/fixtures/palettes.yml new file mode 100644 index 0000000..7d41224 --- /dev/null +++ b/test/fixtures/palettes.yml @@ -0,0 +1,7 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + name: MyString + +two: + name: MyString diff --git a/test/models/color_test.rb b/test/models/color_test.rb new file mode 100644 index 0000000..d532121 --- /dev/null +++ b/test/models/color_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class ColorTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/palette_test.rb b/test/models/palette_test.rb new file mode 100644 index 0000000..9685cc1 --- /dev/null +++ b/test/models/palette_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class PaletteTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end