express_templates 0.9.4 → 0.9.5

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: 05a81ab8ff1f7f2e94240689d4d87cbea72a4381
4
- data.tar.gz: caffe066d6c10c2d0d90730f747a72a73d7008fc
3
+ metadata.gz: 018beee9648be7172909cb241812b004795dbe49
4
+ data.tar.gz: 47e8b5946af001b551ee6e5897de40857409748f
5
5
  SHA512:
6
- metadata.gz: fc9dfd7ad73fd876fd18d51d16e2524a7b9d344ee52ddd9ec713b5378ed802e68e5a29b0d773eba563c3d6b78667072979be676e17e6945dedfbe32195fef3a5
7
- data.tar.gz: b10321d22b52c7a26fbba4105ed363638fd7f4915cc5ed957b2ee2c53926e04fdefc69c1b9ac55256f2513e234d374f0a6e3297b83306290e84bc09b15687aa3
6
+ metadata.gz: 74dcb721d16f6281b1f5b8824ac1314a7cfccbf0fe427cf360a611f9b629688723a9df6caf405a9f60ef38e858928e10b192ba25e8e9bf16dbc40448c1cee11f
7
+ data.tar.gz: 49eb76041d82f329fab1a67232b8e55870b395c62b863d40d3d781c6bbf07fbc8c253555ff1a855e5299ab273acaec59345d7df8ece71c9bcb8b69df254ccf7c
data/lib/arbre/patches.rb CHANGED
@@ -25,6 +25,16 @@ module Arbre
25
25
  end
26
26
  end
27
27
 
28
+ def possible_route_proxy(name)
29
+ if helpers.controller.class.parent &&
30
+ helpers.respond_to?(namespace = helpers.controller.class.parent.to_s.underscore)
31
+ if (route_proxy = helpers.send(namespace)).respond_to?(name)
32
+ return route_proxy
33
+ end
34
+ end
35
+ return nil
36
+ end
37
+
28
38
  # Implements the method lookup chain. When you call a method that
29
39
  # doesn't exist, we:
30
40
  #
@@ -40,6 +50,8 @@ module Arbre
40
50
  assigns[name]
41
51
  elsif helpers.respond_to?(name)
42
52
  helper_method(name, *args, &block)
53
+ elsif route_proxy = possible_route_proxy(name)
54
+ route_proxy.send(name, *args, &block)
43
55
  else
44
56
  super
45
57
  end
@@ -52,6 +64,8 @@ module Arbre
52
64
  def helper_method(name, *args, &block)
53
65
  if name.match /_path$/
54
66
  helpers.send(name, *args, &block)
67
+ elsif (const_get([name, 'engine'].join('/').classify) rescue nil)
68
+ helpers.send(name, *args, &block)
55
69
  else
56
70
  current_arbre_element.add_child helpers.send(name, *args, &block)
57
71
  end
@@ -8,10 +8,13 @@ module ExpressTemplates
8
8
  base.class_eval do
9
9
  has_argument :id, "The name of the collection", type: :symbol, optional: false
10
10
  has_option :collection, 'Provide an explicit collection as a resource.'
11
- has_option :collection_path, 'Provide an explicit path for the collection resource.'
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.'
13
- has_option :resource_path, 'Overides the resource path which is otherwise inferred from the class name.'
13
+ has_option :resource_path, 'Overides the resource path which is otherwise inferred from the class name.', type: [:string, :proc]
14
14
  has_option :path_prefix, 'Rarely used. Override inferred path_prefix for path helpers.'
15
+ # note there is some duplication here.
16
+ # resource_path can be specified as a proc which can specify a namespace
17
+ # TODO: investigate which approach is better and deprecate if desirable
15
18
  has_option :namespace, 'Rarely used. Overrides inferred namespace for resources.'
16
19
  end
17
20
  end
@@ -123,10 +126,21 @@ module ExpressTemplates
123
126
 
124
127
  def collection_path
125
128
  if config[:collection_path]
126
- config[:collection_path]
129
+ if config[:collection_path].respond_to?(:call)
130
+ config[:collection_path].call()
131
+ else
132
+ config[:collection_path]
133
+ end
127
134
  else
128
- #super
129
- helpers.instance_eval "#{collection_name_with_prefix}_path"
135
+ helpers.instance_eval collection_path_helper
136
+ end
137
+ end
138
+
139
+ def collection_path_helper
140
+ if path_namespace
141
+ "#{path_namespace}.#{collection_name_with_prefix}_path"
142
+ else
143
+ "#{collection_name_with_prefix}_path"
130
144
  end
131
145
  end
132
146
 
@@ -139,15 +153,37 @@ module ExpressTemplates
139
153
  end
140
154
 
141
155
  def resource_path_helper
142
- "#{resource_name_with_path_prefix}_path"
156
+ if path_namespace
157
+ "#{path_namespace}.#{resource_name_with_path_prefix}_path"
158
+ else
159
+ "#{resource_name_with_path_prefix}_path"
160
+ end
161
+ end
162
+
163
+ def path_namespace
164
+ resource_class_name = resource_class.to_s
165
+ resource_class_name.match(/::/) ?
166
+ resource_class_name.split("::").first.try(:underscore) : nil
143
167
  end
