express_admin 1.4.5 → 1.4.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/components/express_admin/smart_form.rb +4 -3
- data/app/components/express_admin/smart_table.rb +0 -4
- data/app/controllers/express_admin/standard_controller.rb +13 -0
- data/lib/express_admin/standard_actions.rb +155 -0
- data/lib/express_admin/version.rb +1 -1
- data/lib/generators/express_admin/install/install_generator.rb +0 -5
- data/lib/generators/express_admin/scaffold/templates/controller/controller.rb +1 -8
- data/test/controllers/standard_controller_test.rb +97 -0
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/test/components/smart_form_test.rb +1 -1
- data/test/lib/generators/express_admin/install_generator_test.rb +0 -7
- data/test/lib/generators/express_admin/scaffold_generator_test.rb +1 -4
- data/test/test_helper.rb +5 -0
- data/vendor/gems/express_templates/express_templates-0.9.5.gem +0 -0
- data/vendor/gems/express_templates/lib/arbre/patches.rb +14 -0
- data/vendor/gems/express_templates/lib/express_templates/components/capabilities/resourceful.rb +46 -10
- data/vendor/gems/express_templates/lib/express_templates/components/configurable.rb +2 -2
- data/vendor/gems/express_templates/lib/express_templates/components/forms/express_form.rb +1 -1
- data/vendor/gems/express_templates/lib/express_templates/components/forms/option_support.rb +10 -6
- data/vendor/gems/express_templates/lib/express_templates/version.rb +1 -1
- data/vendor/gems/express_templates/test/components/configurable_test.rb +13 -0
- data/vendor/gems/express_templates/test/components/forms/express_form_test.rb +0 -4
- data/vendor/gems/express_templates/test/dummy/log/test.log +963 -0
- metadata +9 -5
- data/lib/generators/express_admin/install/templates/controllers/admin_controller.rb.erb +0 -32
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 50d6a3fea68da45a25497be729e3f4122d3073ec
|
4
|
+
data.tar.gz: 1ffe48826b7a7a330a6adf849bcfd92b20562de0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5de456daa50c148a1eff7f6119f3edb0213589fb4111ae344fd9e70b9630c436a51d0c9cfa2b068ab7576beb8873b7f4ae0b2276fbfee9469144853646910961
|
7
|
+
data.tar.gz: 7c543198ff8aabf901bc459cb48bde95afede6f011e3c4507e7e6936daf74ce68ccd995e83ae1658470ed470eea30fcf8bbd42cc337835f4b0eff1422ce81d08
|
@@ -4,9 +4,10 @@ module ExpressAdmin
|
|
4
4
|
class SmartForm < ExpressTemplates::Components::Forms::ExpressForm
|
5
5
|
TIMESTAMPS = %w(updated_at created_at)
|
6
6
|
|
7
|
-
has_option :virtual, 'Override the list of virtual attributes of the resource to be displayed'
|
8
|
-
|
9
|
-
has_option :
|
7
|
+
has_option :virtual, 'Override the list of virtual attributes of the resource to be displayed', type: :array,
|
8
|
+
values: -> (*) { resource_class.respond_to?(:virtual_attributes) ? resource_class.virtual_attributes : [] }
|
9
|
+
has_option :exclude, 'Attributes not to be included in the form', type: :array
|
10
|
+
has_option :only, 'Respects the order the attributes are listed in', type: :array
|
10
11
|
has_option :show_timestamps, 'Set to true to show timestamps as labels'
|
11
12
|
|
12
13
|
contains {
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'express_admin/standard_actions'
|
2
|
+
|
3
|
+
module ExpressAdmin
|
4
|
+
class StandardController < ::ApplicationController
|
5
|
+
|
6
|
+
include StandardActions
|
7
|
+
|
8
|
+
def initialize(*args)
|
9
|
+
raise "StandardController must be subclassed" if self.class.eql?(StandardController)
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,155 @@
|
|
1
|
+
module ExpressAdmin
|
2
|
+
module StandardActions
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.class_eval do
|
6
|
+
extend ClassMethods
|
7
|
+
include InstanceMethods
|
8
|
+
helper_method :collection
|
9
|
+
helper_method :resource
|
10
|
+
|
11
|
+
class_attribute :defaults
|
12
|
+
self.defaults = {layout: 'layouts/express_admin/admin'}
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
module ClassMethods
|
17
|
+
def inherited(base)
|
18
|
+
base.class_eval <<-RUBY
|
19
|
+
def #{base.resource_name}_params
|
20
|
+
params.require(:#{base.resource_name}).permit!
|
21
|
+
end
|
22
|
+
RUBY
|
23
|
+
end
|
24
|
+
|
25
|
+
def resource_name
|
26
|
+
self.to_s.demodulize.gsub(/Controller$/, '').singularize.underscore
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
module InstanceMethods
|
31
|
+
def index
|
32
|
+
build_resource
|
33
|
+
load_collection
|
34
|
+
respond_to do |format|
|
35
|
+
format.html { render :index, layout: defaults[:layout] }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def new
|
40
|
+
build_resource
|
41
|
+
respond_to do |format|
|
42
|
+
format.html { render :new, layout: defaults[:layout] }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def create
|
47
|
+
build_resource(resource_params)
|
48
|
+
if resource.save
|
49
|
+
respond_to do |format|
|
50
|
+
format.html { redirect_to resource_path }
|
51
|
+
end
|
52
|
+
else
|
53
|
+
respond_to do |format|
|
54
|
+
format.html { render :new, layout: defaults[:layout] }
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def show
|
60
|
+
load_resource
|
61
|
+
load_collection
|
62
|
+
respond_to do |format|
|
63
|
+
format.html { render :show, layout: defaults[:layout] }
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def edit
|
68
|
+
load_resource
|
69
|
+
respond_to do |format|
|
70
|
+
format.html { render :edit, layout: defaults[:layout] }
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def update
|
75
|
+
load_resource
|
76
|
+
if resource.update_attributes(resource_params)
|
77
|
+
respond_to do |format|
|
78
|
+
format.html { redirect_to resource_path }
|
79
|
+
end
|
80
|
+
else
|
81
|
+
respond_to do |format|
|
82
|
+
format.html { render :edit, layout: defaults[:layout] }
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def destroy
|
88
|
+
load_resource
|
89
|
+
resource.destroy!
|
90
|
+
respond_to do |format|
|
91
|
+
format.html { redirect_to :index }
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
protected
|
96
|
+
def resource_path
|
97
|
+
namespace_route_proxy.send("#{resource_name}_path", resource)
|
98
|
+
end
|
99
|
+
|
100
|
+
def namespace_route_proxy
|
101
|
+
if parent_namespace = self.class.to_s.match(/(\w+)::/).try(:[], 1)
|
102
|
+
self.send(parent_namespace.underscore)
|
103
|
+
else
|
104
|
+
self
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def resource_params
|
109
|
+
self.send("#{resource_name}_params")
|
110
|
+
end
|
111
|
+
|
112
|
+
def load_collection
|
113
|
+
self.instance_variable_set(collection_ivar, resource_class.all)
|
114
|
+
end
|
115
|
+
|
116
|
+
def collection_ivar
|
117
|
+
"@#{collection_name}".to_sym
|
118
|
+
end
|
119
|
+
|
120
|
+
def collection
|
121
|
+
self.instance_variable_get(collection_ivar)
|
122
|
+
end
|
123
|
+
|
124
|
+
def collection_name
|
125
|
+
self.class.to_s.demodulize.gsub(/Controller$/, '').pluralize.underscore
|
126
|
+
end
|
127
|
+
|
128
|
+
def resource
|
129
|
+
self.instance_variable_get(resource_ivar)
|
130
|
+
end
|
131
|
+
|
132
|
+
def build_resource(*args)
|
133
|
+
self.instance_variable_set(resource_ivar, resource_class.new(*args))
|
134
|
+
end
|
135
|
+
|
136
|
+
def load_resource
|
137
|
+
self.instance_variable_set(resource_ivar, resource_class.find(params[:id]))
|
138
|
+
end
|
139
|
+
|
140
|
+
def resource_ivar
|
141
|
+
"@#{resource_name}".to_sym
|
142
|
+
end
|
143
|
+
|
144
|
+
def resource_class
|
145
|
+
self.class.to_s.gsub(/Controller$/, '').singularize.classify.constantize
|
146
|
+
end
|
147
|
+
|
148
|
+
# Foo::WidgetsController -> widget
|
149
|
+
def resource_name
|
150
|
+
self.class.resource_name
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
end
|
155
|
+
end
|
@@ -19,11 +19,6 @@ module ExpressAdmin
|
|
19
19
|
File.join('test/lib/generators', @project_name, 'install', 'install_generator_test.rb')
|
20
20
|
end
|
21
21
|
|
22
|
-
def create_admin_controller
|
23
|
-
template 'controllers/admin_controller.rb.erb',
|
24
|
-
File.join('app/controllers', @project_name, 'admin_controller.rb')
|
25
|
-
end
|
26
|
-
|
27
22
|
def create_admin_layout
|
28
23
|
template 'views/layouts/admin.html.et',
|
29
24
|
File.join('app/views/layouts', @project_name, 'admin.html.et')
|
@@ -1,12 +1,5 @@
|
|
1
1
|
<% module_namespacing do -%>
|
2
|
-
class <%= controller_class_name %>Controller <
|
2
|
+
class <%= controller_class_name %>Controller < ExpressAdmin::StandardController
|
3
3
|
|
4
|
-
defaults resource_class: <%= model_class_name %>
|
5
|
-
|
6
|
-
private
|
7
|
-
|
8
|
-
def <%= singular_table_name %>_params
|
9
|
-
params.require(:<%= singular_table_name %>).permit!
|
10
|
-
end
|
11
4
|
end
|
12
5
|
<% end -%>
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ExpressAdmin
|
4
|
+
|
5
|
+
class StandardControllerTest < ActiveSupport::TestCase
|
6
|
+
|
7
|
+
class WidgetsController < StandardController
|
8
|
+
def resource_class
|
9
|
+
Widget
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def widgets_controller(params = {})
|
14
|
+
@widgets_controller ||= WidgetsController.new
|
15
|
+
eigenclass = class << @widgets_controller ; self ; end
|
16
|
+
eigenclass.send(:define_method, :params) do
|
17
|
+
ActionController::Parameters.new(params.with_indifferent_access)
|
18
|
+
end
|
19
|
+
eigenclass.send(:define_method, :respond_to) do |&block|
|
20
|
+
block.call(ActionController::MimeResponds::Collector.new([], 'text/html'))
|
21
|
+
end
|
22
|
+
@widgets_controller
|
23
|
+
end
|
24
|
+
|
25
|
+
test "it should have all the standard actions" do
|
26
|
+
[:new, :create, :show, :index, :edit, :update, :destroy].each do |action|
|
27
|
+
assert StandardController.instance_methods.include?(action), "StandardController does not implement ##{action}"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
test "should require a subclass for instantiation" do
|
32
|
+
assert_raises(RuntimeError) do
|
33
|
+
StandardController.new
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def resource
|
38
|
+
widgets_controller.instance_variable_get(:@widget)
|
39
|
+
end
|
40
|
+
|
41
|
+
def collection
|
42
|
+
widgets_controller.instance_variable_get(:@widgets)
|
43
|
+
end
|
44
|
+
|
45
|
+
test "#new should expose a new instance of resource" do
|
46
|
+
widgets_controller.new
|
47
|
+
assert resource, "@widget not provided by #new"
|
48
|
+
refute resource.persisted?, "@widget should not be persisted"
|
49
|
+
end
|
50
|
+
|
51
|
+
test "#create should expose an instance of resource" do
|
52
|
+
widgets_controller(widget: {column2: 'nothing'}).create
|
53
|
+
assert resource, "@widget not provided by #create"
|
54
|
+
assert resource.persisted?, "@widget not saved"
|
55
|
+
end
|
56
|
+
|
57
|
+
test "#show should expose an instance of resource if found" do
|
58
|
+
widgets_controller(id: widgets(:one).id).show
|
59
|
+
assert resource, "@widget not provided by #show"
|
60
|
+
assert resource.persisted?, "@widget not saved"
|
61
|
+
end
|
62
|
+
|
63
|
+
test "#index should expose a collection of resources" do
|
64
|
+
widgets_controller.index
|
65
|
+
assert collection, "@widgets not provided by #index"
|
66
|
+
refute collection.empty?, "@widgets is empty"
|
67
|
+
end
|
68
|
+
|
69
|
+
test "#edit should expose an instance of resource if found" do
|
70
|
+
widgets_controller(id: widgets(:one).id).edit
|
71
|
+
assert resource, "@widget not provided by #edit"
|
72
|
+
assert resource.persisted?, "@widget not saved"
|
73
|
+
end
|
74
|
+
|
75
|
+
test "#update should expose an updated instance of resource if found" do
|
76
|
+
widgets_controller(id: widgets(:one).id, widget: {column2: "Updated"}).update
|
77
|
+
assert resource, "@widget not provided by #update"
|
78
|
+
assert_equal "Updated", resource.column2
|
79
|
+
assert resource.persisted?, "@widget not saved"
|
80
|
+
end
|
81
|
+
|
82
|
+
test "#destroy destroys" do
|
83
|
+
@widget = Widget.create!(column2: 'Destroy Me')
|
84
|
+
assert @widget.persisted?
|
85
|
+
widgets_controller(id: @widget.id).destroy
|
86
|
+
assert_raises(ActiveRecord::RecordNotFound) do
|
87
|
+
Widget.find(@widget.id)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
# test "it should provide collection and resource helpers"
|
92
|
+
|
93
|
+
# test "it should provide a default strong params"
|
94
|
+
|
95
|
+
# test "for nested resources it should try to expose a current method for parent resources"
|
96
|
+
end
|
97
|
+
end
|
data/test/dummy/db/test.sqlite3
CHANGED
Binary file
|
@@ -10,13 +10,6 @@ class ExpressAdmin::Generators::InstallGeneratorTest < Rails::Generators::TestCa
|
|
10
10
|
def test_install_on_invoke
|
11
11
|
run_generator
|
12
12
|
|
13
|
-
assert_file 'app/controllers/tmp/admin_controller.rb' do |content|
|
14
|
-
assert_match /module Tmp/, content
|
15
|
-
assert_match /class AdminController < ApplicationController/, content
|
16
|
-
assert_match /before_filter :authenticate_user! if defined\?\(Devise\)/, content
|
17
|
-
assert_match /layout "tmp\/admin"/, content
|
18
|
-
end
|
19
|
-
|
20
13
|
assert_file 'app/views/layouts/tmp/admin.html.et' do |content|
|
21
14
|
assert_match /render\(template: 'layouts\/express_admin\/admin'\)/, content
|
22
15
|
end
|
@@ -22,10 +22,7 @@ class ExpressAdmin::Generators::ScaffoldGeneratorTest < Rails::Generators::TestC
|
|
22
22
|
|
23
23
|
# Controller
|
24
24
|
assert_file "app/controllers/agents_controller.rb" do |content|
|
25
|
-
assert_match(/class AgentsController <
|
26
|
-
|
27
|
-
assert_match(/defaults resource_class: Agent/, content)
|
28
|
-
assert_match(/def agent_params()/, content)
|
25
|
+
assert_match(/class AgentsController < ExpressAdmin::StandardController/, content)
|
29
26
|
end
|
30
27
|
|
31
28
|
# Routes
|
data/test/test_helper.rb
CHANGED
Binary file
|
@@ -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
|
data/vendor/gems/express_templates/lib/express_templates/components/capabilities/resourceful.rb
CHANGED
@@ -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
|
-
|
129
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
150
|
-
|
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: :
|
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
|
@@ -7,18 +7,22 @@ module ExpressTemplates
|
|
7
7
|
module OptionSupport
|
8
8
|
|
9
9
|
def has_many_through_association
|
10
|
-
|
11
|
-
|
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
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
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
|
|
@@ -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
|