app/helpers/dry_crud/form/builder.rb in dry_crud-3.0.0 vs app/helpers/dry_crud/form/builder.rb in dry_crud-5.0.0
- old
+ new
@@ -73,11 +73,13 @@
# Render a boolean field.
def boolean_field(attr, html_options = {})
content_tag(:div, class: 'checkbox') do
content_tag(:label) do
+ # rubocop:disable Rails/OutputSafety
detail = html_options.delete(:detail) || ' '.html_safe
+ # rubocop:enable Rails/OutputSafety
safe_join([check_box(attr, html_options), ' ', detail])
end
end
end
@@ -95,13 +97,13 @@
add_css_class(html_options, 'form-control')
html_options[:rows] ||= 5
super(attr, html_options)
end
- alias_method :integer_field, :number_field
- alias_method :float_field, :number_field
- alias_method :decimal_field, :number_field
+ alias integer_field number_field
+ alias float_field number_field
+ alias decimal_field number_field
# Render a select element for a :belongs_to association defined by attr.
# Use additional html_options for the select element.
# To pass a custom element list, specify the list with the :list key or
# define an instance variable with the pluralized name of the
@@ -112,12 +114,14 @@
add_css_class(html_options, 'form-control')
collection_select(attr, list, :id, :to_s,
select_options(attr, html_options),
html_options)
else
- static_text(
- ta(:none_available, association(@object, attr)).html_safe)
+ # rubocop:disable Rails/OutputSafety
+ none = ta(:none_available, association(@object, attr)).html_safe
+ # rubocop:enable Rails/OutputSafety
+ static_text(none)
end
end
# rubocop:disable PredicateName
@@ -206,12 +210,12 @@
attr, attr_id = assoc_and_id_attr(attr)
validators = @object.class.validators_on(attr) +
@object.class.validators_on(attr_id)
validators.any? do |v|
v.kind == :presence &&
- !v.options.key?(:if) &&
- !v.options.key?(:unless)
+ !v.options.key?(:if) &&
+ !v.options.key?(:unless)
end
end
# Render a label for the given attribute with the passed content.
# The content may be given as an argument or as a block:
@@ -276,21 +280,23 @@
# As a last resort, all entries from the association class are returned.
def association_entries(attr, options)
list = options.delete(:list)
unless list
assoc = association(@object, attr)
- list = @template.send(:instance_variable_get,
- :"@#{assoc.name.to_s.pluralize}")
+ ivar = :"@#{assoc.name.to_s.pluralize}"
+ list = @template.send(:instance_variable_defined?, ivar) &&
+ @template.send(:instance_variable_get, ivar)
list ||= load_association_entries(assoc)
end
list
end
# Automatically load the entries for the given association.
def load_association_entries(assoc)
klass = assoc.klass
- list = klass.all.merge(assoc.scope)
+ list = klass.all
+ list = list.merge(assoc.scope) if assoc.scope
# Use special scopes if they are defined
if klass.respond_to?(:options_list)
list.options_list
elsif klass.respond_to?(:list)
list.list
@@ -301,12 +307,14 @@
# Get the cancel url for the given object considering options:
# 1. Use :cancel_url_new or :cancel_url_edit option, if present
# 2. Use :cancel_url option, if present
def cancel_url
- url = @object.new_record? ? options[:cancel_url_new] :
- options[:cancel_url_edit]
- url || options[:cancel_url]
+ if @object.new_record?
+ options[:cancel_url_new] || options[:cancel_url]
+ else
+ options[:cancel_url_edit] || options[:cancel_url]
+ end
end
end
end
end