Created
November 19, 2012 20:09
-
-
Save joshwlewis/4113551 to your computer and use it in GitHub Desktop.
Rails validation for scss (or sass)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Client < ActiveRecord::Base | |
attr_accessible :style_text | |
validates :style_text, scss_format: true | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment