express_templates 0.5.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +22 -41
  3. data/lib/arbre/patches.rb +50 -0
  4. data/lib/core_extensions/proc.rb +1 -0
  5. data/lib/express_templates/compiler.rb +2 -7
  6. data/lib/express_templates/components/base.rb +33 -37
  7. data/lib/express_templates/components/capabilities/resourceful.rb +30 -18
  8. data/lib/express_templates/components/configurable.rb +41 -0
  9. data/lib/express_templates/components/form_rails_support.rb +5 -6
  10. data/lib/express_templates/components/forms/basic_fields.rb +3 -6
  11. data/lib/express_templates/components/forms/checkbox.rb +1 -1
  12. data/lib/express_templates/components/forms/express_form.rb +15 -25
  13. data/lib/express_templates/components/forms/form_component.rb +20 -11
  14. data/lib/express_templates/components/forms/option_support.rb +2 -2
  15. data/lib/express_templates/components/forms/radio.rb +7 -8
  16. data/lib/express_templates/components/forms/select.rb +61 -37
  17. data/lib/express_templates/components/forms/select_collection.rb +2 -8
  18. data/lib/express_templates/components/forms/submit.rb +7 -6
  19. data/lib/express_templates/components/forms.rb +0 -1
  20. data/lib/express_templates/components/tree_for.rb +26 -42
  21. data/lib/express_templates/components.rb +1 -9
  22. data/lib/express_templates/version.rb +1 -1
  23. data/lib/express_templates.rb +2 -3
  24. data/test/components/base_test.rb +17 -15
  25. data/test/components/capabilities/resourceful_test.rb +3 -3
  26. data/test/components/configurable_test.rb +27 -21
  27. data/test/components/forms/basic_fields_test.rb +57 -17
  28. data/test/components/forms/checkbox_test.rb +16 -22
  29. data/test/components/forms/express_form_test.rb +23 -76
  30. data/test/components/forms/radio_test.rb +31 -27
  31. data/test/components/forms/select_test.rb +46 -71
  32. data/test/components/forms/submit_test.rb +10 -5
  33. data/test/components/tree_for_test.rb +24 -52
  34. data/test/dummy/app/views/hello/show.html.et +4 -3
  35. data/test/dummy/app/views/layouts/application.html.et +1 -1
  36. data/test/dummy/log/test.log +4794 -0
  37. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/-cDLO4NJHMndDHchOLz8KLD7QBc68WtaAbYxK2r6GsU.cache +0 -0
  38. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/0t68EIwZ96sAqQSxFka9MQyIk4viw8ZIoREMCGiJRx0.cache +0 -0
  39. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/2zt1IbQCnmzGyeZS_I4sYQzrgneSCKIJBRxcffVHlWY.cache +1 -0
  40. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/EDVlXrcn_wEfaZ5nc_4QJBT7lPiIBcX96jBo7PBz-vc.cache +1 -0
  41. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/Eso3tkb79hXQ1XdQgjbV03KyQwSeZFAHxVHImsjQ-HQ.cache +2 -0
  42. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/I8LhheC1OlOyvp_qY8kWpqKcZFiqv_BO-l5bExvDEi0.cache +1 -0
  43. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/Kf4VrP5sW-qzziYSN-m7p4nETjLiEwwwRfwUOm7rOr0.cache +0 -0
  44. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/QJ0Z8hROMfWpY6f5Chb7GSuDHE4gpabKVQTr9BCZe9s.cache +1 -0
  45. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/T1pSAx767vY7X1UDN0vdF7YGEkI5wMSVUKopsdvp7kI.cache +1 -0
  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/g2xQygPqA5kMmp5L3wVGWaUIhdVsxFmOel9FF6M0Fuw.cache +0 -0
  49. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/kaif59owjUr3HBharCH5GUxU6KoW_zXD9a9JiTjIYPY.cache +1 -0
  50. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/p1cwnBd8RQBm9x5HzDmiiU7RhvnxhPX8VTfi0tTElJY.cache +3 -0
  51. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/qT6_0ti_v6fAjS3wETXqvTkr6W0LP4PB942uLYQzWK8.cache +3 -0
  52. data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/y8T65xpx0s3RgT_YiqWEEXIaBsgK4woCNSuZ1zjQZBk.cache +2 -0
  53. data/test/express_templates_test.rb +5 -1
  54. data/test/handler_test.rb +19 -17
  55. data/test/performance_test.rb +11 -7
  56. data/test/test_helper.rb +162 -1
  57. metadata +50 -53
  58. data/lib/express_templates/components/capabilities/adoptable.rb +0 -20
  59. data/lib/express_templates/components/capabilities/building.rb +0 -14
  60. data/lib/express_templates/components/capabilities/conditionality.rb +0 -54
  61. data/lib/express_templates/components/capabilities/configurable.rb +0 -90
  62. data/lib/express_templates/components/capabilities/iterating.rb +0 -75
  63. data/lib/express_templates/components/capabilities/parenting.rb +0 -72
  64. data/lib/express_templates/components/capabilities/rendering.rb +0 -30
  65. data/lib/express_templates/components/capabilities/templating.rb +0 -198
  66. data/lib/express_templates/components/capabilities/wrapping.rb +0 -84
  67. data/lib/express_templates/components/column.rb +0 -13
  68. data/lib/express_templates/components/container.rb +0 -7
  69. data/lib/express_templates/components/content_for.rb +0 -42
  70. data/lib/express_templates/components/for_each.rb +0 -30
  71. data/lib/express_templates/components/forms/form_support.rb +0 -13
  72. data/lib/express_templates/components/null_wrap.rb +0 -41
  73. data/lib/express_templates/components/row.rb +0 -28
  74. data/lib/express_templates/components/table_for.rb +0 -163
  75. data/lib/express_templates/components/unless_block.rb +0 -40
  76. data/lib/express_templates/expander.rb +0 -140
  77. data/lib/express_templates/macro.rb +0 -45
  78. data/lib/express_templates/markup/html_tag.rb +0 -62
  79. data/lib/express_templates/markup/tag.rb +0 -150
  80. data/lib/express_templates/markup/wrapper.rb +0 -94
  81. data/lib/express_templates/markup/yielder.rb +0 -21
  82. data/lib/express_templates/markup.rb +0 -9
  83. data/test/components/column_test.rb +0 -11
  84. data/test/components/conditionality_test.rb +0 -37
  85. data/test/components/container_test.rb +0 -66
  86. data/test/components/content_for_test.rb +0 -60
  87. data/test/components/iterating_test.rb +0 -127
  88. data/test/components/null_wrap_test.rb +0 -28
  89. data/test/components/row_test.rb +0 -16
  90. data/test/components/table_for_test.rb +0 -211
  91. data/test/expander_stack_test.rb +0 -41
  92. data/test/expander_test.rb +0 -99
  93. data/test/markup/tag_test.rb +0 -149
  94. data/test/markup/wrapper_test.rb +0 -42
  95. data/test/markup/yielder_test.rb +0 -9
@@ -16,8 +16,7 @@ module ExpressTemplates
16
16
  }
17
17
 
18
18
  def use_options_from_collection_radio_buttons_helper
19
- # Note {{ }} will get stripped. This prevents the collection finder string being passed as string.
20
- collection_radio_buttons(resource_var, field_name.to_sym, "{{#{collection_from_association}}}",
19
+ collection_radio_buttons(resource_var, field_name.to_sym, collection_from_association,
21
20
  option_value_method, option_name_method,
22
21
  field_options, html_options) do |b|
23
22
  b.label(class: "radio") {
@@ -31,11 +30,11 @@ module ExpressTemplates
31
30
  end
32
31
 
33
32
  def option_collection
34
- @args.second
33
+ @args.first
35
34
  end
36
35
 
37
36
  def wrapper_class
38
- @config[:wrapper_class] || 'wrapper-class'
37
+ config[:wrapper_class] || 'wrapper-class'
39
38
  end
40
39
 
41
40
  def generate_options_from_specified_values
@@ -44,14 +43,14 @@ module ExpressTemplates
44
43
  option_collection.each_with_index do |option, index|
45
44
  label(class: wrapper_class) {
46
45
  radio_button(resource_var, field_name.to_sym, option, class: 'radio')
47
- null_wrap { option }
46
+ current_arbre_element.add_child option
48
47
  }
49
48
  end
50
49
  when option_collection.kind_of?(Hash)
51
50
  option_collection.each_pair do |key, value|
52
51
  label(class: wrapper_class) {
53
52
  radio_button(resource_var, field_name.to_sym, key, class: 'radio')
54
- null_wrap { value }
53
+ current_arbre_element.add_child value
55
54
  }
56
55
  end
57
56
  else
@@ -66,8 +65,8 @@ module ExpressTemplates
66
65
  def field_options
67
66
  # If field_otions is omitted the Expander will be
68
67
  # in last or 3rd position and we don't want that
69
- if @args.size > 3 && @args[2].is_a?(Hash)
70
- @args[2]
68
+ if @args[1] && @args[1].is_a?(Hash)
69
+ @args[1]
71
70
  else
72
71
  {}
73
72
  end
@@ -26,39 +26,63 @@ module ExpressTemplates
26
26
  }
27
27
 
28
28
  def select_tag_args
29
- args = [field_name_attribute, select_options, field_options]
30
- args << html_options unless html_options.nil? or html_options.empty?
29
+ args = [field_name_attribute, select_options, select_helper_options]
31
30
  args
32
31
  end
33
32
 
34
- # Returns the options which will be supplied to the select_tag helper.
35
- def select_options
36
- options_specified = [Array, Hash, Proc].include?(@args.second.class) && @args.size > 2
37
- if options_specified
38
- if @args.second.respond_to?(:source) # can be a proc
39
- options = "#{@args.second.source}.call()"
40
- else
41
- options = @args.second
42
- end
33
+ def select_options_supplied?
34
+ [Array, Hash, Proc].include?(supplied_component_options[:options].class)
35
+ end
43
36
 
37
+ def use_supplied_options
38
+ opts = supplied_component_options[:options]
39
+ if opts.respond_to?(:call) # can be a proc
40
+ opts.call()
44
41
  else
45
- options = "@#{resource_var}.class.distinct(:#{field_name}).pluck(:#{field_name})"
42
+ opts
46
43
  end
44
+ end
47
45
 
48
- if belongs_to_association && !options_specified
49
- if belongs_to_association.polymorphic?
50
- "{{options_for_select([[]])}}"
51
- else
52
- "{{options_from_collection_for_select(#{related_collection}, :id, :#{option_name_method}, @#{resource_name}.#{field_name})}}"
53
- end
46
+ def generate_options_from_field_values
47
+ resource.class.distinct(field_name.to_sym).pluck(field_name.to_sym)
48
+ end
49
+
50
+ def options_from_supplied_or_field_values
51
+ if select_options_supplied?
52
+ use_supplied_options
53
+ else
54
+ generate_options_from_field_values
55
+ end
56
+ end
57
+
58
+ def options_from_belongs_to
59
+ if belongs_to_association.polymorphic?
60
+ helpers.options_for_select([[]]) # we can't really handle polymorhic yet
61
+ else
62
+ helpers.options_from_collection_for_select(related_collection, :id, option_name_method, resource.send(field_name))
63
+ end
64
+ end
65
+
66
+ def options_from_has_many_through
67
+ helpers.options_from_collection_for_select(related_collection, :id, option_name_method, resource.send(field_name).map(&:id))
68
+ end
69
+
70
+ def simple_options_with_selection
71
+ if selection = field_options[:selected]
72
+ helpers.options_for_select(options_from_supplied_or_field_values, selection)
73
+ else
74
+ helpers.options_for_select(options_from_supplied_or_field_values, resource.send(field_name))
75
+ end
76
+ end
77
+
78
+ # Returns the options which will be supplied to the select_tag helper.
79
+ def select_options
80
+ if belongs_to_association && !select_options_supplied?
81
+ options_from_belongs_to
54
82
  elsif has_many_through_association
55
- "{{options_from_collection_for_select(#{related_collection}, :id, :#{option_name_method}, @#{resource_name}.#{field_name}.map(&:id))}}"
83
+ options_from_has_many_through
56
84
  else
57
- if selection = field_options.delete(:selected)
58
- "{{options_for_select(#{options}, \"#{selection}\")}}"
59
- else
60
- "{{options_for_select(#{options}, @#{resource_name}.#{field_name})}}"
61
- end
85
+ simple_options_with_selection
62
86
  end
63
87
  end
64
88
 
@@ -74,26 +98,26 @@ module ExpressTemplates
74
98
  # If field_otions is omitted the Expander will be
75
99
  # in last or 3rd position and we don't want that
76
100
  defaults = {include_blank: true}
77
- defaults.merge(supplied_field_options.reject {|k,v| k.eql?(:select2)})
101
+ defaults.merge(supplied_component_options)
78
102
  end
79
103
 
80
- def html_options
81
- supplied_html_options
104
+ def select_helper_options
105
+ component_option_names = [:select2, :options, :selected]
106
+ add_select2_class( field_options.reject {|k,v| component_option_names.include?(k)})
82
107
  end
83
108
 
84
109
  protected
85
110
 
86
- def supplied_field_options
87
- if @args.size > 3 && @args[2].is_a?(Hash)
88
- @args[2]
89
- else
90
- {}
91
- end
111
+ def add_select2_class(helper_options)
112
+ add_class(helper_options[:class]) if helper_options[:class]
113
+ add_class('select2') if supplied_component_options[:select2] === true
114
+ helper_options[:class] = (class_list - ["select"]).to_s
115
+ helper_options
92
116
  end
93
117
 
94
- def supplied_html_options
95
- if @args.size > 4 && @args[3].is_a?(Hash)
96
- @args[3]
118
+ def supplied_component_options
119
+ if @args.last && @args.last.is_a?(Hash)
120
+ @args.last
97
121
  else
98
122
  {}
99
123
  end
@@ -102,4 +126,4 @@ module ExpressTemplates
102
126
  end
103
127
  end
104
128
  end
105
- end
129
+ end
@@ -17,7 +17,6 @@ module ExpressTemplates
17
17
  # need this because the collection_select helper does not provide
18
18
  # the hidden_field_tag trick (see rails api docs for select)
19
19
  hidden_field_tag(multi_field_name, '')
20
-
21
20
  collection_select(*collection_select_tag_args)
22
21
  }
