formidable 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +10 -0
- data/CHANGELOG +2 -0
- data/Gemfile +7 -0
- data/LICENSE +20 -0
- data/README.textile +78 -0
- data/TODO.txt +3 -0
- data/deps.rip +5 -0
- data/examples/basic.rb +41 -0
- data/examples/post.rb +65 -0
- data/examples/posts/config.ru +48 -0
- data/examples/posts/form.html.haml +19 -0
- data/examples/posts/models.rb +7 -0
- data/formidable.gemspec +38 -0
- data/formidable.pre.gemspec +8 -0
- data/lib/formidable.rb +9 -0
- data/lib/formidable/coercions.rb +50 -0
- data/lib/formidable/elements.rb +196 -0
- data/lib/formidable/renderers/nokogiri.rb +53 -0
- data/lib/formidable/renderers/string.rb +115 -0
- data/lib/formidable/rendering.rb +42 -0
- data/lib/formidable/validations.rb +94 -0
- data/lib/formidable/validations/class.rb +24 -0
- data/lib/formidable/validations/confirmation.rb +58 -0
- data/lib/formidable/validations/equality.rb +25 -0
- data/lib/formidable/validations/format.rb +23 -0
- data/lib/formidable/validations/length.rb +39 -0
- data/lib/formidable/validations/presence.rb +17 -0
- data/lib/formidable/version.rb +5 -0
- data/spec/formidable/coercions_spec.rb +106 -0
- data/spec/formidable/elements_spec.rb +21 -0
- data/spec/formidable/renderers/nokogiri_spec.rb +9 -0
- data/spec/formidable/renderers/string_spec.rb +9 -0
- data/spec/formidable/rendering_spec.rb +0 -0
- data/spec/formidable/validations/class_spec.rb +0 -0
- data/spec/formidable/validations/confirmation_spec.rb +0 -0
- data/spec/formidable/validations/equality_spec.rb +0 -0
- data/spec/formidable/validations/format_spec.rb +0 -0
- data/spec/formidable/validations/length_spec.rb +0 -0
- data/spec/formidable/validations/presence_spec.rb +0 -0
- data/spec/formidable/validations_spec.rb +9 -0
- data/spec/formidable_spec.rb +51 -0
- data/spec/spec.opts +6 -0
- data/spec/spec_helper.rb +13 -0
- data/tasks.rb +38 -0
- data/vendor/cache/validatable-1.6.7.gem +0 -0
- metadata +107 -0
@@ -0,0 +1,17 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Formidable
|
4
|
+
module Validations
|
5
|
+
class ValidatePresence < Validation
|
6
|
+
register(:validate_presence)
|
7
|
+
|
8
|
+
def valid?
|
9
|
+
! element.cleaned_data.nil?
|
10
|
+
end
|
11
|
+
|
12
|
+
def message
|
13
|
+
"can't be empty"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require_relative "../spec_helper"
|
4
|
+
|
5
|
+
require "formidable/coercions"
|
6
|
+
|
7
|
+
describe "Formidable::Coercions" do
|
8
|
+
base_class = Class.new do
|
9
|
+
def self.to_s; "BaseClass"; end
|
10
|
+
attr_accessor :raw_data, :cleaned_data
|
11
|
+
end
|
12
|
+
|
13
|
+
coercions_class = Class.new(base_class) do
|
14
|
+
def self.to_s; "CoercionsClass"; end
|
15
|
+
include Formidable::Coercions
|
16
|
+
end
|
17
|
+
|
18
|
+
describe ".coercions" do
|
19
|
+
it "should have defined accessors" do
|
20
|
+
Formidable::Coercions.coercions.should respond_to(:[])
|
21
|
+
Formidable::Coercions.coercions.should respond_to(:[]=)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should raise MissingCoercion if there isn't any coercion for given key" do
|
25
|
+
-> { Formidable::Coercions.coercions[:not_found] }.should raise_error(Formidable::Coercions::MissingCoercion)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe ".included" do
|
30
|
+
before(:each) do
|
31
|
+
@klass = Class.new
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should fail if the class doesn't have method #raw_data nor #cleaned_data= defined" do
|
35
|
+
-> { Class.new.send(:include, Formidable::Coercions) }.should raise_error(Formidable::Coercions::IncompatibleInterface)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should fail if the class doesn't have method #raw_data defined" do
|
39
|
+
@klass.send(:attr_writer, :cleaned_data)
|
40
|
+
-> { @klass.send(:include, Formidable::Coercions) }.should raise_error(Formidable::Coercions::IncompatibleInterface)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should fail if the class doesn't have method #cleaned_data= defined" do
|
44
|
+
@klass.send(:attr_reader, :raw_data)
|
45
|
+
-> { @klass.send(:include, Formidable::Coercions) }.should raise_error(Formidable::Coercions::IncompatibleInterface)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should pass otherwise" do
|
49
|
+
@klass.send(:attr_accessor, :raw_data, :cleaned_data)
|
50
|
+
-> { @klass.send(:include, Formidable::Coercions) }.should_not raise_error(Formidable::Coercions::IncompatibleInterface)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe "#coercions" do
|
55
|
+
before(:each) do
|
56
|
+
@instance = coercions_class.new
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should have defined accessors" do
|
60
|
+
@instance.coercions.should respond_to(:[])
|
61
|
+
@instance.coercions.should respond_to(:[]=)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe "#coerce" do
|
66
|
+
before(:each) do
|
67
|
+
@instance = coercions_class.new
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should fail with ArgumentError if both block and type are provided" do
|
71
|
+
-> { @instance.coerce(:integer) { |value| } }.should raise_error(ArgumentError)
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should fail with ArgumentError if neither block nor type are provided" do
|
75
|
+
-> { @instance.coerce }.should raise_error(ArgumentError)
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should take a block as an argument and run it against the #raw_data" do
|
79
|
+
@instance.coerce { |value| value.to_i }
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should take a block as an argument and run it against" do
|
83
|
+
@instance.coerce(:integer)
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should -- not existing key" do
|
87
|
+
-> { @instance.coerce(:class) }.should raise_error()
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
describe "#coerce!" do
|
92
|
+
before(:each) do
|
93
|
+
@instance = coercions_class.new
|
94
|
+
end
|
95
|
+
|
96
|
+
it "should run all coercions against raw_data defined in the #raw_data method" do
|
97
|
+
end
|
98
|
+
|
99
|
+
it "should set #cleaned_data to the result of coercion" do
|
100
|
+
@instance.coerce!
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should be chainable, so the next coercion proc will receive the result of the previous on" do
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require_relative "../spec_helper"
|
4
|
+
require_relative "../../examples/basic"
|
5
|
+
|
6
|
+
describe "Formidable::Elements" do
|
7
|
+
before(:each) do
|
8
|
+
@form = BasicForm.new(action: "/create", method: "POST")
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "#renderer" do
|
12
|
+
it "should be an object responding to #render method" do
|
13
|
+
# @form.renderer.should respond_to(:render)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "#render" do
|
18
|
+
it "should call the #call method on renderer" do
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require_relative "spec_helper"
|
4
|
+
require_relative "../examples/basic"
|
5
|
+
|
6
|
+
# TODO: odstranovani elementu (accessory)
|
7
|
+
describe "Formidable" do
|
8
|
+
before(:each) do
|
9
|
+
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "#renderer" do
|
13
|
+
it "should be an object responding to #render method" do
|
14
|
+
# @form.renderer.should respond_to(:render)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "#render" do
|
19
|
+
it "should call the #render method on renderer" do
|
20
|
+
#params = {rating: "3"} # validations
|
21
|
+
params = {name: "Hello", rating: 3}
|
22
|
+
# params = {name: "Hello", rating: 3, :i_brake_it => {nono: 'test'}}
|
23
|
+
@form = BasicForm.new(:my_form, {action: "/create", method: "POST"}, params)
|
24
|
+
|
25
|
+
puts @form.inspect
|
26
|
+
puts "=== RENDER ==="
|
27
|
+
puts @form.render
|
28
|
+
|
29
|
+
puts "\n=== VALID? ==="
|
30
|
+
puts @form.valid?
|
31
|
+
|
32
|
+
puts "\n=== ERRORS ==="
|
33
|
+
p @form.errors
|
34
|
+
|
35
|
+
puts "\n=== ACCESSORS ==="
|
36
|
+
puts @form.rating.raw_data
|
37
|
+
puts @form.name.raw_data
|
38
|
+
puts @form.i_brake_it.raw_data.inspect
|
39
|
+
puts @form.i_brake_it.nono.raw_data
|
40
|
+
|
41
|
+
puts @form.i_brake_it.nono.raw_data = "NOTEST"
|
42
|
+
puts @form.i_brake_it.raw_data.inspect
|
43
|
+
# puts @form.submit
|
44
|
+
|
45
|
+
puts '=' * 80
|
46
|
+
@form1 = MyForm.new(:test_form)
|
47
|
+
puts @form1.render
|
48
|
+
p @form1.errors
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
data/tasks.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
#!/usr/bin/env nake
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
begin
|
5
|
+
require File.expand_path("../.bundle/environment", __FILE__)
|
6
|
+
rescue LoadError
|
7
|
+
require "bundler"
|
8
|
+
Bundler.setup
|
9
|
+
end
|
10
|
+
|
11
|
+
$LOAD_PATH.unshift(File.expand_path("../lib", __FILE__))
|
12
|
+
|
13
|
+
require "nake/tasks/gem"
|
14
|
+
require "nake/tasks/spec"
|
15
|
+
require "nake/tasks/release"
|
16
|
+
|
17
|
+
require_relative "lib/formidable/version"
|
18
|
+
|
19
|
+
begin
|
20
|
+
load "code-cleaner.nake"
|
21
|
+
Nake::Task["hooks:whitespace:install"].tap do |task|
|
22
|
+
task.config[:path] = "script"
|
23
|
+
task.config[:encoding] = "utf-8"
|
24
|
+
task.config[:whitelist] = '(bin/[^/]+|.+\.(rb|rake|nake|thor|task))$'
|
25
|
+
end
|
26
|
+
rescue LoadError
|
27
|
+
warn "If you want to contribute to Form, please install code-cleaner and then run ./tasks.rb hooks:whitespace:install to get Git pre-commit hook for removing trailing whitespace."
|
28
|
+
end
|
29
|
+
|
30
|
+
# Setup encoding, so all the operations
|
31
|
+
# with strings from another files will work
|
32
|
+
Encoding.default_internal = "utf-8"
|
33
|
+
Encoding.default_external = "utf-8"
|
34
|
+
|
35
|
+
Task[:build].config[:gemspec] = "form.gemspec"
|
36
|
+
Task[:prerelease].config[:gemspec] = "form.pre.gemspec"
|
37
|
+
Task[:release].config[:name] = "form"
|
38
|
+
Task[:release].config[:version] = Formidable::VERSION
|
Binary file
|
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: formidable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Jakub Stastny aka Botanicus
|
13
|
+
- Pavel Kunc
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain:
|
17
|
+
date: 2010-03-30 00:00:00 +01:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: ""
|
22
|
+
email: stastny@101ideas.cz
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- .gitignore
|
31
|
+
- CHANGELOG
|
32
|
+
- Gemfile
|
33
|
+
- LICENSE
|
34
|
+
- README.textile
|
35
|
+
- TODO.txt
|
36
|
+
- deps.rip
|
37
|
+
- examples/basic.rb
|
38
|
+
- examples/post.rb
|
39
|
+
- examples/posts/config.ru
|
40
|
+
- examples/posts/form.html.haml
|
41
|
+
- examples/posts/models.rb
|
42
|
+
- formidable.gemspec
|
43
|
+
- formidable.pre.gemspec
|
44
|
+
- lib/formidable.rb
|
45
|
+
- lib/formidable/coercions.rb
|
46
|
+
- lib/formidable/elements.rb
|
47
|
+
- lib/formidable/renderers/nokogiri.rb
|
48
|
+
- lib/formidable/renderers/string.rb
|
49
|
+
- lib/formidable/rendering.rb
|
50
|
+
- lib/formidable/validations.rb
|
51
|
+
- lib/formidable/validations/class.rb
|
52
|
+
- lib/formidable/validations/confirmation.rb
|
53
|
+
- lib/formidable/validations/equality.rb
|
54
|
+
- lib/formidable/validations/format.rb
|
55
|
+
- lib/formidable/validations/length.rb
|
56
|
+
- lib/formidable/validations/presence.rb
|
57
|
+
- lib/formidable/version.rb
|
58
|
+
- spec/formidable/coercions_spec.rb
|
59
|
+
- spec/formidable/elements_spec.rb
|
60
|
+
- spec/formidable/renderers/nokogiri_spec.rb
|
61
|
+
- spec/formidable/renderers/string_spec.rb
|
62
|
+
- spec/formidable/rendering_spec.rb
|
63
|
+
- spec/formidable/validations/class_spec.rb
|
64
|
+
- spec/formidable/validations/confirmation_spec.rb
|
65
|
+
- spec/formidable/validations/equality_spec.rb
|
66
|
+
- spec/formidable/validations/format_spec.rb
|
67
|
+
- spec/formidable/validations/length_spec.rb
|
68
|
+
- spec/formidable/validations/presence_spec.rb
|
69
|
+
- spec/formidable/validations_spec.rb
|
70
|
+
- spec/formidable_spec.rb
|
71
|
+
- spec/spec.opts
|
72
|
+
- spec/spec_helper.rb
|
73
|
+
- tasks.rb
|
74
|
+
- vendor/cache/validatable-1.6.7.gem
|
75
|
+
has_rdoc: true
|
76
|
+
homepage: http://github.com/botanicus/formidable
|
77
|
+
licenses: []
|
78
|
+
|
79
|
+
post_install_message: "[\e[32mVersion 0.0.1\e[0m] Support for renderers\n"
|
80
|
+
rdoc_options: []
|
81
|
+
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
segments:
|
89
|
+
- 1
|
90
|
+
- 9
|
91
|
+
version: "1.9"
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
segments:
|
97
|
+
- 0
|
98
|
+
version: "0"
|
99
|
+
requirements: []
|
100
|
+
|
101
|
+
rubyforge_project: formidable
|
102
|
+
rubygems_version: 1.3.6
|
103
|
+
signing_key:
|
104
|
+
specification_version: 3
|
105
|
+
summary: ""
|
106
|
+
test_files: []
|
107
|
+
|