Skip to content

Instantly share code, notes, and snippets.

@joshwlewis
Created November 19, 2012 20:09
Show Gist options
  • Select an option

  • Save joshwlewis/4113551 to your computer and use it in GitHub Desktop.

Select an option

Save joshwlewis/4113551 to your computer and use it in GitHub Desktop.

Revisions

  1. joshwlewis created this gist Nov 19, 2012.
    12 changes: 12 additions & 0 deletions scss_format_validator.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    # app/validators/scss_format_validator.rb
    class ScssFormatValidator < ActiveModel::EachValidator
    def validate_each(object, attribute, value)
    begin
    # Attempt to parse SCSS
    Sass::Engine.new(value, syntax: :scss).render
    rescue Exception => e
    # Add error if parsing fails
    object.errors.add(attribute, :invalid_scss, error: e.inspect)
    end
    end
    end
    4 changes: 4 additions & 0 deletions widget.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    class Client < ActiveRecord::Base
    attr_accessible :style_text
    validates :style_text, scss_format: true
    end