23
22
  }
@@ -25,14 +24,13 @@ module ExpressTemplates
25
24
  def collection_select_tag_args
26
25
  [ resource_name,
27
26
  multi_field_name,
28
- select_options, :id, :name,
27
+ related_collection, :id, :name,
29
28
  field_options,
30
29
  html_options ]
31
30
  end
32
31
 
33
32
  def field_options
34
- selected_options_ruby = "@#{resource_name}.#{multi_field_name}".to_view_code
35
- super.merge(include_blank: false, selected: selected_options_ruby)
33
+ super.merge(include_blank: false)
36
34
  end
37
35
 
38
36
  def html_options
@@ -47,10 +45,6 @@ module ExpressTemplates
47
45
  end
48
46
  end
49
47
 
50
-
51
- def select_options
52
- "{{#{related_collection}}}"
53
- end
54
48
  end
55
49
  end
56
50
  end
@@ -3,11 +3,15 @@ module ExpressTemplates
3
3
  module Forms
4
4
  class Submit < FormComponent
5
5
 
6
- emits -> {
6
+ def build(*args)
7
7
  div(class: field_wrapper_class) {
8
- submit_tag(value, html_options)
8
+ if args.first.is_a?(String) or args.empty?
9
+ submit_tag(args.first || 'Save', (args[1]||{}))
10
+ else
11
+ submit_tag 'Save', (args[1] || {})
12
+ end
9
13
  }
10
- }
14
+ end
11
15
 
