express_templates 0.2.7 → 0.3.0

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 (41) hide show
  1. checksums.yaml +4 -4
  2. data/lib/core_extensions/proc.rb +72 -20
  3. data/lib/express_templates/compiler.rb +9 -1
  4. data/lib/express_templates/components/base.rb +1 -1
  5. data/lib/express_templates/components/capabilities/building.rb +8 -0
  6. data/lib/express_templates/components/capabilities/conditionality.rb +8 -6
  7. data/lib/express_templates/components/capabilities/configurable.rb +3 -4
  8. data/lib/express_templates/components/capabilities/iterating.rb +18 -23
  9. data/lib/express_templates/components/capabilities/parenting.rb +4 -18
  10. data/lib/express_templates/components/capabilities/rendering.rb +2 -65
  11. data/lib/express_templates/components/capabilities/templating.rb +24 -22
  12. data/lib/express_templates/components/capabilities/wrapping.rb +23 -39
  13. data/lib/express_templates/components/column.rb +1 -1
  14. data/lib/express_templates/components/for_each.rb +28 -0
  15. data/lib/express_templates/components/form_for.rb +19 -0
  16. data/lib/express_templates/components/form_rails_support.rb +1 -1
  17. data/lib/express_templates/components/null_wrap.rb +9 -0
  18. data/lib/express_templates/components/row.rb +1 -1
  19. data/lib/express_templates/components/table_for.rb +119 -0
  20. data/lib/express_templates/components/tree_for.rb +41 -0
  21. data/lib/express_templates/components/unless_block.rb +40 -0
  22. data/lib/express_templates/components.rb +4 -0
  23. data/lib/express_templates/expander.rb +15 -4
  24. data/lib/express_templates/indenter.rb +8 -5
  25. data/lib/express_templates/markup/tag.rb +62 -30
  26. data/lib/express_templates/markup/wrapper.rb +1 -1
  27. data/lib/express_templates/version.rb +1 -1
  28. data/test/components/base_test.rb +5 -38
  29. data/test/components/conditionality_test.rb +1 -1
  30. data/test/components/configurable_test.rb +2 -2
  31. data/test/components/container_test.rb +1 -1
  32. data/test/components/iterating_test.rb +30 -9
  33. data/test/components/table_for_test.rb +116 -0
  34. data/test/core_extensions/proc_test.rb +35 -5
  35. data/test/dummy/log/test.log +645 -0
  36. data/test/fixtures/{a_big_page.html → a_big_page2.html} +0 -0
  37. data/test/indenter_test.rb +6 -4
  38. data/test/markup/tag_test.rb +15 -2
  39. data/test/performance_test.rb +1 -1
  40. data/test/test_helper.rb +2 -0
  41. metadata +26 -3
