bootstrap3_form_builder 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5fefe7e1087f0ce25abda0eb9530119255983248
4
+ data.tar.gz: 3e45e232473187c828a0d93a81f1a3ca38866d4d
5
+ SHA512:
6
+ metadata.gz: 77efd9bd64ee50f6c40ba473d3b20520afbf57434c086dc5fc202cd1a07ea3b166d9b077fe3c670ec883b4c968a15e2ffe9890fbfbc1821e9ec7bc2f0a614b09
7
+ data.tar.gz: c27cbb295a6631d1ab6ff769aa17e8a5f8c90fb7324fbb05f4efb0538309d2cc7ddc367fa39db892d647a4e02d7e6d7489f6ceed1081d1cb26dafbc3912c9f4f
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Zachary Wright
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rspec/core/rake_task'
8
+
9
+ Bundler::GemHelper.install_tasks
10
+
11
+ desc 'Run tests'
12
+ task :default => :spec
13
+
14
+ RSpec::Core::RakeTask.new('spec') do |t|
15
+ t.pattern = FileList['spec/**/*_spec.rb']
16
+ end
17
+
18
+
19
+
20
+
21
+
22
+
23
+
24
+
@@ -0,0 +1,134 @@
1
+ module Bootstrap3FormBuilder
2
+ class FormBuilder < ActionView::Helpers::FormBuilder
3
+ #Replace form submit input with styled buttons
4
+ def submit(label, *args)
5
+ options = args.extract_options!
6
+ new_class = options[:class] || Bootstrap3FormBuilder.default_submit_style
7
+ super(label, *(args << options.merge(:class => new_class)))
8
+ end
9
+
10
+ =begin
11
+ Generates form fields that work with Twitter Bootstrap 3.
12
+ =end
13
+ def self.create_tagged_field(method_name)
14
+ define_method(method_name) do |label, *args|
15
+ options = args.extract_options!
16
+
17
+ custom_label = options[:label] || label.to_s.humanize
18
+ options[:title] = options[:title] || custom_label.capitalize
19
+ label_class = options[:label_class] || ""
20
+
21
+ validators = @object.class.validators_on(label)
22
+
23
+ if validators.collect(&:class).include?(ActiveRecord::Validations::PresenceValidator) || validators.collect(&:class).include?(ActiveModel::Validations::PresenceValidator)
24
+ options[:required] = true
25
+ end
26
+
27
+ allow_pattern = true
28
+ if options[:pattern]
29
+ allow_pattern = false
30
+ end
31
+
32
+ length_validator = (validators.select { |v| v.class == ActiveModel::Validations::LengthValidator}).first
33
+
34
+ if length_validator && allow_pattern
35
+ maximum = length_validator.options[:maximum] || length_validator.options[:is] || (length_validator.options[:in] ? length_validator.options[:in].max : "")
36
+ minimum = length_validator.options[:minimum] || length_validator.options[:is] || (length_validator.options[:in] ? length_validator.options[:in].min : "0")
37
+
38
+ options[:pattern] = ".{#{minimum},#{maximum}}"
39
+
40
+ if minimum == "0"
41
+ options[:title] = "#{options[:title]} - #{maximum} characters maximum"
42
+ elsif maximum == ""
43
+ options[:title] = "#{options[:title]} - #{minimum} characters minimum"
44
+ elsif minimum == maximum
45
+ options[:title] = "#{options[:title]} - Must be exactly #{maximum} characters"
46
+ else
47
+ options[:title] = "#{options[:title]} - #{minimum} to #{maximum} characters"
48
+ end
49
+ end
50
+
51
+ inclusion_validator = (validators.select { |v| v.class == ActiveModel::Validations::InclusionValidator}).first
52
+
53
+ if inclusion_validator && allow_pattern
54
+ valid_options = inclusion_validator.options[:in] || inclusion_validator.options[:within]
55
+
56
+ options[:pattern] = "(#{valid_options.join("|")})"
57
+ options[:title] = inclusion_validator.options[:message] || "#{options[:title]} - Must be one of the following: #{valid_options.join(", ")}"
58
+ end
59
+
60
+ numericality_validator = (validators.select{ |v| v.class == ActiveModel::Validations::NumericalityValidator}).first
61
+
62
+ if numericality_validator && !options[:type]
63
+ options[:type] = "number"
64
+
65
+ if allow_pattern
66
+ options[:pattern] = "\\d*"
67
+ end
68
+
69
+ if numericality_validator.options[:only_integer]
70
+ options[:step] = 1
71
+ else
72
+ options[:step] = "any"
73
+ end
74
+ end
75
+
76
+ format_validator = (validators.select { |v| v.class == ActiveModel::Validations::FormatValidator}).first
77
+
78
+ if format_validator && allow_pattern
79
+ options[:pattern] = format_validator.options[:with].source.html_safe
80
+ options[:title] = format_validator.options[:message] || "#{options[:title]} is not a valid format"
81
+ end
82
+
83
+ control_group_class = "form-group"
84
+ if @object.errors.messages[label]
85
+ control_group_class = control_group_class + " error"
86
+ end
87
+
88
+ if !options[:class]
89
+ options[:class] = "form-control"
90
+ end
91
+
92
+ input_prefix = ""
93
+ input_suffix = ""
94
+
95
+ if options[:input_prefix]
96
+ input_prefix = @template.content_tag("div", options[:input_prefix], :class => "input-group-addon")
97
+ end
98
+
99
+ if options[:input_suffix]
100
+ input_suffix = @template.content_tag("div", options[:input_suffix], :class => "input-group-addon")
101
+ end
102
+
103
+ if !input_prefix.empty? || !input_suffix.empty?
104
+ input = @template.content_tag("div",
105
+ input_prefix.html_safe +
106
+ super(label, *(args << options)) +
107
+ input_suffix.html_safe,
108
+ :class => "input-group " + (options[:input_container_class] || ""))
109
+ else
110
+ input = super(label, *(args << options))
111
+
112
+ if options[:input_container_class]
113
+ input = @template.content_tag("div", input.html_safe, :class => options[:input_container_class])
114
+ end
115
+ end
116
+
117
+ input = input.html_safe +
118
+ (options[:help_block] ? @template.content_tag("p", options[:help_block], :class => "help-block") : "" ) +
119
+ (options[:help_inline] ? @template.content_tag("span", options[:help_inline], :class => "help-inline") : "" )
120
+
121
+ @template.content_tag("div",
122
+ @template.content_tag("label",
123
+ custom_label,
124
+ :for => "#{@object_name}_#{label}",
125
+ :class => label_class) + input.html_safe,
126
+ :class => control_group_class)
127
+ end
128
+ end
129
+
130
+ field_helpers.each do |name|
131
+ create_tagged_field(name)
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,3 @@
1
+ module Bootstrap3FormBuilder
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,52 @@
1
+ require 'rails'
2
+ require 'bootstrap3_form_builder/form_builder'
3
+
4
+ module Bootstrap3FormBuilder
5
+ mattr_reader :error_partial
6
+ mattr_writer :error_partial
7
+ @@error_partial = nil
8
+
9
+ mattr_reader :default_submit_style
10
+ mattr_writer :default_submit_style
11
+ @@error_partial = nil
12
+
13
+ mattr_reader :default_form_layout
14
+ mattr_writer :default_form_layout
15
+ @@default_form_layout = nil
16
+
17
+
18
+ def self.setup
19
+ yield self
20
+ end
21
+
22
+ module BootstrapFormHelper
23
+ def bootstrap_form_for(name, *args, &block)
24
+ options = args.extract_options!
25
+
26
+ target = name
27
+ if target.kind_of?(Array)
28
+ target = name[1]
29
+ end
30
+
31
+ #Default form class
32
+ if Bootstrap3FormBuilder.default_form_layout
33
+ form_html = {:html => {:class => Bootstrap3FormBuilder.default_form_layout}}
34
+
35
+ #Work our default class back into the form options. Defer to whatever is passed in if present
36
+ options.merge!(form_html) do |html_key, passed_in_html, default_html|
37
+ passed_in_html.merge(default_html) do |key, oldval, newval|
38
+ oldval
39
+ end
40
+ end
41
+ end
42
+
43
+ partial_location = Bootstrap3FormBuilder.error_partial || "bootstrap3_form_builder/error_messages"
44
+
45
+ render(partial_location, :target => target) + form_for(name, *(args << options.merge(:builder => Bootstrap3FormBuilder::FormBuilder)), &block)
46
+ end
47
+ end
48
+ end
49
+
50
+ ActiveSupport.on_load(:action_view) do
51
+ include Bootstrap3FormBuilder::BootstrapFormHelper
52
+ end
@@ -0,0 +1,23 @@
1
+ module Bootstrap3FormBuilder
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path("../../templates/bootstrap3_form_builder", __FILE__)
5
+
6
+ class_option :view_engine, :type => :string, :aliases => "-t", :desc => "Template engine for the views. Available options are 'erb' and 'haml'.", :default => "erb"
7
+ class_option :haml, :type => :boolean, :default => false
8
+
9
+ # all public methods in here will be run in order
10
+ def add_my_initializer
11
+ template "initializer.rb", "config/initializers/bootstrap3_form_builder.rb"
12
+ end
13
+
14
+ def create_error_partials
15
+ if options[:view_engine] == "haml" || options[:haml]
16
+ copy_file "_error_messages.html.haml", "app/views/bootstrap3_form_builder/_error_messages.html.haml"
17
+ elsif
18
+ copy_file "_error_messages.html.erb", "app/views/bootstrap3_form_builder/_error_messages.html.erb"
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,14 @@
1
+ <% if target.errors.any? %>
2
+ <div class="alert alert-danger" role="alert">
3
+ <strong>
4
+ <%= "#{pluralize(target.errors.count, "error")} prohibited this from being saved:" %>
5
+ </strong>
6
+ <ul>
7
+ <% target.errors.full_messages.each do |msg| %>
8
+ <li>
9
+ <%= msg %>
10
+ </li>
11
+ <% end %>
12
+ </ul>
13
+ </div>
14
+ <% end %>
@@ -0,0 +1,8 @@
1
+ - if target.errors.any?
2
+ %div.alert.alert-danger{:role => 'alert'}
3
+ %strong
4
+ =pluralize(target.errors.count, "error") + " prohibited this record from being saved:"
5
+ %ul
6
+ - target.errors.full_messages.each do |msg|
7
+ %li
8
+ =msg
@@ -0,0 +1,9 @@
1
+ Bootstrap3FormBuilder.setup do |config|
2
+ # The error messages partial to render with the form
3
+ config.error_partial = "bootstrap3_form_builder/error_messages"
4
+
5
+ config.default_submit_style = "btn btn-default"
6
+
7
+ # The default class to give generated forms.
8
+ # config.default_form_layout = "form-horizontal"
9
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :bootstrap3_form_builder do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bootstrap3_form_builder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Zachary Wright
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: ammeter
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Custom form builder to create forms and inputs that work with Bootstrap
84
+ 3. Also automatically sets pattern and required HTML5 attributes based on validators
85
+ on domain objects.
86
+ email:
87
+ - zacharygwright@gmail.com
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - lib/bootstrap3_form_builder/form_builder.rb
93
+ - lib/bootstrap3_form_builder/version.rb
94
+ - lib/bootstrap3_form_builder.rb
95
+ - lib/generators/bootstrap3_form_builder/install_generator.rb
96
+ - lib/generators/templates/bootstrap3_form_builder/initializer.rb
97
+ - lib/generators/templates/bootstrap3_form_builder/_error_messages.html.erb
98
+ - lib/generators/templates/bootstrap3_form_builder/_error_messages.html.haml
99
+ - lib/tasks/bootstrap3_form_builder_tasks.rake
100
+ - MIT-LICENSE
101
+ - Rakefile
102
+ homepage: https://github.com/zacharyw/bootstrap3-form-builder
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.0.14
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Builds forms with Twitter Bootstrap 3 styling.
126
+ test_files: []