dynamic_scaffold 1.8.1 → 1.12.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +31 -0
- data/app/assets/javascripts/dynamic_scaffold.js +1 -0
- data/app/assets/javascripts/dynamic_scaffold/auto_width.js +32 -0
- data/app/assets/javascripts/dynamic_scaffold/common.js +20 -1
- data/app/views/dynamic_scaffold/bootstrap/_list.html.erb +32 -26
- data/app/views/dynamic_scaffold/bootstrap/form/_row.html.erb +1 -1
- data/lib/dynamic_scaffold/controller.rb +4 -0
- data/lib/dynamic_scaffold/form/item/base.rb +11 -3
- data/lib/dynamic_scaffold/form/item/single_option.rb +2 -2
- data/lib/dynamic_scaffold/form/item/two_options.rb +2 -2
- data/lib/dynamic_scaffold/form/item/two_options_with_block.rb +2 -2
- data/lib/dynamic_scaffold/list_builder.rb +35 -0
- data/lib/dynamic_scaffold/version.rb +1 -1
- data/lib/generators/dynamic_scaffold/templates/controller.erb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c42e88067995f3753891fcc0e2a29aa8a8b8fd7ff9b093780f20455db67eb731
|
4
|
+
data.tar.gz: 1872ac09ed02e4a0524e7bc0d26900d1e493596bcafe47b177bfda380e741d9a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9f893ff9dbb40db07c63c6929c11ccae374ac17e82621bf8f878325b7b2393da1f7ffc12e354ccac152579b8f888d2c66780179c015be4cb361bfa9e7837835b
|
7
|
+
data.tar.gz: d2ce03bded48b344d364a737b38bed3a4aa4129b431189fcb213092c5385652cb148513aaef7bb4070ee62b53d3edd42225405335d1357947ddec65a7a374c42
|
data/README.md
CHANGED
@@ -157,6 +157,34 @@ You can customize the list through the `DynamicScaffold::Config#list` property.
|
|
157
157
|
class ShopController < ApplicationController
|
158
158
|
include DynamicScaffold::Controller
|
159
159
|
dynamic_scaffold Shop do |config|
|
160
|
+
# Add button and new action to disabled, You can still post to create action.
|
161
|
+
config.list.add_button = false
|
162
|
+
# or
|
163
|
+
config.list.add_button do
|
164
|
+
# This block is called in view scope
|
165
|
+
false if params[:foo] == 1
|
166
|
+
end
|
167
|
+
|
168
|
+
# Edit button and edit action to disabled, You can still post to update action.
|
169
|
+
config.list.edit_buttons = false
|
170
|
+
# or
|
171
|
+
config.list.edit_buttons do |record|
|
172
|
+
# This block is called in view scope
|
173
|
+
# You can disable only specific record's button.
|
174
|
+
false if record.id != 1
|
175
|
+
end
|
176
|
+
|
177
|
+
# Destroy button to disabled, You can still post to destroy action.
|
178
|
+
config.list.destroy_buttons = false
|
179
|
+
# or
|
180
|
+
config.list.destroy_buttons do |record|
|
181
|
+
# This block is called in view scope
|
182
|
+
# You can disable only specific record's button.
|
183
|
+
false if record.id != 1
|
184
|
+
end
|
185
|
+
|
186
|
+
|
187
|
+
|
160
188
|
# If you want filtering that can not be handled by `config.scope`, you can use the filter method.
|
161
189
|
# Please note that returning nil will be ignored.
|
162
190
|
config.list.filter do |query|
|
@@ -253,6 +281,9 @@ class ShopController < ApplicationController
|
|
253
281
|
config.form.item(:collection_check_boxes, :state_ids, State.all, :id, :name)
|
254
282
|
config.form.item(:collection_radio_buttons, :status, Shop.statuses.map{|k, _v| [k, k.titleize]}, :first, :last)
|
255
283
|
|
284
|
+
# If you want to use parameters to get list, Pass Proc or Lambda to argument. It will be called in view scope.
|
285
|
+
config.form.item(:collection_select, :status, -> { Shop.where(area_id: params[:area_id]) }, :first, :last)
|
286
|
+
|
256
287
|
# If you want to display more free form field, use block.
|
257
288
|
# The block is executed in the context of view, so you can call the method of view.
|
258
289
|
config.form.item :block, :free do |form, field|
|
@@ -0,0 +1,32 @@
|
|
1
|
+
var calcWidth = function(){
|
2
|
+
var firstRow = document.querySelector('.js-ds-list-row:first-child')
|
3
|
+
|
4
|
+
var wrapperWidth = firstRow.clientWidth
|
5
|
+
|
6
|
+
var autoWidthItemIndexes = Array.prototype.filter.call(firstRow.querySelectorAll('.js-ds-list-item'), function(item){
|
7
|
+
return item.classList.contains('ds-auto-width')
|
8
|
+
}).map(function(item){
|
9
|
+
return parseInt(item.getAttribute('data-index'), 10)
|
10
|
+
})
|
11
|
+
|
12
|
+
autoWidthItemIndexes.forEach(function(index){
|
13
|
+
var items = document.querySelectorAll('.js-ds-list-item[data-index="' + index + '"]')
|
14
|
+
|
15
|
+
Array.prototype.forEach.call(items, function(item){
|
16
|
+
item.style.width = 'auto'
|
17
|
+
})
|
18
|
+
|
19
|
+
var maxWidth = Array.prototype.reduce.call(items, function(currentMax, item){
|
20
|
+
return Math.max(currentMax, item.offsetWidth)
|
21
|
+
}, 0)
|
22
|
+
|
23
|
+
maxWidth = Math.min(maxWidth, wrapperWidth)
|
24
|
+
|
25
|
+
Array.prototype.forEach.call(items, function(item){
|
26
|
+
item.style.width = maxWidth + 'px'
|
27
|
+
})
|
28
|
+
})
|
29
|
+
}
|
30
|
+
|
31
|
+
document.addEventListener('dynamic_scaffold:load', calcWidth)
|
32
|
+
document.addEventListener('dynamic_scaffold:window_resized', calcWidth)
|
@@ -13,6 +13,7 @@ if (window.Element && !Element.prototype.closest) {
|
|
13
13
|
};
|
14
14
|
}
|
15
15
|
|
16
|
+
// dynamic_scaffold:load
|
16
17
|
(function(){
|
17
18
|
var fireEvent = function(){
|
18
19
|
var event = document.createEvent("HTMLEvents");
|
@@ -25,7 +26,25 @@ if (window.Element && !Element.prototype.closest) {
|
|
25
26
|
// Not fire after the initial page load
|
26
27
|
if (e.data.timing.visitEnd) fireEvent()
|
27
28
|
})
|
28
|
-
})()
|
29
|
+
})();
|
30
|
+
|
31
|
+
|
32
|
+
// dynamic_scaffold:window_resized
|
33
|
+
(function(){
|
34
|
+
var fireEvent = function(){
|
35
|
+
var event = document.createEvent("HTMLEvents");
|
36
|
+
event.initEvent('dynamic_scaffold:window_resized', false, false)
|
37
|
+
document.dispatchEvent(event)
|
38
|
+
}
|
39
|
+
|
40
|
+
var timeout = 0
|
41
|
+
window.addEventListener('resize', function(e){
|
42
|
+
this.clearTimeout(timeout)
|
43
|
+
timeout = setTimeout(function(){
|
44
|
+
fireEvent()
|
45
|
+
}, 300)
|
46
|
+
})
|
47
|
+
})();
|
29
48
|
|
30
49
|
// promise polyfill
|
31
50
|
// IE 11 has no Promise? Are you kidding?
|
@@ -5,16 +5,18 @@
|
|
5
5
|
<%end%>
|
6
6
|
<input type="hidden" class="authenticity_param_name" value="<%= request_forgery_protection_token %>">
|
7
7
|
<%= form_with method: :patch, url: dynamic_scaffold_path(:sort, request_queries(dynamic_scaffold.list.page_param_name)), local: true do%>
|
8
|
-
|
9
|
-
|
10
|
-
<%=
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
8
|
+
<%- if dynamic_scaffold.list.add_button -%>
|
9
|
+
<div class="ds-row">
|
10
|
+
<%= link_to dynamic_scaffold_path(:new, request_queries), class: class_names('btn btn-outline-primary btn-primary btn-sm spec-ds-add', 'disabled': dynamic_scaffold.max_count?(@count)) do%>
|
11
|
+
<%= dynamic_scaffold_icon(:add) %> <%= t('dynamic_scaffold.button.add') %>
|
12
|
+
<%- unless dynamic_scaffold.max_count.nil? -%>
|
13
|
+
<span class="badge badge-light">
|
14
|
+
<%= @count %> / <%= dynamic_scaffold.max_count%>
|
15
|
+
</span>
|
16
|
+
<% end %>
|
17
|
+
<%end%>
|
18
|
+
</div>
|
19
|
+
<%- end -%>
|
18
20
|
<%= render 'dynamic_scaffold/bootstrap/pagination' %>
|
19
21
|
<div class="ds-row">
|
20
22
|
<%= render 'dynamic_scaffold/bootstrap/save_order'%>
|
@@ -36,9 +38,9 @@
|
|
36
38
|
<div class="ds-list-heading"><%= dynamic_scaffold.list.title(record) %></div>
|
37
39
|
<% end %>
|
38
40
|
<div class="ds-list-items">
|
39
|
-
<%dynamic_scaffold.list.items.
|
41
|
+
<%dynamic_scaffold.list.items.each_with_index do |disp, index|%>
|
40
42
|
<%- if disp.show?(self, record) -%>
|
41
|
-
<%= content_tag :div, class: class_names('ds-list-item', disp.classnames), **disp.html_attributes do%>
|
43
|
+
<%= content_tag :div, class: class_names('ds-list-item js-ds-list-item', disp.classnames), **{**disp.html_attributes, 'data-index': index} do%>
|
42
44
|
<div class="ds-list-label"><%= disp.label %></div>
|
43
45
|
<div class="ds-list-value"><%= disp.value self, record %></div>
|
44
46
|
<%end%>
|
@@ -47,11 +49,13 @@
|
|
47
49
|
</div>
|
48
50
|
<div class="ds-list-footer clearfix">
|
49
51
|
<div class="float-right pull-right">
|
50
|
-
|
51
|
-
|
52
|
-
<%=
|
53
|
-
|
54
|
-
|
52
|
+
<%- if dynamic_scaffold.list.edit_buttons(record) -%>
|
53
|
+
<div class="btn-group">
|
54
|
+
<%= link_to dynamic_scaffold_path(:edit, request_queries.merge(id: record[record.class.primary_key])), class: 'btn btn-primary btn-outline-primary btn-sm spec-ds-edit' do %>
|
55
|
+
<%= dynamic_scaffold_icon(:edit) %> <%= t('dynamic_scaffold.button.edit') %>
|
56
|
+
<%end%>
|
57
|
+
</div>
|
58
|
+
<%- end -%>
|
55
59
|
<% if dynamic_scaffold.list.sorter %>
|
56
60
|
<%[*dynamic_scaffold.model.primary_key].each do |pkey|%>
|
57
61
|
<input type="hidden" name="pkeys[][<%=pkey%>]" value="<%= record[pkey] %>">
|
@@ -71,15 +75,17 @@
|
|
71
75
|
</button>
|
72
76
|
</div>
|
73
77
|
<% end %>
|
74
|
-
|
75
|
-
<
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
78
|
+
<%- if dynamic_scaffold.list.destroy_buttons(record) -%>
|
79
|
+
<div class="btn-group">
|
80
|
+
<button
|
81
|
+
data-action="<%= dynamic_scaffold_path(:update, request_queries(dynamic_scaffold.list.page_param_name).merge(id: record[record.class.primary_key])) %>"
|
82
|
+
data-confirm-message="<%= t('dynamic_scaffold.message.destroy_confirm') %>"
|
83
|
+
class="btn btn-danger btn-sm js-ds-destory spec-ds-destroy"
|
84
|
+
>
|
85
|
+
<%= dynamic_scaffold_icon(:delete) %>
|
86
|
+
</button>
|
87
|
+
</div>
|
88
|
+
<%- end -%>
|
83
89
|
</div>
|
84
90
|
</div>
|
85
91
|
</li>
|
@@ -44,6 +44,8 @@ module DynamicScaffold
|
|
44
44
|
end
|
45
45
|
|
46
46
|
def new # rubocop:disable Metrics/AbcSize
|
47
|
+
raise ActionController::RoutingError, '`Add Button` is disabled.' unless dynamic_scaffold.list.add_button
|
48
|
+
|
47
49
|
unless dynamic_scaffold.max_count.nil?
|
48
50
|
count = dynamic_scaffold.list.build_sql(scope_params).count
|
49
51
|
raise Error::InvalidOperation, 'You can not add any more.' if dynamic_scaffold.max_count?(count)
|
@@ -60,6 +62,8 @@ module DynamicScaffold
|
|
60
62
|
|
61
63
|
def edit
|
62
64
|
@record = find_record(edit_params)
|
65
|
+
|
66
|
+
raise ActionController::RoutingError, '`Edit Button` is disabled.' unless dynamic_scaffold.list.edit_buttons(@record)
|
63
67
|
end
|
64
68
|
|
65
69
|
def create
|
@@ -124,10 +124,10 @@ module DynamicScaffold
|
|
124
124
|
self
|
125
125
|
end
|
126
126
|
|
127
|
-
def needs_rendering?(view)
|
127
|
+
def needs_rendering?(view, record)
|
128
128
|
return true unless @if_block || @unless_block
|
129
|
-
return view.instance_exec(view.controller.params, &@if_block) if @if_block
|
130
|
-
return !view.instance_exec(view.controller.params, &@unless_block) if @unless_block
|
129
|
+
return view.instance_exec(view.controller.params, record, &@if_block) if @if_block
|
130
|
+
return !view.instance_exec(view.controller.params, record, &@unless_block) if @unless_block
|
131
131
|
end
|
132
132
|
|
133
133
|
def proxy(attribute_name)
|
@@ -184,6 +184,14 @@ module DynamicScaffold
|
|
184
184
|
options[:class] = classnames_list.join(' ') unless classnames_list.empty?
|
185
185
|
options
|
186
186
|
end
|
187
|
+
|
188
|
+
def build_args(view, args)
|
189
|
+
args.map do |arg|
|
190
|
+
next arg unless arg.is_a? Proc
|
191
|
+
|
192
|
+
view.instance_exec(&arg)
|
193
|
+
end
|
194
|
+
end
|
187
195
|
end
|
188
196
|
end
|
189
197
|
end
|
@@ -9,11 +9,11 @@ module DynamicScaffold
|
|
9
9
|
super(config, type, name, html_attributes)
|
10
10
|
end
|
11
11
|
|
12
|
-
def render(
|
12
|
+
def render(view, form, classnames = nil)
|
13
13
|
html_attributes = build_html_attributes(classnames)
|
14
14
|
# Retain the value of the password field on error.
|
15
15
|
html_attributes[:value] = form.object.public_send(@name) if @type == :password_field
|
16
|
-
form.public_send(@type, @name,
|
16
|
+
form.public_send(@type, @name, *build_args(view, @args), html_attributes)
|
17
17
|
end
|
18
18
|
end
|
19
19
|
end
|
@@ -18,9 +18,9 @@ module DynamicScaffold
|
|
18
18
|
super(config, type, name, html_attributes)
|
19
19
|
end
|
20
20
|
|
21
|
-
def render(
|
21
|
+
def render(view, form, classnames = nil)
|
22
22
|
html_attributes = build_html_attributes(classnames)
|
23
|
-
form.public_send(@type, @name,
|
23
|
+
form.public_send(@type, @name, *build_args(view, @args), @options, html_attributes)
|
24
24
|
end
|
25
25
|
end
|
26
26
|
end
|
@@ -2,11 +2,11 @@ module DynamicScaffold
|
|
2
2
|
module Form
|
3
3
|
module Item
|
4
4
|
class TwoOptionsWithBlock < TwoOptions
|
5
|
-
def render(
|
5
|
+
def render(view, form, classnames = nil)
|
6
6
|
form.public_send(
|
7
7
|
@type,
|
8
8
|
@name,
|
9
|
-
|
9
|
+
*build_args(view, @args),
|
10
10
|
@options,
|
11
11
|
build_html_attributes(classnames)
|
12
12
|
) do |builder|
|
@@ -1,5 +1,7 @@
|
|
1
1
|
module DynamicScaffold
|
2
2
|
class ListBuilder
|
3
|
+
attr_writer :add_button, :edit_buttons, :destroy_buttons
|
4
|
+
|
3
5
|
def initialize(config)
|
4
6
|
@config = config
|
5
7
|
@items = []
|
@@ -8,6 +10,9 @@ module DynamicScaffold
|
|
8
10
|
@title = nil
|
9
11
|
@filter = nil
|
10
12
|
@row_class_block = nil
|
13
|
+
@add_button = true
|
14
|
+
@edit_buttons = true
|
15
|
+
@destroy_buttons = true
|
11
16
|
end
|
12
17
|
|
13
18
|
def pagination(options = nil)
|
@@ -97,5 +102,35 @@ module DynamicScaffold
|
|
97
102
|
@config.controller.view_context.instance_exec(record, &@row_class_block)
|
98
103
|
end
|
99
104
|
end
|
105
|
+
|
106
|
+
def add_button(&block)
|
107
|
+
if block_given?
|
108
|
+
@add_button_block = block
|
109
|
+
elsif @add_button_block
|
110
|
+
@config.controller.view_context.instance_exec(&@add_button_block)
|
111
|
+
else
|
112
|
+
@add_button
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def edit_buttons(record = nil, &block)
|
117
|
+
if block_given?
|
118
|
+
@edit_buttons_block = block
|
119
|
+
elsif record.present? && @edit_buttons_block
|
120
|
+
@config.controller.view_context.instance_exec(record, &@edit_buttons_block)
|
121
|
+
else
|
122
|
+
@edit_buttons
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
def destroy_buttons(record = nil, &block)
|
127
|
+
if block_given?
|
128
|
+
@destroy_buttons_block = block
|
129
|
+
elsif record.present? && @destroy_buttons_block
|
130
|
+
@config.controller.view_context.instance_exec(record, &@destroy_buttons_block)
|
131
|
+
else
|
132
|
+
@destroy_buttons
|
133
|
+
end
|
134
|
+
end
|
100
135
|
end
|
101
136
|
end
|
@@ -62,7 +62,7 @@ class <%= @class_scope.present? ? "#{@class_scope}::" : '' %><%= @plural_model_n
|
|
62
62
|
# Please see https://github.com/gomo/dynamic_scaffold#customize-list for details.
|
63
63
|
|
64
64
|
<%- @model.column_names.each do |column| -%>
|
65
|
-
config.list.item(:<%= column %>,
|
65
|
+
config.list.item(:<%= column %>, class: 'ds-auto-width')
|
66
66
|
<%- end -%>
|
67
67
|
|
68
68
|
# You can customize the form fields through `config.form`.
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dynamic_scaffold
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.12.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Masamoto Miyata
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-08-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: classnames-rails-view
|
@@ -252,6 +252,7 @@ files:
|
|
252
252
|
- app/assets/images/dynamic_scaffold/fontawesome/step-forward.svg
|
253
253
|
- app/assets/images/dynamic_scaffold/fontawesome/times.svg
|
254
254
|
- app/assets/javascripts/dynamic_scaffold.js
|
255
|
+
- app/assets/javascripts/dynamic_scaffold/auto_width.js
|
255
256
|
- app/assets/javascripts/dynamic_scaffold/common.js
|
256
257
|
- app/assets/javascripts/dynamic_scaffold/delete.js
|
257
258
|
- app/assets/javascripts/dynamic_scaffold/image.js
|