dynamic_scaffold 1.8.0 → 1.11.0

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: 83178747c82566649957d2845a6e425dd16b320b8fe8ef3954432981c941aa16
4
- data.tar.gz: c8e689ee3fd60cb006f29cf053009de07799b45542c6187f91332eca005d2046
3
+ metadata.gz: fa5ec9c069fde80c1ee44d34bafe454d67c1b8e9ad291175b65f2174d4fedd73
4
+ data.tar.gz: d23f8d2d63c3657288a1c1ac1b76fd81c68720b60e8d22d0ba986d52b1f4b610
5
5
  SHA512:
6
- metadata.gz: b4fb71172557f91a3241555f6469ec2bb9c8aa7f264b0c284a134c05f4392f8b362c46dcf53a9447ba4efb91188939f066337286bca694f55fb6b71025cc16c7
7
- data.tar.gz: 3fc6eadf855760330b0840a4148f18a37a42a4aa2d37e433b5c9c09a9293a4d1571f8adb80e0ee70932be77903dab830dcc324080b1d7196604961d5009f77f3
6
+ metadata.gz: cdc7b66a1db5b926895223a02a37fe69db8a4d1dd0f765f80419fe2b185956d9b8f519180baaa4f7fce692840ac754d03557c58853f8333d374adc582cc71115
7
+ data.tar.gz: a915af903ec7fa690bb03922146306f30664547e2644ad806b846107008495b20f0b6a2142fcd5fa413f28a3678e39f4a90b3c94a83c2da92aaa9c8cede871c6
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|
@@ -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'%>
@@ -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
@@ -16,7 +16,7 @@ module DynamicScaffold
16
16
  end
17
17
 
18
18
  attr_accessor :parent_item
19
- attr_reader :name
19
+ attr_reader :name, :multiple
20
20
  def initialize(config, type, name, html_attributes = {})
21
21
  @config = config
22
22
  @name = name
@@ -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
@@ -2,8 +2,8 @@ module DynamicScaffold
2
2
  module Form
3
3
  module Item
4
4
  class Block < Base
5
- def initialize(config, type, name, &block)
6
- super(config, type, name, {})
5
+ def initialize(config, type, name, options = {}, &block)
6
+ super(config, type, name, options)
7
7
  @block = block
8
8
  end
9
9
 
@@ -9,11 +9,11 @@ module DynamicScaffold
9
9
  super(config, type, name, html_attributes)
10
10
  end
11
11
 
12
- def render(_view, form, classnames = nil)
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, *@args, html_attributes)
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(_view, form, classnames = nil)
21
+ def render(view, form, classnames = nil)
22
22
  html_attributes = build_html_attributes(classnames)
23
- form.public_send(@type, @name, *@args, @options, html_attributes)
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(_view, form, classnames = nil)
5
+ def render(view, form, classnames = nil)
6
6
  form.public_send(
7
7
  @type,
8
8
  @name,
9
- *@args,
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
@@ -1,3 +1,3 @@
1
1
  module DynamicScaffold
2
- VERSION = '1.8.0'.freeze
2
+ VERSION = '1.11.0'.freeze
3
3
  end
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.8.0
4
+ version: 1.11.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-07-01 00:00:00.000000000 Z
11
+ date: 2021-07-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: classnames-rails-view