formular 0.2.1 → 0.2.2

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.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +5 -4
  3. data/CHANGELOG.md +29 -2
  4. data/README.md +5 -4
  5. data/formular.gemspec +1 -1
  6. data/lib/formular/attributes.rb +10 -21
  7. data/lib/formular/builders/basic.rb +4 -3
  8. data/lib/formular/builders/bootstrap3.rb +2 -1
  9. data/lib/formular/builders/bootstrap4.rb +4 -4
  10. data/lib/formular/element.rb +54 -23
  11. data/lib/formular/element/bootstrap3.rb +40 -8
  12. data/lib/formular/element/bootstrap3/checkable_control.rb +5 -8
  13. data/lib/formular/element/bootstrap3/horizontal.rb +7 -7
  14. data/lib/formular/element/bootstrap3/input_group.rb +2 -2
  15. data/lib/formular/element/bootstrap4.rb +17 -8
  16. data/lib/formular/element/bootstrap4/checkable_control.rb +5 -4
  17. data/lib/formular/element/bootstrap4/custom_control.rb +8 -4
  18. data/lib/formular/element/bootstrap4/horizontal.rb +3 -3
  19. data/lib/formular/element/bootstrap4/input_group.rb +12 -0
  20. data/lib/formular/element/foundation6.rb +5 -5
  21. data/lib/formular/element/foundation6/checkable_control.rb +2 -4
  22. data/lib/formular/element/foundation6/input_group.rb +2 -2
  23. data/lib/formular/element/foundation6/{wrapped_control.rb → wrapped.rb} +4 -4
  24. data/lib/formular/element/modules/checkable.rb +10 -11
  25. data/lib/formular/element/modules/control.rb +12 -4
  26. data/lib/formular/element/modules/error.rb +6 -1
  27. data/lib/formular/element/modules/escape_value.rb +14 -0
  28. data/lib/formular/element/modules/hint.rb +5 -3
  29. data/lib/formular/element/modules/label.rb +5 -2
  30. data/lib/formular/element/modules/{wrapped_control.rb → wrapped.rb} +14 -13
  31. data/lib/formular/elements.rb +62 -19
  32. data/lib/formular/helper.rb +18 -4
  33. data/lib/formular/html_block.rb +1 -1
  34. data/lib/formular/html_escape.rb +19 -0
  35. data/lib/formular/path.rb +1 -6
  36. data/lib/formular/version.rb +1 -1
  37. metadata +16 -7
@@ -7,7 +7,7 @@ module Formular
7
7
  class Element
8
8
  module Bootstrap3
9
9
  module Horizontal
10
- module WrappedControl
10
+ module Wrapped
11
11
  include Formular::Element::Module
12
12
 
13
13
  html(:label_column) do |input|
@@ -35,11 +35,11 @@ module Formular
35
35
  has_label? ? [] : builder.class.column_classes[:left_offset]
36
36
  end
37
37
  end
38
- end # module WrappedControl
38
+ end # module Wrapped
39
39
 
40
40
  module WrappedCheckableControl
41
41
  include Formular::Element::Module
42
- include WrappedControl
42
+ include Wrapped
43
43
 
44
44
  html(:label_column) do |input|
45
45
  input.group_label
@@ -80,19 +80,19 @@ module Formular
80
80
  end # class Form
81
81
 
82
82
  class Select < Formular::Element::Bootstrap3::Select
83
- include WrappedControl
83
+ include Wrapped
84
84
  end # class Select
85
85
 
86
86
  class Textarea < Formular::Element::Bootstrap3::Textarea
87
- include WrappedControl
87
+ include Wrapped
88
88
  end # class Textarea
89
89
 
90
90
  class Input < Formular::Element::Bootstrap3::Input
91
- include WrappedControl
91
+ include Wrapped
92
92
  end # class Input
93
93
 
94
94
  class InputGroup < Formular::Element::Bootstrap3::InputGroup
95
- include WrappedControl
95
+ include Wrapped
96
96
 
97
97
  html(:start) do |input|
98
98
  wrapper = input.wrapper
@@ -1,12 +1,12 @@
1
1
  require 'formular/elements'
2
2
  require 'formular/element/modules/container'
3
- require 'formular/element/modules/wrapped_control'
3
+ require 'formular/element/modules/wrapped'
4
4
 
5
5
  module Formular
6
6
  class Element
