Class: RuboCop::Cop::PhlexForms::LegacyFormMethod

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/phlex_forms/cop/legacy_form_method.rb

Overview

Flags Rails-style legacy form-builder methods (text_field, select, ...) in favor of the phlex-forms API. The preferred primary path is the Control-first form.field(:name, ...); the PascalCase component methods (form.Input/Select/Textarea/...) remain as escape hatches.

Examples:

# bad
form.text_field(:name, :primary)
form.select(:role, choices: roles)

# good (primary)
form.field(:name, :primary)
form.field(:role, as: :select, choices: roles)

# good (escape hatch)
form.Input(:name, :primary)
form.Select(:role, choices: roles)

Constant Summary collapse

INPUT_TYPE_METHODS =
{
  text_field: :text,
  email_field: :email,
  password_field: :password,
  number_field: :number,
  date_field: :date,
  time_field: :time,
  datetime_field: :datetime,
  url_field: :url,
  tel_field: :tel,
  search_field: :search,
  range_field: :range,
  color_field: :color
}.freeze
OTHER_LEGACY_METHODS =
{
  textarea: "Textarea",
  text_area: "Textarea",
  select: "Select",
  checkbox: "Checkbox",
  check_box: "Checkbox",
  radio_button: "Radio",
  toggle: "Toggle",
  file_field: "FileInput",
  hidden_field: "Hidden",
  form_control: "Control",
  label: "Label"
}.freeze
FORM_RECEIVERS =
%i[form f af].freeze
FORM_SUFFIX =
/_form\z/

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/rubocop/phlex_forms/cop/legacy_form_method.rb', line 56

def on_send(node)
  return unless form_receiver?(node)

  method_name = node.method_name
  return unless legacy_method?(method_name)

  add_offense(node.loc.selector, message: build_message(method_name, node))
end