Skip to content

Duplicate field names when using ActiveHash::Enum #322

@hatsu38

Description

@hatsu38

Environment

Ruby version: 3.3.4
ActiveHash version: 3.3.1

Description of the issue

When using ActiveHash::Enum in combination with ActiveHash::Base, the field_names method returns duplicate field names. This occurs only when enum_accessor is used.

Steps to reproduce

  1. Create an ActiveHash::Base class with some data
  2. Include ActiveHash::Enum
  3. Use enum_accessor
  4. Call .field_names on the class

Example code

class Sport < ActiveHash::Base
  self.data = [
    {
      key: "tennis",
      category: "ball",
    },
    {
      key: "soccer",
      category: "ball",
    },
  ]

  include ActiveHash::Enum
  enum_accessor :key
end

Sport.field_names
# => [:key, :category, :key, :category]

Expected behavior

The field_names method should return unique field names:

Sport.field_names
# => [:key, :category]

Additional information

This issue does not occur when ActiveHash::Enum is included without using enum_accessor:

class Sport < ActiveHash::Base
  self.data = [
    {
      key: "tennis",
      category: "ball",
    },
    {
      key: "soccer",
      category: "ball",
    },
  ]

  include ActiveHash::Enum
end

Sport.field_names
# => [:key, :category]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions