{{{
Rails comes with a massive amount of helpful gems. Those gems are part of Rails’ success and they make Rails the most versatile framework in the Ruby world.
No longer shall using those gems be limited to a Rails environment! The new “Cells 3.8.5”:https://github.com/apotonick/cells allows using many gems in any project, from simple scripts to Sinatra applications, without the Rails dependency. Beside @actionpack@, no other Rails gem is required.
Let’s see how the great gem “simple_form”:https://github.com/plataformatec/simple_form can be used in a Ruby script.
h3. The Gemfile.
For demonstration purpose I want to pass a real ActiveRecord model to simple_form. That is why the @Gemfile@ might look bloated at first sight.
source :gemcutter gem "cells" , "~> 3.8.5" gem "sqlite3" gem "activerecord" gem "simple_form"
h3. The New Module.
If you’re not familiar with “Cells”:https://github.com/apotonick/cells yet, “check this post”:http://nicksda.apotomo.de/2010/11/lets-write-a-reusable-sidebar-component-in-rails-3/. The cell we’re writing is pretty straight-forward with two new lines.
require 'cell/base' require "cell/rails/helper_api" require "simple_form" class MusicianCell < Cell::Base include Cell::Rails::HelperAPI self._helpers = RoutingHelpers def show @musician = Musician.find(:first) end end
First, note that we use @Cell::Base@ to derive from since we don’t want the Rails dependency (line 5). We then include the module necessary to provide the helpers outside of Rails (line 6).
h3. How Does URLs Work?
Most gems rely on routing helpers. For instance, simple_form is using polymorphic routes helpers like @musician_path@ to compute URLs. Naturally, outside of Rails we don’t have those routes and need to provide them ourselves. This is what happens in line 8.
self._helpers = RoutingHelpers
The corresponding @RoutingHelpers@ module now is up to you.
module RoutingHelpers def musician_path(model, *args) "/musicians/#{model.id}" end end
h3. Using simple_form In The View.
The state view sitting at @musician/play.html.erb@ might use @simple_form@ now. And, hey, you are not limited to ERB. Cells comes with all the template engines that Rails supports. Use HAML if you fancy. Use Slim to loose weight. Or Whatever.
See how easy that is?
h3. The Ruby Script.
Now using all that is as simple as any other steps. This example is just a Ruby script – note that the following code could also be in a Sinatra action, a Webmachine resource, a mailer or whatever you prefer.
require 'musician_cell' MusicianCell.append_view_path(".") puts Cell::Base.render_cell_for(:musician, :play)
This is enough to render the form with simple_form.
Name
Cool, isn’t it?
h3. Using Cells Caching.
If you want to use Cells caching you just have to provide a cache store. You may use any ActiveSupport compatible cache store here, no limits!
cache = ActiveSupport::Cache::MemoryStore.new Cell::Base.render_cell_for(:musician, :play) do |c| c.cache_configured = true c.cache_store = cache end
See how we use dependency injection now to configure the cell? This makes the whole workflow completely encapsulated and clean. We might provide some utility methods in the near future to make this setup a little bit more convenient. Let me know what you need. Shap!
}}}
This is pretty awesome.
LikeLike
Thanks for these insights into life without Rails! It helps to see how Cells can be used with other apps and it demystifies the Rails framework.
LikeLike
A nice example how people use a standalone cell in celluloid, an object framework: https://github.com/francescoagati/cell-celluloid
LikeLike
Any chance you could post a trivial working sinatra application into github with cells working and wired in?
LikeLike
Fenton: Great idea! Putting it on my TODO list as I type.
LikeLike
Here is a sample Sinatra app using cells!
LikeLike
This was a damn smart article. Thanks for writing it.
LikeLike