12
16
  def value
13
17
  if @args.first.is_a?(String)
@@ -17,9 +21,6 @@ module ExpressTemplates
17
21
  end
18
22
  end
19
23
 
20
- def html_options
21
- @config
22
- end
23
24
  end
24
25
  end
25
26
  end
@@ -5,7 +5,6 @@ module ExpressTemplates
5
5
  end
6
6
  end
7
7
 
8
- require 'express_templates/components/forms/form_support'
9
8
  require 'express_templates/components/forms/express_form'
10
9
  require 'express_templates/components/forms/form_component'
11
10
  require 'express_templates/components/forms/option_support'
@@ -11,8 +11,8 @@ module ExpressTemplates
11
11
  # Example:
12
12
  #
13
13
  # ```ruby
14
- # tree_for(:roles) {
15
- # "{{role.name}}"
14
+ # tree_for(:roles) { |role|
15
+ # role.name
16
16
  # }
17
17
  # ```
18
18
  #
@@ -36,51 +36,35 @@ module ExpressTemplates
36
36
  # </li>
37
37
  # </ul>
38
38
  #
39
- class TreeFor < Container
40
- def node_renderer
41
- return (-> (node, renderer) {
42
- ExpressTemplates::Indenter.for(:tree) do |ws, wsnl|
43
- "#{wsnl}<li>"+
44
- _yield +
45
- if node.children.any?
46
- ExpressTemplates::Indenter.for(:tree) do |ws, wsnl|
47
- "#{wsnl}<ul>" +
48
- node.children.map do |child|
49
- renderer.call(child, renderer)
50
- end.join +
51
- "#{wsnl}</ul>"
52
- end +
53
- "#{wsnl}</li>"
54
- else
55
- "</li>"
56
- end
57
- end
58
- }).source.sub(/\W_yield\W/, compile_children.lstrip)
39
+
40
+ class TreeFor < Configurable
41
+ emits -> (customize_block) {
42
+ @customize_block = customize_block
43
+ ul(id: config[:id], class: "#{config[:id]} tree") {
44
+ list_items(eval(config[:id].to_s))
45
+ }
46
+ }
47
+
48
+ def list_items(nodes)
49
+ nodes.each do |node|
50
+ list_item(node)
51
+ end
59
52
  end
