Przeglądaj źródła

Ensure tag_fg_color and tag_bg_color are valid hex colors.

Guilherme J. Tramontina 10 lat temu
rodzic
commit
454f43fddc
2 zmienionych plików z 14 dodań i 0 usunięć
  1. 4 0
      app/models/scenario.rb
  2. 10 0
      spec/models/scenario_spec.rb

+ 4 - 0
app/models/scenario.rb

@@ -9,6 +9,10 @@ class Scenario < ActiveRecord::Base
 
   validates_presence_of :name, :user
 
+  validates_format_of :tag_fg_color, :tag_bg_color,
+    :with => /\A#(?:[0-9a-fA-F]{3}){1,2}\z/, :message => "must be a valid hex color."
+    # Regex adapted from: http://stackoverflow.com/a/1636354/3130625
+
   validate :agents_are_owned
 
   protected

+ 10 - 0
spec/models/scenario_spec.rb

@@ -29,6 +29,16 @@ describe Scenario do
       new_instance.should_not be_valid
     end
 
+    it "validates tag_fg_color is hex color" do
+      new_instance.tag_fg_color = '#N07H3X'
+      new_instance.should_not be_valid
+    end
+
+    it "validates tag_bg_color is hex color" do
+      new_instance.tag_bg_color = '#N07H3X'
+      new_instance.should_not be_valid
+    end
+
     it "only allows Agents owned by user" do
       new_instance.agent_ids = [agents(:bob_website_agent).id]
       new_instance.should be_valid