How to customize post type routes

Sometimes is necessary to use custom url's for post types (content groups), these are the steps to customize:

  1. Append custom url support in your config/route.rb
    scope PluginRoutes.system_info["relative_url_root"], as: "cama" do
        scope "(:locale)", locale: /#{PluginRoutes.all_locales}/, :defaults => {  } do
          controller "camaleon_cms/frontend" do
            CamaleonCms::PostType.decorate.each do |ptype|
              get "/#{ptype.the_title.parameterize}" => :post_type, defaults: {post_type_id: ptype.id}, as: "custom_post_type_#{ptype.id}"
            end
          end
        end
      end                    
  2. Create a new initializer file "config/initializers/custom_post_type_route.rb" to customize the_url method for post_types and put this code.
    Rails.application.config.to_prepare do
      CamaleonCms::PostTypeDecorator.class_eval do
        def the_url(*args)
          args = args.extract_options!
          args[:locale] = get_locale unless args.include?(:locale)
          args[:format] = "html"
          h.cama_url_to_fixed("cama_custom_post_type_#{the_id}_#{args.delete(:as_path).present? ? "path" : "url"}", args)
        end
      end
    end
  3. restart server
    Note: You will need to restart your server after you created a new post_type (production env)

Screenshots:

Route:


Initializer:

Created at: 24 Nov 08:34 | Updated at: 20 Dec 13:15