Home

Awesome

Sir Trevor Rails

A Rails gem for integrating Sir Trevor JS into your Rails 3/4/5/6 application.

Upgrade guide to v0.6.0

There is a breaking change which needs to be applied if you've defined custom ruby blocks.

Create an initializer config/initializers/sir_trevor_rails.rb

Add your custom block types in the initializer:

class SirTrevorRails::Block
  def self.custom_block_types
    # Type should be string based and prefix of your class name.
    ["Custom"] # Would relate to CustomBlock
  end
end

Upgrade guide from v0.4.0-rc.1

Version 0.4.0 represents a major refactor and contains significant API changes.

Usage

Add Sir Trevor to your Gemfile

gem 'sir_trevor_rails'
bundle install

Include Sir Trevor JS in your project following the instructions set out here

Generators: Views

Grab all of the default block type partials by running the following generator command:

rails g sir_trevor_rails:views

This will copy all of the SirTrevor block partials into app/views/sir_trevor/blocks/

Rendering

In your models pass the attribute name of your Sir Trevor content to sir_trevor_content

sir_trevor_content :content

In your view files, call render to display the Sir Trevor content

<%= render @item.content %>

Handling image uploads

We don't provide a default image uploader out of the box, because everyone will have different requirements. To see an example of an image uploader, please refer to our Rails examples in the Sir Trevor JS repository.

Handling markdown

Use the sir_trevor_markdown method in your custom block partials to correctly parse the markdown in a block

Querying the block content

Use the following methods to query the Sir Trevor content

@item.content.has_block_of_type?(:image)

Does this content have an image block?

@item.content.first_block_of_type(:video)

Return the first video block in the content

Add custom block types with custom methods

Create an initializer config/initializers/sir_trevor_rails.rb

Add your custom block types in the initializer:

class SirTrevorRails::Block
  def self.custom_block_types
    # Type should be string based and prefix of your class name which would be for the following.
    # TextBlock, HeadingBlock, CustomBlock
    ["Text", "Heading", "Custom"]
  end
end

Add custom methods for block content

Create an initializer config/initializers/sir_trevor_rails.rb

Write your method in the initializer:

class SirTrevorRails::BlockArray
  def your_custom_method_here
    # some code
  end
end

Call your method on the ST content

@item.content.your_custom_method

Generators: Blocks

Run the blocks generator to create templates for your custom blocks

rails g sir_trevor_rails:block my_custom_block

This will generate an html file for rendering the block, a javascript file for the editor and a ruby block class.

Block Classes

Ruby block classes can be used like decorators for the block content. See the tweet block class for an example

Requirements

To do

Licence

Sir Trevor Rails is released under the MIT Licence

Changelog

See the CHANGELOG