Skip to content

Refactor hardcoded Post model to support acts_as_bunko_post on custom models #44

@kanejamison

Description

@kanejamison

Description

The Collection concern has a post_model method that's hardcoded to return Post, which blocks the ability to use acts_as_bunko_post on models with different names (see #40). This creates a false abstraction and prevents the flexibility the concern pattern is designed to provide.

Current code

lib/bunko/controllers/collection.rb:112-114:

def post_model
  @post_model ||= Post
end

Problems this causes

  1. Blocks testing/using acts_as_bunko_post on models other than Post (Confirm acts_as_post and acts_as_post_type behavior on other models #40)
  2. Creates false expectation of configurability (method exists but always returns same value)
  3. Users can't use a different model name if they already have a Post model
  4. Violates the concern pattern's goal of being reusable across models

Pros of refactoring

Cons of refactoring

  • Adds complexity to the bunko_collection macro
  • Need to decide on default behavior (Post vs require explicit configuration)
  • May require documentation updates
  • Need to handle model name validation/existence

Recommended approach

Make the model configurable via the bunko_collection macro with a sensible default:

# In controller
bunko_collection :blog, model: "Article"  # Optional, defaults to "Post"

# In Collection concern
class_attribute :bunko_post_model_name, default: "Post"

def bunko_collection(collection_name, model: "Post", **options)
  self.bunko_post_model_name = model
  # ... rest of setup
end

def post_model
  @post_model ||= bunko_post_model_name.constantize
end

This maintains backward compatibility (defaults to Post) while enabling custom models.

Related issues

References

  • lib/bunko/controllers/collection.rb:112-114

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