Random Things in Random Time
Rails 3 and mongodb through mongoid

I start playing with rails 3 and mongo. Found lot of articles on this theme, but only some steps works for me. Most useful is post by Nicola Racco.

I’ve created simple web app with authentication and attachments photo/video.

As js framework I use jQuery 1.4.2. For integration use smth like:

http://github.com/CodeOfficer/jquery-helpers-for-rails3

I use http://gist.github.com/399682, that found somewhere on github.

For authentication: devise

for attachements: carrierwave

ORM: mongoid

Paginations: Mongoid paginate + will_paginate for views

Here is my steps:

Install rails 3 beta 3

gem install rails --pre

Creating app

rails myproject --skip-activerecord

ensure that in config/application.rb


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

instead of:

require 'rails/all'

As ORM I choose Mongoid. Beacuse in devise 1.1.rc1 orm adapter only for mongoid and there are no adapter for mongo_mapper

gem "mongoid", "2.0.0.beta4"

Now lets start implement app logic

Authentication:

 gem 'devise', :git => 'git://github.com/plataformatec/devise.git'

I’ve installed device 1.1.rc1,

 bundle install

Next install devise

 rails generate devise_install

Rails will generate the file “config/initializers/devise.rb”, responsable of the Devise initialization. Opening the file you’ll see several options that you can configure as you like to customize the framework behavior. In this article the task is to make Devise to work with Mongoid, so we don’t care of these options. Let’s change the line:


require 'devise/orm/active_record'

in:


require 'devise/orm/mongoid'

Next steps following devise docs:

class User

  include Mongoid::Document

  include Mongoid::Timestamps

  devise :database_authenticatable, :registerable,

           :recoverable, :rememberable, :trackable#, :validatable

  has_many_related :posts

end

Pagination: 

Post.all.paginate(:page => params[:page], :per_page => 10)

for views simply use will_paginate

Attachements:

http://github.com/jnicklas/carrierwave

Uploader placed in app/uploaders

lass PhotoUploader < CarrierWave::Uploader::Base 

  include CarrierWave::Compatibility::Paperclip 

  include CarrierWave::RMagick 

  storage :file 

  def store_dir 

    ":rails_root/public/photos" 

  end 

  def paperclip_path 

    ":rails_root/public/photos/:style/:basename.:extension" 

  end 

  version :thumb do 

    process :resize_to_fit => [800,600] 

  end 

end

Model

require 'carrierwave/orm/mongoid' 

class Photo

  include Mongoid::Document 

  include Mongoid::Timestamps 

  field :title, :type => String 

  field :permalink, :type => String 

  field :content, :type => String 

  # field :hit, :type => Integer, :default => 0 

  field :photo, :type => String 

  field :photo_file_name, :type => String 

  belongs_to_related :map 

  mount_uploader :photo, PhotoUploader, :mount_on => :photo_file_name 

end

Controller

class PhotosController < ApplicationController

  def create

    photo = Photo.create(params[:photo])

    redirect_to(:back)

  end

And views:


<% form_tag videos_path, {:multipart => true} do %>

  <p>

    <label>Photo</label>

    <%= file_field_tag 'video[video]' %>

  </p>

  <%=submit_tag 'Upload'%>


<% end %>