Skip to content

Refactor bunko_collection method name collision #46

@kanejamison

Description

@kanejamison

Description

The bunko_collection macro has a confusing name collision where a class method includes a concern, then immediately calls an instance method with the exact same name. This violates the principle of least surprise and makes the code difficult to follow.

Current code

lib/bunko/controllers/acts_as.rb:8-14:

class_methods do
  def bunko_collection(collection_name, **options)
    include Bunko::Controllers::Collection
    
    bunko_collection(collection_name, **options)  # Calls instance method!
  end
end

Problems this causes

  1. Extremely confusing - same name for class and instance methods
  2. Hard to trace which method is being called
  3. Violates principle of least surprise
  4. Makes debugging difficult
  5. Could cause issues with metaprogramming or introspection
  6. New contributors will struggle to understand the flow

Pros of refactoring

  • Clear separation between macro invocation and configuration
  • Easier to understand code flow
  • Better for debugging and stack traces
  • Follows Rails naming conventions (e.g., has_many vs internal setup methods)
  • More maintainable

Cons of refactoring

  • Small internal change (not user-facing)
  • Need to choose a good alternative name

Recommended approach

Rename the instance method in the Collection concern to something like configure_bunko_collection or setup_bunko_collection:

# In acts_as.rb
class_methods do
  def bunko_collection(collection_name, **options)
    include Bunko::Controllers::Collection
    
    setup_bunko_collection(collection_name, **options)
  end
end

# In collection.rb
class_methods do
  def setup_bunko_collection(collection_name, **options)
    # existing implementation
  end
end

This makes it clear that the user-facing API is bunko_collection, while the internal setup is separate.

References

  • lib/bunko/controllers/acts_as.rb:8-14
  • lib/bunko/controllers/collection.rb:14-36

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions