Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
A managable block system for Rails based on Cells
Ruby
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
app
lib
.gitignore
Gemfile
MIT-LICENSE
README.md
Rakefile
selectra_blocks.gemspec

README.md

Selectra Blocks

Blocks mechanism for Rails

Installation

Add this line to your application's Gemfile:

gem 'selectra_blocks'

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.

Usage

Selectra Blocks provides a SelectraBlock model you can use to manage your blocks.

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.

The 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.

Regions

Selectra Blocks defines two default regions: after_content and sidebar.

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 %>

Filtering

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 like this:

^/hello$

Be careful though that if you visit /hello?arg1=val1 the block will not be shown. To prevent this behavior, use the following filter:

^/hello(\?.*)?$

You can specify several filters by separating them with a new line (\n)

Something went wrong with that request. Please try again.