Home > cream-rails3-app

cream-rails3-app

Cream-rails3-app is a project mainly written in ..., it's free.

Rails 3 sample app using Cream for Session, Roles and Permission system config

Cream Rails 3 sample app

This app will be the template for the design of the Cream config generator.

Status: Sept. 25, 2010

The gems which this project rely on have all been updated and released. So it should work!

Steps

These are the steps to be taken to get a Cream enabled app up and running with Mongo Mapper. I hope my coming generator will minimize the amount of manual steps a great deal!

You are most welcome to join in the effort ;)

Setup Rails 3 for Mongo DB

Either: Create new app

rails my_app --skip-activerecord

Or in existing app, edit 'config/application.rb'

  # require 'rails/all'  

  require "action_controller/railtie"
  require "action_mailer/railtie"
  require "active_resource/railtie"
  require "rails/test_unit/railtie"  

Configure gemfile

in Gemfile insert:

  gem 'cream',        '~> 0.5.5' 
  gem 'devise',       '~> 1.2.0' 

  # use mongo mapper version of devise
  gem 'mm-devise',    '~> 1.1.3'

  # gem "mongo_mapper"

  # use mongo mapper version of roles
  gem 'roles_mongo_mapper',    '~> 0.1.2'

Install devise

rails g devise:install

Add to application.rb
config.action_mailer.default_url_options = { :host => 'localhost:3000' }

If no Welcome controller, create one with an 'index' view.

  get :to => "welcome#index"
  root :to => "welcome#index"  

Generate Welcome controller with index action and index view

rails g controller Welcome index

In 'app/views/layouts/application.html.erb'

Insert after opening :body element

  

<%= notice %>

<%= alert %>

Create Devise views for customizing Session forms and such (optional):

rails g devise:views

Edit any of the view files (optional)

Create devise user

Note ORM argument optional as it will use the correct ORM if configured correctly in the devise initializer!

Inside 'config/initializers/devise.rb' change to: require 'devise/orm/mongo_mapper'

Create devise User for Mongo Mapper rails g mongo_mapper:devise User

Permits

Configure default permits

rails g permits

Configure Rails to autoload permits files

Inside config/application.rb config.autoload_paths += %W(#{Rails.root}/app/permits)

Configure and generate roles

Add :admin_flag role strategy to User model

rails g mongo_mapper:roles User --strategy admin_flag

rails s

Module RoleStrategy::MongoMapper::RoleString has not been registered

Add to user at the top:

  use_roles_strategy :admin_flag

Note: Should this be added (as an option?) to the Roles generator?

Add Devise protection

In Welcome controller:

before_filter :authenticate_user!

Add Default user (optional)

Edit 'db/seeds.rb'

  User.delete_all
  user = User.create(:email => '[email protected]', :password => '123456', :password_confirmation => '123456')  

Create 'lib/tasks/db.rake'

  namespace :db do
    task :seed => :environment do
      load "#{Rails.root}/db/seeds.rb"
    end
  end  

rake db:seed

Test application

Start web server rails s

In browser go to: localhost:3000/welcome/index

This should redirect to the Devise 'Sign up' form :)

Scaffold some app code

Now off to test the Cream functionality

Scaffold a Blog model using the mongo_mapper:model generator from rails3-generators (included by mm-devise) A Blog consists of Posts, with an :index action listing all blog Posts, and :show to show an individual blog Post rails g scaffold Post title:string body:string --orm mongo_mapper

Inside 'app/views/posts/index.erb.html':

  <%= show_link post %>
  <%= edit_link post %>
  <%= delete_link post %>

Now see if the permissions work

Previous:ut-lab