Skip to content

Commit

Permalink
Fix rubocop issues in spree/admin/base_helper
Browse files Browse the repository at this point in the history
  • Loading branch information
luisramos0 committed Dec 16, 2019
1 parent e5d9e1c commit db88455
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 39 deletions.
4 changes: 4 additions & 0 deletions .rubocop_manual_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
87 changes: 48 additions & 39 deletions app/helpers/spree/admin/base_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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('<br />').html_safe
content_tag(:span, errors, :class => 'formError')
content_tag(:span, errors, class: 'formError')
else
''
end
Expand All @@ -32,7 +35,7 @@ def error_message_on(object, method, options = {})
# <%= form_for @project do |project_form| %>
# <div id="tasks">
# <%= project_form.fields_for :tasks do |task_form| %>
# <%= render :partial => 'task', :locals => { :f => task_form } %>
# <%= render partial: 'task', locals: { f: task_form } %>
# <% end %>
# </div>
# <% end %>
Expand All @@ -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 = {})
Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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("<br />").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)
Expand Down

0 comments on commit db88455

Please sign in to comment.