Skip to content

Commit

Permalink
Merge pull request #894 from ghiculescu/rails-env-local
Browse files Browse the repository at this point in the history
Support `local` as an environment for `Rails/UnknownEnv` from Rails 7.1 onward.
  • Loading branch information
koic authored Oct 6, 2023
2 parents 6051d88 + 1eb9593 commit e773322
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/change_rails_unknown_env_to_add_local.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#893](https://github.com/rubocop/rubocop-rails/issues/893): Support `local` as an environment for `Rails/UnknownEnv` from Rails 7.1 onward. ([@ghiculescu][])
6 changes: 5 additions & 1 deletion lib/rubocop/cop/rails/unknown_env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ def unknown_env_name?(name)
end

def environments
cop_config['Environments']
@environments ||= begin
e = cop_config['Environments']
e += ['local'] if target_rails_version >= 7.1
e
end
end
end
end
Expand Down
37 changes: 37 additions & 0 deletions spec/rubocop/cop/rails/unknown_env_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
^^^^^^^^^^^^^ Unknown environment `developpment`. Did you mean `development`?
Rails.env.something?
^^^^^^^^^^ Unknown environment `something`.
Rails.env.local?
^^^^^^ Unknown environment `local`.
RUBY
end

Expand All @@ -36,6 +38,9 @@
'something' === Rails.env
^^^^^^^^^^^ Unknown environment `something`.
'local' === Rails.env
^^^^^^^ Unknown environment `local`.
RUBY
end
end
Expand All @@ -49,6 +54,9 @@
^^^^^^^^^^^^^ Unknown environment `developpment`.
Rails.env.something?
^^^^^^^^^^ Unknown environment `something`.
Rails.env.local?
^^^^^^ Unknown environment `local`.
RUBY
end

Expand All @@ -61,6 +69,9 @@
'something' === Rails.env
^^^^^^^^^^^ Unknown environment `something`.
'local' === Rails.env
^^^^^^^ Unknown environment `local`.
RUBY
end
end
Expand All @@ -72,4 +83,30 @@
Rails.env == 'production'
RUBY
end

context 'Rails 7.1' do
let(:config) do
RuboCop::Config.new(
{
'AllCops' => {
'TargetRailsVersion' => '7.1'
},
'Rails/UnknownEnv' => {
'Environments' => %w[
development
production
test
]
}
}
)
end

it 'accepts local as an environment name on Rails 7.1' do
expect_no_offenses(<<~RUBY)
Rails.env.local?
Rails.env == 'local'
RUBY
end
end
end

0 comments on commit e773322

Please sign in to comment.