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
In your Gemfile add the following
gem 'tb_core'Run bundle install
Copy in database migrations to your new rails project
bundle exec rake tb_core:install:migrations rake db:migrate
run a rails server instance and point your browser to /admin
Adding Apps to the Dashboard
Admin apps can be 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.('my_website.clients.my_clients')
redirect_to root_path, :notice => 'Permission Denied!'
end
Permissions are created one of two ways:
- Every dashboard app automatically generates a "Full Access" permission, with a tag of
admin.(app name).full_access. - You as the developer may append custom permissions to the
Spud::Core.permissionsarray.
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. += [
{:tag => 'my_website.profile.avatar', :name => 'Upload an avatar to my profile'}
]
// some_view.html.erb
<% if current_user.('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. += [{
:tag => 'my_website.projects.project_management',
:name => 'Manage clients and projects, but cannot delete them or view private info',
:apps => [:clients, :projects]
}]
Testing
Twice Baked uses RSpec for testing. Get the tests running with a few short commands:
Create and migrate the databases:
rake db:create rake db:migrateLoad the schema in to the test database:
rake app:db:test:prepareRun 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.