Rails 3 sample app using Cream. Note: An updated app is available as cream_app
Ruby JavaScript
Switch branches/tags
Nothing to show
Pull request Compare This branch is 2 commits ahead of yoda:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
app
config
db
doc
lib/tasks
public
script
test
vendor/plugins
.gitignore
Gemfile
Gemfile.lock
README.markdown
Rakefile
config.ru

README.markdown

Cream Rails 3 sample app

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

Status: Dec 1, 2010

Cream, CanCan-Permits and some of the other gems have since minor or major upgraded since this app was created. Please take a look at cream-app, the new Demo app for the latest version of Cream.

Most of the manual steps below are no longer necessary as the generators in the latest Cream automatically configures most if not all of this.

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"  

It should also be possible to setup using the mongoid:configure generator that comes with mongoid.

Configure gemfile

in Gemfile insert:

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

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

  # gem "mongo_mapper"

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

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 => 'kmandrup@gmail.com', :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