Skip to content

Refactor large private methods in Collection concern #51

@kanejamison

Description

@kanejamison

Description

The Collection concern's load_collection and load_post methods are quite large (40+ lines each) and handle multiple responsibilities including configuration lookup, database queries, error handling, pagination, and ordering. Breaking these down would improve testability and maintainability.

Current code

lib/bunko/controllers/collection.rb:40-75, 77-110:

def load_collection
  @collection_name = bunko_collection_name

  # Smart lookup: Check PostType first, then Collection
  post_type_config = Bunko.configuration.find_post_type(@collection_name)
  collection_config = Bunko.configuration.find_collection(@collection_name)
  
  # ... 40+ more lines of configuration lookup, querying, ordering, pagination
end

def load_post
  # ... similar pattern, 33+ lines
end

Problems this causes

  1. Methods violate Single Responsibility Principle (doing configuration, querying, error handling, pagination all in one place)
  2. Hard to test individual pieces of logic in isolation
  3. Difficult to understand the full flow at a glance
  4. Makes modifications risky - changing one aspect could break another
  5. Logic is locked inside the concern - can't reuse elsewhere

Pros of refactoring

  • Easier to test each piece independently
  • More maintainable - changes are localized
  • Clearer what each piece is responsible for
  • Could enable reusing logic outside controller context
  • Easier for new contributors to understand

Cons of refactoring

  • May add more complexity through additional abstractions
  • Need to decide on the right boundaries
  • Could feel like over-engineering

Potential approaches

  • Extract smaller private methods for each responsibility
  • Move query building logic to model scopes or query objects
  • Separate configuration lookup from data fetching
  • Consider extracting pagination/ordering to separate concerns

The exact refactoring approach can be determined during implementation.

References

  • lib/bunko/controllers/collection.rb:40-75
  • lib/bunko/controllers/collection.rb:77-110

Metadata

Metadata

Assignees

No one assigned

    Labels

    refactorStuff we should improve

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions