express_templates 0.11.4 → 0.11.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9974ff2ee9b43a2995de75414d2b03b7923d589d
4
- data.tar.gz: 62bdff14270e90b4b3ce41a5a208f1645cf8fa84
3
+ metadata.gz: 837a8240902bdcc3580f426982c97c7059db1fab
4
+ data.tar.gz: c45b6746eeff2ce8f1a202c408f8673126f41f0b
5
5
  SHA512:
6
- metadata.gz: 50e19ae0db48d7831f1ba311ffdacb6fb458e744064c6ebb907334f0558971fdce62550f3e455b5505f5a5ab09237e5fa328499bcc3b87b23f32dd154665212d
7
- data.tar.gz: 05c26ab0186720edde5d339dff39b9511cb2b594106836629f02791c2d7033efee61cfc825510522ce79b65607054d6d5f22e7cf3085d78cac0a7c7d02ea79d4
6
+ metadata.gz: 1d2a7fc15d2c72e934b55fbc3bb38ad788321d72307a526d247018d1017a6436bedb12a28ac5995990930a3e3cf37f4d5f3b7226f7d69c58b4357a12fd1b860c
7
+ data.tar.gz: af25e8b9959674d6dba0ae9b3ac524ff03a437a98c1eb22a22138160c44746f0946e5f4b242a1e6d990f558d2c9628cedbab3ac70aa0106bb9a4f02b3076084e
data/lib/arbre/patches.rb CHANGED
@@ -31,6 +31,7 @@ module Arbre
31
31
  def on_component_error(tag, exception)
32
32
  tag.content = "Error rendering #{tag.class} component: #{exception.message}"
33
33
  ::Rails.logger.error exception
34
+ ::Rails.logger.error exception.backtrace.slice(0..20).join("\n")
34
35
  end
35
36
  end
36
37
 
@@ -121,7 +121,6 @@ module ExpressTemplates
121
121
  self.class.name.demodulize.underscore.dasherize
122
122
  end
123
123
 
124
-
125
124
  private
126
125
  def _run_before_build_hooks
127
126
  before_build_hooks.each do |hook|
@@ -6,7 +6,7 @@ module ExpressTemplates
6
6
 
7
7
  def self.included(base)
8
8
  base.class_eval do
9
- has_argument :id, "The name of the collection", type: :symbol, optional: false
9
+ has_argument :id, "The name of the collection. A resourceful component will look for the resource based on the ID.", type: :symbol, optional: false
10
10
  has_option :collection, 'Provide an explicit collection as a resource.'
11
11
  has_option :collection_path, 'Provide an explicit path for the collection resource.', type: [:string, :proc]
12
12
  has_option :resource_class, 'Overrides namespaced resource_class for using resources from a different module or namespace.'
@@ -29,7 +29,15 @@ module ExpressTemplates
29
29
 
30
30
  def resource_class
31
31
  resource_class = config[:resource_class] || _namespaced_resource_class
32
- resource_class.constantize
32
+ if Object.const_defined?(resource_class)
33
+ resource_class.constantize
34
+ else
35
+ message = [
36
+ "Could not find the class `#{resource_class}`.",
37
+ "You may need to define the option `:resource_class`.",
38
+ ].join(" ")
39
+ fail ArgumentError, message
40
+ end
33
41
  end
34
42
 
35
43
  private
@@ -206,7 +214,11 @@ module ExpressTemplates
206
214
  end
207
215
 
208
216
  def resource
209
- self.send(resource_name)
217
+ begin
218
+ self.send(resource_name)
219
+ rescue NoMethodError
220
+ nil
221
+ end
210
222
  end
211
223
  end
212
224
  end
@@ -36,10 +36,16 @@ RUBY
36
36
  end
37
37
 
38
38
  class Textarea < FormComponent
39
+ has_option :value, 'The initial textarea value'
40
+
39
41
  contains {
40
42
  label_tag(label_name, label_text)
41
43
  text_area_tag field_name_attribute, field_value, field_helper_options
42
44
  }
45
+
46
+ def field_value
47
+ config[:value] || super
48
+ end
43
49
  end
44
50
 
45
51
  class Hidden < FormComponent
@@ -6,7 +6,7 @@ module ExpressTemplates
6
6
 
7
7
  tag :form
8
8
 
9
- has_option :method, 'The form method', default: 'POST', attribute: true #, options: ['PUT', 'POST', 'GET', 'DELETE']
9
+ has_option :method, 'The form method' #, options: ['PUT', 'POST', 'GET', 'DELETE']
10
10
  has_option :action, 'The form action containing the resource path or url.'
11
11
  has_option :on_success, 'Pass a form value indicating where to go on a successful submission.'
