green_light 0.0.3 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/.DS_Store ADDED
Binary file
data/Gemfile CHANGED
@@ -1,5 +1,18 @@
1
1
  source 'http://rubygems.org'
2
- gem 'rails'
3
- gem "activerecord", :require => "active_record"
4
- gem 'rspec'
5
- gem 'supermodel'
2
+ gem 'rails',"3.1.0.rc5"
3
+
4
+ # Gems used only for assets and not required
5
+ # in production environments by default.
6
+ group :assets do
7
+ gem 'sass-rails', "~> 3.1.0.rc"
8
+ gem 'coffee-rails', "~> 3.1.0.rc"
9
+ gem 'uglifier'
10
+ end
11
+
12
+ gem 'jquery-rails'
13
+
14
+ group :test do
15
+ gem "activerecord", :require => "active_record"
16
+ gem 'rspec'
17
+ gem 'supermodel'
18
+ end
data/README.rdoc CHANGED
@@ -1,6 +1,6 @@
1
1
  = green_light
2
2
 
3
- Provides client side validation (with the help of the jquery validation plugin) while keeping validation in the model, where it belongs.
3
+ Provides client side validation, with the help of the {jquery validation plugin}[http://bassistance.de/jquery-plugins/jquery-plugin-validation/], while keeping validation in the model, where it belongs.
4
4
 
5
5
 
6
6
  == Installation
@@ -13,19 +13,18 @@ Add green_light to your Gemfile and run the bundle install command.
13
13
 
14
14
  == Usage
15
15
 
16
- Run this generator to copy the required assets to your public folder:-
16
+ Generate the required green_light configuration:-
17
17
 
18
- rails g green_light_assets
18
+ rails g green_light:install
19
19
 
20
- Download {jQuery}[http://jquery.com/] and the {jQuery Validation Plugin}[http://bassistance.de/jquery-plugins/jquery-plugin-validation/] and include them in your layout header.
20
+ Include the JQuery validation plugin and green_light javascript in your application.js file:-
21
21
 
22
- <%= javascript_include_tag 'jquery-1.x.x.min', 'jquery.validate.min' %>
22
+ //= require jquery.validate
23
+ //= require green_light
23
24
 
24
- Also, insert the following javascript include tag in your layout html header:-
25
-
26
- <%= javascript_include_tag 'jquery.validate_regex', 'green_light' %>
25
+ Specify which models you want to have client-side validation in the config/green_light.yml file e.g.:-
27
26
 
28
- Note, the green_light file specified above is generated dynamically, so it wont appear in your public javascripts folder.
27
+ validate_models: ['ModelName1', 'ModelName2']
29
28
 
30
29
  Add the <tt>green_light</tt> class to the forms that you wish to have client side validation:-
31
30
 
@@ -42,10 +41,12 @@ And finally, add some validations to your models!
42
41
  validates_uniqueness_of
43
42
  validates_numericality_of
44
43
 
44
+ For validations that are not yet supported, the gem will degrade gracefully to the standard Rails server-side validation.
45
+
45
46
 
46
- == Requirements
47
+ == Legecy Rails 3 Support
47
48
 
48
- jQuery versions 1.3.2, 1.4.2, 1.4.4, 1.5.0, 1.5.1
49
+ For Rails 3 support use the older version (0.0.3) of green_light
49
50
 
50
51
 
51
52
  == Running the Tests
data/app/.DS_Store ADDED
Binary file
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Build validation rules based on models specified in the green_light config
3
+ */
4
+ <% unless GREEN_LIGHT[:validate_models].empty? %>
5
+ <% @rules = GreenLight::Rules.generate(GREEN_LIGHT[:validate_models]) %>
6
+ $().ready(function() {
7
+ $(".green_light").validate(<%= @rules.html_safe %>);
8
+ });
9
+ <% end %>
10
+
11
+ /**
12
+ * Custom code for regex validation
13
+ */
14
+ $.validator.addMethod(
15
+ "regex",
16
+ function(value, element, regexp) {
17
+ var check = false;
18
+ var re = new RegExp(regexp);
19
+ return this.optional(element) || re.test(value);
20
+ },
21
+ "You have entered an invalid value for this field"
22
+ );
23
+
@@ -1,10 +1,5 @@
1
1
  class JavascriptsController < ApplicationController
2
- respond_to :js, :text
3
-
4
- def green_light
5
- @rules = GreenLight::Rules.generate(all_models)
6
- respond_with(@rules)
7
- end
2
+ respond_to :text
8
3
 
9
4
  def check_for_uniqueness
10
5
  record = params[:model].constantize.where("#{params[:field]} = ?", params[params[:model].downcase.underscore][params[:field]])
@@ -14,11 +9,4 @@ class JavascriptsController < ApplicationController
14
9
  render :text => true
15
10
  end
16
11
  end
17
-
18
- private
19
- def all_models
20
- tables = ActiveRecord::Base.connection.tables
21
- tables.delete("schema_migrations")
22
- tables.map { |table| table.camelize.singularize.constantize }
23
- end
24
12
  end
data/config/routes.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  Rails.application.routes.draw do
2
- match "/javascripts/green_light" => "javascripts#green_light"
2
+ match "/assets/green_light" => "assets#green_light"
3
3
  match "/javascripts/check_for_uniqueness" => "javascripts#check_for_uniqueness"
4
4
  end
data/lib/.DS_Store ADDED
Binary file
File without changes
@@ -0,0 +1,20 @@
1
+ require 'rails/generators'
2
+
3
+ module GreenLight
4
+ class InstallGenerator < Rails::Generators::Base
5
+ desc 'Generates config file for green_light'
6
+
7
+ def self.source_root
8
+ @source_root ||= File.join(File.dirname(__FILE__), 'templates')
9
+ end
10
+
11
+ def generate_config
12
+ copy_file "green_light.yml", "config/green_light.yml"
13
+ end
14
+
15
+ def generate_initializer
16
+ copy_file "green_light.rb", "config/initializers/green_light.rb"
17
+ end
18
+ end
19
+ end
20
+
@@ -0,0 +1,2 @@
1
+ config = YAML.load_file(File.join(Rails.root, 'config', 'green_light.yml'))
2
+ GREEN_LIGHT = HashWithIndifferentAccess.new(config)
@@ -0,0 +1 @@
1
+ validate_models: []
Binary file
@@ -9,7 +9,7 @@ module GreenLight
9
9
  def self.generate(models)
10
10
  data, rules = {}, {}
11
11
  models.each do |model|
12
- model._validators.each do |field_name, validations|
12
+ model.constantize._validators.each do |field_name, validations|
13
13
  rules["#{model.to_s.underscore.downcase}[#{field_name}]"] = parse_each_validation(model, field_name, validations)
14
14
  data[:rules] = rules
15
15
  end
@@ -1,3 +1,3 @@
1
1
  module GreenLight
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -5,27 +5,27 @@ describe GreenLight do
5
5
  end
6
6
 
7
7
  it "should return a valid json string when validating the format of a field" do
8
- rules = GreenLight::Rules.generate([FormatOfModel])
8
+ rules = GreenLight::Rules.generate(['FormatOfModel'])
9
9
  rules.should == "{\"errorElement\":\"span\",\"rules\":{\"format_of_model[title]\":{\"regex\":\"(^[A-Za-z]$)\"}}}"
10
10
  end
11
11
 
12
12
  it "should return a json string when validating the presence of a field" do
13
- rules = GreenLight::Rules.generate([PresenceOfModel])
13
+ rules = GreenLight::Rules.generate(['PresenceOfModel'])
14
14
  rules.should == "{\"errorElement\":\"span\",\"rules\":{\"presence_of_model[title]\":{\"required\":true}}}"
15
15
  end
16
16
 
17
17
  it "should return a json string when validating the length of a field" do
18
- rules = GreenLight::Rules.generate([LengthOfModel])
18
+ rules = GreenLight::Rules.generate(['LengthOfModel'])
19
19
  rules.should == "{\"errorElement\":\"span\",\"rules\":{\"length_of_model[title]\":{\"maxlength\":10,\"minlength\":5}}}"
20
20
  end
21
21
 
22
22
  it "should return a json string when validating the numericality of a field" do
23
- rules = GreenLight::Rules.generate([NumericalityOfModel])
23
+ rules = GreenLight::Rules.generate(['NumericalityOfModel'])
24
24
  rules.should == "{\"errorElement\":\"span\",\"rules\":{\"numericality_of_model[age]\":{\"regex\":\"^[0-9]*$\"}}}"
25
25
  end
26
26
 
27
27
  it "should return a json string when validating the uniqueness of a field" do
28
- rules = GreenLight::Rules.generate([UniquenessOfModel])
28
+ rules = GreenLight::Rules.generate(['UniquenessOfModel'])
29
29
  rules.should == "{\"errorElement\":\"span\",\"rules\":{\"uniqueness_of_model[title]\":{\"remote\":\"/javascripts/check_for_uniqueness?model=UniquenessOfModel&field=title\"}}}"
30
30
  end
31
31
  end
data/vendor/.DS_Store ADDED
Binary file
Binary file