jcheck_rails 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source :gemcutter
2
+
3
+ gem 'rails', '3.0.0.rc'
4
+ gem 'rspec', '2.0.0.beta.19'
5
+ gem 'jeweler'
data/Gemfile.lock ADDED
@@ -0,0 +1,91 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ abstract (1.0.0)
5
+ actionmailer (3.0.0.rc)
6
+ actionpack (= 3.0.0.rc)
7
+ mail (~> 2.2.5)
8
+ actionpack (3.0.0.rc)
9
+ activemodel (= 3.0.0.rc)
10
+ activesupport (= 3.0.0.rc)
11
+ builder (~> 2.1.2)
12
+ erubis (~> 2.6.6)
13
+ i18n (~> 0.4.1)
14
+ rack (~> 1.2.1)
15
+ rack-mount (~> 0.6.9)
16
+ rack-test (~> 0.5.4)
17
+ tzinfo (~> 0.3.22)
18
+ activemodel (3.0.0.rc)
19
+ activesupport (= 3.0.0.rc)
20
+ builder (~> 2.1.2)
21
+ i18n (~> 0.4.1)
22
+ activerecord (3.0.0.rc)
23
+ activemodel (= 3.0.0.rc)
24
+ activesupport (= 3.0.0.rc)
25
+ arel (~> 0.4.0)
26
+ tzinfo (~> 0.3.22)
27
+ activeresource (3.0.0.rc)
28
+ activemodel (= 3.0.0.rc)
29
+ activesupport (= 3.0.0.rc)
30
+ activesupport (3.0.0.rc)
31
+ arel (0.4.0)
32
+ activesupport (>= 3.0.0.beta)
33
+ builder (2.1.2)
34
+ diff-lcs (1.1.2)
35
+ erubis (2.6.6)
36
+ abstract (>= 1.0.0)
37
+ gemcutter (0.6.1)
38
+ git (1.2.5)
39
+ i18n (0.4.1)
40
+ jeweler (1.4.0)
41
+ gemcutter (>= 0.1.0)
42
+ git (>= 1.2.5)
43
+ rubyforge (>= 2.0.0)
44
+ json_pure (1.4.3)
45
+ mail (2.2.5)
46
+ activesupport (>= 2.3.6)
47
+ mime-types
48
+ treetop (>= 1.4.5)
49
+ mime-types (1.16)
50
+ polyglot (0.3.1)
51
+ rack (1.2.1)
52
+ rack-mount (0.6.9)
53
+ rack (>= 1.0.0)
54
+ rack-test (0.5.4)
55
+ rack (>= 1.0)
56
+ rails (3.0.0.rc)
57
+ actionmailer (= 3.0.0.rc)
58
+ actionpack (= 3.0.0.rc)
59
+ activerecord (= 3.0.0.rc)
60
+ activeresource (= 3.0.0.rc)
61
+ activesupport (= 3.0.0.rc)
62
+ bundler (>= 1.0.0.rc.1)
63
+ railties (= 3.0.0.rc)
64
+ railties (3.0.0.rc)
65
+ actionpack (= 3.0.0.rc)
66
+ activesupport (= 3.0.0.rc)
67
+ rake (>= 0.8.3)
68
+ thor (~> 0.14.0)
69
+ rake (0.8.7)
70
+ rspec (2.0.0.beta.19)
71
+ rspec-core (= 2.0.0.beta.19)
72
+ rspec-expectations (= 2.0.0.beta.19)
73
+ rspec-mocks (= 2.0.0.beta.19)
74
+ rspec-core (2.0.0.beta.19)
75
+ rspec-expectations (2.0.0.beta.19)
76
+ diff-lcs (>= 1.1.2)
77
+ rspec-mocks (2.0.0.beta.19)
78
+ rubyforge (2.0.4)
79
+ json_pure (>= 1.1.7)
80
+ thor (0.14.0)
81
+ treetop (1.4.8)
82
+ polyglot (>= 0.3.1)
83
+ tzinfo (0.3.22)
84
+
85
+ PLATFORMS
86
+ ruby
87
+
88
+ DEPENDENCIES
89
+ jeweler
90
+ rails (= 3.0.0.rc)
91
+ rspec (= 2.0.0.beta.19)
data/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright (c) 2010 Wilker Lúcio <wilkerlucio@gmail.com>
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
data/README.textile ADDED
@@ -0,0 +1,12 @@
1
+ h1. jCheck Rails
2
+
3
+ jCheck Rails provide a ease way to generate "jCheck":http://jcheck.net code
4
+ to validate form according to ActiveModel record validations.
5
+
6
+ h2. Installation
7
+
8
+ h2. Documentation
9
+
10
+ h2. Copyright
11
+
12
+ Copyright (c) 2010 Wilker Lucio. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,30 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ Bundler.setup
5
+ Bundler.require
6
+
7
+ begin
8
+ require 'jeweler'
9
+ Jeweler::Tasks.new do |gem|
10
+ gem.name = "jcheck_rails"
11
+ gem.summary = %Q{Generate jCheck code according to ActiveModel validations}
12
+ gem.description = %Q{Generate jCheck code according to ActiveModel validations}
13
+ gem.email = "wilkerlucio@gmail.com"
14
+ gem.homepage = "http://github.com/wilkerlucio/jcheck_rails"
15
+ gem.authors = ["Wilker Lucio"]
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'rake/rdoctask'
23
+ Rake::RDocTask.new do |rdoc|
24
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
25
+
26
+ rdoc.rdoc_dir = 'rdoc'
27
+ rdoc.title = "jcheck_rails #{version}"
28
+ rdoc.rdoc_files.include('README*')
29
+ rdoc.rdoc_files.include('lib/**/*.rb')
30
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'jcheck_rails'
@@ -0,0 +1,57 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{jcheck_rails}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Wilker Lucio"]
12
+ s.date = %q{2010-08-15}
13
+ s.description = %q{Generate jCheck code according to ActiveModel validations}
14
+ s.email = %q{wilkerlucio@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.textile"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE",
25
+ "README.textile",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "init.rb",
29
+ "jcheck_rails.gemspec",
30
+ "lib/jcheck_rails.rb",
31
+ "lib/jcheck_rails/encoder.rb",
32
+ "spec/encoder_spec.rb",
33
+ "spec/jcheck_rails_spec.rb",
34
+ "spec/spec_helper.rb"
35
+ ]
36
+ s.homepage = %q{http://github.com/wilkerlucio/jcheck_rails}
37
+ s.rdoc_options = ["--charset=UTF-8"]
38
+ s.require_paths = ["lib"]
39
+ s.rubygems_version = %q{1.3.7}
40
+ s.summary = %q{Generate jCheck code according to ActiveModel validations}
41
+ s.test_files = [
42
+ "spec/encoder_spec.rb",
43
+ "spec/jcheck_rails_spec.rb",
44
+ "spec/spec_helper.rb"
45
+ ]
46
+
47
+ if s.respond_to? :specification_version then
48
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
49
+ s.specification_version = 3
50
+
51
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
52
+ else
53
+ end
54
+ else
55
+ end
56
+ end
57
+
@@ -0,0 +1,58 @@
1
+ require 'action_view'
2
+
3
+ module JcheckRails
4
+ module Encoder
5
+ class << self
6
+ include ActionView::Helpers::JavaScriptHelper
7
+
8
+ def convert_to_javascript(object)
9
+ return "true" if object == {}
10
+
11
+ case object
12
+ when TrueClass
13
+ "true"
14
+ when FalseClass
15
+ "false"
16
+ when NilClass
17
+ "null"
18
+ when Fixnum, Float
19
+ object.to_s
20
+ when Symbol, String
21
+ "'#{escape_javascript object.to_s}'"
22
+ when Regexp
23
+ "/#{object.source}/#{regex_options_string(object.options)}"
24
+ when Array
25
+ convert_array(object)
26
+ when Hash
27
+ convert_hash(object)
28
+ else
29
+ raise ArgumentError.new("can't parse object type #{object.class}")
30
+ end
31
+ end
32
+
33
+ def convert_array(array)
34
+ data = array.map { |x| convert_to_javascript(x) }
35
+ "[#{data.join(', ')}]"
36
+ end
37
+
38
+ def convert_hash(hash)
39
+ hash_pairs = []
40
+
41
+ hash.each do |k, v|
42
+ hash_pairs << "#{convert_to_javascript(k)}: #{convert_to_javascript(v)}"
43
+ end
44
+
45
+ "{#{hash_pairs.join(", ")}}"
46
+ end
47
+
48
+ def regex_options_string(options)
49
+ letters = [[1, "i"], [2, "x"], [4, "m"]]
50
+
51
+ letters.inject("") do |acc, (v, l)|
52
+ acc << l if (options & v) > 0
53
+ acc
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,55 @@
1
+ require 'jcheck_rails/encoder'
2
+
3
+ module JcheckRails
4
+ extend self
5
+
6
+ def jcheck_for(object, attribute = nil, options = {})
7
+ return jcheck_for_object_attribute(object, attribute) if attribute
8
+
9
+ options.reverse_merge!(
10
+ :variable => "validator",
11
+ :form_id => ActionController::RecordIdentifier.dom_id(object, (object.respond_to?(:persisted?) && object.persisted? ? :edit : nil)),
12
+ :field_prefix => ActiveModel::Naming.singular(object)
13
+ )
14
+
15
+ variable = options.delete :variable
16
+ form_id = options.delete :form_id
17
+
18
+ validations = []
19
+
20
+ object.class._validators.each do |attribute, validators|
21
+ attr_validations = jcheck_for_object_attribute(object, attribute)
22
+
23
+ validations << "#{variable}.validates(#{Encoder.convert_to_javascript attribute}, #{attr_validations});"
24
+ end
25
+
26
+ %{<script type="text/javascript"> jQuery(function() { var #{variable} = jQuery('##{form_id}').jcheck(#{Encoder.convert_to_javascript(options)}); #{validations.join(" ")} }); </script>}.html_safe
27
+ end
28
+
29
+ def jcheck_for_object_attribute(object, attribute)
30
+ validations = object.class._validators[attribute].inject([]) do |acc, validator|
31
+ options = filter_validator_options(validator)
32
+
33
+ acc << "#{Encoder.convert_to_javascript validator.kind}: #{Encoder.convert_to_javascript(options)}"
34
+ end
35
+
36
+ "{#{validations.join(', ')}}"
37
+ end
38
+
39
+ protected
40
+
41
+ def filter_validator_options(validator)
42
+ options = validator.options.dup
43
+
44
+ case validator.kind
45
+ when :acceptance
46
+ options.delete :allow_nil
47
+ when :length
48
+ options.delete :tokenizer
49
+ end
50
+
51
+ options
52
+ end
53
+ end
54
+
55
+ ActionView::Base.send :include, JcheckRails
@@ -0,0 +1,69 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "JcheckRails Encoder" do
4
+ context "parsing regexp options" do
5
+ it "should see ignore case option" do
6
+ JcheckRails::Encoder.regex_options_string(/a/i.options).should == "i"
7
+ end
8
+
9
+ it "should see extended option" do
10
+ JcheckRails::Encoder.regex_options_string(/a/x.options).should == "x"
11
+ end
12
+
13
+ it "should see multiline option" do
14
+ JcheckRails::Encoder.regex_options_string(/a/m.options).should == "m"
15
+ end
16
+
17
+ it "should work with combined options" do
18
+ JcheckRails::Encoder.regex_options_string(/a/im.options).should == "im"
19
+ end
20
+ end
21
+
22
+ context "parsing hashes" do
23
+ it "convert hash keys and values" do
24
+ JcheckRails::Encoder.convert_hash({:key => "value", "other" => "thing"}).should == "{'key': 'value', 'other': 'thing'}"
25
+ end
26
+ end
27
+
28
+ context "converting values" do
29
+ it "should parse TrueClass" do
30
+ JcheckRails::Encoder.convert_to_javascript(true).should == "true"
31
+ end
32
+
33
+ it "should parse FalseClass" do
34
+ JcheckRails::Encoder.convert_to_javascript(false).should == "false"
35
+ end
36
+
37
+ it "should parse NilClass" do
38
+ JcheckRails::Encoder.convert_to_javascript(nil).should == "null"
39
+ end
40
+
41
+ it "should parse Fixnum" do
42
+ JcheckRails::Encoder.convert_to_javascript(5).should == "5"
43
+ end
44
+
45
+ it "should parse Float" do
46
+ JcheckRails::Encoder.convert_to_javascript(6.78).should == "6.78"
47
+ end
48
+
49
+ it "should convert arrays" do
50
+ JcheckRails::Encoder.convert_to_javascript(["some", "cool", 3, :values, true]).should == "['some', 'cool', 3, 'values', true]"
51
+ end
52
+
53
+ it "should convert blank hashes to true" do
54
+ JcheckRails::Encoder.convert_to_javascript({}).should == "true"
55
+ end
56
+
57
+ it "should generate strings with single quotes" do
58
+ JcheckRails::Encoder.convert_to_javascript("some string").should == "'some string'"
59
+ end
60
+
61
+ it "should generate strings with escaped quotes" do
62
+ JcheckRails::Encoder.convert_to_javascript("it can't happen").should == "'it can\\'t happen'"
63
+ end
64
+
65
+ it "should convert regular expressions" do
66
+ JcheckRails::Encoder.convert_to_javascript(/^[sS]ome_reg/im).should == "/^[sS]ome_reg/im"
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,253 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "JcheckRails" do
4
+ context "rails validators" do
5
+ it "should return blank object if attribute has no validations" do
6
+ m = mock_model do
7
+ attr_accessor :name
8
+ end
9
+
10
+ jcheck(m, :name).should == "{}"
11
+ end
12
+
13
+ context "acceptance validator" do
14
+ it "should generate jcheck validator" do
15
+ m = mock_model do
16
+ attr_accessor :terms
17
+
18
+ validates_acceptance_of :terms
19
+ end
20
+
21
+ jcheck(m, :terms).should == "{'acceptance': {'accept': '1'}}"
22
+ end
23
+
24
+ it "should accept a custom acceptance" do
25
+ m = mock_model do
26
+ attr_accessor :terms
27
+
28
+ validates_acceptance_of :terms, :accept => "3"
29
+ end
30
+
31
+ jcheck(m, :terms).should == "{'acceptance': {'accept': '3'}}"
32
+ end
33
+ end
34
+
35
+ context "confirmation" do
36
+ it "should generate jcheck validator" do
37
+ m = mock_model do
38
+ attr_accessor :password, :password_confirmation
39
+
40
+ validates_confirmation_of :password
41
+ end
42
+
43
+ jcheck(m, :password).should == "{'confirmation': true}"
44
+ end
45
+ end
46
+
47
+ context "exclusion" do
48
+ it "should generate jcheck validator" do
49
+ m = mock_model do
50
+ attr_accessor :login
51
+
52
+ validates_exclusion_of :login, :in => ["admin", "bot"]
53
+ end
54
+
55
+ jcheck(m, :login).should == "{'exclusion': {'in': ['admin', 'bot']}}"
56
+ end
57
+ end
58
+
59
+ context "format validator" do
60
+ it "should generate jcheck validator" do
61
+ m = mock_model do
62
+ attr_accessor :name
63
+ validates_format_of :name, :with => /^[a-z]regex$/i
64
+ end
65
+
66
+ jcheck(m, :name).should == "{'format': {'with': /^[a-z]regex$/i}}"
67
+ end
68
+
69
+ it "should work with 'without' option" do
70
+ m = mock_model do
71
+ attr_accessor :name
72
+ validates_format_of :name, :without => /^[a-z]regex$/i
73
+ end
74
+
75
+ jcheck(m, :name).should == "{'format': {'without': /^[a-z]regex$/i}}"
76
+ end
77
+ end
78
+
79
+ context "inclusion validator" do
80
+ it "should generate jcheck validator" do
81
+ m = mock_model do
82
+ attr_accessor :civil_state
83
+
84
+ validates_inclusion_of :civil_state, :in => ["single", "married"]
85
+ end
86
+
87
+ jcheck(m, :civil_state).should == "{'inclusion': {'in': ['single', 'married']}}"
88
+ end
89
+ end
90
+
91
+ context "length validator" do
92
+ it "should generate jcheck with is" do
93
+ m = mock_model do
94
+ attr_accessor :message
95
+
96
+ validates_length_of :message, :is => 10
97
+ end
98
+
99
+ jcheck(m, :message).should == "{'length': {'is': 10}}"
100
+ end
101
+
102
+ it "should generate jcheck with minimum" do
103
+ m = mock_model do
104
+ attr_accessor :message
105
+
106
+ validates_length_of :message, :minimum => 10
107
+ end
108
+
109
+ jcheck(m, :message).should == "{'length': {'minimum': 10}}"
110
+ end
111
+
112
+ it "should generate jcheck with maximum" do
113
+ m = mock_model do
114
+ attr_accessor :message
115
+
116
+ validates_length_of :message, :maximum => 10
117
+ end
118
+
119
+ jcheck(m, :message).should == "{'length': {'maximum': 10}}"
120
+ end
121
+
122
+ it "should generate jcheck with minimum and maximum" do
123
+ m = mock_model do
124
+ attr_accessor :message
125
+
126
+ validates_length_of :message, :minimum => 2, :maximum => 10
127
+ end
128
+
129
+ jcheck(m, :message).should == "{'length': {'minimum': 2, 'maximum': 10}}"
130
+ end
131
+ end
132
+
133
+ context "numericality validator" do
134
+ it "should generate jcheck" do
135
+ m = mock_model do
136
+ attr_accessor :number
137
+
138
+ validates_numericality_of :number
139
+ end
140
+
141
+ jcheck(m, :number).should == "{'numericality': {'only_integer': false, 'allow_nil': false}}"
142
+ end
143
+
144
+ it "should accept the only_integer parameter" do
145
+ m = mock_model do
146
+ attr_accessor :number
147
+
148
+ validates_numericality_of :number, :only_integer => true
149
+ end
150
+
151
+ jcheck(m, :number).should == "{'numericality': {'only_integer': true, 'allow_nil': false}}"
152
+ end
153
+
154
+ [:greater_than, :greater_than_or_equal_to, :equal_to, :less_than, :less_than_or_equal_to].each do |parameter|
155
+ it "should accept #{parameter} parameter" do
156
+ m = mock_model do
157
+ attr_accessor :number
158
+
159
+ validates_numericality_of :number, parameter => 10
160
+ end
161
+
162
+ jcheck(m, :number).should == "{'numericality': {'only_integer': false, 'allow_nil': false, '#{parameter}': 10}}"
163
+ end
164
+ end
165
+
166
+ [:odd, :even].each do |type|
167
+ it "should accept #{type}" do
168
+ m = mock_model do
169
+ attr_accessor :number
170
+
171
+ validates_numericality_of :number, type => true
172
+ end
173
+
174
+ jcheck(m, :number).should == "{'numericality': {'only_integer': false, 'allow_nil': false, '#{type}': true}}"
175
+ end
176
+ end
177
+ end
178
+
179
+ context "presence validator" do
180
+ it "should generate correct jcheck validation" do
181
+ m = mock_model do
182
+ attr_accessor :name
183
+ validates_presence_of :name
184
+ end
185
+
186
+ jcheck(m, :name).should == "{'presence': true}"
187
+ end
188
+ end
189
+ end
190
+
191
+ context "generating all script for model" do
192
+ before :all do
193
+ @m = mock_model do
194
+ attr_accessor :name
195
+
196
+ validates_presence_of :name
197
+
198
+ def persisted?
199
+ false
200
+ end
201
+
202
+ def to_key
203
+ nil
204
+ end
205
+ end
206
+
207
+ SampleModel = @m.class
208
+ end
209
+
210
+ it "should generate all data" do
211
+ jcheck(@m).should == "<script type=\"text/javascript\"> jQuery(function() { var validator = jQuery('#new_sample_model').jcheck({'field_prefix': 'sample_model'}); validator.validates('name', {'presence': true}); }); </script>"
212
+ end
213
+
214
+ it "should be able to customize form id" do
215
+ jcheck(@m, nil, :form_id => "custom_form_id").should include("jQuery('#custom_form_id')")
216
+ end
217
+
218
+ it "should be able to customize field prefix" do
219
+ jcheck(@m, nil, :field_prefix => "custom_field_prefix").should include("'field_prefix': 'custom_field_prefix'")
220
+ end
221
+
222
+ it "should be able to add custom values" do
223
+ jcheck(@m, nil, :notifiers => ["custom_notifier"], :prevent_submit => false).should include("'notifiers': ['custom_notifier']", "'prevent_submit': false")
224
+ end
225
+
226
+ it "should be able customize javascript variable name" do
227
+ jcheck(@m, nil, :variable => "v").should include("var v = jQuery", "v.validates")
228
+ end
229
+ end
230
+
231
+ context "custom validations" do
232
+ it "should ignore validations made with validate" do
233
+ m = mock_model do
234
+ attr_accessor :name
235
+
236
+ validates_presence_of :name
237
+ validate :something
238
+
239
+ def something
240
+ errors.add(:name, "some message")
241
+ end
242
+
243
+ def to_key
244
+ nil
245
+ end
246
+ end
247
+
248
+ CusCla = m.class
249
+
250
+ jcheck(m).should == "<script type=\"text/javascript\"> jQuery(function() { var validator = jQuery('#new_cus_cla').jcheck({'field_prefix': 'cus_cla'}); validator.validates('name', {'presence': true}); }); </script>"
251
+ end
252
+ end
253
+ end
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ Bundler.setup
5
+ Bundler.require
6
+
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
9
+
10
+ require 'jcheck_rails'
11
+
12
+ RSpec.configure do |c|
13
+ c.filter_run :focus => true
14
+ c.run_all_when_everything_filtered = true
15
+ end
16
+
17
+ def jcheck(*args)
18
+ JcheckRails.jcheck_for(*args)
19
+ end
20
+
21
+ def mock_model(&block)
22
+ cls = Class.new
23
+ cls.send :include, ActiveModel::Validations
24
+ cls.class_eval &block
25
+ cls.new
26
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jcheck_rails
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 1
9
+ version: 0.1.1
10
+ platform: ruby
11
+ authors:
12
+ - Wilker Lucio
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-08-15 00:00:00 -03:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Generate jCheck code according to ActiveModel validations
22
+ email: wilkerlucio@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - LICENSE
29
+ - README.textile
30
+ files:
31
+ - .document
32
+ - .gitignore
33
+ - Gemfile
34
+ - Gemfile.lock
35
+ - LICENSE
36
+ - README.textile
37
+ - Rakefile
38
+ - VERSION
39
+ - init.rb
40
+ - jcheck_rails.gemspec
41
+ - lib/jcheck_rails.rb
42
+ - lib/jcheck_rails/encoder.rb
43
+ - spec/encoder_spec.rb
44
+ - spec/jcheck_rails_spec.rb
45
+ - spec/spec_helper.rb
46
+ has_rdoc: true
47
+ homepage: http://github.com/wilkerlucio/jcheck_rails
48
+ licenses: []
49
+
50
+ post_install_message:
51
+ rdoc_options:
52
+ - --charset=UTF-8
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: -1590808180276074311
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ requirements: []
73
+
74
+ rubyforge_project:
75
+ rubygems_version: 1.3.7
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: Generate jCheck code according to ActiveModel validations
79
+ test_files:
80
+ - spec/encoder_spec.rb
81
+ - spec/jcheck_rails_spec.rb
82
+ - spec/spec_helper.rb