60
53
 
61
- def compile
62
- collection = if @options[:collection]
63
- "#{@options[:collection].source}.call()"
54
+ def list_item(node)
55
+ li {
56
+ if @customize_block
57
+ @customize_block.call(node)
64
58
  else
65
- _variablize(@options[:id])
59
+ text_node "#{node.name}#{"\n" if node.children.any?}"
66
60
  end
67
- member = @options[:id].to_s.singularize
68
- return 'ExpressTemplates::Components::TreeFor.render_in(self) {
69
- node_renderer = '+node_renderer.gsub(/node/, member)+'
70
- ExpressTemplates::Indenter.for(:tree) do |ws, wsnl|
71
- "#{ws}<ul id=\"'+@options[:id]+'\" class=\"'+@options[:id]+' tree\">" +
72
- '+collection+'.map do |'+member+'|
73
- node_renderer.call('+member+', node_renderer)
74
- end.join +
75
- "#{wsnl}</ul>\n"
76
- end
77
- }'
61
+ if node.children.any?
62
+ ul {
63
+ list_items(node.children)
64
+ }
65
+ end
66
+ }
78
67
  end
79
-
80
- private
81
- def _variablize(sym)
82
- "@#{sym}"
83
- end
84
68
  end
85
69
  end
86
70
  end