12
12
  has_option :on_failure, 'Pass a form value indicating where to go on a failed submission.'
@@ -15,7 +15,7 @@ module ExpressTemplates
15
15
  prepends -> {
16
16
  div(style: 'display:none') {
17
17
  add_child helpers.utf8_enforcer_tag
18
- add_child helpers.send(:method_tag, resource.persisted? ? :put : :post)
18
+ add_child helpers.send(:method_tag, method_tag) if method_tag
19
19
  add_child helpers.send(:token_tag)
20
20
  hidden_field_tag :on_success, config[:on_success] if config[:on_success]
21
21
  hidden_field_tag :on_failure, config[:on_failure] if config[:on_failure]
@@ -24,6 +24,7 @@ module ExpressTemplates
24
24
 
25
25
  before_build -> {
26
26
  set_attribute(:id, form_id)
27
+ set_attribute(:method, form_method)
27
28
  set_attribute(:action, form_action)
28
29
  set_attribute(:enctype, form_enctype) if form_enctype
29
30
  add_class(config[:id])
@@ -41,6 +42,35 @@ module ExpressTemplates
41
42
  config[:enctype]
42
43
  end
43
44
 
45
+ private
46
+
47
+ def form_method
48
+ case config[:method]
49
+ when :put, :delete, :post, :patch
50
+ 'POST'
51
+ when :get
52
+ 'GET'
53
+ when nil
54
+ 'POST'
55
+ end
56
+ end
57
+
58
+ def method_tag
59
+ case config[:method]
60
+ when :get, :post
61
+ nil
62
+ when :put, :patch
63
+ :patch
64
+ when :delete
65
+ :delete
66
+ else
67
+ determine_method_tag
68
+ end
69
+ end
70
+
71
+ def determine_method_tag
72
+ :patch if resource.try(:persisted?)
73
+ end
44
74
  end
45
75
  end
46
76
  end
@@ -75,7 +75,7 @@ module ExpressTemplates
75
75
 
76
76
  def options_from_belongs_to
77
77
  if belongs_to_association.polymorphic?
78
- helpers.options_for_select([[]]) # we can't really handle polymorhic yet
78
+ raise 'No options for Polymorphic association'
79
79
  else
80
80
  helpers.options_from_collection_for_select(related_collection, :id, option_name_method, resource.send(field_name))
81
81
  end
@@ -1,3 +1,3 @@
1
1
  module ExpressTemplates
2
- VERSION = "0.11.4"
2
+ VERSION = "0.11.5"
3
3
  end
@@ -59,5 +59,19 @@ module ExpressTemplates
59
59
  assert_equal FooBar, AdminModule::SmartThing.new('somethings/index', resource_class: 'FooBar').resource_class
60
60
  assert_equal Something, AdminModule::SmartThing.new('somethings/index', id: :something).resource_class
61
61
  end
62
+
63
+ test "#resource_class raises a helpful error message when the class does not exist" do
64
+ expected_error_message = [
65
+ "Could not find the class `FooBar`.",
66
+ "You may need to define the option `:resource_class`.",
67
+ ].join(" ")
68
+
69
+ smart_thing = AdminModule::SmartThing.
70
+ new('somethings/index', resource_class: 'NonExistentBar')
71
+
72
+ assert_raises(ArgumentError, expected_error_message) do
73
+ smart_thing.resource_class
74
+ end
75
+ end
62
76
  end
63
77
  end
@@ -74,10 +74,22 @@ class BasicFieldsTest < ActiveSupport::TestCase
74
74
  textarea :bar, rows: 5, class: 'tinymce form-field'
75
75
  }
76
76
  }
77
+
77
78
  assert_match label_html, html
78
79
  assert_match /<textarea name="foo\[bar\]" id="foo_bar" rows="5" class="tinymce form-field"><\/textarea>/, html.gsub("\n", '')
79
80
  end
80
81
 
82
+ test 'textare has default value' do
83
+ html = arbre {
84
+ express_form(:foo) {
85
+ textarea :bar, value: "This is a default value", rows: 3
86
+ }
87
+ }
88
+
89
+ assert_match label_html, html
90
+ assert_match /<textarea name="foo\[bar\]" id="foo_bar" rows="3">This is a default value<\/textarea>/, html.gsub("\n", '')
91
+ end
92
+
81
93
  test "hidden uses rails hidden_tag helper" do
82
94
  html = arbre {
83
95
  express_form(:foo) {
@@ -1,9 +1,5 @@
1
1
  require 'test_helper'
2
2
 
3
- class Foo
4
- def self.columns ; [] ; end
5
- end
6
-
7
3
  class ExpressFormTest < ActiveSupport::TestCase
8
4
 
9
5
  def simplest_form
@@ -22,6 +18,14 @@ class ExpressFormTest < ActiveSupport::TestCase
22
18
  end
23
19
  end
24
20
 
21
+ def form_with_method(method)
22
+ arbre(foo: resource) {
23
+ express_form(:foo, method: method) {
24
+ submit value: 'Save it!'
25
+ }
26
+ }
27
+ end
28
+
25
29
  test "simplest form renders" do
26
30
  assert simplest_form
27
31
  end
@@ -41,7 +45,6 @@ class ExpressFormTest < ActiveSupport::TestCase
41
45
  test "simplest form contains rails form helpers" do
42
46
  compiled_src = simplest_form
43
47
  assert_match "input name=\"utf8\" type=\"hidden\"", compiled_src
44
- assert_match "input type=\"hidden\" name=\"_method\"", compiled_src
45
48
  assert_match "name=\"authenticity_token\" value=\"AUTH_TOKEN\"", compiled_src
46
49
  assert_match /<form.*authenticity_token.*\/form>/, compiled_src.gsub("\n",'')
47
50
  end
@@ -51,12 +54,56 @@ class ExpressFormTest < ActiveSupport::TestCase
51
54
  end
52
55
 
53
56
  test "simplest_form uses form_action for the action" do
54
- form_open_tag = simplest_form.match(/<form[^>]*>/)[0]
55
- assert_match 'action="/foos"', form_open_tag
57
+ assert_includes form_open_tag_attrs(simplest_form), 'action="/foos"'
56
58
  end
57
59
 
58
60
  test "express_form default method is POST" do
59
- assert_match 'method="POST"', simplest_form
61
+ assert_includes form_open_tag_attrs(simplest_form), 'method="POST"'
62
+ end
63
+
64
+ test "express_form generates correct markup when method :get is passed in" do
65
+ get_form = form_with_method(:get)
66
+
67
+ assert_includes form_open_tag_attrs(get_form), 'method="GET"'
68
+ refute_match hidden_method_tag, get_form
69
+ end
70
+
71
+ test "express_form generates correct markup when method :post is passed in" do
72
+ post_form = form_with_method(:post)
73
+
74
+ assert_includes form_open_tag_attrs(post_form), 'method="POST"'
75
+ refute_match hidden_method_tag, post_form
76
+ end
77
+
78
+ test "express_form generates correct markup when method :put is passed in" do
79
+ put_form = form_with_method(:put)
80
+
81
+ assert_includes form_open_tag_attrs(put_form), 'method="POST"'
82
+ assert_match hidden_method_tag('value="patch"'), put_form
83
+ end
84
+
85
+ test "express_form generates correct markup when method :patch is passed in" do
86
+ patch_form = form_with_method(:patch)
87
+
88
+ assert_includes form_open_tag_attrs(patch_form), 'method="POST"'
89
+ assert_match hidden_method_tag('value="patch"'), patch_form
90
+ end
91
+
92
+ test "express_form generates correct markup when method :delete is passed in" do
93
+ delete_form = form_with_method(:delete)
94
+
95
+ assert_includes form_open_tag_attrs(delete_form), 'method="POST"'
96
+ assert_match hidden_method_tag('value="delete"'), delete_form
97
+ end
98
+
99
+ private
100
+
101
+ def form_open_tag_attrs(form)
102
+ form.match(/<form[^>]*>/)[0]
103
+ end
104
+
105
+ def hidden_method_tag(attrs="")
106
+ /<input type="hidden" name="_method" #{attrs}[^>]*>/
60
107
  end
61
108
 
62
109
  end
data/test/test_helper.rb CHANGED
@@ -40,10 +40,24 @@ module AdditionalHelpers
40
40
 
41
41
  end
42
42
 
43
-
43
+ # For express form and basic fields test
44
+ class Foo
45
+ def self.columns ; [] ; end
46
+ end
44
47
 
45
48
  module ActiveSupport
46
49
  class TestCase
50
+
51
+ class Context
52
+ def assigns
53
+ {}
54
+ end
55
+ end
56
+
57
+ def render(&block)
58
+ ExpressTemplates.render(Context.new, &block)
59
+ end
60
+
47
61
  def arbre(additional_assigns = {}, &block)
48
62
  Arbre::Context.new assigns.merge(additional_assigns), helpers, &block
49
63
  end
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.11.4
4
+ version: 0.11.5
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-10-07 00:00:00.000000000 Z
12
+ date: 2015-11-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -280,7 +280,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
280
280
  version: '0'
281
281
  requirements: []
282
282
  rubyforge_project:
283
- rubygems_version: 2.4.7
283
+ rubygems_version: 2.4.8
284
284
  signing_key:
285
285
  specification_version: 4
286
286
  summary: Write HTML templates in declarative Ruby. Create reusable view components.