diff --git a/app/controllers/rules_controller.rb b/app/controllers/rules_controller.rb index 318ed60d..a8e4a43a 100644 --- a/app/controllers/rules_controller.rb +++ b/app/controllers/rules_controller.rb @@ -64,7 +64,7 @@ class RulesController < ApplicationController def rule_params params.require(:rule).permit( - :resource_type, :effective_date, :active, + :resource_type, :effective_date, :active, :name, conditions_attributes: [ :id, :condition_type, :operator, :value, :_destroy, sub_conditions_attributes: [ :id, :condition_type, :operator, :value, :_destroy ] diff --git a/app/models/rule.rb b/app/models/rule.rb index db8a99ae..93ed47f4 100644 --- a/app/models/rule.rb +++ b/app/models/rule.rb @@ -8,7 +8,10 @@ class Rule < ApplicationRecord accepts_nested_attributes_for :conditions, allow_destroy: true accepts_nested_attributes_for :actions, allow_destroy: true + before_validation :normalize_name + validates :resource_type, presence: true + validates :name, length: { minimum: 1 }, allow_nil: true validate :no_nested_compound_conditions # Every rule must have at least 1 action @@ -87,4 +90,8 @@ class Rule < ApplicationRecord end end end + + def normalize_name + self.name = nil if name.is_a?(String) && name.strip.empty? + end end diff --git a/app/views/rules/_form.html.erb b/app/views/rules/_form.html.erb index 5a03eff6..30f170c0 100644 --- a/app/views/rules/_form.html.erb +++ b/app/views/rules/_form.html.erb @@ -9,6 +9,16 @@ <%= render "shared/form_errors", model: @rule %> <% end %> +
+

+ <%= lucide_icon("tag", class: "w-4 h-4") %> + Rule name (optional) +

+
+ <%= f.text_field :name, placeholder: "Enter a name for this rule", class: "form-field__input" %> +
+
+

If <%= rule.resource_type %>

diff --git a/app/views/rules/_rule.html.erb b/app/views/rules/_rule.html.erb index 2e433c72..a86b8c54 100644 --- a/app/views/rules/_rule.html.erb +++ b/app/views/rules/_rule.html.erb @@ -2,6 +2,9 @@
+ <% if rule.name.present? %> +

<%= rule.name %>

+ <% end %> <% if rule.conditions.any? %>

diff --git a/app/views/rules/confirm.html.erb b/app/views/rules/confirm.html.erb index 4749dea8..3e937367 100644 --- a/app/views/rules/confirm.html.erb +++ b/app/views/rules/confirm.html.erb @@ -2,7 +2,13 @@

-

Confirm changes

+

+ <% if @rule.name.present? %> + Confirm changes to "<%= @rule.name %>" + <% else %> + Confirm changes + <% end %> +

diff --git a/app/views/rules/edit.html.erb b/app/views/rules/edit.html.erb index e5693fa2..94c23a14 100644 --- a/app/views/rules/edit.html.erb +++ b/app/views/rules/edit.html.erb @@ -1,5 +1,5 @@ <%= link_to "Back to rules", rules_path %> -<%= modal_form_wrapper title: "Edit #{@rule.resource_type} rule" do %> +<%= modal_form_wrapper title: @rule.name.present? ? "Edit #{@rule.resource_type} rule \"#{@rule.name}\"" : "Edit #{@rule.resource_type} rule" do %> <%= render "rules/form", rule: @rule %> <% end %> diff --git a/db/migrate/20250429021255_add_name_to_rules.rb b/db/migrate/20250429021255_add_name_to_rules.rb new file mode 100644 index 00000000..f69e1140 --- /dev/null +++ b/db/migrate/20250429021255_add_name_to_rules.rb @@ -0,0 +1,5 @@ +class AddNameToRules < ActiveRecord::Migration[7.2] + def change + add_column :rules, :name, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index adb03c7d..1e16d0bc 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: 2025_04_16_235758) do +ActiveRecord::Schema[7.2].define(version: 2025_04_29_021255) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" enable_extension "plpgsql" @@ -18,6 +18,7 @@ ActiveRecord::Schema[7.2].define(version: 2025_04_16_235758) do # Custom types defined in this database. # Note that some types may not work with other database engines. Be careful if changing database. create_enum "account_status", ["ok", "syncing", "error"] + create_enum "user_role", ["admin", "member"] create_table "accounts", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| t.string "subtype" @@ -502,6 +503,7 @@ ActiveRecord::Schema[7.2].define(version: 2025_04_16_235758) do t.boolean "active", default: false, null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "name" t.index ["family_id"], name: "index_rules_on_family_id" end