Rubocop. Align the elements of a hash literal if they span more than one line

13.3k views Asked by At

I have some code

# Filters
filter :name
filter :email
filter :organization_status, label: 'Status'
filter :subscriptions_subscription_status_id,
       as: :select,
       label: 'Subscription Status',
  collection: proc do
    Organization
      .includes(subscriptions: [:subscription_status])
      .map(&:subscriptions)
      .flatten
      .map(&:subscription_status)
      .uniq
  end

Rubocop says: Align the elements of a hash literal if they span more than one line.

collection: proc do
^^^^^^^^^^^^^^^^

What can I do with it?

4

There are 4 answers

2
Bob Mazanec On BEST ANSWER

I align :s and wrap the sections in

# rubocop:disable AlignHash
# rubocop:enable AlignHash

(cf https://github.com/bbatsov/rubocop#disabling-cops-within-source-code)

With your code:

  # Filters
  filter :name
  filter :email
  filter :organization_status, label: 'Status'
  # rubocop:disable AlignHash
  filter :subscriptions_subscription_status_id,
         as: :select,
      label: 'Subscription Status',
 collection: proc do
   Organization
     .includes(subscriptions: [:subscription_status])
     .map(&:subscriptions)
     .flatten
     .map(&:subscription_status)
     .uniq
 end
  # rubocop:enable AlignHash
0
Bob Mazanec On

You can also disable it in your .rubocop.yml configuration:

Style/Encoding:
  Enabled: false
1
Dorian On

In your .rubocop.yml, add:

Style/AlignHash:
  Enabled: false
0
sofs1 On

In RubyMine or IntelliJ, If you follow the solution suggested by the IDE, that fixes it. Basically the IDE aligns the code for you.