Blocks mechanism for Rails
Add this line to your application's Gemfile:
And then execute:
$ bundle install
Once the command is done, call the following rake tasks:
$ rake selectra_blocks_engine:install:migrations $ rake db:migrate
This will create the
selectra_blocks table in database.
Selectra Blocks provides a
SelectraBlock model you can use to manage your
It also provides a convenient integration with Active Admin in which you'll find a Blocks section.
You can create a test block using the following Ruby code:
SelectraBlock.create( :text => "This is **markdown** text !", :region => "sidebar" )
Then in your Rails template :
<div id="sidebar"> <%= render_blocks_for :sidebar %> </div>
Reload the page and if you see your block, it's working.
Creating code blocks
Selectra Blocks is based on Cells. Creating a block is very similar to creating a Rails Cell.
$ mkdir app/blocks
# app/blocks/test_block_cell.rb class TestBlockCell < Cell::Rails def block render end end SelectraBlocks.register TestBlockCell
Restart your application to take account of the new block.
block method is required because it's the one called by SelectraBlocks
when rendering the blocks.
You can do what ever you do in a classic Cell except taking arguments. A block is supposed to be out of context so it should not take any page-specific argument.
Selectra Blocks defines two default regions:
You can add as many regions as you want, through configuration:
# config/initializers/selectra_blocks.rb SelectraBlocks.configure do |config| # Set a precise list of regions config.regions = [:header, :footer] # OR just add new ones config.regions += [:header, :footer] end
Once you've created regions, simply call their blocks by adding the following lines in your layout or template:
<%= render_blocks_for :header %> ... <%= render_blocks_for :footer %>
You can filter the page on which a block appears. The filters are regular expression tested against the current page URL.
For instance, to show a block only on the
/hello page, your filter would look
Be careful though that if you visit
/hello?arg1=val1 the block will not be
shown. To prevent this behavior, use the following filter:
You can specify several filters by separating them with a new line (\n)