A developer's journey through BrowserCms.

Tuesday, June 4, 2013

Adding a custom route engine

When customizing BrowserCms, I have found it useful to separate core BrowserCms code from my own. Namespacing is a great way to accomplish that. I like grouping my custom content blocks in related sections and not simply adding yet another content block to the 'cms' namespace.



Note: the last diagram in the attachment is from the browsercms code (I forked the github repo to include this change). The other diagrams are from a BrowserCms app instance.

See the detailed diagrams for more information.

The 'block_path' route view helper

‘block_path’ is an interesting beast.  It looks like a typical view helper (i.e., _path, _url), but it introspects the content block object and creates an array of objects and values that will be used by ‘link_to’ to generate a path.  (For more details on this style of route specification, see Creating Paths and URLs From Objects, section '2.8 Creating Paths and URLs From Objects')



The engine can be the default cms engine that comes with BrowserCms, the vanilla Rails engine, or another custom engine.

The action parameter is filled with special bcms actions, like ‘:versions’.

Then using the block, objects are extracted and included in the path; these objects will be translated into keys values for tables (this is nothing new).

Resolving undefined method 'cms_number_field'

After creating a custom content block for BrowserCms, if you had specified any 'integer' type in your generate command, you might see something like this when you go to create an instance of that content:

NAMESPACE=bcms_my401k ./bin/rails g cms:content_block BcmsMy401k::Widget user_id:integer name:string description:text
For some reason, when generating content blocks, integers are mapped to ‘cms_number_field’ form helpers, but they don’t seem to exist.

To resolve this issue, we simply need to provide the missing components that BrowserCms is complaining about.
  1. include 'number_field' in the list of form helper methods generated at load time
  2. associate a partial with the new form helper


Although this solution (creating a partial) is currently identical to ‘cms_text_field’, the partial could be updated later to validate a numeric entry.

Down migration issues with custom content blocks

While running down migrations that were created as a result from generating a custom content block type, 'def change' doesn't seem to be creating a 'down' migration. Perhaps it doesn't know what to do with the call to 'create_content_table'. I am hopeful that is the problem and not something with the latest ActiveRecord code.

In any case, when a migration is complex enough that it does not know how to create a 'down' migration from the 'up' syntax in the 'def change' method block, it is required to revert back to the old style, specifying 'def self.up' and 'def self.down' method blocks: