Skip to content

Instantly share code, notes, and snippets.

@lin
Last active August 29, 2015 14:08
Show Gist options
  • Save lin/11ee1a2591a9b490d24a to your computer and use it in GitHub Desktop.
Save lin/11ee1a2591a9b490d24a to your computer and use it in GitHub Desktop.
Installation of a Rails app with mongoid, devise and activeadmin

1, a new rails app without active record

rails new template --skip-active-record

If you haven't installed mongoid, you should follow:

gem update --system
gem install mongo
gem install bson
gem install bson_ext

Official instruction: Mongo for Ruby

2, add mongoid and bson

add those two lines to Gemfile

# Use mongoid as the database solution
gem "mongoid", "~> 4.0.0"
gem 'bson', '~> 2.3.0'

bundle install

bundle install
rails g mongoid:config

1, install devise gem

# Use devise as the user management tool
gem 'devise'
bundle install
rails g devise:install

2, open "config/environments/development.rb" add folowing codes before "end"

config.action_mailer.default_url_options = { :host => 'localhost:3000' }

3, a user model based on devise

rails g devise user

4, if you want to customize views,

rails g devise:views users

5, if you want to customize controllers,

rails generate devise:controllers users

If you want you can change your routes as:

# users routes from devise
devise_for :users, 
           :path => "",
           :controllers => { :registrations => 'users/registrations',
                             :sessions      => 'users/sessions', 
                             :passwords     => 'users/passwords' },
           :path_names  => { sign_in:   'login', 
                             password:  'forgot_password', 
                             sign_up:   'register', 
                             sign_out:  'logout' }

For more:

http://guides.railsgirls.com/devise/

1, You can add these two lines in any model after include Mongoid::Document

include Mongoid::Timestamps::Created
include Mongoid::Timestamps::Updated

2, mongoid json builder (at views/items/show.json.jbuilder)

json.id @item._id.to_s
json.extract! @item, :name, :price, :type, :desc, :date

For more:

Mongoid Docs Rails command line

# admin page
gem 'ransack', github: 'activerecord-hackery/ransack'
gem 'activeadmin', github: 'activeadmin'
gem 'activeadmin-mongoid', github: 'Zhomart/activeadmin-mongoid', branch: 'ransack'
rails g active_admin:install User

Then restart the server and goto http://localhost:3000/admin

Go to rails console

rails c

Create a new admin user

User.create :email => 'admin@example.com', :password => 'password', :password_confirmation => 'password'

Include a model into admin page

rails generate active_admin:resource MyModel

Then, open app/admin/my_model.rb, and set up the ui like,

ActiveAdmin.register Item do

  permit_params :name, :price

  index do
    selectable_column
    id_column
    column :name
    column :price
    actions
  end

  form do |f|
    f.inputs "Items Details" do
      f.input :name
      f.input :price
    end
    f.actions
  end

end

1, To add new css/js, you should open /config/initializers/assets.rb and add

(This will compile all file end with scss and css, but ignore partial scss like _header.scss)

Rails.application.config.assets.precompile  << Proc.new do |path|
  # path is something like 'modules/_buttons.css' or 'stylesheets/posts.css'
  # is subpath under app/assets or vendor assets
  if path =~ /\.(css|js)\z/

    # full_path is something like '/Users/albert/Documents/Projects/philly/app/assets/sass/modules/_utilities.scss'
    # is the absolute path of a file
    full_path = Rails.application.assets.resolve(path).to_path

    # if path is 'modules/_buttons.css', then sub_path is '_buttons.css'
    sub_path = path.split('/')[-1]

    # include paths that is in following array
    asset_paths = %w( app/assets vendor/assets lib/assets )
    if (asset_paths.any? {|ap| full_path.include? ap}) && (!sub_path.starts_with? '_')
      true
    else
      false
    end
  else
    false
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment