express_templates 0.7.1 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/lib/express_templates/compiler.rb +0 -9
  3. data/lib/express_templates/components/base.rb +62 -10
  4. data/lib/express_templates/components/capabilities/resourceful.rb +12 -0
  5. data/lib/express_templates/components/configurable.rb +143 -16
  6. data/lib/express_templates/components/forms/basic_fields.rb +11 -16
  7. data/lib/express_templates/components/forms/checkbox.rb +6 -6
  8. data/lib/express_templates/components/forms/express_form.rb +18 -26
  9. data/lib/express_templates/components/forms/form_component.rb +17 -16
  10. data/lib/express_templates/components/forms/radio.rb +14 -32
  11. data/lib/express_templates/components/forms/select.rb +18 -35
  12. data/lib/express_templates/components/forms/select_collection.rb +11 -17
  13. data/lib/express_templates/components/forms/submit.rb +18 -14
  14. data/lib/express_templates/components/tree_for.rb +11 -4
  15. data/lib/express_templates/components.rb +0 -1
  16. data/lib/express_templates/renderer.rb +1 -3
  17. data/lib/express_templates/version.rb +1 -1
  18. data/test/components/base_test.rb +45 -24
  19. data/test/components/capabilities/resourceful_test.rb +1 -1
  20. data/test/components/configurable_test.rb +108 -30
  21. data/test/components/forms/basic_fields_test.rb +3 -3
  22. data/test/components/forms/express_form_test.rb +4 -1
  23. data/test/components/forms/radio_test.rb +3 -3
  24. data/test/components/forms/submit_test.rb +3 -3
  25. data/test/components/tree_for_test.rb +1 -1
  26. data/test/core_extensions/proc_test.rb +10 -0
  27. data/test/test_helper.rb +3 -0
  28. metadata +2 -69
  29. data/lib/express_templates/components/form_rails_support.rb +0 -21
  30. data/test/dummy/log/test.log +0 -21858
  31. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/-1ax0k6FO5drSUN6jbogg4G0JliUHLffvQUvzSePKxA.cache +0 -1
  32. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/-cDLO4NJHMndDHchOLz8KLD7QBc68WtaAbYxK2r6GsU.cache +0 -0
  33. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/0t68EIwZ96sAqQSxFka9MQyIk4viw8ZIoREMCGiJRx0.cache +0 -0
  34. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/28LzLLDYjhr3jmu0GxjZ-ms5Bol6JilDRXpg8Zgbjqs.cache +0 -1
  35. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/2zt1IbQCnmzGyeZS_I4sYQzrgneSCKIJBRxcffVHlWY.cache +0 -1
  36. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/5AeGDyXQbv_BTj3PD4MJpNnGVwUxLsA8H1VcwB69_wE.cache +0 -0
  37. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/B0RgtiEmqwrCQuw4AnDa3fdENtJeBtO_TqtGJuWOeNs.cache +0 -3
  38. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/D-tMNp19G2zWOPPhnQRUm4K8DPa8SpKPfGALkkofTeE.cache +0 -0
  39. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/EDVlXrcn_wEfaZ5nc_4QJBT7lPiIBcX96jBo7PBz-vc.cache +0 -1
  40. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/Eso3tkb79hXQ1XdQgjbV03KyQwSeZFAHxVHImsjQ-HQ.cache +0 -2
  41. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/I8LhheC1OlOyvp_qY8kWpqKcZFiqv_BO-l5bExvDEi0.cache +0 -1
  42. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/Kf4VrP5sW-qzziYSN-m7p4nETjLiEwwwRfwUOm7rOr0.cache +0 -0
  43. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/Pd5YOD1DAL7QtTnwETZYBCabg5DkCFgbjt4iuBOcSoY.cache +0 -1
  44. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/QJ0Z8hROMfWpY6f5Chb7GSuDHE4gpabKVQTr9BCZe9s.cache +0 -1
  45. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/T1pSAx767vY7X1UDN0vdF7YGEkI5wMSVUKopsdvp7kI.cache +0 -1
  46. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/VlSPFPDK05c7ImadRiRVKzsaa6e15RwX77QR3NYabME.cache +0 -0
  47. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/Y7_BbgB9jgJIWYjDCsASySd26DTfPADAu_3DDnLlFog.cache +0 -0
  48. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/bSfZQxeyghTF4WIVnzGavxlg9afmSNuqcW6bA1Bm1OA.cache +0 -0
  49. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/fvD_ZNFRzd8Sc4PoTjyHPnkg4f7WMietFunnKqNjlvc.cache +0 -2
  50. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/g2xQygPqA5kMmp5L3wVGWaUIhdVsxFmOel9FF6M0Fuw.cache +0 -0
  51. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/hKbgrf5CbMO8pe9fCHc-rI5mp1ejAhivBfvfDxBNhQA.cache +0 -0
  52. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/kaif59owjUr3HBharCH5GUxU6KoW_zXD9a9JiTjIYPY.cache +0 -1
  53. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/kpvKX5UlhhoLJv-faeq7Ibv2KQh4ROjTiarh13gHuWI.cache +0 -2
  54. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/lfyrNtbNtwuTXAWlmPCKTS-D3FHoPTY6h53wnUN36-A.cache +0 -0
  55. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/lyDRhWNhfDw_YCCSbxQw_iOIV3eTfeAoX6mTREuVZSA.cache +0 -3
  56. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/p17lC0HAHCtk1ds_NHl9xhEyMtRjfQInw1c6fmFWguc.cache +0 -0
  57. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/p1cwnBd8RQBm9x5HzDmiiU7RhvnxhPX8VTfi0tTElJY.cache +0 -3
  58. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/qT6_0ti_v6fAjS3wETXqvTkr6W0LP4PB942uLYQzWK8.cache +0 -3
  59. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/qzU7DVwQZ7z6i6pYUpssYsAj0y33GN83B4O1bLvkW_4.cache +0 -1
  60. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/r56eI1z7iFKeySym_9zw5hVOPjb0d6IQPn5pAYTKk04.cache +0 -2
  61. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/rGWvoeLyiyqb813IXgfDpDxks23JQoLLZOa69bzKPE8.cache +0 -1
  62. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/y8T65xpx0s3RgT_YiqWEEXIaBsgK4woCNSuZ1zjQZBk.cache +0 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9db9820dd106746a62d12884baa1ddf142b779f0
4
- data.tar.gz: 69057e642521690e13fd7859e068eb935498e987
3
+ metadata.gz: 5d87ad2dbe75a19f388bac13ec434c7068a49eb6
4
+ data.tar.gz: 10a0ef4a3f481e39f1f6420ebd53d41f51fd1ccc
5
5
  SHA512:
6
- metadata.gz: 10408221b9075cccfdc2c1619202defe7a4524404f045236d9af046ca168734905e02b3185f587c91f0965c55f398bb80d52fc2e2c1b91216eb036230b3615db
7
- data.tar.gz: 8672d0ae9224d3c9662fcdfd21afb94b04a68d9b6e251f5ae7d4b0e86772ae96944b95a2304853254088bd01fde81d837f72e0140e31a124eb848311e0f20331
6
+ metadata.gz: 390bb3ecf10fa33f64988c331b347ef756a6a8e7b2c725ec416200de80982d9e2a2d44bb18110c5983634a792ca6fb98cd7c994842b8951438a71308284237ab
7
+ data.tar.gz: b03da196e200da6915bbd3e51b49532003475b8e642f44cd405b7e400f4d60898e132850feb13dcdf3276e98c6d97dceeb894fcc8037635a6dbddec7b425a583
@@ -1,16 +1,7 @@
1
- require 'ostruct'
2
1
  module ExpressTemplates
3
2
  module Compiler
4
3
  def compile(template_or_src=nil, &block)
5
4
 
6
- if block
7
- begin
8
- block.source
9
- rescue
10
- raise "block must have source - did you do compile(&:label) ?"
11
- end
12
- end
13
-
14
5
  template, src = _normalize(template_or_src)
