express_templates 0.9.3 → 0.9.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9e93820b0563b5a327dea57ec300ff2878641cb3
4
- data.tar.gz: 5a5f7d0954ca6be289f8e0ba6de35e7b01c04988
3
+ metadata.gz: 05a81ab8ff1f7f2e94240689d4d87cbea72a4381
4
+ data.tar.gz: caffe066d6c10c2d0d90730f747a72a73d7008fc
5
5
  SHA512:
6
- metadata.gz: 03b9541ab3544bbbb721f1efd2b92adafdd1c68bb33626702b9623ebcb0e314cb21ecdd7fa205aa991b70a9c731c9583bb7d267c07f2989204cf4b2e494c58b6
7
- data.tar.gz: 3e262ad21e99a696376ba705a33103c1bf375cd83e8bb8643b3052b41e44722df3a8c23e1e1341dd0eb343ef6284e83163c82420688c3e64a8dce8e0393b7136
6
+ metadata.gz: fc9dfd7ad73fd876fd18d51d16e2524a7b9d344ee52ddd9ec713b5378ed802e68e5a29b0d773eba563c3d6b78667072979be676e17e6945dedfbe32195fef3a5
7
+ data.tar.gz: b10321d22b52c7a26fbba4105ed363638fd7f4915cc5ed957b2ee2c53926e04fdefc69c1b9ac55256f2513e234d374f0a6e3297b83306290e84bc09b15687aa3
data/README.md CHANGED
@@ -40,7 +40,7 @@ Everything should work as you would expect.
40
40
 
41
41
  Set your editor syntax for .et files to Ruby.
42
42
 
43
- You can now utilize components which are found with documentation and examples in <tt>ExpressTemplates::Components<tt>.
43
+ You can now utilize components which are found with documentation and examples in <tt>ExpressTemplates::Components</tt>.
44
44
 
45
45
  Components are the real strength of both arbre and express_templates.
46
46
 
@@ -8,6 +8,8 @@ module ExpressTemplates
8
8
  before_build -> {
9
9
  set_attribute(:id, "#{resource_name}_#{field_name}_wrapper")
10
10
  add_class(config[:wrapper_class])
11
+ add_class('error') if resource.respond_to?(:errors) &&
12
+ resource.errors.include?(field_name.to_sym)
11
13
  }
12
14
 
13
15
  has_option :wrapper_class, 'Override the class of the wrapping div of a form component', default: 'field-wrapper'
@@ -45,7 +45,7 @@ module ExpressTemplates
45
45
 
46
46
  contains -> (&customize_block) {
47
47
  @customize_block = customize_block
48
- list_items(eval(config[:id].to_s))
48
+ list_items(send(config[:id]))
49
49
  }
50
50
 
