Skip to content

Commit 0892dc9

Browse files
committed
Change to kwargs syntax
1 parent 6024aa4 commit 0892dc9

File tree

11 files changed

+87
-92
lines changed

11 files changed

+87
-92
lines changed

app/controllers/crud_controller.rb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ def new
5252
# in the given block will take precedence over the one defined here.
5353
#
5454
# Specify a :location option if you wish to do a custom redirect.
55-
def create(options = {}, &block)
55+
def create(**options, &block)
5656
model_class.transaction do
5757
assign_attributes
5858
created = with_callbacks(:create, :save) { entry.save }
5959
respond(created,
60-
options.merge(status: :created, render_on_failure: :new),
60+
**options.merge(status: :created, render_on_failure: :new),
6161
&block)
6262
raise ActiveRecord::Rollback unless created
6363
end
@@ -80,12 +80,12 @@ def edit; end
8080
# in the given block will take precedence over the one defined here.
8181
#
8282
# Specify a :location option if you wish to do a custom redirect.
83-
def update(options = {}, &block)
83+
def update(**options, &block)
8484
model_class.transaction do
8585
assign_attributes
8686
updated = with_callbacks(:update, :save) { entry.save }
8787
respond(updated,
88-
options.merge(status: :ok, render_on_failure: :edit),
88+
**options.merge(status: :ok, render_on_failure: :edit),
8989
&block)
9090
raise ActiveRecord::Rollback unless updated
9191
end
@@ -103,11 +103,11 @@ def update(options = {}, &block)
103103
# in the given block will take precedence over the one defined here.
104104
#
105105
# Specify a :location option if you wish to do a custom redirect.
106-
def destroy(options = {}, &block)
106+
def destroy(**options, &block)
107107
model_class.transaction do
108108
destroyed = run_callbacks(:destroy) { entry.destroy }
109109
respond(destroyed,
110-
options.merge(status: :no_content),
110+
**options.merge(status: :no_content),
111111
&block)
112112
raise ActiveRecord::Rollback unless destroyed
113113
end
@@ -152,39 +152,39 @@ def show_path
152152
path_args(entry)
153153
end
154154

155-
def respond(success, options)
155+
def respond(success, **options)
156156
respond_to do |format|
157157
yield(format, success) if block_given?
158158
if success
159-
format.html { redirect_on_success(options) }
159+
format.html { redirect_on_success(**options) }
160160
format.json { render_success_json(options[:status]) }
161161
else
162-
format.html { render_or_redirect_on_failure(options) }
162+
format.html { render_or_redirect_on_failure(**options) }
163163
format.json { render_failure_json }
164164
end
165165
end
166166
end
167167

168168
# If the option :render_on_failure is given, render the corresponding
169169
# template, otherwise redirect.
170-
def render_or_redirect_on_failure(options)
170+
def render_or_redirect_on_failure(**options)
171171
if options[:render_on_failure]
172172
render options[:render_on_failure]
173173
else
174-
redirect_on_failure(options)
174+
redirect_on_failure(**options)
175175
end
176176
end
177177

178178
# Perform a redirect after a successfull operation and set a flash notice.
179-
def redirect_on_success(options = {})
179+
def redirect_on_success(**options)
180180
location = options[:location] ||
181181
(entry.destroyed? ? index_path : show_path)
182182
flash[:notice] ||= flash_message(:success)
183183
redirect_to location
184184
end
185185

186186
# Perform a redirect after a failed operation and set a flash alert.
187-
def redirect_on_failure(options = {})
187+
def redirect_on_failure(**options)
188188
location = options[:location] ||
189189
request.env['HTTP_REFERER'].presence ||
190190
index_path

app/helpers/dry_crud/form/builder.rb

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ class Builder < ActionView::Helpers::FormBuilder
2929

3030
# Render multiple input controls together with a label for the given
3131
# attributes.
32-
def labeled_input_fields(*attrs)
33-
options = attrs.extract_options!
34-
safe_join(attrs) { |a| labeled_input_field(a, options.dup) }
32+
def labeled_input_fields(*attrs, **options)
33+
safe_join(attrs) { |a| labeled_input_field(a, **options.dup) }
3534
end
3635

3736
# Render a corresponding input control and label for the given attribute.
@@ -45,8 +44,8 @@ def labeled_input_fields(*attrs)
4544
# * <tt>:field_method</tt> - Different method to create the input field.
4645
#
4746
# Use additional html_options for the input element.
48-
def labeled_input_field(attr, html_options = {})
49-
control_class.new(self, attr, html_options).render_labeled
47+
def labeled_input_field(attr, **html_options)
48+
control_class.new(self, attr, **html_options).render_labeled
5049
end
5150

5251
# Render a corresponding input control for the given attribute.
@@ -59,18 +58,18 @@ def labeled_input_field(attr, html_options = {})
5958
# * <tt>:field_method</tt> - Different method to create the input field.
6059
#
6160
# Use additional html_options for the input element.
62-
def input_field(attr, html_options = {})
63-
control_class.new(self, attr, html_options).render_content
61+
def input_field(attr, **html_options)
62+
control_class.new(self, attr, **html_options).render_content
6463
end
6564

6665
# Render a standard string field with column contraints.
67-
def string_field(attr, html_options = {})
66+
def string_field(attr, **html_options)
6867
html_options[:maxlength] ||= column_property(@object, attr, :limit)
69-
text_field(attr, html_options)
68+
text_field(attr, **html_options)
7069
end
7170

7271
# Render a boolean field.
73-
def boolean_field(attr, html_options = {})
72+
def boolean_field(attr, **html_options)
7473
tag.div(class: 'checkbox') do
7574
tag.label do
7675
detail = html_options.delete(:detail) || '&nbsp;'.html_safe
@@ -82,47 +81,47 @@ def boolean_field(attr, html_options = {})
8281
# Add form-control class to all input fields.
8382
%w[text_field password_field email_field
8483
number_field date_field time_field datetime_field].each do |method|
85-
define_method(method) do |attr, html_options = {}|
84+
define_method(method) do |attr, **html_options|
8685
add_css_class(html_options, 'form-control')
8786
super(attr, html_options)
8887
end
8988
end
9089

91-
def integer_field(attr, html_options = {})
90+
def integer_field(attr, **html_options)
9291
html_options[:step] ||= 1
93-
number_field(attr, html_options)
92+
number_field(attr, **html_options)
9493
end
9594

96-
def float_field(attr, html_options = {})
95+
def float_field(attr, **html_options)
9796
html_options[:step] ||= 'any'
98-
number_field(attr, html_options)
97+
number_field(attr, **html_options)
9998
end
10099

101-
def decimal_field(attr, html_options = {})
100+
def decimal_field(attr, **html_options)
102101
html_options[:step] ||=
103102
(10**-column_property(object, attr, :scale)).to_f
104-
number_field(attr, html_options)
103+
number_field(attr, **html_options)
105104
end
106105

107106
# Customize the standard text area to have 5 rows by default.
108-
def text_area(attr, html_options = {})
107+
def text_area(attr, **html_options)
109108
add_css_class(html_options, 'form-control')
110109
html_options[:rows] ||= 5
111-
super(attr, html_options)
110+
super(attr, **html_options)
112111
end
113112

