formal 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +1 -0
- data/README.md +5 -1
- data/Rakefile +9 -0
- data/formal.gemspec +3 -1
- data/lib/formal/version.rb +1 -1
- data/lib/formal.rb +4 -3
- data/spec/formal_spec.rb +50 -0
- data/spec/spec_helper.rb +56 -0
- metadata +26 -5
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/README.md
CHANGED
@@ -38,6 +38,10 @@ f.text_area
|
|
38
38
|
f.select
|
39
39
|
f.email_field
|
40
40
|
f.search_field
|
41
|
+
f.telephone_field
|
42
|
+
f.number_field
|
43
|
+
f.file_field
|
44
|
+
f.range_field
|
41
45
|
```
|
42
46
|
|
43
47
|
__Example:__
|
@@ -58,7 +62,7 @@ Also provided is a helper for a label with a checkbox __inside__ it (which is
|
|
58
62
|
also wrapped in a ```<dt>```). Use:
|
59
63
|
|
60
64
|
```ruby
|
61
|
-
f.
|
65
|
+
f.check_box_with_label :published
|
62
66
|
```
|
63
67
|
|
64
68
|
which returns:
|
data/Rakefile
CHANGED
@@ -1,2 +1,11 @@
|
|
1
1
|
#!/usr/bin/env rake
|
2
2
|
require "bundler/gem_tasks"
|
3
|
+
require 'rspec/core/rake_task'
|
4
|
+
|
5
|
+
desc 'Default: run tests.'
|
6
|
+
task :default => :spec
|
7
|
+
|
8
|
+
desc 'Run formal tests.'
|
9
|
+
RSpec::Core::RakeTask.new('spec') do |t|
|
10
|
+
t.pattern = FileList['spec/**/*_spec.rb']
|
11
|
+
end
|
data/formal.gemspec
CHANGED
@@ -6,7 +6,7 @@ Gem::Specification.new do |gem|
|
|
6
6
|
gem.email = [""]
|
7
7
|
gem.description = %q{Helpful form builder used by Rocketeers}
|
8
8
|
gem.summary = %q{A form builder that wraps form fields the way we like it at Hashrocket}
|
9
|
-
gem.homepage = ""
|
9
|
+
gem.homepage = "http://github.com/mrmicahcooper/formal"
|
10
10
|
|
11
11
|
gem.files = `git ls-files`.split($\)
|
12
12
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
@@ -14,4 +14,6 @@ Gem::Specification.new do |gem|
|
|
14
14
|
gem.name = "formal"
|
15
15
|
gem.require_paths = ["lib"]
|
16
16
|
gem.version = Formal::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency("rspec-rails")
|
17
19
|
end
|
data/lib/formal/version.rb
CHANGED
data/lib/formal.rb
CHANGED
@@ -5,7 +5,7 @@ module Formal
|
|
5
5
|
|
6
6
|
ActionView::Base.field_error_proc = proc { |input, instance| input }
|
7
7
|
|
8
|
-
@@field_helper_methods = %w(label_tag text_field password_field text_area select email_field search_field)
|
8
|
+
@@field_helper_methods = %w(label_tag text_field password_field text_area select email_field search_field telephone_field number_field file_field range_field)
|
9
9
|
|
10
10
|
def label(method, text = nil, options = {}, &block)
|
11
11
|
text = text || method.to_s.humanize
|
@@ -23,7 +23,8 @@ module Formal
|
|
23
23
|
|
24
24
|
@@field_helper_methods.each do |method_name|
|
25
25
|
define_method method_name do |*args|
|
26
|
-
|
26
|
+
method_name = args.first
|
27
|
+
if object.errors[method_name].present?
|
27
28
|
@template.content_tag(:dd, super(*args), class: 'error')
|
28
29
|
else
|
29
30
|
@template.content_tag(:dd, super(*args))
|
@@ -36,6 +37,6 @@ module Formal
|
|
36
37
|
box = [check_box(method, *args).html_safe, text].join(" ")
|
37
38
|
label(method, box, *args)
|
38
39
|
end
|
39
|
-
|
40
40
|
end
|
41
|
+
|
41
42
|
end
|
data/spec/formal_spec.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Formal do
|
4
|
+
include FormalSpecHelper
|
5
|
+
|
6
|
+
before do
|
7
|
+
@post = Post.new
|
8
|
+
@output = ''
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should yield the formal builder' do
|
12
|
+
form_for(@post, builder: Formal::Builder) do |f|
|
13
|
+
f.should be_kind_of(Formal::Builder)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should wrap input inside dd tag' do
|
18
|
+
with_builder do |f|
|
19
|
+
f.text_field :body
|
20
|
+
end
|
21
|
+
|
22
|
+
expected = %q{<dd><input id="post_body" name="post[body]" size="30" type="text" /></dd>}
|
23
|
+
output.should include(expected)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should wrap check box with label inside' do
|
27
|
+
with_builder do |f|
|
28
|
+
f.check_box_with_label :published
|
29
|
+
end
|
30
|
+
expected = %q{<dt><label for="post_published"><input name="post[published]" type="hidden" value="0" /><input id="post_published" name="post[published]" type="checkbox" value="1" /> published</label></dt>}
|
31
|
+
output.should include(expected)
|
32
|
+
end
|
33
|
+
|
34
|
+
context "with error" do
|
35
|
+
let(:invalid_post) { InvalidPost.new }
|
36
|
+
|
37
|
+
it 'should add error class ONLY to the erroneous dd tag' do
|
38
|
+
with_builder(invalid_post) { |f| f.text_field :body }
|
39
|
+
expected_error = %q{<dd class="error"><input id="invalid_post_body" name="invalid_post[body]" size="30" type="text" /></dd>}
|
40
|
+
output.should include(expected_error)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "does not add the error class to non erroneous dd tags" do
|
44
|
+
with_builder(invalid_post) { |f| f.text_field :title }
|
45
|
+
expected_error = %q{<dd><input id="invalid_post_title" name="invalid_post[title]" size="30" type="text" /></dd>}
|
46
|
+
output.should include(expected_error)
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'action_view'
|
2
|
+
require 'active_model'
|
3
|
+
|
4
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '../lib/formal'))
|
5
|
+
require 'formal'
|
6
|
+
|
7
|
+
class Post < Struct.new(:body, :published)
|
8
|
+
extend ActiveModel::Naming
|
9
|
+
include ActiveModel::Conversion
|
10
|
+
|
11
|
+
def persisted?
|
12
|
+
false
|
13
|
+
end
|
14
|
+
|
15
|
+
def errors
|
16
|
+
{}
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class InvalidPost < Struct.new(:body, :title)
|
21
|
+
extend ActiveModel::Naming
|
22
|
+
include ActiveModel::Conversion
|
23
|
+
|
24
|
+
def persisted?
|
25
|
+
false
|
26
|
+
end
|
27
|
+
|
28
|
+
def errors
|
29
|
+
{ :body => ["can't be blank"] }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
module FormalSpecHelper
|
34
|
+
include ActionView::Context if defined?(ActionView::Context)
|
35
|
+
include ActionController::RecordIdentifier
|
36
|
+
include ActionView::Helpers::FormHelper
|
37
|
+
|
38
|
+
def posts_path(*args)
|
39
|
+
"/posts"
|
40
|
+
end
|
41
|
+
alias :invalid_posts_path :posts_path
|
42
|
+
|
43
|
+
def protect_against_forgery?
|
44
|
+
false
|
45
|
+
end
|
46
|
+
|
47
|
+
def with_builder(model = Post.new)
|
48
|
+
@output = form_for(model, builder: Formal::Builder) do |f|
|
49
|
+
yield f
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def output
|
54
|
+
@output
|
55
|
+
end
|
56
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: formal
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,8 +10,24 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-09
|
14
|
-
dependencies:
|
13
|
+
date: 2012-11-09 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec-rails
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ! '>='
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: '0'
|
15
31
|
description: Helpful form builder used by Rocketeers
|
16
32
|
email:
|
17
33
|
- ''
|
@@ -20,6 +36,7 @@ extensions: []
|
|
20
36
|
extra_rdoc_files: []
|
21
37
|
files:
|
22
38
|
- .gitignore
|
39
|
+
- .rspec
|
23
40
|
- Gemfile
|
24
41
|
- LICENSE
|
25
42
|
- README.md
|
@@ -27,7 +44,9 @@ files:
|
|
27
44
|
- formal.gemspec
|
28
45
|
- lib/formal.rb
|
29
46
|
- lib/formal/version.rb
|
30
|
-
|
47
|
+
- spec/formal_spec.rb
|
48
|
+
- spec/spec_helper.rb
|
49
|
+
homepage: http://github.com/mrmicahcooper/formal
|
31
50
|
licenses: []
|
32
51
|
post_install_message:
|
33
52
|
rdoc_options: []
|
@@ -51,4 +70,6 @@ rubygems_version: 1.8.24
|
|
51
70
|
signing_key:
|
52
71
|
specification_version: 3
|
53
72
|
summary: A form builder that wraps form fields the way we like it at Hashrocket
|
54
|
-
test_files:
|
73
|
+
test_files:
|
74
|
+
- spec/formal_spec.rb
|
75
|
+
- spec/spec_helper.rb
|