@@ -3,16 +3,8 @@ module ExpressTemplates
3
3
  end
4
4
  end
5
5
 
6
- require 'express_templates/expander'
7
6
  require 'express_templates/components/base'
8
- require 'express_templates/components/container'
9
- require 'express_templates/components/null_wrap'
10
- require 'express_templates/components/for_each'
11
- require 'express_templates/components/unless_block'
12
- require 'express_templates/components/row'
13
- require 'express_templates/components/column'
7
+ require 'express_templates/components/configurable'
14
8
  require 'express_templates/components/form_rails_support'
15
- require 'express_templates/components/content_for'
16
- require 'express_templates/components/table_for'
17
9
  require 'express_templates/components/tree_for'
18
10
  require 'express_templates/components/forms'
@@ -1,3 +1,3 @@
1
1
  module ExpressTemplates
2
- VERSION = "0.5.0"
2
+ VERSION = "0.7.0"
3
3
  end
@@ -1,13 +1,12 @@
1
1
  module ExpressTemplates
2
+ require 'arbre'
3
+ require 'arbre/patches'
2
4
  require 'core_extensions/proc'
3
5
  require 'core_extensions/string'
4
6
  require 'express_templates/indenter'
5
- require 'express_templates/macro'
6
- require 'express_templates/markup'
7
7
  require 'express_templates/components'
8
8
  require 'express_templates/template/handler'
9
9
  require 'express_templates/renderer'
10
- require 'express_templates/expander'
11
10
  require 'express_templates/compiler'
12
11
  require 'express_templates/interpolator'
13
12
  extend Renderer
@@ -2,33 +2,36 @@ require 'test_helper'
2
2
 
3
3
  class BaseTest < ActiveSupport::TestCase
4
4
 
5
+ def assigns
6
+ {}
7
+ end
8
+
5
9
  class NoLogic < ExpressTemplates::Components::Base
