Sir Trevor Rails
A Rails gem for integrating the Sir Trevor JS into your Rails 3.x application.
Usage
Add Sir Trevor to your Gemfile
gem 'sir-trevor-rails'
bundle install
Require SirTrevor in your application_controller.rb
require 'sir-trevor-rails'
Include Sir Trevor in your application.css
file
*= require sir-trevor
Include Sir Trevor in your application.js
file
//= require sir-trevor
In your view file for your editable content (must be a 'text' field as we store the JSON here) here we have a field called 'content'
f.sir_trevor_text_area :content
And instantiate a new SirTrevor.Editor
instance in your Javascript.
$(function(){
var editor = new SirTrevor.Editor({ el: $('.sir-trevor-area') });
});
Or for multiple instances:
$(function(){
var instances = $('.sir-trevor-area'),
l = instances.length, instance;
while (l--) {
instance = $(instances[l]);
new SirTrevor.Editor({ el: instance });
}
});
To render your content (in your view file)
<%= render_sir_trevor(post.content) %>
There's an example Rails 3.2.7 project with all of this already done in the Sir Trevor JS repository.
Generators
Views
To grab all of the default block type partials into your application run the following generator command:
rails g sir_trevor:views
This will copy all of the SirTrevor block partials into app/views/sir-trevor/blocks/
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.
Helper methods
render_sir_trevor
Parses the blocks JSON content, loops through each piece of block content and render the appropriate partial for the block.
render_sir_trevor_image
Returns the first available SirTrevor image from the supplied JSON.
sir_trevor_image_tag
Returns an image tag from a SirTrevor Image block
pluck_sir_trevor_type
(Private)
Get the first instance of a specified SirTrevor block type from the supplied JSON
Requirements
- Rails 3.x
- jQuery
- Underscore.js (bundled)
To do
- Add tests
Licence
Sir Trevor Rails is released under the MIT Licence