Module: Bunko::Routing::MapperMethods

Defined in:
lib/bunko/routing/mapper_methods.rb

Instance Method Summary collapse

Instance Method Details

#bunko_collection(collection_name, **options) ⇒ Object

Defines routes for a Bunko collection

Examples:

Basic usage

bunko_collection :blog
# Generates: /blog -> blog#index, /blog/:slug -> blog#show

Custom path

bunko_collection :case_study, path: "case-studies"
# Generates: /case-studies -> case_study#index, /case-studies/:slug -> case_study#show

Custom controller

bunko_collection :blog, controller: "articles"
# Generates: /blog -> articles#index, /blog/:slug -> articles#show

Parameters:

  • collection_name (Symbol)

    The name identifier for the collection (e.g., :blog, :case_study)

  • options (Hash)

    Routing options

Options Hash (**options):

  • :path (String)

    Custom URL path (default: name with hyphens)

  • :controller (String)

    Custom controller name (default: name)

  • :only (Array<Symbol>)

    Actions to route (default: [:index, :show])



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/bunko/routing/mapper_methods.rb', line 26

def bunko_collection(collection_name, **options)
  # Extract options with defaults
  custom_path = options.delete(:path)
  controller = options.delete(:controller) || collection_name.to_s

  # Smart detection: Collections (multi-type aggregations) only get index routes
  # PostTypes get both index and show routes
  collection_config = Bunko.configuration.find_collection(collection_name.to_s)

  # Default actions: Collections get [:index], PostTypes get [:index, :show]
  # Users can override with :only option
  default_actions = collection_config ? [:index] : [:index, :show]
  actions = options.delete(:only) || default_actions

  # Resource name must use underscores (for path helpers)
  # Path can use hyphens (for URLs)
  if custom_path
    # User provided custom path - use it for URLs, underscored version for resource name
    resource_name = custom_path.to_s.tr("-", "_").to_sym
    path_value = custom_path
  else
    # No custom path - use collection_name for resource name, hyphenate for path
    resource_name = collection_name
    path_value = collection_name.to_s.dasherize
  end

  # Define the routes
  resources resource_name,
    controller: controller,
    path: path_value,
    only: actions,
    param: :slug,
    **options
end

#bunko_page(page_name, **options) ⇒ Object

Defines a route for a standalone Bunko page

Examples:

Basic usage

bunko_page :about
# Generates: GET /about -> pages#show with params[:page] = "about"

Custom path

bunko_page :about, path: "about-us"
# Generates: GET /about-us -> pages#show with params[:page] = "about"

Custom controller

bunko_page :contact, controller: "static_pages"
# Generates: GET /contact -> static_pages#show with params[:page] = "contact"

Parameters:

  • page_name (Symbol)

    The name identifier for the page (e.g., :about, :contact)

  • options (Hash)

    Routing options

Options Hash (**options):

  • :path (String)

    Custom URL path (default: name with hyphens)

  • :controller (String)

    Custom controller name (default: "pages")



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/bunko/routing/mapper_methods.rb', line 80

def bunko_page(page_name, **options)
  # Extract options with defaults
  custom_path = options.delete(:path)
  controller = options.delete(:controller) || "pages"

  # Convert to underscores for Ruby conventions (route name, helpers)
  slug = page_name.to_s.underscore

  # URL path uses hyphens (Rails convention)
  path_value = custom_path || slug.dasherize

  # Route name uses underscores for path helpers (e.g., about_path)
  route_name = slug.to_sym

  # Define single GET route
  # Pass hyphenated slug to match Post.slug format in database
  get path_value,
    to: "#{controller}#show",
    defaults: {page: slug.dasherize},
    as: route_name
end