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
- Methods violate Single Responsibility Principle (doing configuration, querying, error handling, pagination all in one place)
- Hard to test individual pieces of logic in isolation
- Difficult to understand the full flow at a glance
- Makes modifications risky - changing one aspect could break another
- 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
Description
The Collection concern's
load_collectionandload_postmethods 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:Problems this causes
Pros of refactoring
Cons of refactoring
Potential approaches
The exact refactoring approach can be determined during implementation.
References
lib/bunko/controllers/collection.rb:40-75lib/bunko/controllers/collection.rb:77-110