Currently Rubocop can check the indentation of private, protected and public modifiers. Unfortunately it isn't able to check whether the code that follows uses the Rails convention of indenting private and protected methods with respect to private and protected. For example the style
class MyClass
def a_public_method
end
private
def a_private_method
end
end
should be supported.
The Style/AccessModifierIndentation cop could support a new EnforcedStyle called rails that would use the Rails convention.
rubocop -V returns
0.26.1 (using Parser 2.2.0.pre.4, running on ruby 2.0.0 x86_64-linux)
Isn't the Rails convention actually:
class MyClass
def a_public_method
end
private
def a_private_method
end
end
The guide isn't clear. It just says _Indent after private/protected_. I just browsed the Rails source code a little bit and it uses all three styles: indent, dedent and the one I described in the issue. An example of the last one is https://github.com/rails/rails/blob/master/activerecord/lib/active_record/aggregations.rb#L230.
Either way I'd like to enforce this style in my project and it'd be great if Rubocop gave me a hand.
I wouldn't want to call your preferred style the "rails" style for the reasons below. I can't think of a good name though. (Naming things is hard.)
Absolutely agree, @mikegee. Initially I assumed that this is the style used in Rails source code but in reality it's very inconsistent. I must admit that I don't have an idea for a nice name. double_indent, maybe?
I would like to implement this. In my style guide it may looks like:
public access modifier. Keep all public methods on top of class defenition.protected and private keywords at the same level of indentation as public methods.protected and private keywords.Example:
class Cat
def meow
puts('Meow!')
end
protected
def can_we_be_friends?(another_cat)
# some logic
end
private
def meow_at_3am?
rand < 0.8
end
end
As I understand changes could affect Cop::Style::IndentationConsistency and Cop::Style::IndentationWidth. I'm not sure where to store config for this. I think about:
Style/IndentationConsistency:
AllowProtectedAndPrivateMethodsBeIndented: true
but is it ok if I will check this config in IndentationWidth cop?
@bbatsov can you please help me? Will it be merged?
Initially I assumed that this is the style used in Rails source code but in reality it's very inconsistent.
@mikegee. I thought that Rails source code was inconsistent because they could not use Rubocop goodness to solve it :relieved:
I'm OK with adding support for the suggested style. Not sure what'd be best regarding IndentationConsistency. Guess @jonas054 will be of more help regarding this.
@jonas054 What do you think about it?
@jonas054 is on vacation. He'll be back in about a month.
Oh, ok. Thank you for response, Bozhidar @bbatsov.
On Feb 11, 2015, at 4:25 PM, Bozhidar Batsov [email protected] wrote:
@jonas054 https://github.com/jonas054 is on vacation. He'll be back in about a month.
—
Reply to this email directly or view it on GitHub https://github.com/bbatsov/rubocop/issues/1342#issuecomment-73880061.
@Proghat Go ahead and start coding if you like. I'll try to review your work if you submit a PR. Some thoughts:
Hello.
How's the hear?
This rubocop is designed for pure Ruby. Rails is a full framework and uses different style guides all together. Just use Rail generator and you will see the style is different, which will not change for 1000 years and same goes for the community. For Rails please use the gem "rubocop-rails" instead with will work with the '.rubocop.yml' file. For more visit https://github.com/bbatsov/rubocop
This rubocop is designed for pure Ruby. Rails is a full framework and uses different style guides all together. Just use Rail generator and you will see the style is different, which will not change for 1000 years and same goes for the community. For Rails please use the gem "rubocop-rails" instead with will work with the '.rubocop.yml' file. For more visit https://github.com/bbatsov/rubocop
Internally they can use whatever code style they want, but no framework defines the code style of a project.
For those who came here looking for a solution (noobs like me), just use:
Layout/IndentationConsistency:
EnforcedStyle: rails
EDIT 2019/08/01:
As @Molszew pointed out, as of Rubocop v0.72 you now have to use indented_internal_methods instead of rails. Thanks for the update.
Since Rubocop v0.72 EnforcedStyle: rails have beed renamed. Use this instead:
Layout/IndentationConsistency:
EnforcedStyle: indented_internal_methods
Most helpful comment
For those who came here looking for a solution (noobs like me), just use:
EDIT 2019/08/01:
As @Molszew pointed out, as of Rubocop v0.72 you now have to use
indented_internal_methodsinstead ofrails. Thanks for the update.