Twice Baked Core

TB Core is the base Twice Baked engine that provides user authentication, roles, and an admin dashboard upon which CRUD apps can be built. Use TB Core to power content-managed websites or as a quick-and-simple way to add a backend to your existing data models.

Installation/Usage

  1. In your Gemfile add the following

    gem 'tb_core'
    
  2. Run bundle install

  3. Run the setup generator to copy in the base templates and migrations

    rails g spud:setup
    
  4. run a rails server instance and point your browser to /admin

Setup Tasks

The spud:setup generator takes care of a few tasks for you. If you missed that step or would like to complete them manually, they are listed below:

  1. Your base ApplicationController should extend from Spud::ApplicationController.
  2. Your base application.html.erb should include a few special head tags (see generator template for example).
  3. You should copy in the base migrations with rake railties:install:migrations.

Adding Apps to the Dashboard

The fastest way to add an app to the dashboard is via the geneator. The generator will take care of creating a model as well as a set of controllers/views.

rails g spud:module Cars make:string model:string year:integer classic:boolean notes:text

Admin apps are added via the Spud::Core.config.admin_applications array. We recommend you perform this action in either an initializer or within your application.rb file.

Spud::Core.config.admin_applications += [{
  :name => "Clients",
  :thumbnail => "spud/admin/clients.png",
  :url => "/admin/clients"
}]

Build out your RESTful controller and views like you normally would in any Rails app. Extend your controller from Admin::ApplicationController in order to inherit the default admin behaviors, and look at the core admin controllers for example implementations.

Roles & Permissions

A user can be assigned to a role, which itself has one or more permissions. These permissions serve two distinct purposes: Determining what dashboard apps a user has access to (if any), and being used within your application logic to establish fine-grained user abilities.

Checking permissions is easy. Each permission has a tag which is a unique, namespaced identifier. Use the has_permission? and has_any_permission? methods on the user object to check for their existence. Keep in mind that these calls will always return true for users who have the super_admin flag set.

if !@spud_user.has_permission?('my_website.clients.my_clients')
  redirect_to root_path, :notice => 'Permission Denied!'
end

Permissions are created one of two ways:

  1. Every dashboard app automatically generates a "Full Access" permission, with a tag of admin.(app name).full_access.
  2. You as the developer may append custom permissions to the Spud::Core.permissions array.

Create custom permissions whenever you need to permit an action that falls outside of the standard "full access to an app" use case; For example, turning on/off the ability for a user to upload an avatar.

// application.rb
Spud::Core.permissions += [
  {:tag => 'my_website.profile.avatar', :name => 'Upload an avatar to my profile'}
]
// some_view.html.erb
<% if current_user.has_permission?('my_website.profile.avatar') %>
  <%= link_to 'Upload Avatar', upload_avatar_path %>
<% end %>

Finally, custom permissions may optionally be tied to one or more dashboard apps. A user who has the permission shown below would have access to the the Clients and Projects dashboard apps. After that is is up to you to code your view and controller logic in accorance to what permissions the user has.

// application.rb
Spud::Core.permissions += [{
  :tag => 'my_website.projects.project_management',
  :name => 'Manage clients and projects, but cannot delete them or view private info',
  :apps => [:clients, :projects]
}]  

404 Handling

The base Spud::ApplicationController will automatically rescue from any Spud::NotFoundError errors by rendering out the template specified in Spud::Core.not_found_template. The default tempalte is layouts/not_found.html. To customize this view, create a template by that name in your own project.

When building your own apps you may raise a Spud::NotFoundError at any time to halt further execution and cause the 404 page to render. For example:

class CarsController
  before_filter :get_record
  # ... actions here
  def get_record
    @car = Car.where(:id => params[:id]).first
    if @car.blank?
      raise Spud::NotFoundError.new(:item => 'car')
    end
  end
end

Testing

Twice Baked uses RSpec, Capybara and Jasmine for testing. Get the tests running with a few short commands:

  1. Create and migrate the databases:

    rake db:create
    rake db:migrate
    
  2. Load the schema in to the test database:

    rake app:db:test:prepare
    
  3. Run the tests with RSpec

    rspec spec
    

After the tests have completed the current code coverage stats is available by opening /coverage/index.html in a browser.

Jasmine

For running the javascript unit tets:

    rake jasmine:ci