51
51
  before_build -> {
@@ -1,3 +1,5 @@
1
+ require 'ripper'
2
+
1
3
  module ExpressTemplates
2
4
  module Template
3
5
  class Handler
@@ -6,10 +8,24 @@ module ExpressTemplates
6
8
  end
7
9
 
8
10
  def call(template)
11
+ # call ripper stuff method
12
+
13
+ warn_contains_logic("(#{ExpressTemplates.compile(template)}).html_safe") # pass the source code
9
14
  # returns a string to be eval'd
10
15
  "(#{ExpressTemplates.compile(template)}).html_safe"
11
16
  end
12
17
 
18
+ def warn_contains_logic(compiled_template)
19
+ keywords = %w(if until unless case for do loop while) # array of conditional keywords
20
+ tokens = []
21
+ if Ripper.lex(compiled_template).select do |element| # since it outputs an array [[line, col], type, token]
22
+ element[1]==:on_kw # type must match ':on_kw' type (type is keyword)
23
+ end.each { |match| tokens.push(match) if keywords.include? match[2] } # check if token is in given /keyword/ array, then push to new array match
24
+ tokens.each do |first|
25
+ warn "PAGE TEMPLATE INCLUDES #{first[2]} STATEMENT AT LINE #{first[0][0]}: #{first}\n#{compiled_template}" # warn on first occurence of conditional logic
26
+ end
27
+ end
28
+ end
13
29
  end
14
30
  end
15
- end
31
+ end
@@ -1,3 +1,3 @@
1
1
  module ExpressTemplates
2
- VERSION = "0.9.3"
2
+ VERSION = "0.9.4"
3
3
  end
@@ -1,4 +1,5 @@
1
1
  require 'test_helper'
2
+ require 'active_model'
2
3
 
3
4
  class BasicFieldsTest < ActiveSupport::TestCase
4
5
 
@@ -89,4 +90,61 @@ class BasicFieldsTest < ActiveSupport::TestCase
89
90
  assert_match /<input class="hidden form-field" value="ninja" type="hidden" name="foo\[bar\]" id="foo_bar"/, html
90
91
  end
91
92
 
93
+ def resource_with_errors
94
+ mock_resource = resource
95
+ class << mock_resource
96
+ def errors
97
+ errors = ActiveModel::Errors.new(self)
98
+ errors.add(:name, "Can't be Foo")
99
+ errors
100
+ end
101
+ end
102
+ mock_resource
103
+ end
104
+
105
+ def has_error_class
106
+ /div[^>]*class="[^"]*error[^"]*"/
107
+ end
108
+
109
+ def has_error_class_on(field, html)
110
+ md = html.match(/(<div[^>]*id="[^"]*#{field}_wrapper[^"]*"[^>]*>)/)
111
+ assert md, "field has no wrapper"
112
+ return !!md[1].match(has_error_class)
113
+ end
114
+
115
+ test "adds error class if there are errors on a field with no input attributes" do
116
+ html_with_error = arbre(resource: resource_with_errors) {
117
+ express_form(:foo) {
118
+ text :name
119
+ text :body
120
+ }
121
+ }
122
+ assert resource_with_errors.errors.any?
123
+ assert assigns[:resource].errors.any?
124
+ assert has_error_class_on(:name, html_with_error), "name field has no error when expected"
125
+ refute has_error_class_on(:body, html_with_error), "body field has error class when it should not"
126
+ end
127
+
128
+ test "adds error class if there are errors on a field with no class set" do
129
+ html_with_error = arbre(resource: resource_with_errors) {
130
+ express_form(:foo) {
131
+ text :name, value: 'ninja'
132
+ }
133
+ }
134
+ assert resource_with_errors.errors.any?
135
+ assert assigns[:resource].errors.any?
136
+ assert_match has_error_class, html_with_error
137
+ end
138
+
139
+ test "adds error to class if there are errors on a field with existing class" do
140
+ html_with_error = arbre(resource: resource_with_errors) {
141
+ express_form(:foo) {
142
+ text :name, value: 'ninja', class: 'slug'
143
+ }
144
+ }
145
+ assert resource_with_errors.errors.any?
146
+ assert assigns[:resource].errors.any?
147
+ assert_match has_error_class, html_with_error
148
+ end
149
+
92
150
  end
@@ -7,6 +7,10 @@ class Stuff
7
7
  end
8
8
 
9
9
  class SubmitTest < ActiveSupport::TestCase
10
+
11
+ def assigns
12
+ {resource: nil}
13
+ end
10
14
  test "submit takes string param for value" do
11
15
  fragment = -> (ctx) {
12
16
  submit value: "Save it!"
@@ -2,11 +2,6 @@ require 'test_helper'
2
2
  require 'ostruct'
3
3
 
4
4
  class TreeForTest < ActiveSupport::TestCase
5
- class Context
6
- def initialize(roles)
7
- @roles = roles
8
- end
9
- end
10
5
 
11
6
  class Role
12
7
  attr :name, :children
@@ -17,7 +12,7 @@ class TreeForTest < ActiveSupport::TestCase
17
12
  end
18
13
 
19
14
  def roles
20
- [Role.new('SuperAdmin', children:
15
+ @roles ||= [Role.new('SuperAdmin', children:
21
16
  [Role.new('Admin', children:
22
17
  [Role.new('Publisher', children:
23
18
  [Role.new('Author')]),
data/test/handler_test.rb CHANGED
@@ -121,4 +121,28 @@ class HandlerTest < ActiveSupport::TestCase
121
121
  assert_equal "<li>\n#{A_LINK}#{A_LINK}</li>\n", render
122
122
  end
123
123
 
124
+ test "raises warning if template has conditional logic" do
125
+ temp = "dd(class: 'date') {
126
+ if blog_post.publish_at
127
+ blog_post.publish_at
128
+ else
129
+ link_to('Edit', '#'')
130
+ end
131
+ }"
132
+ keywords = %w(if unless until case for do loop while)
133
+ tokens = []
134
+
135
+ if Ripper.lex(temp).select do |element|
136
+ element[1]==:on_kw
137
+ end.each { |match| tokens.push(match) if keywords.include? match[2] }
138
+ tokens.each do |first|
139
+ out, @err = capture_io do
140
+ warn 'foo'
141
+ end
142
+ end
143
+ end
144
+
145
+ assert_equal @err, "foo\n"
146
+ end
147
+
124
148
  end
data/test/test_helper.rb CHANGED
@@ -44,16 +44,16 @@ end
44
44
 
45
45
  module ActiveSupport
46
46
  class TestCase
47
- def arbre(&block)
48
- Arbre::Context.new assigns, helpers, &block
47
+ def arbre(additional_assigns = {}, &block)
48
+ Arbre::Context.new assigns.merge(additional_assigns), helpers, &block
49
49
  end
50
50
  def assigns
51
- {}
51
+ @arbre_assigns ||={}
52
52
  end
53
53
  def helpers
54
- mock_action_view(assigns)
54
+ mock_action_view
55
55
  end
56
- def mock_action_view(assigns = {})
56
+ def mock_action_view
57
57
  controller = ActionView::TestCase::TestController.new
58
58
  ActionView::Base.send :include, ActionView::Helpers
59
59
  ActionView::Base.send :include, ActionView::Helpers::UrlHelper
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: express_templates
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.3
4
+ version: 0.9.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steven Talcott Smith
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-07-28 00:00:00.000000000 Z
12
+ date: 2015-08-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport