Class: RuboCop::Cop::Locallingo::StrftimeInView

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/locallingo/strftime_in_view.rb

Overview

Enforces I18n.l(value, format: :name) instead of .strftime(...) in view files.

Hardcoded strftime patterns bypass locale-aware formatting. Use I18n.l with named formats defined in config/locales/time..yml and date..yml.

.strftime inside a value: pair is exempt: HTML datetime-local input values must follow the HTML spec, not locale display formatting.

Scope with the standard RuboCop Include/Exclude in your config:

Locallingo/StrftimeInView:
Include: [app/views/**/*.rb]
Exclude: [app/views/**/*_mailer/**/*.rb]

Examples:

# bad
@user.created_at.strftime("%B %d, %Y")

# good
I18n.l(@user.created_at, format: :long)

Constant Summary collapse

MSG =
"Use `I18n.l(value, format: :name)` instead of `.strftime(...)`. " \
"Define formats in config/locales/{time,date}.*.yml."
RESTRICT_ON_SEND =
%i[strftime].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



33
34
35
36
37
# File 'lib/rubocop/cop/locallingo/strftime_in_view.rb', line 33

def on_send(node)
  return if in_html_input_context?(node)

  add_offense(node.loc.selector)
end