Sha256: 18dc9c4239ba8cb6257a78fe91609739de4d005b3c3f61b49b8e4a488dfd338d

Contents?: true

Size: 913 Bytes

Versions: 1

Compression:

Stored size: 913 Bytes

Contents

require 'open3'
require 'active_model'
require 'active_model/validations'

class CssValidator < ActiveModel::EachValidator
  VENDOR_DIR = File.join(File.expand_path('../..', __FILE__), 'vendor')

  def validate_each(record, attribute, value)
    node_path = `which node`.rstrip
    raise 'You do not have a Node.js installed, but it is required.' unless node_path && !node_path.empty?

    return if options[:allow_nil] && value.nil?
    return if options[:allow_blank] && value.blank?

    css_file = Tempfile.new(['css', '.css'])
    css_file.write(value)
    css_file.close

    cmd = "#{node_path} #{VENDOR_DIR}/cli.js #{css_file.path}"
    Open3.popen3(cmd) do |stdin, stdout, stderr|
      result = stdout.read
      if result =~ /error/
        record.errors.add attribute, (options[:message] || 'is invalid')
      end
    end

  ensure
    css_file.unlink if css_file   # deletes the temp file
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
css_validator-3.0.0 lib/css_validator.rb