From db88455fe08539267ec3270064792a9df396d3c8 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Mon, 16 Dec 2019 15:50:34 +0000 Subject: [PATCH] Fix rubocop issues in spree/admin/base_helper --- .rubocop_manual_todo.yml | 4 ++ .rubocop_todo.yml | 1 + app/helpers/spree/admin/base_helper.rb | 87 ++++++++++++++------------ 3 files changed, 53 insertions(+), 39 deletions(-) diff --git a/.rubocop_manual_todo.yml b/.rubocop_manual_todo.yml index 8bb059012736..97c90964759a 100644 --- a/.rubocop_manual_todo.yml +++ b/.rubocop_manual_todo.yml @@ -385,6 +385,7 @@ Metrics/AbcSize: - app/helpers/checkout_helper.rb - app/helpers/i18n_helper.rb - app/helpers/order_cycles_helper.rb + - app/helpers/spree/admin/base_helper.rb - app/helpers/spree/admin/navigation_helper_decorator.rb - app/helpers/spree/orders_helper.rb - app/jobs/subscription_placement_job.rb @@ -498,6 +499,7 @@ Metrics/CyclomaticComplexity: - app/helpers/checkout_helper.rb - app/helpers/i18n_helper.rb - app/helpers/order_cycles_helper.rb + - app/helpers/spree/admin/base_helper.rb - app/helpers/spree/admin/navigation_helper_decorator.rb - app/models/enterprise.rb - app/models/enterprise_relationship.rb @@ -580,6 +582,7 @@ Metrics/MethodLength: - app/controllers/user_registrations_controller.rb - app/helpers/checkout_helper.rb - app/helpers/order_cycles_helper.rb + - app/helpers/spree/admin/base_helper.rb - app/jobs/subscription_placement_job.rb - app/mailers/producer_mailer.rb - app/models/column_preference.rb @@ -674,6 +677,7 @@ Metrics/ModuleLength: Exclude: - app/helpers/admin/injection_helper.rb - app/helpers/injection_helper.rb + - app/helpers/spree/admin/base_helper.rb - lib/open_food_network/column_preference_defaults.rb - spec/controllers/admin/enterprises_controller_spec.rb - spec/controllers/admin/order_cycles_controller_spec.rb diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 1e83f1c3042a..7ec7bc6e9d98 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -189,6 +189,7 @@ Rails/OutputSafety: Exclude: - 'app/controllers/spree/admin/reports_controller_decorator.rb' - 'app/helpers/angular_form_helper.rb' + - 'app/helpers/spree/admin/base_helper.rb' - 'app/helpers/spree/reports_helper.rb' - 'app/serializers/api/product_serializer.rb' - 'lib/spree/money_decorator.rb' diff --git a/app/helpers/spree/admin/base_helper.rb b/app/helpers/spree/admin/base_helper.rb index 6521fff8762f..e3924a04a56c 100644 --- a/app/helpers/spree/admin/base_helper.rb +++ b/app/helpers/spree/admin/base_helper.rb @@ -7,16 +7,19 @@ def field_container(model, method, options = {}, &block) if error_message_on(model, method).present? css_classes << 'withError' end - content_tag(:div, capture(&block), :class => css_classes.join(' '), :id => "#{model}_#{method}_field") + content_tag(:div, + capture(&block), + class: css_classes.join(' '), + id: "#{model}_#{method}_field") end - def error_message_on(object, method, options = {}) + def error_message_on(object, method, _options = {}) object = convert_to_model(object) obj = object.respond_to?(:errors) ? object : instance_variable_get("@#{object}") if obj && obj.errors[method].present? errors = obj.errors[method].map { |err| h(err) }.join('
').html_safe - content_tag(:span, errors, :class => 'formError') + content_tag(:span, errors, class: 'formError') else '' end @@ -32,7 +35,7 @@ def error_message_on(object, method, options = {}) # <%= form_for @project do |project_form| %> #
# <%= project_form.fields_for :tasks do |task_form| %> - # <%= render :partial => 'task', :locals => { :f => task_form } %> + # <%= render partial: 'task', locals: { f: task_form } %> # <% end %> #
# <% end %> @@ -41,10 +44,9 @@ def generate_html(form_builder, method, options = {}) options[:partial] ||= method.to_s.singularize options[:form_builder_local] ||= :f - form_builder.fields_for(method, options[:object], :child_index => 'NEW_RECORD') do |f| - render(:partial => options[:partial], :locals => { options[:form_builder_local] => f }) + form_builder.fields_for(method, options[:object], child_index: 'NEW_RECORD') do |f| + render(partial: options[:partial], locals: { options[:form_builder_local] => f }) end - end def generate_template(form_builder, method, options = {}) @@ -54,7 +56,7 @@ def generate_template(form_builder, method, options = {}) def remove_nested(fields) out = '' out << fields.hidden_field(:_destroy) unless fields.object.new_record? - out << (link_to icon('icon-remove'), "#", :class => 'remove') + out << (link_to icon('icon-remove'), "#", class: 'remove') out.html_safe end @@ -64,7 +66,7 @@ def preference_field_tag(name, value, options) text_field_tag(name, value, preference_field_options(options)) when :boolean hidden_field_tag(name, 0) + - check_box_tag(name, 1, value, preference_field_options(options)) + check_box_tag(name, 1, value, preference_field_options(options)) when :string text_field_tag(name, value, preference_field_options(options)) when :password @@ -97,58 +99,65 @@ def preference_field_for(form, field, options) def preference_field_options(options) field_options = case options[:type] - when :integer - { :size => 10, - :class => 'input_integer' } - when :boolean - {} - when :string - { :size => 10, - :class => 'input_string fullwidth' } - when :password - { :size => 10, - :class => 'password_string fullwidth' } - when :text - { :rows => 15, - :cols => 85, - :class => 'fullwidth' } - else - { :size => 10, - :class => 'input_string fullwidth' } - end - - field_options.merge!({ - :readonly => options[:readonly], - :disabled => options[:disabled], - :size => options[:size] - }) + when :integer + { size: 10, + class: 'input_integer' } + when :boolean + {} + when :string + { size: 10, + class: 'input_string fullwidth' } + when :password + { size: 10, + class: 'password_string fullwidth' } + when :text + { rows: 15, + cols: 85, + class: 'fullwidth' } + else + { size: 10, + class: 'input_string fullwidth' } + end + + field_options.merge!( + readonly: options[:readonly], + disabled: options[:disabled], + size: options[:size] + ) end def preference_fields(object, form) return unless object.respond_to?(:preferences) + object.preferences.keys.map{ |key| form.label("preferred_#{key}", Spree.t(key) + ": ") + - preference_field_for(form, "preferred_#{key}", :type => object.preference_type(key)) + preference_field_for(form, "preferred_#{key}", type: object.preference_type(key)) }.join("
").html_safe end def link_to_add_fields(name, target, options = {}) name = '' if options[:no_text] css_classes = options[:class] ? options[:class] + " spree_add_fields" : "spree_add_fields" - link_to_with_icon('icon-plus', name, 'javascript:', :data => { :target => target }, :class => css_classes) + link_to_with_icon('icon-plus', + name, + 'javascript:', + data: { target: target }, + class: css_classes) end # renders hidden field and link to remove record using nested_attributes # add support for options[:html], allowing additional HTML attributes - def link_to_remove_fields(name, f, options = {}) + def link_to_remove_fields(name, form, options = {}) name = '' if options[:no_text] options[:class] = '' unless options[:class] options[:class] += 'no-text with-tip' if options[:no_text] - html_options = { class: "remove_fields #{options[:class]}", data: { action: 'remove' }, title: t(:remove) } + html_options = { class: "remove_fields #{options[:class]}", + data: { action: 'remove' }, + title: t(:remove) } html_options.merge!(options[:html]) if options.key? :html - link_to_with_icon('icon-trash', name, '#', html_options) + f.hidden_field(:_destroy) + link_to_with_icon('icon-trash', name, '#', html_options) + form.hidden_field(:_destroy) end def spree_dom_id(record)