bootstrap3_form_builder 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bc3951fe0fc7fda12703f114b3ff5ac5f11615a3
4
- data.tar.gz: fa9e5ee5ca9ca2e78a53ab5ff339e7b136558043
3
+ metadata.gz: ddc817fe53c8092e66dcd7eca2995694e244b3c7
4
+ data.tar.gz: 46f68dc84ff1589abd0d8f8bafd3ba58316cc844
5
5
  SHA512:
6
- metadata.gz: 638c2133d75e8569307b773b61c7529c71069a18c8b2eddd3aa82395207ed4206af2f26dab4f58ad3e0ba58c0bed976b1b1f04e16a307be08740f15488d9502d
7
- data.tar.gz: f38e42d88da03b7900ecf280e061a32e8bd94718b4ca00d8785426d2d200b030b15b2a6c3713d97c6a079fb9d2dc447151f23f3aa9ef4d127be47aa3eab6ba53
6
+ metadata.gz: 617e8916bce502c0365eab4f123f8ab6fec9294ed0a6694ba70eac7109ffadab5c16129a1ed713d6178d3c2c6f4e7921781b0d50ab8eb6717deca9d819e77b5f
7
+ data.tar.gz: c66acb6ebb27109e86fed5969df45acf5cace740847d91adc0a5356e5232ee719e22da1aacfc45a55248b4cb6ab035a8d8091dfb0f5a46c7e465acd94282a188
data/MIT-LICENSE CHANGED
File without changes
data/Rakefile CHANGED
File without changes
@@ -18,87 +18,22 @@ Generates form fields that work with Twitter Bootstrap 3.
18
18
  options[:title] = options[:title] || custom_label.capitalize
19
19
  label_class = options[:label_class] || ""
20
20
 
21
- validators = @object.class.validators_on(label)
21
+ allow_pattern = allow_pattern?(options)
22
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"
23
+ validators = @object.class.validators_on(label).select { |validator| !validator.options.key? :if }
64
24
 
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 && !method_name.to_s.include?('email')
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
25
+ set_presence_options(validators, options)
26
+ set_length_options(validators, options)
27
+ set_inclusion_options(validators, options)
28
+ set_numericality_options(validators, options)
29
+ set_format_options(validators, options, method_name)
87
30
 
88
31
  if !options[:class]
89
32
  options[:class] = "form-control"
90
33
  end
91
34
 
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
35
+ input_prefix = options[:input_prefix] ? @template.content_tag("div", options[:input_prefix], :class => "input-group-addon") : ""
36
+ input_suffix = options[:input_suffix] ? @template.content_tag("div", options[:input_suffix], :class => "input-group-addon") : ""
102
37
 
103
38
  if !input_prefix.empty? || !input_suffix.empty?
104
39
  input = @template.content_tag("div",
@@ -123,12 +58,94 @@ Generates form fields that work with Twitter Bootstrap 3.
123
58
  custom_label,
124
59
  :for => "#{@object_name}_#{label}",
125
60
  :class => label_class) + input.html_safe,
126
- :class => control_group_class)
61
+ :class => control_group_class(label))
127
62
  end
128
63
  end
129
64
 
130
65
  field_helpers.each do |name|
131
66
  create_tagged_field(name)
132
67
  end
68
+
69
+ private
70
+ def allow_pattern?(options)
71
+ if options[:pattern]
72
+ return false
73
+ end
74
+
75
+ true
76
+ end
77
+
78
+ def set_presence_options(validators, options)
79
+ if validators.collect(&:class).include?(ActiveRecord::Validations::PresenceValidator) || validators.collect(&:class).include?(ActiveModel::Validations::PresenceValidator)
80
+ options[:required] = true
81
+ end
82
+ end
83
+
84
+ def set_length_options(validators, options)
85
+ length_validator = (validators.select { |v| v.class == ActiveModel::Validations::LengthValidator}).first
86
+
87
+ if length_validator && allow_pattern?(options)
88
+ maximum = length_validator.options[:maximum] || length_validator.options[:is] || (length_validator.options[:in] ? length_validator.options[:in].max : "")
89
+ minimum = length_validator.options[:minimum] || length_validator.options[:is] || (length_validator.options[:in] ? length_validator.options[:in].min : "0")
90
+
91
+ options[:pattern] = ".{#{minimum},#{maximum}}"
92
+
93
+ if minimum == "0"
94
+ options[:title] = "#{options[:title]} - #{maximum} characters maximum"
95
+ elsif maximum == ""
96
+ options[:title] = "#{options[:title]} - #{minimum} characters minimum"
97
+ elsif minimum == maximum
98
+ options[:title] = "#{options[:title]} - Must be exactly #{maximum} characters"
99
+ else
100
+ options[:title] = "#{options[:title]} - #{minimum} to #{maximum} characters"
101
+ end
102
+ end
103
+ end
104
+
105
+ def set_inclusion_options(validators, options)
106
+ inclusion_validator = (validators.select { |v| v.class == ActiveModel::Validations::InclusionValidator}).first
107
+
108
+ if inclusion_validator && allow_pattern?(options)
109
+ valid_options = inclusion_validator.options[:in] || inclusion_validator.options[:within]
110
+
111
+ options[:pattern] = "(#{valid_options.join("|")})"
112
+ options[:title] = inclusion_validator.options[:message] || "#{options[:title]} - Must be one of the following: #{valid_options.join(", ")}"
113
+ end
114
+ end
115
+
116
+ def set_numericality_options(validators, options)
117
+ numericality_validator = (validators.select{ |v| v.class == ActiveModel::Validations::NumericalityValidator}).first
118
+
119
+ if numericality_validator && !options[:type]
120
+ options[:type] = "number"
121
+
122
+ if allow_pattern?(options)
123
+ options[:pattern] = "\\d*"
124
+ end
125
+
126
+ if numericality_validator.options[:only_integer]
127
+ options[:step] = 1
128
+ else
129
+ options[:step] = "any"
130
+ end
131
+ end
132
+ end
133
+
134
+ def set_format_options(validators, options, method_name)
135
+ format_validator = (validators.select { |v| v.class == ActiveModel::Validations::FormatValidator}).first
136
+
137
+ if format_validator && allow_pattern?(options) && !method_name.to_s.include?('email')
138
+ options[:pattern] = format_validator.options[:with].source.html_safe
139
+ options[:title] = format_validator.options[:message] || "#{options[:title]} is not a valid format"
140
+ end
141
+ end
142
+
143
+ def control_group_class(label)
144
+ if @object.errors.messages[label]
145
+ return "form-group error"
146
+ end
147
+
148
+ "form-group"
149
+ end
133
150
  end
134
151
  end
@@ -1,3 +1,3 @@
1
1
  module Bootstrap3FormBuilder
2
- VERSION = "1.0.1"
2
+ VERSION = "1.0.2"
3
3
  end
File without changes
File without changes
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bootstrap3_form_builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zachary Wright
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-14 00:00:00.000000000 Z
11
+ date: 2016-05-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails