Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

CoffeeScript processor

  • Loading branch information...
commit f84214b7dbd9d2219e73fae215178ef2a0d7c912 1 parent 5987611
@bcardarella bcardarella authored
View
26 Rakefile
@@ -1,4 +1,6 @@
require 'bundler'
+require 'git'
+require File.join(File.expand_path('..', __FILE__), 'coffeescript/processor')
Bundler::GemHelper.install_tasks
multitask :default => 'test:ruby'
@@ -35,6 +37,30 @@ namespace :test do
end
end
+desc %{Regenerate and commit JavaScript file}
+task :regenerate_javascript do
+ regenerate_javascript
+end
+
+Rake::Task[:build].instance_eval { @actions.clear }
+task :build do
+ begin
+ regenerate_javascript
+ git = Git.open(File.expand_path('..', __FILE__))
+ git.add('vendor')
+ git.commit('Regenerated JavaScript')
+ puts 'Committed changes'
+ rescue
+ puts 'No changes, no commit'
+ end
+ Bundler::GemHelper.new(Dir.pwd).build_gem
+end
+
+def regenerate_javascript
+ ClientSideValidations::Processor.run
+ puts 'Regenerated JavaScript'
+end
+
PORT = 4567
# Returns an array e.g.: ['open', 'http://example.com']
View
1  client_side_validations-simple_form.gemspec
@@ -24,4 +24,5 @@ Gem::Specification.new do |gem|
gem.add_development_dependency 'shotgun'
gem.add_development_dependency 'thin'
gem.add_development_dependency 'json'
+ gem.add_development_dependency 'coffee-script'
end
View
35 coffeescript/processor.rb
@@ -0,0 +1,35 @@
+$:.unshift(File.expand_path('../../lib', __FILE__))
+
+require 'client_side_validations/simple_form/version'
+require 'coffee_script'
+require 'erb'
+
+module ClientSideValidations
+ class Processor
+ def self.run
+ write_file
+ end
+
+ def self.root_path
+ File.expand_path('../..', __FILE__)
+ end
+
+ def self.file_name
+ 'rails.validations.simple_form'
+ end
+
+ def self.template
+ ERB.new(File.open(File.join(root_path, 'coffeescript', "#{file_name}.coffee")).read)
+ end
+
+ def self.compiled_coffeescript
+ CoffeeScript.compile(template.result(binding))
+ end
+
+ def self.write_file
+ file = File.new(File.join(root_path, "vendor/assets/javascripts/#{file_name}.js"), 'w')
+ file << compiled_coffeescript
+ file.close
+ end
+ end
+end
View
25 coffeescript/rails.validations.simple_form.coffee
@@ -0,0 +1,25 @@
+###
+ Client Side Validations - SimpleForm - v<%= ClientSideValidations::SimpleForm::VERSION %>
+ https://github.com/dockyard/client_side_validations-simple_form
+
+ Copyright (c) 2012 DockYard, LLC
+ Licensed under the MIT license
+ http://www.opensource.org/licenses/mit-license.php
+###
+
+ClientSideValidations.formBuilders['SimpleForm::FormBuilder'] =
+ add: (element, settings, message) ->
+ if element.data('valid') != false
+ wrapper = element.closest(settings.wrapper_tag)
+ wrapper.addClass(settings.wrapper_error_class)
+ errorElement = $('<' + settings.error_tag + ' class="' + settings.error_class + '">' + message + '</' + settings.error_tag + '>')
+ wrapper.append(errorElement)
+ else
+ element.parent().find(settings.error_tag + '.' + settings.error_class).text(message)
+
+ remove: (element, settings) ->
+ wrapper = element.closest(settings.wrapper_tag + '.' + settings.wrapper_error_class)
+ wrapper.removeClass(settings.wrapper_error_class)
+ errorElement = wrapper.find(settings.error_tag + '.' + settings.error_class)
+ errorElement.remove()
+
View
5 test/javascript/server.rb
@@ -1,6 +1,11 @@
+require 'bundler'
+Bundler.setup
require 'sinatra'
require 'json'
require 'ruby-debug'
+require File.join(File.expand_path('../../..', __FILE__), 'coffeescript/processor')
+
+ClientSideValidations::Processor.run
def rails_validations_path
File.expand_path('../', $:.grep(/client_side_validations-\d/).first)
Please sign in to comment.
Something went wrong with that request. Please try again.