7
7
  module Bootstrap3
8
8
  class InputGroup < Formular::Element::Input
9
- include Formular::Element::Modules::WrappedControl
9
+ include Formular::Element::Modules::Wrapped
10
10
  include Formular::Element::Modules::Container
11
11
  include Formular::Element::Bootstrap3::ColumnControl
12
12
 
@@ -1,9 +1,10 @@
1
1
  require 'formular/element'
2
2
  require 'formular/elements'
3
- require 'formular/element/modules/wrapped_control'
3
+ require 'formular/element/modules/wrapped'
4
4
  require 'formular/element/module'
5
5
  require 'formular/element/bootstrap4/checkable_control'
6
6
  require 'formular/element/bootstrap4/custom_control'
7
+ require 'formular/element/bootstrap3'
7
8
  require 'formular/element/bootstrap3/column_control'
8
9
 
9
10
  module Formular
@@ -18,22 +19,30 @@ module Formular
18
19
  end # class Submit
19
20
 
20
21
  class Error < Formular::Element::Error
21
- tag :span
22
+ tag :div
22
23
  set_default :class, ['form-control-feedback']
23
24
  end # class Error
24
25
 
25
26
  class Hint < Formular::Element::Small
26
- set_default :class, ['text-muted']
27
+ set_default :class, ['form-text', 'text-muted']
27
28
  end # class Hint
28
29
 
29
- class Input < Formular::Element::Input
30
- include Formular::Element::Modules::WrappedControl
31
- include Formular::Element::Bootstrap3::ColumnControl
30
+ class Select < Formular::Element::Bootstrap3::Select
31
+ set_default :label_options, { class: ['form-control-label'] }
32
+ end # class Textarea
32
33
 
33
- set_default :class, :input_class
34
+
35
+ class Textarea < Formular::Element::Bootstrap3::Textarea
36
+ set_default :label_options, { class: ['form-control-label'] }
37
+ end # class Textarea
38
+
39
+ class Input < Formular::Element::Bootstrap3::Input
40
+ set_default :label_options, { class: ['form-control-label'] }
34
41
 
35
42
  def input_class
36
- attributes[:type].to_s == 'file' ? %(form-control-file) : %(form-control)
43
+ return %(form-control-file) if options[:type].to_s == 'file'
44
+
45
+ has_errors? ? ['form-control', 'form-control-danger'] : ['form-control']
37
46
  end
38
47
  end # class Input
39
48
 
@@ -1,5 +1,5 @@
1
1
  require 'formular/elements'
2
- require 'formular/element/modules/wrapped_control'
2
+ require 'formular/element/modules/wrapped'
3
3
  require 'formular/element/module'
4
4
 
5
5
  module Formular
@@ -7,15 +7,14 @@ module Formular
7
7
  module Bootstrap4
8
8
  module CheckableControl
9
9
  class Checkbox < Formular::Element::Checkbox
10
- include Formular::Element::Modules::WrappedControl
10
+ include Formular::Element::Modules::Wrapped
11
11
  set_default :class, ['form-check-input']
12
- set_default :value, '1' # instead of reader value
13
12
 
14
13
  html { closed_start_tag }
15
14
  end # class Checkbox
16
15
 
17
16
  class Radio < Formular::Element::Radio
18
- include Formular::Element::Modules::WrappedControl
17
+ include Formular::Element::Modules::Wrapped
19
18
  set_default :class, ['form-check-input']
20
19
 
21
20
  def hidden_tag
@@ -26,6 +25,7 @@ module Formular
26
25
  module InlineCheckable
27
26
  include Formular::Element::Module
28
27
 
28
+ set_default :label_options, { class: ['form-control-label'] }
29
29
  set_default :control_label_options, { class: ['form-check-inline'] }
30
30
 
31
31
  html(:wrapped) do |input|
@@ -72,6 +72,7 @@ module Formular
72
72
  }.join('')
73
73
  end
74
74
 
75
+ set_default :label_options, { class: ['form-control-label'] }
75
76
  set_default :control_label_options, { class: ['form-check-label'] }
76
77
 
77
78
  module InstanceMethods
@@ -1,5 +1,5 @@
1
1
  require 'formular/elements'
2
- require 'formular/element/modules/wrapped_control'
2
+ require 'formular/element/modules/wrapped'
3
3
  require 'formular/element/bootstrap4'
4
4
  require 'formular/element/bootstrap3'
5
5
  require 'formular/element/module'
@@ -10,8 +10,9 @@ module Formular
10
10
  module CustomControl
11
11
  module CustomCheckable
12
12
  include Formular::Element::Module
13
- include Formular::Element::Modules::WrappedControl
13
+ include Formular::Element::Modules::Wrapped
14
14
  set_default :class, ['custom-control-input']
15
+ set_default :label_options, { class: ['form-control-label'] }
15
16
 
16
17
  html(:checkable_label) do |input|
17
18
  Formular::Element::Label.(input.label_options) do
@@ -57,7 +58,6 @@ module Formular
57
58
  html { closed_start_tag }
58
59
 
59
60
  set_default :control_label_options, { class: ['custom-control custom-checkbox'] }
60
- set_default :value, '1' # instead of reader value
61
61
  end # class Checkbox
62
62
 
63
63
  class CustomRadio < Formular::Element::Radio
@@ -71,13 +71,15 @@ module Formular
71
71
 
72
72
  class CustomSelect < Formular::Element::Bootstrap3::Select
73
73
  set_default :class, ['custom-select']
74
+ set_default :label_options, { class: ['form-control-label'] }
74
75
  end # class CustomSelect
75
76
 
76
77
  class CustomFile < Formular::Element::Input
77
- include Formular::Element::Modules::WrappedControl
78
+ include Formular::Element::Modules::Wrapped
78
79
  include Formular::Element::Bootstrap3::ColumnControl
79
80
 
80
81
  set_default :class, ['custom-file-input']
82
+ set_default :label_options, { class: ['form-control-label'] }
81
83
  set_default :type, 'file'
82
84
 
83
85
  rename_html_context(:default, :control)
@@ -93,6 +95,7 @@ module Formular
93
95
 
94
96
  class CustomSelect < Inline::CustomSelect
95
97
  rename_html_context(:default, :input)
98
+ set_default :label_options, { class: ['form-control-label'] }
96
99
 
97
100
  html do |input|
98
101
  Formular::Element::Div.(content: input.to_html(context: :input))
@@ -101,6 +104,7 @@ module Formular
101
104
 
102
105
  class CustomFile < Inline::CustomFile
103
106
  rename_html_context(:default, :input)
107
+ set_default :label_options, { class: ['form-control-label'] }
104
108
 
105
109
  html do |input|
106
110
  Formular::Element::Div.(content: input.to_html(context: :input))
@@ -26,15 +26,15 @@ module Formular
26
26
  end # class Legend
27
27
 
28
28
  class Input < Formular::Element::Bootstrap4::Input
29
- include Formular::Element::Bootstrap3::Horizontal::WrappedControl
29
+ include Formular::Element::Bootstrap3::Horizontal::Wrapped
30
30
  end # class Input
31
31
 
32
32
  class CustomFile < Formular::Element::Bootstrap4::CustomFile
33
- include Formular::Element::Bootstrap3::Horizontal::WrappedControl
33
+ include Formular::Element::Bootstrap3::Horizontal::Wrapped
34
34
  end # class CustomFile
35
35
 
36
36
  class CustomSelect < Formular::Element::Bootstrap4::CustomSelect
37
- include Formular::Element::Bootstrap3::Horizontal::WrappedControl
37
+ include Formular::Element::Bootstrap3::Horizontal::Wrapped
38
38
  end # class CustomFile
39
39
 
40
40
  class Submit < Formular::Element::Bootstrap4::Submit
@@ -0,0 +1,12 @@
1
+ require 'formular/elements'
2
+ require 'formular/element/bootstrap3/input_group'
3
+
4
+ module Formular
5
+ class Element
6
+ module Bootstrap4
7
+ class InputGroup < Formular::Element::Bootstrap3::InputGroup
8
+ set_default :label_options, { class: ['form-control-label'] }
9
+ end # class InputGroup
10
+ end # module Bootstrap4
11
+ end # class Element
12
+ end # module Formular
@@ -1,8 +1,8 @@
1
1
  require 'formular/elements'
2
- require 'formular/element/modules/wrapped_control'
2
+ require 'formular/element/modules/wrapped'
3
3
  require 'formular/element/module'
4
4
  require 'formular/element/foundation6/checkable_control'
