diff --git a/app/assets/stylesheets/components/form.css b/app/assets/stylesheets/components/form.css
index 3895f6780..6fe87be1f 100644
--- a/app/assets/stylesheets/components/form.css
+++ b/app/assets/stylesheets/components/form.css
@@ -37,6 +37,10 @@
@apply flex items-center justify-center gap-4 text-center border-t border-solid text-gray border-light_gray;
}
+ /* Input styles */
+ .color-input {
+ @apply p-2 w-28 h-14;
+ }
/* Formula field */
.formula{
border-radius: inherit !important;
diff --git a/app/controllers/account/calculators_controller.rb b/app/controllers/account/calculators_controller.rb
index beb21c3f4..01c86a219 100644
--- a/app/controllers/account/calculators_controller.rb
+++ b/app/controllers/account/calculators_controller.rb
@@ -81,7 +81,7 @@ def collect_fields_for_kind(kind)
def calculator_params
params.require(:calculator).permit(
- :id, :en_name, :uk_name,
+ :id, :en_name, :uk_name, :color,
formulas_attributes: [:id, :expression, :en_label, :uk_label, :calculator_id, :en_unit, :uk_unit, :_destroy],
fields_attributes: [:id, :en_label, :uk_label, :var_name, :kind, :_destroy,
categories_attributes: [:id, :en_name, :uk_name, :price, :_destroy]]
diff --git a/app/models/calculator.rb b/app/models/calculator.rb
index d6e72e586..393cf8cbb 100644
--- a/app/models/calculator.rb
+++ b/app/models/calculator.rb
@@ -5,6 +5,7 @@
# Table name: calculators
#
# id :bigint not null, primary key
+# color :string default("#000000")
# en_name :string default(""), not null
# slug :string
# uk_name :string default(""), not null
@@ -35,6 +36,7 @@ class Calculator < ApplicationRecord
validates :en_name, :uk_name, presence: true
validates :en_name, :uk_name, length: { minimum: 3, maximum: 50 }
validates :slug, presence: true, uniqueness: true
+ validates :color, format: { with: /\A#[0-9a-fA-F]{6}\z/ }
def self.ransackable_attributes(auth_object = nil)
["created_at", "id", "name", "preferable", "slug", "updated_at", "uuid"]
diff --git a/app/views/account/calculators/partials/_color_selector.html.erb b/app/views/account/calculators/partials/_color_selector.html.erb
new file mode 100644
index 000000000..a1db2e287
--- /dev/null
+++ b/app/views/account/calculators/partials/_color_selector.html.erb
@@ -0,0 +1,6 @@
+
+ <%= f.input :color, as: :color, label: 'Choose a Color for Calculator Page:',
+ input_html: {
+ class: 'color-input',
+ } %>
+
diff --git a/app/views/account/calculators/partials/_form.html.erb b/app/views/account/calculators/partials/_form.html.erb
index 438b34288..4bf810999 100644
--- a/app/views/account/calculators/partials/_form.html.erb
+++ b/app/views/account/calculators/partials/_form.html.erb
@@ -5,6 +5,9 @@
<%= f.input :en_name, label: "Calculator Name:", class: 'form-control' %>
<%= f.input :uk_name, label: "Uk Calculator Name:", class: 'form-control' %>
+
+ <%= render "account/calculators/partials/color_selector", f: f %>
+
diff --git a/app/views/calculators/show.html.erb b/app/views/calculators/show.html.erb
index 948e85762..747749a18 100644
--- a/app/views/calculators/show.html.erb
+++ b/app/views/calculators/show.html.erb
@@ -1,10 +1,7 @@
-<%# TODO: Delete this and use user provided value %>
-<% color = "#088F8F" %>
-
<%# TODO: Delete this and use user provided value %>
<% calculator_image = "scales.png" %>
-
+
Calculator <%= @calculator.name %> <%= @text %>
diff --git a/db/migrate/20241201141708_add_color_to_calculators.rb b/db/migrate/20241201141708_add_color_to_calculators.rb
new file mode 100644
index 000000000..f01cfdcd7
--- /dev/null
+++ b/db/migrate/20241201141708_add_color_to_calculators.rb
@@ -0,0 +1,5 @@
+class AddColorToCalculators < ActiveRecord::Migration[7.2]
+ def change
+ add_column :calculators, :color, :string, default: "#8fba3b"
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index e9e42b813..0db79e658 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema[7.2].define(version: 2024_11_19_192344) do
+ActiveRecord::Schema[7.2].define(version: 2024_12_01_141708) do
# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
enable_extension "plpgsql"
@@ -49,6 +49,7 @@
t.string "slug"
t.string "uk_name", default: "", null: false
t.string "en_name", default: "", null: false
+ t.string "color", default: "#8fba3b"
t.index ["slug"], name: "index_calculators_on_slug", unique: true
end
diff --git a/spec/factories/calculators.rb b/spec/factories/calculators.rb
index 8f6db7dc2..4da7f10c6 100644
--- a/spec/factories/calculators.rb
+++ b/spec/factories/calculators.rb
@@ -5,6 +5,7 @@
# Table name: calculators
#
# id :bigint not null, primary key
+# color :string default("#000000")
# en_name :string default(""), not null
# slug :string
# uk_name :string default(""), not null
diff --git a/spec/models/calculator_spec.rb b/spec/models/calculator_spec.rb
index 6be5b5005..de3911cba 100644
--- a/spec/models/calculator_spec.rb
+++ b/spec/models/calculator_spec.rb
@@ -5,6 +5,7 @@
# Table name: calculators
#
# id :bigint not null, primary key
+# color :string default("#000000")
# en_name :string default(""), not null
# slug :string
# uk_name :string default(""), not null
@@ -28,6 +29,12 @@
it { is_expected.to validate_presence_of(:uk_name) }
it { is_expected.to validate_length_of(:uk_name).is_at_least(3).is_at_most(50) }
it { is_expected.to validate_uniqueness_of(:slug) }
+ it { is_expected.to allow_value("#123abc").for(:color) }
+ it { is_expected.to allow_value("#ABCDEF").for(:color) }
+ it { is_expected.not_to allow_value("123abc").for(:color) }
+ it { is_expected.not_to allow_value("#12345").for(:color) }
+ it { is_expected.not_to allow_value("#1234567").for(:color) }
+ it { is_expected.not_to allow_value("#GHIJKL").for(:color) }
end
describe "associations" do