15
6
 
16
7
  %Q[Arbre::Context.new(assigns.merge(template_virtual_path: @virtual_path), self) { #{src || block.source_body} }.to_s]
@@ -21,12 +21,46 @@ module ExpressTemplates
21
21
  # puts "added #{method_name} -> #{klass.name}"
22
22
  end
23
23
 
24
- def self.emits(proc = nil, &block)
25
- define_method(:build, &(proc || block))
24
+ def initialize(*)
25
+ super
26
+ _default_attributes.each do |name, value|
27
+ set_attribute(name, value)
28
+ end
29
+ add_class _default_classes
26
30
  end
27
31
 
28
- def build(*args, block)
29
- raise "#build method must be overridden"
32
+ def self.contains(proc = nil, &block)
33
+ define_method(:_build_body, &(proc || block))
34
+ end
35
+
36
+ # Override the tag_name method for other than <div>
37
+ def self.tag(tag)
38
+ define_method(:tag_name) { tag }
39
+ end
40
+
41
+ # Provide default attributes for the enclosing tag
42
+ # of the component
43
+ def self.has_attributes(attribs)
44
+ self._default_classes = attribs.delete(:class)
45
+ _default_attributes.merge!(attribs)
46
+ end
47
+
48
+ def self.before_build(proc_or_symbol = nil, &block)
49
+ if proc_or_symbol.kind_of?(Symbol)
50
+ define_method(:_before_build) do
51
+ self.send(proc_or_symbol)
52
+ end
53
+ else
54
+ define_method(:_before_build, &(proc_or_symbol || block))
55
+ end
56
+ end
57
+
58
+ def build(*args, &block)
59
+ _extract_class!(args)
60
+ _before_build if respond_to?(:_before_build)
61
+ super(*args) {
62
+ _build_body(&block) if respond_to?(:_build_body)
63
+ }
30
64
  end
31
65
 
32
66
  def resource
@@ -37,13 +71,31 @@ module ExpressTemplates
37
71
  builder_method_and_class subclass.to_s.demodulize.underscore, subclass
38
72
  end
39
73
 
40
- def indent_level
41
- parent_indent_level = parent.try(:indent_level) || 0
42
- end
74
+ protected
75
+ def default_class_name
76
+ self.class.name.demodulize.underscore.dasherize
77
+ end
43
78
 
44
- def to_s
45
- children.to_s
46
- end
79
+
80
+ private
81
+ def _extract_class!(args)
82
+ add_class args.last.delete(:class) if args.last.try(:kind_of?, Hash)
83
+ end
84
+ def _default_attributes
85
+ self.class._default_attributes
86
+ end
87
+ def self._default_attributes
88
+ @default_attributes ||= {}
89
+ end
90
+ def _default_classes
91
+ self.class._default_classes
92
+ end
93
+ def self._default_classes
94
+ @default_classes ||= ''
95
+ end
96
+ def self._default_classes=(classes)
97
+ @default_classes = classes
98
+ end
47
99
 
48
100
  end
49
101
  end
@@ -3,6 +3,18 @@ module ExpressTemplates
3
3
  module Capabilities
4
4
 
5
5
  module Resourceful
6
+
7
+ def self.included(base)
8
+ base.class_eval do
9
+ has_option :collection, 'Provide an explicit collection as a resource.'
10
+ has_option :collection_path, 'Provide an explicit path for the collection resource.'
11
+ has_option :resource_class, 'Overrides namespaced resource_class for using resources from a different module or namespace.'
12
+ has_option :resource_path, 'Overides the resource path which is otherwise inferred from the class name.'
13
+ has_option :path_prefix, 'Rarely used. Override inferred path_prefix for path helpers.'
14
+ has_option :namespace, 'Rarely used. Overrides inferred namespace for resources.'
15
+ end
16
+ end
17
+
6
18
  def namespace
7
19
  config[:namespace] || infer_namespace
8
20
  end
@@ -1,41 +1,168 @@
1
1
  module ExpressTemplates
2
2
  module Components
3
+ # Configurable components support configuration options supplied to the
4
+ # builder method. Supported options must be declared. All other options
5
+ # are passed along and converted to html attributes.
6
+ #
7
+ # Example:
8
+ #
9
+ # ```ruby
10
+ #
11
+ # class Pane < ExpressTemplates::Components::Configurable
12
+ # has_option :title, "Displayed in the title area", required: true
13
+ # has_option :status, "Displayed in the status area"
14
+ # end
15
+ #
16
+ # # Usage:
17
+ #
18
+ # pane(title: "People", status: "#{people.count} people")
19
+ #
20
+ # ```ruby
21
+ #
22
+ # Options specified as required must be supplied.
23
+ #
24
+ # Default values may be supplied for options with a default: keyword.
25
+ #
26
+ # Options may be passed as html attributes with attribute: true
27
+ #
3
28
  class Configurable < Base
4
29
 
5
- def self.emits(proc = nil, &block)
6
- define_method(:markup, &(proc || block))
30
+ class_attribute :supported_options
31
+ self.supported_options = {}
32
+
33
+ class_attribute :supported_arguments
34
+ self.supported_arguments = {}
35
+
36
+ def self.emits(*args, &block)
37
+ warn ".emits is deprecrated"
38
+ self.contains(*args, &block)
7
39
  end
8
40
 
9
41
  def build(*args, &block)
10
- _process_args!(args)
11
- if method(:markup).arity > 0
12
- markup(block)
13
- else
14
- markup(&block)
15
- end
42
+ _process_builder_args!(args)
43
+ super(*args, &block)
16
44
  end
17
45
 
18
46
  def config
19
47
  @config ||= {}
20
48
  end
21
49
 
22
- alias :my :config
50
+ def self.has_option(name, description, type: :text, required: nil, default: nil, attribute: nil)
51
+ raise "name must be a symbol" unless name.kind_of?(Symbol)
52
+ option_definition = {description: description}
53
+ option_definition.merge!(type: type, required: required, default: default, attribute: attribute)
54
+ self.supported_options =
55
+ self.supported_options.merge(name => option_definition)
56
+ end
23
57
 
58
+ def required_options
59
+ supported_options.select {|k,v| v[:required] unless v[:default] }
60
+ end
61
+
62
+ def self.has_argument(name, description, as: nil, type: :string, default: nil, optional: false)
63
+ raise "name must be a symbol" unless name.kind_of?(Symbol)
64
+ argument_definition = {description: description, as: as, type: type, default: default, optional: optional}
65
+ self.supported_arguments =
66
+ self.supported_arguments.merge(name => argument_definition)
67
+ end
68
+
69
+ has_argument :id, "The id of the component.", type: :symbol, optional: true
24
70
 
25
71
  protected
26
72
 
27
- def _process_args!(args)
28
- if args.first.kind_of?(Symbol)
29
- config.merge!(id: args.shift)
30
- attributes[:id] = config[:id]
73
+ def _default_options
74
+ supported_options.select {|k,v| v[:default] }
75
+ end
76
+
77
+ def _check_required_options(supplied)
78
+ missing = required_options.keys - supplied.keys
79
+ if missing.any?
80
+ raise "#{self.class} missing required option(s): #{missing}"
31
81
  end
32
- args.each do |arg|
33
- if arg.kind_of?(Hash)
34
- config.merge!(arg)
82
+ end
83
+
84
+ def _set_defaults
85
+ _default_options.each do |key, value|
86
+ if !!value[:attribute]
87
+ set_attribute key, value[:default]
88
+ else
89
+ config[key] ||= value[:default]
35
90
  end
36
91
  end
37
92
  end
38
93
 
94
+ def _valid_types(definition)
95
+ valid_type_names = if definition[:type].kind_of?(Symbol)
96
+ [definition[:type]]
97
+ elsif definition[:type].respond_to?(:keys)
98
+ definition[:type].keys
99
+ else
100
+ definition[:type] || []
101
+ end
102
+ valid_type_names.map(&:to_s).map(&:classify).map(&:constantize)
103
+ end
104
+
105
+ def _is_valid?(value, definition)
106
+ valid_types = _valid_types(definition)
107
+ if valid_types.empty? && value.kind_of?(String)
108
+ true
109
+ elsif valid_types.include?(value.class)
110
+ true
111
+ else
112
+ false
113
+ end
114
+ end
115
+
116
+ def _optional_argument?(definition)
117
+ definition[:default] || definition[:optional]
118
+ end
119
+
120
+ def _required_argument?(definition)
121
+ !_optional_argument?(definition)
122
+ end
123
+
124
+ def _extract_supported_arguments!(args)
125
+ supported_arguments.each do |key, definition|
126
+ value = args.shift
127
+ if value.nil? && _required_argument?(definition)
128
+ raise "argument for #{key} not supplied"
129
+ end
130
+ unless _is_valid?(value, definition)
131
+ if _required_argument?(definition)
132
+ raise "argument for #{key} invalid (#{value.class}) '#{value.to_s}'; Allowable: #{_valid_types(definition).inspect}"
133
+ else
134
+ args.unshift value
135
+ next
136
+ end
137
+ end
138
+ config_key = definition[:as] || key
139
+ config[config_key] = value || definition[:default]
140
+ end
141
+ end
142
+
143
+ def _set_id_attribute
144
+ attributes[:id] = config[:id]
145
+ end
146
+
147
+ def _extract_supported_options!(builder_options)
148
+ builder_options.each do |key, value|
149
+ if supported_options.keys.include?(key)
150
+ unless supported_options[key][:attribute]
151
+ config[key] = builder_options.delete(key)
152
+ end
153
+ end
154
+ end
155
+ end
156
+
157
+ def _process_builder_args!(args)
158
+ _extract_supported_arguments!(args)
159
+ builder_options = args.last.try(:kind_of?, Hash) ? args.last : {}
160
+ _check_required_options(builder_options)
161
+ _extract_supported_options!(builder_options)
162
+ _set_defaults
163
+ _set_id_attribute
164
+ end
165
+
39
166
  end
40
167
  end
41
168
  end
@@ -9,11 +9,9 @@ module ExpressTemplates
9
9
  ALL.each do |type|
10
10
  class_definition = <<-RUBY
11
11
  class #{type.classify} < FormComponent
12
- emits -> {
13
- div(class: field_wrapper_class) {
14
- label_tag(label_name, label_text)
15
- #{type}_field resource_var, field_name.to_sym, html_options
16
- }
12
+ contains {
13
+ label_tag(label_name, label_text)
14
+ #{type}_field resource_var, field_name.to_sym, input_attributes
17
15
  }
18
16
  end
19
17
  RUBY
@@ -24,25 +22,22 @@ RUBY
24
22
  end
25
23
 
26
24
  # class Email < FormComponent
27
- # emits -> {
28
- # div(class: field_wrapper_class) {
29
- # email_field resource_var, field_name.to_sym
30
- # }
25
+ # contains {
26
+ # label_tag label_name, label_text
27
+ # email_field resource_var, field_name.to_sym, input_attributes
31
28
  # }
32
29
  # end
33
30
 
34
31
  class Textarea < FormComponent
35
- emits -> {
36
- div(class: field_wrapper_class) {
37
- label_tag(label_name, label_text)
38
- text_area resource_var, field_name.to_sym, html_options
39
- }
32
+ contains {
33
+ label_tag(label_name, label_text)
34
+ text_area resource_var, field_name.to_sym, input_attributes
40
35
  }
41
36
  end
42
37
 
43
38
  class Hidden < FormComponent
44
- emits -> {
45
- hidden_field resource_var, field_name.to_sym, html_options
39
+ contains {
40
+ hidden_field resource_var, field_name.to_sym, input_attributes
46
41
  }
47
42
  end
48
43
  end
@@ -3,12 +3,12 @@ module ExpressTemplates
3
3
  module Forms
4
4
  class Checkbox < FormComponent
5
5
 
6
- emits -> {
7
- div(class: field_wrapper_class) {
8
- label_tag(label_name, label_text) if label_before?
9
- check_box(resource_var, field_name.to_sym, field_options, checked_value, unchecked_value)
10
- label_tag(label_name, label_text) if label_after?
11
- }
6
+ has_option :label_after, 'Positions the option label after the checkbox.', default: false
7
+
8
+ contains {
9
+ label_tag(label_name, label_text) if label_before?
10
+ check_box(resource_var, field_name.to_sym, field_options, checked_value, unchecked_value)
11
+ label_tag(label_name, label_text) if label_after?
12
12
  }
13
13
 
14
14
  def label_before?
@@ -4,42 +4,34 @@ module ExpressTemplates
4
4
  class ExpressForm < Configurable
5
5
  include ExpressTemplates::Components::Capabilities::Resourceful
6
6
 
7
- emits -> (block) {
8
- form(form_tag_options ) {
9
- form_rails_support form_method
10
- block.call(self) if block
7
+ tag :form
8
+
9
+ has_option :method, 'The form method', default: 'POST', attribute: true #, options: ['PUT', 'POST', 'GET', 'DELETE']
10
+ has_option :action, 'The form action containing the resource path or url.'
11
+
12
+ contains -> (&block) {
13
+ div(style: 'display:none') {
14
+ add_child helpers.utf8_enforcer_tag
15
+ add_child helpers.send(:method_tag, resource.persisted? ? :put : :post)
16
+ helpers.send(:token_tag)
11
17
  }
18
+ block.call(self) if block
12
19
  }
13
20
 
14
- def form_id
15
- "#{resource_name}_#{resource.id}"
16
- end
21
+ before_build -> {
22
+ set_attribute(:id, form_id)
23
+ set_attribute(:action, form_action)
24
+ add_class(config[:id])
25
+ }
17
26
 
18
- def form_method
19
- config[:method].to_s.upcase || 'POST'
27
+ def form_id
28
+ [config[:id], resource.try(:id)].compact.join('_')
20
29
  end
21
30
 
22
31
  def form_action
23
32
  config[:action] || (resource.try(:persisted?) ? resource_path(ivar: true) : collection_path)
24
33
  end
25
34
 
26
- def form_tag_options
27
- args = {id: form_id, action: form_action}.merge!(config).merge!(method: 'POST')
28
-
29
- if html_options = args.delete(:html_options)
30
- args.merge!(html_options)
31
- end
32
- args
33
- end
34
-
35
- def resource_name_for_path
36
- config[:id].to_s
37
- end
38
-
39
- def resource_name
40
- (config[:resource_name] || config[:id]).to_s
41
- end
42
-
43
35
  end
44
36
  end
45
37
  end
@@ -3,10 +3,20 @@ module ExpressTemplates
3
3
  module Forms
4
4
  class FormComponent < Configurable
5
5
 
6
+ attr :input_attributes
7
+
8
+ before_build -> {
9
+ set_attribute(:id, "#{resource_name}_#{field_name}_wrapper")
10
+ add_class(config[:class])
11
+ }
12
+
13
+ has_option :class, 'Override the class of the wrapping div of a form component', default: 'field-wrapper', attribute: true
14
+ has_option :label, 'Override the inferred label of a form component'
15
+
6
16
  # Lookup the resource_name from the parent ExpressForm.
7
17
  def resource_name
8
18
  raise "FormComponent must have a parent form" unless parent_form
9
- parent_form.resource_name
19
+ parent_form.config[:id].to_s
10
20
  end
11
21
 
12
22
  def resource_var
@@ -17,7 +27,7 @@ module ExpressTemplates
17
27
  parent_form.resource_class
18
28
  end
19
29
 
20
- # Return the name attribute for the lable
30
+ # Return the name attribute for the label
21
31
  def label_name
22
32
  "#{resource_name.singularize}_#{field_name}"
23
33
  end
@@ -39,10 +49,6 @@ module ExpressTemplates
39
49
  "#{resource_name.singularize}[#{field_name}]"
40
50
  end
41
51
 
42
- def field_wrapper_class
43
- config[:wrapper_class] || 'field-wrapper'
44
- end
45
-
46
52
  # Search the parent graph until we find an ExpressForm. Returns nil if none found.
47
53
  def parent_form
48
54
  @my_form ||= parent
@@ -52,19 +58,14 @@ module ExpressTemplates
52
58
  return @my_form
53
59
  end
54
60
 
55
- def default_html_options
56
- (config || {}).reject {|k,v| k.eql?(:id)}
57
- end
58
-
59
- def html_options
60
- default_html_options.merge(config[:html_options] || {})
61
- end
62
-
63
61
  protected
64
62
 
65
- def _process_args!(args)
66
- @args = args
63
+ # saving attributes for passing to the input field
64
+ def _process_builder_args!(args)
67
65
  super(args)
66
+ @input_attributes = args.last if args.last.kind_of?(Hash)
67
+ @input_attributes ||= {}
68
+ args.clear
68
69
  end
69
70
 
70
71
  end
@@ -4,21 +4,22 @@ module ExpressTemplates
4
4
  class Radio < FormComponent
5
5
  include OptionSupport
6
6
 
7
- emits -> {
8
- div(class: field_wrapper_class) {
9
- label_tag(label_name, label_text)
10
- if option_values_specified?
11
- generate_options_from_specified_values
12
- else
13
- use_options_from_collection_radio_buttons_helper
14
- end
15
- }
7
+ has_option :options, 'Supplies a list of options for the radio tag'
8
+ has_option :label_wrapper_class, 'Specify a class for labels which wrap each radio option.'
9
+
10
+ contains -> {
11
+ label_tag(label_name, label_text)
12
+ if option_values_specified?
13
+ generate_options_from_specified_values
14
+ else
15
+ use_options_from_collection_radio_buttons_helper
16
+ end
16
17
  }
17
18
 
18
19
  def use_options_from_collection_radio_buttons_helper
19
20
  collection_radio_buttons(resource_var, field_name.to_sym, collection_from_association,
20
21
  option_value_method, option_name_method,
21
- field_options, html_options) do |b|
22
+ input_attributes) do |b|
22
23
  b.label(class: "radio") {
23
24
  b.radio_button + b.text
24
25
  }
@@ -30,25 +31,21 @@ module ExpressTemplates
30
31
  end
31
32
 
32
33
  def option_collection
33
- @args.first
34
- end
35
-
36
- def wrapper_class
37
- config[:wrapper_class] || 'wrapper-class'
34
+ config[:options]
38
35
  end
39
36
 
40
37
  def generate_options_from_specified_values
41
38
  case
42
39
  when option_collection.kind_of?(Array)
43
40
  option_collection.each_with_index do |option, index|
44
- label(class: wrapper_class) {
41
+ label(class: config[:label_wrapper_class]) {
45
42
  radio_button(resource_var, field_name.to_sym, option, class: 'radio')
46
43
  current_arbre_element.add_child option
47
44
  }
48
45
  end
49
46
  when option_collection.kind_of?(Hash)
50
47
  option_collection.each_pair do |key, value|
51
- label(class: wrapper_class) {
48
+ label(class: config[:label_wrapper_class]) {
52
49
  radio_button(resource_var, field_name.to_sym, key, class: 'radio')
53
50
  current_arbre_element.add_child value
54
51
  }
@@ -62,21 +59,6 @@ module ExpressTemplates
62
59
  related_collection or raise "No association collection for: #{resource_name}.#{field_name}"
63
60
  end
64
61
 
65
- def field_options
66
- # If field_otions is omitted the Expander will be
67
- # in last or 3rd position and we don't want that
68
- if @args[1] && @args[1].is_a?(Hash)
69
- @args[1]
70
- else
71
- {}
72
- end
73
- end
74
-
75
- # TODO: implement
76
- def html_options
77
- {}
78
- end
79
-
80
62
  end
81
63
  end
82
64
  end