114113
# Render a select element for a :belongs_to association defined by attr.
115114
# Use additional html_options for the select element.
116115
# To pass a custom element list, specify the list with the :list key or
117116
# define an instance variable with the pluralized name of the
118117
# association.
119-
def belongs_to_field(attr, html_options = {})
120-
list = association_entries(attr, html_options).to_a
118+
def belongs_to_field(attr, **html_options)
119+
list = association_entries(attr, **html_options).to_a
121120
if list.present?
122121
add_css_class(html_options, 'form-control')
123122
collection_select(attr, list, :id, :to_s,
124-
select_options(attr, html_options),
125-
html_options)
123+
select_options(attr, **html_options),
124+
**html_options)
126125
else
127126
# rubocop:disable Rails/OutputSafety
128127
none = ta(:none_available, association(@object, attr)).html_safe
@@ -139,10 +138,10 @@ def belongs_to_field(attr, html_options = {})
139138
# To pass a custom element list, specify the list with the :list key or
140139
# define an instance variable with the pluralized name of the
141140
# association.
142-
def has_many_field(attr, html_options = {})
141+
def has_many_field(attr, **html_options)
143142
html_options[:multiple] = true
144143
add_css_class(html_options, 'multiselect')
145-
belongs_to_field(attr, html_options)
144+
belongs_to_field(attr, **html_options)
146145
end
147146
# rubocop:enable Naming/PredicateName
148147

@@ -194,7 +193,7 @@ def cancel_link(url = nil)
194193

195194
# Depending if the given attribute must be present, return
196195
# only an initial selection prompt or a blank option, respectively.
197-
def select_options(attr, options = {})
196+
def select_options(attr, **options)
198197
prompt = options.delete(:prompt)
199198
blank = options.delete(:include_blank)
200199
if options[:multiple]
@@ -238,7 +237,7 @@ def labeled(attr, content = {}, options = {}, &block)
238237
options = content
239238
content = capture(&block)
240239
end
241-
control = control_class.new(self, attr, options)
240+
control = control_class.new(self, attr, **options)
242241
control.render_labeled(content)
243242
end
244243

@@ -275,18 +274,17 @@ def labeled_field_method?(name)
275274

276275
# Renders the corresponding field together with a label, required mark
277276
# and an optional help block.
278-
def build_labeled_field(field_method, *args)
279-
options = args.extract_options!
277+
def build_labeled_field(field_method, *args, **options)
280278
options[:field_method] = field_method
281-
control_class.new(self, *(args << options)).render_labeled
279+
control_class.new(self, *args, **options).render_labeled
282280
end
283281

284282
# Returns the list of association entries, either from options[:list] or
285283
# the instance variable with the pluralized association name.
286284
# Otherwise, if the association defines a #options_list or #list scope,
287285
# this is used to load the entries.
288286
# As a last resort, all entries from the association class are returned.
289-
def association_entries(attr, options)
287+
def association_entries(attr, **options)
290288
list = options.delete(:list)
291289
unless list
292290
assoc = association(@object, attr)

app/helpers/dry_crud/form/control.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ class Control
3939
# (The value for this option usually is 'required').
4040
#
4141
# All the other options will go to the field_method.
42-
def initialize(builder, attr, *args)
42+
def initialize(builder, attr, *args, **options)
4343
@builder = builder
4444
@attr = attr
45-
@options = args.extract_options!
45+
@options = options
4646
@args = args
4747

4848
@addon = options.delete(:addon)
@@ -100,7 +100,7 @@ def input
100100
@input ||= begin
101101
options[:required] = 'required' if required
102102
add_css_class(options, 'is-invalid') if errors?
103-
builder.send(field_method, attr, *(args << options))
103+
builder.send(field_method, attr, *args, **options)
104104
end
105105
end
106106

app/helpers/dry_crud/table/actions.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,40 +26,40 @@ def attr_with_show_link(attr, &block)
2626
# Action column to show the row entry.
2727
# A block may be given to define the link path for the row entry.
2828
# If the block returns nil, no link is rendered.
29-
def show_action_col(html_options = {}, &block)
29+
def show_action_col(**html_options, &block)
3030
action_col do |entry|
3131
path = action_path(entry, &block)
3232
if path
3333
table_action_link('zoom-in',
3434
path,
35-
html_options.clone)
35+
**html_options.clone)
3636
end
3737
end
3838
end
3939