144
168
 
145
- def resource_path(ivar=false)
169
+ # accepts boolean to indicate whether to use an ivar or not
170
+ # and also may accept a resource on which we call to_param
171
+ def resource_path(ivar_or_resource = nil)
146
172
  if config[:resource_path]
147
- config[:resource_path]
173
+ if config[:resource_path].respond_to?(:call) &&
174
+ ivar_or_resource.respond_to?(:to_param) &&
175
+ ![true, false].include?(ivar_or_resource)
176
+ config[:resource_path].call(ivar_or_resource)
177
+ else
178
+ config[:resource_path]
179
+ end
148
180
  else
149
- # super
150
- helpers.instance_eval("#{resource_path_helper}(#{ivar ? '@' : ''}#{resource_name})")
181
+ if ivar_or_resource.respond_to?(:to_param) &&
182
+ ![true, false].include?(ivar_or_resource)
183
+ helpers.instance_eval("#{resource_path_helper}('#{ivar_or_resource.to_param}')")
184
+ else
185
+ helpers.instance_eval("#{resource_path_helper}(#{ivar_or_resource ? '@' : ''}#{resource_name})")
186
+ end
151
187
  end
152
188
  end
153
189
 
@@ -47,10 +47,10 @@ module ExpressTemplates
47
47
  @config ||= {}
48
48
  end
49
49
 
50
- def self.has_option(name, description, type: :text, required: nil, default: nil, attribute: nil)
50
+ def self.has_option(name, description, type: :string, required: nil, default: nil, attribute: nil, values: nil)
51
51
  raise "name must be a symbol" unless name.kind_of?(Symbol)
52
52
  option_definition = {description: description}
53
- option_definition.merge!(type: type, required: required, default: default, attribute: attribute)
53
+ option_definition.merge!(type: type, required: required, default: default, attribute: attribute, values: values)
54
54
  self.supported_options =
55
55
  self.supported_options.merge(name => option_definition)
56
56
  end
@@ -28,7 +28,7 @@ module ExpressTemplates
28
28
  end
29
29
 
30
30
  def form_action
31
- config[:action] || (resource.try(:persisted?) ? resource_path(ivar: true) : collection_path)
31
+ config[:action] || (resource.try(:persisted?) ? resource_path(true) : collection_path)
32
32
  end
33
33
 
34
34
  end
@@ -7,18 +7,22 @@ module ExpressTemplates
7
7
  module OptionSupport
8
8
 
9
9
  def has_many_through_association
10
- reflection = resource_class.reflect_on_association(field_name.to_sym)
11
- return reflection if reflection && reflection.macro.eql?(:has_many) && reflection.options.keys.include?(:through)
10
+ if resource_class.respond_to?(:reflect_on_association)
11
+ reflection = resource_class.reflect_on_association(field_name.to_sym)
12
+ return reflection if reflection && reflection.macro.eql?(:has_many) && reflection.options.keys.include?(:through)
13
+ end
12
14
  end
13
15
 
14
16
  # Reflect on any association and return it if the association type
15
17
  # is :belongs_to. Returns false if the association is not :belongs_to.
16
18
  # Returns nil if there was a problem reflecting.
17
19
  def belongs_to_association
18
- # assumes the belongs_to association uses <name>_id
19
- reflection = resource_class.reflect_on_association(field_name.gsub(/_id$/, '').to_sym)
20
- if reflection && reflection.macro.eql?(:belongs_to)
21
- return reflection
20
+ if resource_class.respond_to?(:reflect_on_association)
21
+ # assumes the belongs_to association uses <name>_id
22
+ reflection = resource_class.reflect_on_association(field_name.gsub(/_id$/, '').to_sym)
23
+ if reflection && reflection.macro.eql?(:belongs_to)
24
+ return reflection
25
+ end
22
26
  end
23
27
  end
24
28
 
@@ -1,3 +1,3 @@
1
1
  module ExpressTemplates
2
- VERSION = "0.9.4"
2
+ VERSION = "0.9.5"
3
3
  end
@@ -72,6 +72,19 @@ class ConfigurableTest < ActiveSupport::TestCase
72
72
  assert render { config_with_required_options(title: 'foo') }
73
73
  end
74
74
 
75
+ class ConfigWithOptionValues < ETCC
76
+ has_option :virtual, 'gets the virtual_attributes of a resource', values: -> (*) { resource_class.virtual_attributes }
77
+
78
+ def resource_class
79
+ OpenStruct.new(virtual_attributes: [:password, :password_confirmation])
80
+ end
81
+ end
82
+
83
+ test "values can be set for options" do
84
+ component = ConfigWithOptionValues.new
85
+ assert_equal [:password, :password_confirmation], component.instance_eval(&ConfigWithOptionValues.new.supported_options[:virtual][:values])
86
+ end
87
+
75
88
  class ConfigSubclass < ConfigWithRequiredOptions
76
89
  has_option :status, 'something'
77
90
  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.9.4
4
+ version: 0.9.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-08-05 00:00:00.000000000 Z
12
+ date: 2015-08-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport