Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. @attilagyorffy attilagyorffy created this gist Oct 29, 2011.
    8 changes: 8 additions & 0 deletions css_colour_validator.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    # Put this file under Rails.root /lib

    class CssColourValidator < ActiveModel::EachValidator
    def validate_each(record, attribute, value)
    return false unless value
    record.errors[attribute] << (options[:message] || 'is not a valid CSS colour') unless ::HexadecimalColourValidator.matches?(value) or ::WebSafeColourValidator.matches?(value)
    end
    end
    12 changes: 12 additions & 0 deletions hexadecimal_colour_validator.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    # Put this file under Rails.root /lib

    class HexadecimalColourValidator < ActiveModel::EachValidator
    def validate_each(record, attribute, value)
    record.errors[attribute] << (options[:message] || 'is not a valid hexadecimal colour') unless self.class.matches?(value)
    end

    def self.matches?(value)
    return false unless value
    /^#(?:[0-9a-f]{3})(?:[0-9a-f]{3})?$/i.match(value).nil? ? false : true
    end
    end
    3 changes: 3 additions & 0 deletions theme_element.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    class ThemeElement < ActiveRecord::Base
    validates :value, :css_colour => true
    end
    12 changes: 12 additions & 0 deletions web_safe_colour_validator.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    # Put this file under Rails.root /lib

    class WebSafeColourValidator < ActiveModel::EachValidator
    def validate_each(record, attribute, value)
    record.errors[attribute] << (options[:message] || 'is not a valid websafe colour') unless self.class.matches?(value)
    end

    def self.matches?(value)
    return false unless value
    %w(aliceblue antiquewhite aqua aquamarine azure beige bisque black blanchedalmond blue blueviolet brown burlywood cadetblue chartreuse chocolate coral cornflowerblue cornsilk crimson cyan darkblue darkcyan darkgoldenrod darkgray darkgrey darkgreen darkkhaki darkmagenta darkolivegreen darkorange darkorchid darkred darksalmon darkseagreen darkslateblue darkslategray darkslategrey darkturquoise darkviolet deeppink deepskyblue dimgray dimgrey dodgerblue firebrick floralwhite forestgreen fuchsia gainsboro ghostwhite gold goldenrod gray grey green greenyellow honeydew hotpink indianred indigo ivory khaki lavender lavenderblush lawngreen lemonchiffon lightblue lightcoral lightcyan lightgoldenrodyellow lightgray lightgrey lightgreen lightpink lightsalmon lightseagreen lightskyblue lightslategray lightslategrey lightsteelblue lightyellow lime limegreen linen magenta maroon mediumaquamarine mediumblue mediumorchid mediumpurple mediumseagreen mediumslateblue mediumspringgreen mediumturquoise mediumvioletred midnightblue mintcream mistyrose moccasin navajowhite navy oldlace olive olivedrab orange orangered orchid palegoldenrod palegreen paleturquoise palevioletred papayawhip peachpuff peru pink plum powderblue purple red rosybrown royalblue saddlebrown salmon sandybrown seagreen seashell sienna silver skyblue slateblue slategray slategrey snow springgreen steelblue tan teal thistle tomato turquoise violet wheat white whitesmoke yellow yellowgreen).include? value.downcase
    end
    end