@@ -0,0 +1,116 @@
1
+ require 'test_helper'
2
+ require 'ostruct'
3
+
4
+ class TableForTest < ActiveSupport::TestCase
5
+
6
+ class Context
7
+ attr :items
8
+ def initialize(items)
9
+ @items = items
10
+ end
11
+ def titleize(s)
12
+ s.to_s.try(:titleize)
13
+ end
14
+ def format_price(p)
15
+ "$%0.2f" % p
16
+ end
17
+ end
18
+
19
+ def items
20
+ [OpenStruct.new(id: 1, name: 'Foo', price: 1.23),
21
+ OpenStruct.new(id: 2, name: 'Bar', price: 5.49),
22
+ OpenStruct.new(id: 3, name: 'Baz', price: 99.97)]
23
+ end
24
+
25
+ EXAMPLE_COMPILED = -> {
26
+ ExpressTemplates::Components::TableFor.render_in(self) {
27
+ "<table id=\"items\">
28
+ <thead>
29
+ <tr>
30
+ <th class=\"name\">Name</th>
31
+ <th class=\"price\">Price</th>
32
+ </tr>
33
+ </thead>
34
+
35
+ <tbody>"+(items.each_with_index.map do |item, item_index|
36
+ "
37
+ <tr id=\"#{(-> {"item-#{item.id}"}).call}\" class=\"item\">
38
+ <td class=\"name\">#{item.name}</td>
39
+ <td class=\"price\">#{(-> (price) { '$%0.2f' % price }).call(item.price)}</td>
40
+ </tr>
41
+ "
42
+ end).join+" </tbody>
43
+ </table>
44
+ "
45
+ }
46
+ }
47
+
48
+ EXAMPLE_MARKUP = <<-HTML
49
+ <table id="items">
50
+ <thead>
51
+ <tr>
52
+ <th class="name">Name</th>
53
+ <th class="price">Price</th>
54
+ </tr>
55
+ </thead>
56
+
57
+ <tbody>
58
+ <tr id="item-1" class="item">
59
+ <td class="name">Foo</td>
60
+ <td class="price">$1.23</td>
61
+ </tr>
62
+
63
+ <tr id="item-2" class="item">
64
+ <td class="name">Bar</td>
65
+ <td class="price">$5.49</td>
66
+ </tr>
67
+
68
+ <tr id="item-3" class="item">
69
+ <td class="name">Baz</td>
70
+ <td class="price">$99.97</td>
71
+ </tr>
72
+ </tbody>
73
+ </table>
74
+ HTML
75
+
76
+
77
+ def simple_table(items)
78
+ ctx = Context.new(items)
79
+ fragment = -> {
80
+ table_for(:items) do |t|
81
+ t.column :name
82
+ t.column :price, formatter: -> (price) { '$%0.2f' % price }
83
+ end
84
+ }
85
+ return ctx, fragment
86
+ end
87
+
88
+ def example_compiled_src
89
+ # necessary because the #source method is not perfect yet
90
+ # ideally we would have #source_body
91
+ EXAMPLE_COMPILED.source_body
92
+ end
93
+
94
+ test "example view code evaluates to example markup" do
95
+ assert_equal EXAMPLE_MARKUP, Context.new(items).instance_eval(EXAMPLE_COMPILED.source_body)
96
+ end
97
+
98
+ test "compiled source is legible and transparent" do
99
+ ExpressTemplates::Markup::Tag.formatted do
100
+ ctx, fragment = simple_table(items)
101
+ assert_equal example_compiled_src, ExpressTemplates.compile(&fragment)
102
+ end
103
+ end
104
+
105
+ test "example compiled source renders the markup in the context" do
106
+ ctx, fragment = simple_table(items)
107
+ assert_equal EXAMPLE_MARKUP, ctx.instance_eval(example_compiled_src)
108
+ end
109
+
110
+ test "rendered component matches desired markup" do
111
+ ExpressTemplates::Markup::Tag.formatted do
112
+ ctx, fragment = simple_table(items)
113
+ assert_equal EXAMPLE_MARKUP, ExpressTemplates.render(ctx, &fragment)
114
+ end
115
+ end
116
+ end
@@ -31,11 +31,41 @@ class ProcTest < ActiveSupport::TestCase
31
31
  assert_equal '{ whatever { another } }', block.source
32
32
  end
33
33
 
34
- # TODO: make work (with arguments)
35
- # test "#source works with a stabby lambda" do
36
- # block = return_block -> (something) { whatever }
37
- # assert_equal '-> { whatever }', block.source
38
- # end
34
+ test "#source works with a stabby lambda" do
35
+ block = return_block -> (something) { whatever }
36
+ assert_equal '-> (something) { whatever }', block.source
37
+ end
38
+
39
+ test "#source work with a stabby lambda spanning lines" do
40
+ block = return_block -> {
41
+ whatever { foo }
42
+ }
43
+ assert_equal "-> {\n whatever { foo }\n}", block.source
44
+ end
45
+
46
+ test "#source_body returns the body of a proc" do
47
+ block = return_block -> { whatever }
48
+ assert_equal 'whatever', block.source_body
49
+ end
50
+
51
+ test "#source_body handles funky bodies" do
52
+ block = return_block do
53
+ something(funky) &-> { whatever }
54
+ end
55
+ assert_equal 'something(funky) &-> { whatever }', block.source_body
56
+ end
57
+
58
+ test "#source body raises exception for arity > 0" do
59
+ block = return_block -> (foo) { whatever }
60
+ assert_raises(RuntimeError) do
61
+ block.source_body
62
+ end
63
+ end
39
64
 
65
+ test ".from_source stores source of a dynamicly built proc for later inspection" do
66
+ src = "-> { 'foo' }"
67
+ assert_equal src, Proc.from_source(src).source
68
+ assert_equal 'foo', Proc.from_source(src).call
69
+ end
40
70
 
41
71
  end