4040
# Action column to edit the row entry.
4141
# A block may be given to define the link path for the row entry.
4242
# If the block returns nil, no link is rendered.
43-
def edit_action_col(html_options = {}, &block)
43+
def edit_action_col(**html_options, &block)
4444
action_col do |entry|
4545
path = action_path(entry, &block)
4646
if path
4747
path = path.is_a?(String) ? path : edit_polymorphic_path(path)
48-
table_action_link('pencil', path, html_options.clone)
48+
table_action_link('pencil', path, **html_options.clone)
4949
end
5050
end
5151
end
5252

5353
# Action column to destroy the row entry.
5454
# A block may be given to define the link path for the row entry.
5555
# If the block returns nil, no link is rendered.
56-
def destroy_action_col(html_options = {}, &block)
56+
def destroy_action_col(**html_options, &block)
5757
action_col do |entry|
5858
path = action_path(entry, &block)
5959
if path
6060
table_action_link('remove',
6161
path,
62-
html_options.merge(
62+
**html_options.merge(
6363
data: { confirm: ti(:confirm_delete),
6464
method: :delete }
6565
))
@@ -74,8 +74,8 @@ def action_col(&block)
7474
end
7575

7676
# Generic action link inside a table.
77-
def table_action_link(icon, url, html_options = {})
78-
add_css_class(html_options, "icon icon-#{icon}")
77+
def table_action_link(icon, url, **html_options)
78+
add_css_class(html_options, "bi-#{icon}")
7979
link_to('', url, html_options)
8080
end
8181

app/helpers/dry_crud/table/builder.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Builder
2020
:captionize, :add_css_class, :content_tag_nested,
2121
to: :template
2222

23-
def initialize(entries, template, options = {})
23+
def initialize(entries, template, **options)
2424
@entries = entries
2525
@template = template
2626
@options = options
@@ -30,16 +30,16 @@ def initialize(entries, template, options = {})
3030
# Convenience method to directly generate a table. Renders a row for each
3131
# entry in entries. Takes a block that gets the table object as parameter
3232
# for configuration. Returns the generated html for the table.
33-
def self.table(entries, template, options = {})
34-
t = new(entries, template, options)
33+
def self.table(entries, template, **options)
34+
t = new(entries, template, **options)
3535
yield t
3636
t.to_html
3737
end
3838

3939
# Define a column for the table with the given header, the html_options
4040
# used for each td and a block rendering the contents of a cell for the
4141
# current entry. The columns appear in the order they are defined.
42-
def col(header = '', html_options = {}, &block)
42+
def col(header = '', **html_options, &block)
4343
@cols << Col.new(header, html_options, @template, block)
4444
end
4545

@@ -55,11 +55,11 @@ def attrs(*attrs)
5555
# Define a column for the given attribute and an optional header.
5656
# If no header is given, the attribute name is used. The cell will
5757
# contain the formatted attribute value for the current entry.
58-
def attr(attr, header = nil, html_options = {}, &block)
58+
def attr(attr, header = nil, **html_options, &block)
5959
header ||= attr_header(attr)
6060
block ||= ->(e) { format_attr(e, attr) }
6161
add_css_class(html_options, align_class(attr))
62-
col(header, html_options, &block)
62+
col(header, **html_options, &block)
6363
end
6464

6565
# Renders the table as HTML.
@@ -98,7 +98,7 @@ def html_header
9898
def html_row(entry)
9999
attrs = {}
100100
attrs[:id] = dom_id(entry) if entry.respond_to?(:to_key)
101-
content_tag_nested(:tr, cols, attrs) { |c| c.html_cell(entry) }
101+
content_tag_nested(:tr, cols, **attrs) { |c| c.html_cell(entry) }
102102
end
103103

104104
# Determines the class of the table entries.

0 commit comments

Comments
 (0)