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
@@ -1,43 +1,37 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class CheckboxTest < ActiveSupport::TestCase
4
-
5
- test "Checkbox requires a parent form" do
6
- fragment = -> {
7
- checkbox :permission_granted
8
- }
9
- assert_raises(RuntimeError) {
10
- ExpressTemplates.compile(&fragment)
11
- }
4
+ def assigns
5
+ {resource: resource}
12
6
  end
13
7
 
14
8
  test "checkbox places the label before the input" do
15
- fragment = -> {
9
+ html = arbre {
16
10
  express_form(:account) {
17
11
  checkbox :eula
18
12
  }
19
13
  }
20
- compiled = ExpressTemplates.compile(&fragment)
21
- label_helper = '#{label_tag("account_eula", "Eula")}'
22
- field_helper = '#{check_box(:account, :eula, {}, "1", "0")}'
23
- assert_match label_helper, compiled
24
- assert_match field_helper, compiled
25
- label_idx = compiled.index(label_helper)
26
- field_idx = compiled.index(field_helper)
14
+ label = '<label for="account_eula"'
15
+ field = 'input type="checkbox" value="1" name="account\[eula\]"'
16
+ assert_match /#{label}/, html
17
+ assert_match /#{field}/, html
18
+ label_idx = html.index(label)
19
+ field_idx = html.index(field.gsub('\\', ''))
27
20
  assert (field_idx > label_idx), "label must come first"
28
21
  end
29
22
 
30
23
  test "checkbox respects label_after: true " do
31
- fragment = -> {
24
+ html = arbre {
32
25
  express_form(:account) {
33
26
  checkbox :eula, label_after: true
34
27
  }
35
28
  }
36
- compiled = ExpressTemplates.compile(&fragment)
37
- label_helper = '#{label_tag("account_eula", "Eula")}'
38
- field_helper = '#{check_box(:account, :eula, {}, "1", "0")}'
39
- label_idx = compiled.index(label_helper)
40
- field_idx = compiled.index(field_helper)
29
+ label = '<label for="account_eula"'
30
+ field = 'input type="checkbox" value="1" name="account\[eula\]"'
31
+ assert_match /#{label}/, html
32
+ assert_match /#{field}/, html
33
+ label_idx = html.index(label)
34
+ field_idx = html.index(field.gsub('\\', ''))
41
35
  assert (field_idx < label_idx), "label must come after when label_after: true"
42
36
  end
43
37
 
@@ -1,108 +1,55 @@
1
1
  require 'test_helper'
2
2
  require 'ostruct'
3
3
 
4
- class ExpressFormTest < ActiveSupport::TestCase
5
- class Context
6
- def initialize(resource)
7
- @resource = resource
8
- end
4
+ class Foo
5
+ def self.columns ; [] ; end
9
6
  end
10
7
 
11
- def resource
12
- OpenStruct.new(
13
- id: 1,
14
- name: 'Foo',
15
- body: 'Hello world',
16
- email: 'some@email.com',
17
- phone: '123123123',
18
- url: 'http://someurl.com',
19
- number: 123,
20
- dropdown: 'yes',
21
- gender: 'Male'
22
- )
8
+ class ExpressFormTest < ActiveSupport::TestCase
9
+
10
+ def assigns
11
+ {resource: resource}
23
12
  end
24
13
 
25
- def simplest_form(resource)
26
- ctx = Context.new(resource)
27
- fragment = -> {
14
+ def simplest_form
15
+ arbre {
28
16
  express_form(:resource) {
29
17
  submit value: 'Save it!'
30
18
  }
31
19
  }
32
- return ctx, fragment
33
- end
34
-
35
- def express_form
36
- "ExpressTemplates::Components::Forms::ExpressForm".constantize
37
- end
38
-
39
- test "express_form component exists" do
40
- assert express_form
41
- end
42
-
43
- def compile_simplest_form
44
- ctx, fragment = simplest_form(resource)
45
- ExpressTemplates.compile(&fragment)
46
20
  end
47
21
 
48
22
  test "simplest form renders" do
49
- assert compile_simplest_form
23
+ assert simplest_form
50
24
  end
51
25
 
52
26
  test "simplest form contains form tag" do
53
- assert_match "<form", compile_simplest_form
54
- end
55
-
56
- test "simplest form contains rails form helpers" do
57
- compiled_src = compile_simplest_form
58
- assert_match "utf8_enforcer_tag", compiled_src
59
- assert_match "method_tag(", compiled_src
60
- assert_match "token_tag", compiled_src
61
- end
62
-
63
- test "simplest_form contains submit" do
64
- assert_match 'submit_tag', compile_simplest_form
27
+ assert_match "<form", simplest_form
65
28
  end
66
29
 
67
- test "simplest_form adopts children (submit has reference to parent)" do
68
- ctx, fragment = simplest_form(resource)
69
- expanded_nodes = ExpressTemplates::Expander.new(nil).expand(fragment.source_body)
70
- assert_instance_of ExpressTemplates::Components::Forms::ExpressForm,
71
- expanded_nodes.first.children.last.parent
30
+ test "express_form contents are inside the form" do
31
+ assert_match /<form.*submit.*\/form>/, simplest_form.gsub("\n",'')
72
32
  end
73
33
 
74
- test "#form_action uses url helpers" do
75
- assert_equal "{{@foo.try(:persisted?) ? foo_path(@foo) : foos_path}}", express_form.new(:foo).form_action
34
+ test "simplest form contains rails form helpers" do
35
+ compiled_src = simplest_form
36
+ assert_match "input name=\"utf8\" type=\"hidden\"", compiled_src
37
+ assert_match "input type=\"hidden\" name=\"_method\"", compiled_src
38
+ assert_match "name=\"authenticity_token\" value=\"AUTH_TOKEN\"", compiled_src
39
+ assert_match /<form.*authenticity_token.*\/form>/, compiled_src.gsub("\n",'')
76
40
  end
77
41
 
78
- test "#form_action uses correct path helper for update/patch" do
79
- assert_equal "{{@foo.try(:persisted?) ? foo_path(@foo) : foos_path}}", express_form.new(:foo, method: :put).form_action
42
+ test "simplest_form contains submit" do
43
+ assert_match '<input type="submit" name="commit" value="Save" />', simplest_form
80
44
  end
81
45
 
82
46
  test "simplest_form uses form_action for the action" do
83
- form_open_tag = compile_simplest_form.match(/<form[^>]*>/)[0]
84
- assert_match 'action=\"#{@resource.try(:persisted?) ? resource_path(@resource) : resources_path}\"', form_open_tag
47
+ form_open_tag = simplest_form.match(/<form[^>]*>/)[0]
48
+ assert_match 'action="/resources"', form_open_tag
85
49
  end
86
50
 
87
51
  test "express_form default method is POST" do
88
- form_open_tag = compile_simplest_form.match(/<form[^>]*>/)[0]
89
- assert_match 'method=\"POST\"', form_open_tag
52
+ assert_match 'method="POST"', simplest_form
90
53
  end
91
54
 
92
- test "express_form accepts :resource_name for removing namespace" do
93
- fragment = -> {
94
- express_form(:admin_foo, resource_name: 'foo') {
95
- submit "Save!"
96
- }
97
- }
98
- expanded_nodes = ExpressTemplates::Expander.new(nil).expand(fragment.source_body)
99
- assert_equal 'foo', expanded_nodes.first.resource_name
100
- end
101
-
102
- # test "express_form has a namespace option with nil default" do
103
- # form = ExpressTemplates::Components::Forms::ExpressForm
104
- # assert_nil form.new(:person).namespace
105
- # assert_equal 'express_engine', form.new(:person, namespace: 'express_engine').namespace
106
- # end
107
-
108
55
  end
@@ -2,17 +2,20 @@ require 'test_helper'
2
2
 
3
3
  class RadioTest < ActiveSupport::TestCase
4
4
 
5
+ def assigns
6
+ {resource: resource}
7
+ end
8
+
5
9
  test "radio requires a parent component" do
6
- fragment = -> {
7
- radio :preferred_email_format, ['HTML', 'Text']
8
- }
9
10
  assert_raises(RuntimeError) {
10
- ExpressTemplates.compile(&fragment)
11
+ html = arbre {
12
+ radio :preferred_email_format, ['HTML', 'Text']
13
+ }
11
14
  }
12
15
  end
13
16
 
14
17
  def radio_with_array_options
15
- fragment = -> {
18
+ html = arbre {
16
19
  express_form(:person) {
17
20
  radio :preferred_email_format, ['HTML', 'Text']
18
21
  }
@@ -20,18 +23,17 @@ class RadioTest < ActiveSupport::TestCase
20
23
  end
21
24
 
22
25
  test "radio has correct label field name and text" do
23
- assert_match '#{label_tag("person_preferred_email_format", "Preferred Email Format")}',
24
- ExpressTemplates.compile(&radio_with_array_options)
26
+ assert_match /<label for="person_preferred_email_format"/,
27
+ radio_with_array_options
25
28
  end
26
29
 
27
30
  test "radio options present with class 'radio'" do
28
- compiled = ExpressTemplates.compile(&radio_with_array_options)
29
- assert_match 'radio_button(:person, :preferred_email_format, "Text", class: "radio"', compiled
30
- assert_match '_format, "HTML", class: "radio"', compiled
31
+ assert_match /<input.*class="radio"/,
32
+ radio_with_array_options
31
33
  end
32
34
 
33
35
  def radio_with_hash_options
34
- fragment = -> {
36
+ html = arbre {
35
37
  express_form(:person) {
36
38
  radio :subscribed, {1 => 'Yes', 0 => 'No'}, wrapper_class: 'my-wrapper'
37
39
  }
@@ -39,34 +41,36 @@ class RadioTest < ActiveSupport::TestCase
39
41
  end
40
42
 
41
43
  test "radio options may be specified with a hash" do
42
- compiled = ExpressTemplates.compile(&radio_with_hash_options)
43
- assert_match '<label class=\"my-wrapper\">', compiled
44
- assert_match 'radio_button(:person, :subscribed, 0, class: "radio"', compiled
45
- assert_match 'radio_button(:person, :subscribed, 1, class: "radio"', compiled
44
+ compiled = radio_with_hash_options
45
+ assert_match '<label class="my-wrapper">', compiled
46
+ assert_match 'input class="radio" type="radio" value="0" name="person[subscribed]" id="person_subscribed_0" />No', compiled
47
+ assert_match 'input class="radio" type="radio" value="1" name="person[subscribed]" id="person_subscribed_1" />Yes', compiled
46
48
  end
47
49
 
48
50
  test "radio throws error if given improper options" do
49
- fragment = -> {
50
- express_form(:person) {
51
- radio :subscribed, "Garbage options"
52
- }
53
- }
54
51
  assert_raises(RuntimeError) {
55
- ExpressTemplates.compile(&fragment)
52
+ html = arbre {
53
+ express_form(:person) {
54
+ radio :subscribed, "Garbage options"
55
+ }
56
+ }
56
57
  }
57
58
  end
58
59
 
59
60
  def radio_with_options_omitted
60
- fragment = -> {
61
+ html = arbre {
61
62
  express_form(:employee) {
62
63
  radio :department_id
63
64
  }
64
65
  }
65
66
  end
66
67
 
67
- class ::Department
68
- def self.columns
69
- [OpenStruct.new(name: 'id'), OpenStruct.new(name: 'name')]
68
+ class ::Department < ::Gender
69
+ def self.order(*)
70
+ all
71
+ end
72
+ def self.all
73
+ return [new(1, 'Accounting'), new(2, 'Marketing')]
70
74
  end
71
75
  end
72
76
  class ::Employee
@@ -84,8 +88,8 @@ class RadioTest < ActiveSupport::TestCase
84
88
  end
85
89
 
86
90
  test "radio options from collection when options omitted" do
87
- assert_match 'collection_radio_buttons(:employee, :department_id, Department.all.select(:id, :name).order(:name), :id, :name, {}, {}',
88
- ExpressTemplates.compile(&radio_with_options_omitted)
91
+ assert_match /input type="radio" value="1" name="employee\[department_id\]" id="employee_department_id_1"/,
92
+ radio_with_options_omitted
89
93
  end
90
94
 
91
95
  # test "radio supports html options"
@@ -2,144 +2,119 @@ require 'test_helper'
2
2
  require 'ostruct'
3
3
  class SelectTest < ActiveSupport::TestCase
4
4
 
5
+ def assigns
6
+ {resource: ::Person.new}
7
+ end
8
+
9
+
5
10
  test "select requires a parent component" do
6
- fragment = -> {
7
- select :gender, ['Male', 'Female'], selected: 'Male'
8
- }
9
11
  assert_raises(RuntimeError) {
10
- ExpressTemplates.compile(&fragment)
12
+ html = arbre {
13
+ select :gender, options: ['Male', 'Female'], selected: 'Male'
14
+ }
11
15
  }
12
16
  end
13
17
 
14
18
  test "select comes with a label" do
15
- fragment = -> {
19
+ html = arbre {
16
20
  express_form(:person) {
17
21
  select :gender
18
22
  }
19
23
  }
20
- assert_match '#{label_tag("person_gender", "Gender")}', ExpressTemplates.compile(&fragment)
24
+ assert_match /<label.*for="person_gender"/, html
21
25
  end
22
26
 
23
- test "select uses options_for_select when values are specified" do
24
- fragment = -> {
27
+ test "select generates correct options when values are specified" do
28
+ html = arbre {
25
29
  express_form(:person) {
26
- select :gender, ['Male', 'Female'], selected: 'Male'
30
+ select :gender, options: ['Male', 'Female'], selected: 'Male'
27
31
  }
28
32
  }
29
- assert_match 'options_for_select(["Male", "Female"], "Male")', ExpressTemplates.compile(&fragment)
33
+ assert_match /<option.*selected="selected" value="Male"/, html
34
+ assert_match /<option.*value="Female"/, html
30
35
  end
31
36
 
32
37
  test "selected option is omitted selection is taken from model" do
33
- fragment = -> {
38
+ html = arbre {
34
39
  express_form(:person) {
35
- select :gender, ['Male', 'Female']
40
+ select :gender, options: ['Male', 'Female']
36
41
  }
37
42
  }
38
- assert_match 'options_for_select(["Male", "Female"], @person.gender)', ExpressTemplates.compile(&fragment)
43
+ assert_match /<option.*selected="selected" value="Male"/, html
44
+ assert_match /<option.*value="Female"/, html
39
45
  end
40
46
 
41
47
  test "select generates options from data when options omitted" do
42
- fragment = -> {
48
+ html = arbre {
43
49
  express_form(:person) {
44
50
  select :city
45
51
  }
46
52
  }
47
- assert_match 'options_for_select(@person.class.distinct(:city).pluck(:city), @person.city)', ExpressTemplates.compile(&fragment)
48
- end
49
-
50
- class ::Gender
51
- def self.columns
52
- [OpenStruct.new(name: 'id'), OpenStruct.new(name: 'name')]
53
- end
54
- end
55
- class ::Tagging
56
- def self.columns
57
- [OpenStruct.new(name: 'id'), OpenStruct.new(name: 'name')]
58
- end
59
- end
60
- class ::Person
61
- def self.reflect_on_association(name)
62
- if name.eql? :gender
63
- dummy_belongs_to_association = Object.new
64
- class << dummy_belongs_to_association
65
- def macro ; :belongs_to ; end
66
- def klass ; ::Gender ; end
67
- def polymorphic? ; false ; end
68
- end
69
- return dummy_belongs_to_association
70
- end
71
- if name.eql? :taggings
72
- dummy_has_many_through_association = Object.new
73
- class << dummy_has_many_through_association
74
- def macro ; :has_many ; end
75
- def klass ; ::Tagging ; end
76
- def options ; {:through => :peron_tags} ; end
77
- def polymorphic? ; false ; end
78
- end
79
- return dummy_has_many_through_association
80
- end
81
- end
53
+ assert_match /<option.*selected="selected" value="San Francisco"/, html
54
+ assert_match /<option.*value="Hong Kong"/, html
82
55
  end
83
56
 
84
57
  test "select uses options_from_collect... when field is relation" do
85
- fragment = -> {
58
+ html = arbre {
86
59
  express_form(:person) {
87
- select :gender
60
+ select :gender_id
88
61
  }
89
62
  }
90
63
 
91
- assert_match 'options_from_collection_for_select(Gender.all.select(:id, :name).order(:name), :id, :name, @person.gender)',
92
- ExpressTemplates.compile(&fragment)
64
+ assert_match /<option.*selected="selected" value="1"/, html
65
+ assert_match /<option.*value="2"/, html
93
66
  end
94
67
 
95
68
  test "select defaults to include_blank: true" do
96
- fragment = -> {
69
+ html = arbre {
97
70
  express_form(:person) {
98
71
  select :gender
99
72
  }
100
73
  }
101
- assert_match 'include_blank: true', ExpressTemplates.compile(&fragment)
74
+ assert_match '<option value=""></option>', html
102
75
  end
103
76
 
104
77
 
105
78
  test "select defaults can be overridden" do
106
- fragment = -> {
79
+ html = arbre {
107
80
  express_form(:person) {
108
- select :gender, nil, include_blank: false
81
+ select :gender, include_blank: false
109
82
  }
110
83
  }
111
- assert_no_match 'include_blank: true', ExpressTemplates.compile(&fragment)
84
+ assert_no_match 'include_blank: true', html
112
85
  end
113
86
 
114
87
  test "select multiple: true if passed multiple true" do
115
- fragment = -> {
88
+ html = arbre {
116
89
  express_form(:person) {
117
- select :taggings, nil, include_blank: false, multiple: true
90
+ select :taggings, include_blank: false, multiple: true
118
91
  }
119
92
  }
120
- assert_match 'multiple: true', ExpressTemplates.compile(&fragment)
93
+ assert_match 'multiple="multiple"', html
121
94
  end
122
95
 
123
96
  test "select multiple gets options from associated has_many_through collection" do
124
- fragment = -> {
97
+ html = arbre {
125
98
  express_form(:person) {
126
- select :taggings, nil, include_blank: false, multiple: true
99
+ select :taggings, include_blank: false, multiple: true
127
100
  }
128
101
  }
129
- assert_match 'tagging_ids', ExpressTemplates.compile(&fragment)
130
- assert_match 'options_from_collection_for_select(Tagging.all.select(:id, :name).order(:name), :id, :name, @person.taggings.map(&:id))',
131
- ExpressTemplates.compile(&fragment)
102
+ assert_match 'tagging_ids', html
103
+ assert_match /<option selected="selected" value="1">Friend<\/option>/, html
104
+ assert_match /<option selected="selected" value="2">Enemy<\/option>/, html
105
+ assert_match /<option value="3">Frenemy<\/option>/, html
132
106
  end
133
107
 
134
- test "select_collection uses collection_select" do
135
- fragment = -> {
108
+ test "select_collection works using collection_select" do
109
+ html = arbre {
136
110
  express_form(:person) {
137
111
  select_collection :taggings
138
112
  }
139
113
  }
140
- assert_match 'tagging_ids', ExpressTemplates.compile(&fragment)
141
- assert_match 'collection_select("person", "tagging_ids", Tagging.all.select(:id, :name).order(:name), :id, :name, {:include_blank=>false, :selected=>@person.tagging_ids}, multiple: true)',
142
- ExpressTemplates.compile(&fragment)
114
+ assert_match 'tagging_ids', html
115
+ assert_match /<option value="1">Friend<\/option>/, html
116
+ assert_match /<option value="2">Enemy<\/option>/, html
117
+ assert_match /<option value="3">Frenemy<\/option>/, html
143
118
  end
144
119
 
145
120
 
@@ -1,12 +1,17 @@
1
1
  require 'test_helper'
2
2
 
3
+ class Stuff
4
+ def self.columns
5
+ []
6
+ end
7
+ end
8
+
3
9
  class SubmitTest < ActiveSupport::TestCase
4
10
  test "submit takes string param for value" do
5
- fragment = -> {
6
- express_form(:stuff) {
7
- submit "Save it!"
8
- }
11
+ fragment = -> (ctx) {
12
+ submit "Save it!"
9
13
  }
10
- assert_match '#{submit_tag("Save it!", {})}', ExpressTemplates.compile(&fragment)
14
+ assert_match '<div class="field-wrapper"><input type="submit" name="commit" value="Save it!" /></div>',
15
+ arbre(&fragment)
11
16
  end
12
17
  end
@@ -24,43 +24,16 @@ class TreeForTest < ActiveSupport::TestCase
24
24
  Role.new('Auditor')])])]
25
25
  end
26
26
 
27
- EXAMPLE_COMPILED = -> {
28
- ExpressTemplates::Components::TreeFor.render_in(self) {
29
- node_renderer = -> (role, renderer) {
30
- ExpressTemplates::Indenter.for(:tree) do |ws, wsnl|
31
- "#{wsnl}<li>"+
32
- "#{role.name}"+
33
- if role.children.any?
34
- ExpressTemplates::Indenter.for(:tree) do |ws, wsnl|
35
- "#{wsnl}<ul>" +
36
- role.children.map do |child|
37
- renderer.call(child, renderer)
38
- end.join +
39
- "#{wsnl}</ul>"
40
- end +
41
- "#{wsnl}</li>"
42
- else
43
- "</li>"
44
- end
45
- end
46
- }
47
- ExpressTemplates::Indenter.for(:tree) do |ws, wsnl|
48
- "#{ws}<ul id=\"roles\" class=\"roles tree\">" +
49
- @roles.map do |role|
50
- node_renderer.call(role, node_renderer)
51
- end.join +
52
- "#{wsnl}</ul>\n"
53
- end
54
- }
55
- }
56
-
57
27
  EXAMPLE_MARKUP = <<-HTML
58
28
  <ul id="roles" class="roles tree">
59
- <li>SuperAdmin
29
+ <li>
30
+ SuperAdmin
60
31
  <ul>
61
- <li>Admin
32
+ <li>
33
+ Admin
62
34
  <ul>
63
- <li>Publisher
35
+ <li>
36
+ Publisher
64
37
  <ul>
65
38
  <li>Author</li>
66
39
  </ul>
@@ -73,31 +46,30 @@ ExpressTemplates::Components::TreeFor.render_in(self) {
73
46
  </ul>
74
47
  HTML
75
48
 
76
- def simple_tree_for(roles)
77
- ctx = Context.new(roles)
78
- fragment = -> {
79
- tree_for(:roles) {
80
- "{{role.name}}"
81
- }
82
- }
83
- return ctx, fragment
49
+ def assigns
50
+ {roles: roles}
84
51
  end
85
52
 
53
+ def simple_tree_for
54
+ arbre {
55
+ tree_for(:roles)
56
+ }.to_s
57
+ end
86
58
 
87
- test "example view code renders example markup" do
88
- assert_equal EXAMPLE_MARKUP, Context.new(roles).instance_eval(EXAMPLE_COMPILED.source_body)
59
+ test "tree_for renders correct markup with node.name as default" do
60
+ assert_equal EXAMPLE_MARKUP, simple_tree_for
89
61
  end
90
62
 
91
- test "compiled source is legible and transparent" do
92
- ctx, fragment = simple_tree_for(roles)
93
- assert_equal EXAMPLE_COMPILED.source_body, ExpressTemplates.compile(&fragment)
63
+ def custom_tree_for
64
+ arbre {
65
+ tree_for(:roles) { |node|
66
+ text_node node.name.upcase
67
+ }
68
+ }
94
69
  end
95
70
 
96
- test "rendered component matches desired markup" do
97
- ExpressTemplates::Markup::Tag.formatted do
98
- ctx, fragment = simple_tree_for(roles)
99
- assert_equal EXAMPLE_MARKUP, ExpressTemplates.render(ctx, &fragment)
100
- end
71
+ test "tree_for accepts block with custom content" do
72
+ assert_match 'AUTHOR', custom_tree_for
101
73
  end
102
74
 
103
- end
75
+ end
@@ -1,5 +1,6 @@
1
-
2
1
  content_for :title, "Dummy Show"
3
2
 
4
- h1 '{{@hello}}'
5
- p 'Find me in app/views/hello/show.html.express_templates'
3
+ div {
4
+ h1 "#{hello}"
5
+ p 'Find me in app/views/hello/show.html.express_templates'
6
+ }
@@ -10,6 +10,6 @@ html(lang: "en") {
10
10
  }
11
11
  body {
12
12
  yield
13
- javascript_include_tag "application"
14
13
  }
14
+ javascript_include_tag "application"
15
15
  }