dynamic_scaffold 1.9.0 → 1.12.1

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
  SHA256:
3
- metadata.gz: 7d0ca43e5c5f7d3eac65d3ecb58f9c8db1c5fc56c16afa34ecd09dd5d04ee4e4
4
- data.tar.gz: ef7f572241d232e3579d991d501e49da461baac0297598014b1f145cca959c83
3
+ metadata.gz: 285995723a10d25333538b08b7dcd5132aa84633dd1a03d7bd73bb5513e605ad
4
+ data.tar.gz: 44546b8653ba75a4752b4bae7261747a620f0df559d7e844a7d53fe3e454b8e3
5
5
  SHA512:
6
- metadata.gz: f5df70910ffc6358f63638bd48748910176a889a89ca0ec9667c01aa37a4e3bd97a2981e3915b4efc2a6542a134bb12b7892271a7e438ebd3eb8078bb9d8cf59
7
- data.tar.gz: bcf91e27f537462fe3f9417f0230c21d95c76fb1e13db923121951f6001437d3b328aaf303c2b60c6d1ca0b86c8f96e418851ad7994f5addba7fbdfc3b301169
6
+ metadata.gz: 4091b45f8e9048a5fa2a51065eb0fc77bd0046ebfc239d663851ff5ba8e075063092fb34b91d5b9b0b04f6a4774cd5df24e7f7147dd718fb1330a4220cc9c2b7
7
+ data.tar.gz: a83d5951cc3612b79b180a718d5bff2abe46634212c79c7dd8262c24358c7339d6a1a1fa1fbf0b38aa51ee13f5e79c2e73f48356cf0cc2249ec17dc680cb685e
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|
@@ -3,3 +3,4 @@
3
3
  //= require dynamic_scaffold/pagination
4
4
  //= require dynamic_scaffold/sorter
5
5
  //= require dynamic_scaffold/image
6
+ //= require dynamic_scaffold/auto_width
@@ -0,0 +1,35 @@
1
+ var calcWidth = function(){
2
+ var firstRow = document.querySelector('.js-ds-list-row:first-child')
3
+ if(!firstRow){
4
+ return
5
+ }
6
+
7
+ var wrapperWidth = firstRow.clientWidth
8
+
9
+ var autoWidthItemIndexes = Array.prototype.filter.call(firstRow.querySelectorAll('.js-ds-list-item'), function(item){
10
+ return item.classList.contains('ds-auto-width')
11
+ }).map(function(item){
12
+ return parseInt(item.getAttribute('data-index'), 10)
13
+ })
14
+
15
+ autoWidthItemIndexes.forEach(function(index){
16
+ var items = document.querySelectorAll('.js-ds-list-item[data-index="' + index + '"]')
17
+
18
+ Array.prototype.forEach.call(items, function(item){
19
+ item.style.width = 'auto'
20
+ })
21
+
22
+ var maxWidth = Array.prototype.reduce.call(items, function(currentMax, item){
23
+ return Math.max(currentMax, item.offsetWidth)
24
+ }, 0)
25
+
26
+ maxWidth = Math.min(maxWidth, wrapperWidth)
27
+
28
+ Array.prototype.forEach.call(items, function(item){
29
+ item.style.width = maxWidth + 'px'
30
+ })
31
+ })
32
+ }
33
+
34
+ document.addEventListener('dynamic_scaffold:load', calcWidth)
35
+ 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
- <div class="ds-row">
9
- <%= 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%>
10
- <%= dynamic_scaffold_icon(:add) %> <%= t('dynamic_scaffold.button.add') %>
11
- <%- unless dynamic_scaffold.max_count.nil? -%>
12
- &nbsp;<span class="badge badge-light">
13
- <%= @count %>&nbsp;/&nbsp;<%= dynamic_scaffold.max_count%>
14
- </span>
15
- <% end %>
16
- <%end%>
17
- </div>
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
+ &nbsp;<span class="badge badge-light">
14
+ <%= @count %>&nbsp;/&nbsp;<%= 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.each do |disp|%>
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
- <div class="btn-group">
51
- <%= 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 %>
52
- <%= dynamic_scaffold_icon(:edit) %> <%= t('dynamic_scaffold.button.edit') %>
53
- <%end%>
54
- </div>
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
- <div class="btn-group">
75
- <button
76
- data-action="<%= dynamic_scaffold_path(:update, request_queries(dynamic_scaffold.list.page_param_name).merge(id: record[record.class.primary_key])) %>"
77
- data-confirm-message="<%= t('dynamic_scaffold.message.destroy_confirm') %>"
78
- class="btn btn-danger btn-sm js-ds-destory"
79
- >
80
- <%= dynamic_scaffold_icon(:delete) %>
81
- </button>
82
- </div>
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>
@@ -1,4 +1,4 @@
1
- <% if elem.needs_rendering?(self)%>
1
+ <% if elem.needs_rendering?(self, form.object)%>
2
2
  <% if !elem.label? && elem.type?(:hidden_field) %>
3
3
  <%= elem.render(self, form) %>
4
4
  <% else %>
@@ -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)
@@ -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
@@ -1,3 +1,3 @@
1
1
  module DynamicScaffold
2
- VERSION = '1.9.0'.freeze
2
+ VERSION = '1.12.1'.freeze
3
3
  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 %>, style: 'width: 120px;')
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.9.0
4
+ version: 1.12.1
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-07-20 00:00:00.000000000 Z
11
+ date: 2021-08-04 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