5
- require 'formular/element/foundation6/wrapped_control'
5
+ require 'formular/element/foundation6/wrapped'
6
6
  module Formular
7
7
  class Element
8
8
  module Foundation6
@@ -36,7 +36,7 @@ module Formular
36
36
  end # class Hint
37
37
 
38
38
  class Input < Formular::Element::Input
39
- include WrappedControl
39
+ include Wrapped
40
40
  include InputWithErrors
41
41
  end # class Input
42
42
 
@@ -56,12 +56,12 @@ module Formular
56
56
  end # class File
57
57
 
58
58
  class Select < Formular::Element::Select
59
- include WrappedControl
59
+ include Wrapped
60
60
  include InputWithErrors
61
61
  end # class Select
62
62
 
63
63
  class Textarea < Formular::Element::Textarea
64
- include WrappedControl
64
+ include Wrapped
65
65
  include InputWithErrors
66
66
  end # class Textarea
67
67
  end # module Foundation6
@@ -1,13 +1,13 @@
1
1
  require 'formular/elements'
2
2
  require 'formular/element/module'
3
- require 'formular/element/foundation6/wrapped_control'
3
+ require 'formular/element/foundation6/wrapped'
4
4
  module Formular
5
5
  class Element
6
6
  module Foundation6
7
7
  module CheckableControl
8
8
  module Checkable
9
9
  include Formular::Element::Module
10
- include Formular::Element::Foundation6::WrappedControl
10
+ include Formular::Element::Foundation6::Wrapped
11
11
 
12
12
  set_default :label_options, { class: ['is-invalid-label'] }, if: :has_errors?
13
13
  set_default :control_label_options, { class: ['is-invalid-label'] }, if: :has_errors?
@@ -43,8 +43,6 @@ module Formular
43
43
 
44
44
  class Checkbox < Formular::Element::Checkbox
45
45
  include Checkable
46
-
47
- set_default :value, '1' # instead of reader value
48
46
  set_default :label_options, { class: ['is-invalid-label'] }, if: :has_errors?
49
47
  set_default :control_label_options, { class: ['is-invalid-label'] }, if: :has_errors?
50
48
 
@@ -1,13 +1,13 @@
1
1
  require 'formular/elements'
2
2
  require 'formular/element/modules/container'
3
- require 'formular/element/modules/wrapped_control'
3
+ require 'formular/element/modules/wrapped'
4
4
  module Formular
5
5
  class Element
6
6
  module Foundation6
7
7
  class InputGroup < Formular::Element::Input
8
8
  module WrappedGroup
9
9
  include Formular::Element::Module
10
- include Formular::Element::Modules::WrappedControl
10
+ include Formular::Element::Modules::Wrapped
11
11
 
12
12
  def wrapper(&block)
13
13
  builder.fieldset(Attributes[options[:wrapper_options]], &block)
@@ -1,11 +1,11 @@
1
1
  require 'formular/element/module'
2
- require 'formular/element/modules/wrapped_control'
2
+ require 'formular/element/modules/wrapped'
3
3
  module Formular
4
4
  class Element
5
5
  module Foundation6
6
- module WrappedControl
6
+ module Wrapped
7
7
  include Formular::Element::Module
8
- include Formular::Element::Modules::WrappedControl
8
+ include Formular::Element::Modules::Wrapped
9
9
 
10
10
  html(:wrapped) do |input|
11
11
  input.wrapper do
@@ -15,7 +15,7 @@ module Formular
15
15
  concat input.error
16
16
  end.to_s
17
17
  end
18
- end # module WrappedControl
18
+ end # module Wrapped
19
19
  end # module Foundation6
20
20
  end # class Element
21
21
  end # module Formular
@@ -15,7 +15,6 @@ module Formular
15
15
  include Label
16
16
 
17
17
  add_option_keys :control_label_options
18
-
19
18
  set_default :checked, 'checked', if: :is_checked?
20
19
 
21
20
  html(:checkable_label) do |input|
@@ -38,8 +37,9 @@ module Formular
38
37
  module InstanceMethods
39
38
  def group_label
40
39
  return '' unless has_group_label?
41
- label_options[:content] = label_text
42
- builder.checkable_group_label(label_options).to_s
40
+ label_opts = label_options.dup
41
+ label_opts[:content] = label_text
42
+ builder.checkable_group_label(label_opts).to_s
43
43
  end
44
44
 
45
45
  def has_group_label?
@@ -59,10 +59,10 @@ module Formular
59
59
  opts[:value] = item.send(options[:value_method])
60
60
  opts[:label] = item.send(options[:label_method])
61
61
 
62
- opts[:id] = if attributes[:id]
63
- "#{attributes[:id]}_#{opts[:value]}"
62
+ opts[:id] = if options[:id]
63
+ "#{options[:id]}_#{opts[:value]}"
64
64
  else
65
- "#{attribute_name || attributes[:name].gsub('[]', '')}_#{opts[:value]}"
65
+ "#{attribute_name || options[:name].gsub('[]', '')}_#{opts[:value]}"
66
66
  end
67
67
 
68
68
  self.class.(opts)
@@ -74,18 +74,17 @@ module Formular
74
74
  end
75
75
 
76
76
  private
77
-
78
- # we can't access other defaults
79
77
  def is_checked?
80
- !options[:checked].nil? || reader_value == attributes[:value]
78
+ !options[:checked].nil? || reader_value == options[:value]
81
79
  end
82
80
 
83
81
  def collection_base_options
84
- opts = attributes.select { |k, v| ![:name, :id, :checked, :class].include?(k) } #FIXME due to class merging, we'll end up with duplicate classes...
82
+ opts = attributes.select { |k, v| ![:name, :id, :checked, :class].include?(k) }
83
+ # FIXME due to class merging, we'll end up with duplicate classes...
85
84
  opts[:attribute_name] = attribute_name if attribute_name
86
85
  opts[:builder] = builder if builder
87
86
  opts[:label_options] = options[:control_label_options] if options[:control_label_options]
88
- opts[:name] = attributes[:name] if attributes[:name]
87
+ opts[:name] = options[:name] if options[:name] # do we need this??
89
88
 
90
89
  opts
91
90
  end
@@ -1,4 +1,5 @@
1
1
  require 'formular/element/module'
2
+ require 'formular/element/modules/escape_value'
2
3
  module Formular
3
4
  class Element
4
5
  module Modules
@@ -6,6 +7,7 @@ module Formular
6
7
  # name & value based on the attribute name
7
8
  module Control
8
9
  include Formular::Element::Module
10
+ include Formular::Element::Modules::EscapeValue
9
11
 
10
12
  add_option_keys :attribute_name
11
13
 
@@ -20,20 +22,26 @@ module Formular
20
22
 
21
23
  private
22
24
 
25
+ # FIXME... we should probably find a better way of returning nil to all of these if
26
+ # no attribute_name or builder
27
+ def builder_attribute?
28
+ attribute_name && builder
29
+ end
30
+
23
31
  def path
24
- @path ||= builder.path(attribute_name) if attribute_name && builder
32
+ @path ||= builder.path(attribute_name) if builder_attribute?
25
33
  end
26
34
 
27
35
  def form_encoded_name
28
- path.to_encoded_name if path
36
+ path.to_encoded_name if builder_attribute?
29
37
  end
30
38
 
31
39
  def form_encoded_id
32
- path.to_encoded_id if path
40
+ path.to_encoded_id if builder_attribute?
33
41
  end
34
42
 
35
43
  def reader_value
36
- builder.reader_value(attribute_name) if attribute_name && builder
44
+ builder.reader_value(attribute_name) if builder_attribute?
37
45
  end
38
46
  end # model InstanceMethods
39
47
  end # module Control
@@ -1,10 +1,12 @@
1
1
  require 'formular/element/module'
2
+ require 'formular/html_escape'
2
3
  module Formular
3
4
  class Element
4
5
  module Modules
5
6
  # this module provides error methods and options to a control when included
6
7
  module Error
7
8
  include Formular::Element::Module
9
+ include HtmlEscape
8
10
  add_option_keys :error
9
11
 
10
12
  # options functionality (same as SimpleForm):
@@ -12,7 +14,10 @@ module Formular
12
14
  # options[:error] == String return the string, regardless of model errors
13
15
  module InstanceMethods
14
16
  def error_text
15
- has_custom_error? ? options[:error] : errors_on_attribute.send(error_method) if has_errors?
17
+ return unless has_errors?
18
+
19
+ text = has_custom_error? ? options[:error] : errors_on_attribute.send(error_method)
20
+ html_escape(text)
16
21
  end
17
22
 
18
23
  def has_errors?