6
- has_markup -> {
10
+ emits {
7
11
  h1 { span "Some stuff" }
8
12
  }
9
13
  end
10
14
 
11
15
  test ".has_markup makes compile return the block passed through express compiled" do
12
- assert_equal %Q("<h1><span>Some stuff</span></h1>"), NoLogic.new.compile
16
+ assert_equal "<h1>\n <span>Some stuff</span>\n</h1>\n", ExpressTemplates.render(self) { no_logic }
13
17
  end
14
18
 
15
- test "components register themselves as macros" do
16
- assert ExpressTemplates::Expander.instance_methods.include?(:no_logic)
19
+ test "components register themselves as arbre builder methods" do
20
+ assert Arbre::Element::BuilderMethods.instance_methods.include?(:no_logic)
17
21
  end
18
22
 
19
23
  class Context
20
- def initialize ; @foo = ['bar', 'baz'] ; end
21
- end
22
-
23
- test "fragments and has_markup are synonyms for emits" do
24
- assert_equal NoLogic.method(:emits), NoLogic.method(:fragments)
25
- assert_equal NoLogic.method(:emits), NoLogic.method(:has_markup)
24
+ def assigns
25
+ {:foo => ['bar', 'baz']}
26
+ end
26
27
  end
27
28
 
28
- class Helpers < ECB
29
- helper :title_helper, &-> { @foo.first }
29
+ class HelperExample < ECB
30
+ def title_helper
31
+ foo.first
32
+ end
30
33
 
31
- emits -> {
34
+ emits {
32
35
  h1 {
33
36
  title_helper
34
37
  }
@@ -37,8 +40,7 @@ class BaseTest < ActiveSupport::TestCase
37
40
  end
38
41
 
39
42
  test "helpers defined in component are evaluated in context" do
40
- compiled = Helpers.new.compile
41
- assert_equal "<h1>bar</h1>", Context.new.instance_eval(Interpolator.transform(compiled))
43
+ assert_equal "<h1>bar</h1>\n", ExpressTemplates.render(Context.new) { helper_example }
42
44
  end
43
45
 
44
46
  end
@@ -6,7 +6,7 @@ module AdminModule
6
6
  class SmartThing
7
7
  include ExpressTemplates::Components::Capabilities::Resourceful
8
8
 
9
- attr_accessor :virtual_path
9
+ attr_accessor :virtual_path, :config
10
10
 
11
11
  def initialize(virtual_path, config = {})
12
12
  @virtual_path = virtual_path
@@ -14,8 +14,8 @@ module AdminModule
14
14
  @args = [self]
15
15
  end
16
16
 
17
- def template
18
- self
17
+ def template_virtual_path
18
+ @virtual_path
19
19
  end
20
20
  end
21
21
  end
@@ -4,40 +4,46 @@ class ConfigurableTest < ActiveSupport::TestCase
4
4
 
5
5
  ETC = ExpressTemplates::Components
6
6
 
7
- class ConfigurableComponent < ETC::Base
8
- include ETC::Capabilities::Configurable
9
- emits -> {
10
- div.bar(my[:id])
11
- }
12
- end
13
-
14
- test "a configurable component accepts an id argument" do
15
- assert :foo, ConfigurableComponent.new(:foo).my[:id]
7
+ class ConfigurableComponent < ETC::Configurable
8
+ def markup
9
+ div(id: my[:id], class: 'bar')
10
+ end
16
11
  end
17
12
 
18
13
  test "renders id argument as dom id" do
19
- compiled_src = ConfigurableComponent.new(:foo).compile
20
- assert '<div id="foo" class="bar" />', compiled_src
14
+ compiled_src = ExpressTemplates.render(self) { configurable_component(:foo) }
15
+ assert_equal "<div id=\"foo\" class=\"bar\"></div>\n", compiled_src
21
16
  end
22
17
 
23
- class ConfigurableContainerComponent < ETC::Base
24
- include ETC::Capabilities::Configurable
25
- include ETC::Capabilities::Parenting
18
+ class ConfigurableContainerComponent < ETC::Configurable
26
19
 
27
20
  # make sure a helper can take arguments
28
- helper(:name) {|name| name.to_s }
21
+ # helper(:name) {|name| name.to_s }
22
+ def name(name)
23
+ name.to_s
24
+ end
29
25
 
30
- emits -> {
31
- div(my[:id]) {
26
+ def markup &block
27
+ div(id: my[:id]) {
32
28
  h1 { name(my[:id]) }
33
- _yield
29
+ yield(block) if block
34
30
  }
35
- }
31
+ end
32
+ end
33
+
34
+ def assigns
35
+ {}
36
36
  end
37
37
 
38
38
  test "a configurable component may have also be a container" do
39
- html = ExpressTemplates.render { configurable_container_component(:foo) { p "bar" }}
40
- assert_equal '<div id="foo"><h1>foo</h1><p>bar</p></div>', html
39
+ html = ExpressTemplates.render(self) { configurable_container_component(:foo) { |c| para 'bar'} }
40
+ expected = <<-HTML
41
+ <div id=\"foo\">
42
+ <h1>foo</h1>
43
+ <p>bar</p>
44
+ </div>
45
+ HTML
46
+ assert_equal "<div id=\"foo\">\n <h1>foo</h1>\n <p>bar</p>\n</div>\n", html
41
47
  end
42
48
 
43
49
  end
@@ -6,47 +6,87 @@ class BasicFieldsTest < ActiveSupport::TestCase
6
6
  datetime_local number range
7
7
  search telephone time url week)
8
8
 
9
- test "text requires parent" do
10
- fragment = -> {
11
- text :name
12
- }
13
- assert_raises(RuntimeError) {
14
- ExpressTemplates.compile(&fragment)
15
- }
9
+ def assigns
10
+ {resource: resource}
11
+ end
12
+
13
+ def field_type_map
14
+ Hash[BASIC_FIELDS.map {|f| [f, f]}].merge(
15
+ {'phone' => 'tel',
16
+ 'telephone' => 'tel',
17
+ 'datetime_local' => 'datetime-local' })
18
+ end
19
+
20
+ def label_html
21
+ '<label for="foo_bar">Bar</label>'
16
22
  end
17
23
 
18
24
  test "all fields work" do
19
25
  BASIC_FIELDS.each do |type|
20
- fragment = -> {
26
+ fragment = -> (ctx) {
21
27
  express_form(:foo) {
22
28
  send(type, :bar)
23
29
  }
24
30
  }
25
- assert_match '#{label_tag("foo_bar", "Bar")', ExpressTemplates.compile(&fragment)
26
- assert_match "#{type}_field(:foo, :bar)", ExpressTemplates.compile(&fragment)
31
+ html = arbre(&fragment)
32
+ assert_match label_html, html
33
+ assert_match(/input.*type="#{field_type_map[type]}"/, html)
34
+ # assert_match "#{type}_field(:foo, :bar, {})", arbre(&fragment)
35
+ end
36
+ end
37
+
38
+ test "passing html options to fields work" do
39
+ options = {class: 'form-field'}
40
+ BASIC_FIELDS.each do |type|
41
+ html = arbre {
42
+ express_form(:foo) {
43
+ send(type, :bar, options)
44
+ }
45
+ }
46
+ assert_match label_html, html
47
+ assert_match /input.*type="#{field_type_map[type]}"/, html
48
+ assert_match /input.*class="form-field"/, html
27
49
  end
28
50
  end
29
51
 
30
52
  test "textarea uses rails text_area helper" do
31
- fragment = -> {
53
+ html = arbre {
32
54
  express_form(:foo) {
33
55
  textarea :bar
34
56
  }
35
57
  }
36
- assert_match '#{label_tag("foo_bar", "Bar")', ExpressTemplates.compile(&fragment)
37
- assert_match "text_area(:foo, :bar)", ExpressTemplates.compile(&fragment)
58
+ assert_match label_html, html
59
+ assert_match /<textarea name="foo\[bar\]" id="foo_bar"><\/textarea>/, html.gsub("\n", '')
38
60
  end
39
61
 
62
+ test "textarea passes additional html options to rails helper" do
63
+ html = arbre {
64
+ express_form(:foo) {
65
+ textarea :bar, rows: 5, class: 'tinymce form-field'
66
+ }
67
+ }
68
+ assert_match label_html, html
69
+ assert_match /<textarea rows="5" class="tinymce form-field" name="foo\[bar\]" id="foo_bar"><\/textarea>/, html.gsub("\n", '')
70
+ end
40
71
 
41
72
  test "hidden uses rails hidden_tag helper" do
42
- fragment = -> {
73
+ html = arbre {
43
74
  express_form(:foo) {
44
75
  hidden :bar
45
76
  }
46
77
  }
47
- assert_no_match '#{label_tag("foo_bar", "Bar")', ExpressTemplates.compile(&fragment)
48
- assert_match "hidden_field(:foo, :bar)", ExpressTemplates.compile(&fragment)
78
+ assert_no_match label_html, html
79
+ assert_match '<input type="hidden"', html
49
80
  end
50
81
 
51
- end
82
+ test "hidden field passes additional html options to rails helper" do
83
+ html = arbre {
84
+ express_form(:foo) {
85
+ hidden :bar, class: 'hidden form-field', value: 'ninja'
86
+ }
87
+ }
88
+ assert_no_match label_html, html
89
+ assert_match /<input class="hidden form-field" value="ninja" type="hidden" name="foo\[bar\]" id="foo_bar"/, html
90
+ end
52
91
 
92
+ end