The simplest free-lance board on Ruby on Rails
- Tutorial
- Recovery mode
I assume that you are already minimally familiar with RoR and tried to do something on it. This material will be interesting for the simplest user authentication through OpenID, as well as for those who have long wanted to try MongoDB, but did not know where to start.
We will use:
-O refuse to use ActiveRecord (Mongoid instead of it)
-G refuse to hit (I use Mercurial)
-T refuse tests
We correct the Gemfile, leading it to this form:
Here we add the gems we need and specify the libraries for compiling JS depending on the platform. therubyracer is considered faster, but there are problems with compiling it under win, so we use the Mozilla engine.
We generate a config for MongoDB
rails g mongoid: config
If you want to use a version control system, I recommend Mercurial. In my opinion, it does everything the same as Git, only easier and more logical.
We will need to create a .hgignore file in the root in order to exclude from control temporary and frequently changing files:
Then execute the commands:
-A adds all new files under the control
-m that the comment on the commit is sent on the command line.
rails g model User openid_identity openid_data: hash status
This will create the app / models / user.rb model, bring it to the form:
include Mongoid :: Timestamps adds familiar to us from AR created_at and updated_at
rails g scaffod Project title body budget time
Add lines to the model:
I also made changes to the views, they can be viewed in the repository.
We will not have registration as such; login only through OpenID through Loginza. To do this, we will connect the script at loginza.ru/js/widget.js .
app / views / layouts / application.html.slim:
And to call the widget we will use this link:
localhost : 3000 / signin - this is the address where the login will redirect the user after login
providers_set - a list of allowed providers to enter.
Controller for processing:
rails g controller sessions:
Here we are using the net / http library to request user information using the token that Loginza POST passed to us with the / signin request. TODO: it is necessary to do error handling (Loginza may return an error, but here it is not taken into account).
Then the user with the corresponding OpenID is in the database or a new one is created, and the user id is written into the session.
The rest of the session mechanism is identical to that described in the Rails Tutorial. I advise you to pay attention to the sessions_helper.rb file and to add its methods to application_controller.rb
This code is interesting in that I use InheritedResources. This gem takes on standard CRUD actions for the object. create !, show! - This is a call to the helpers of this gem. I advise you to follow the link, there are detailed examples of how this can be used. Well, before_filter to limit access to create a project and edit it.
In app / views / projects / show.html.slim I display the author’s OpenID
Here we have such a message board with minimal functionality. We still need the admin panel, feedback and reviews, profiles and much much more. But this may well be the first small step for a large company.
I omitted some edits that I thought were not significant, so you may need the bitbucket.org/nleo/lancemine repository .
Translating the RoR documentation
Ruby on Rails Tutorial: Learning Rails with
InheritedResources Mongoid Examples
We will use:
- Ruby on Rails 3.2.8
- Slim
- Mongoid
- Loginza
Project creation
rails new lancemine -O -G -T
-O refuse to use ActiveRecord (Mongoid instead of it)
-G refuse to hit (I use Mercurial)
-T refuse tests
We correct the Gemfile, leading it to this form:
source 'https://rubygems.org'
gem 'rails', '3.2.8'
gem 'inherited_resources'
gem 'slim-rails'
gem 'mongoid'
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'therubyrhino', :platforms => :mswin
gem 'therubyracer', :platforms => :ruby
gem 'execjs'
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
Here we add the gems we need and specify the libraries for compiling JS depending on the platform. therubyracer is considered faster, but there are problems with compiling it under win, so we use the Mozilla engine.
bundle
We generate a config for MongoDB
rails g mongoid: config
Mercurial
If you want to use a version control system, I recommend Mercurial. In my opinion, it does everything the same as Git, only easier and more logical.
We will need to create a .hgignore file in the root in order to exclude from control temporary and frequently changing files:
db/schema.rb
config/mongoid.yml
.bundle
tmp
Gemfile.lock
syntax: glob
db/*.sqlite3
log/*.log
public/uploads/*
public/assets/*
*.swp
*.orig
*~
Then execute the commands:
hg init
hg ci -Am "Init"
-A adds all new files under the control
-m that the comment on the commit is sent on the command line.
User model
rails g model User openid_identity openid_data: hash status
This will create the app / models / user.rb model, bring it to the form:
class User
include Mongoid::Document
include Mongoid::Timestamps
field :openid_identity, type: String
field :openid_data, type: Hash
field :status, type: String
has_many :projects
end
include Mongoid :: Timestamps adds familiar to us from AR created_at and updated_at
Projects
rails g scaffod Project title body budget time
Add lines to the model:
include Mongoid::Timestamps
belongs_to :user
I also made changes to the views, they can be viewed in the repository.
Sessions
We will not have registration as such; login only through OpenID through Loginza. To do this, we will connect the script at loginza.ru/js/widget.js .
app / views / layouts / application.html.slim:
doctype html
html
head
title Lancemine
= stylesheet_link_tag "application", :media => "all"
= javascript_include_tag "application"
script src="http://loginza.ru/js/widget.js" type="text/javascript"
And to call the widget we will use this link:
loginza.ru/api/widget?token_url=#{u 'http://localhost:3000/signin'}&providers_set=vkontakte,facebook,livejournal
localhost : 3000 / signin - this is the address where the login will redirect the user after login
providers_set - a list of allowed providers to enter.
Controller for processing:
rails g controller sessions:
class SessionsController < ApplicationController
require 'net/http'
require 'json'
def create
openid_data = params[:token]
openid_data = Net::HTTP.get(URI.parse("http://loginza.ru/api/authinfo?token=#{params[:token]}"))
openid_data = JSON.parse openid_data
user = User.find_or_create_by(openid_identity: openid_data['identity'])
if user.status != 'banned'
user.openid_data = openid_data
user.save
session[:user_id] = user.id
redirect_to root_url, :notice => "Logged in!"
else
redirect_to root_url, :notice => "You blocked!"
end
end
def destroy
session[:user_id] = nil
redirect_to root_url, :notice => "Logged out!"
end
end
Here we are using the net / http library to request user information using the token that Loginza POST passed to us with the / signin request. TODO: it is necessary to do error handling (Loginza may return an error, but here it is not taken into account).
Then the user with the corresponding OpenID is in the database or a new one is created, and the user id is written into the session.
The rest of the session mechanism is identical to that described in the Rails Tutorial. I advise you to pay attention to the sessions_helper.rb file and to add its methods to application_controller.rb
Project controller
class ProjectsController < InheritedResources::Base
before_filter :signed_in_user, except: [:show, :index]
before_filter :correct_user, only: [:update, :edit, :destroy]
def show
@project = Project.find params[:id]
@author = @project.user.id == current_user.id if signed_in?
show!
end
def create
@project = Project.new params[:project]
@project.user = current_user
create!
end
def index
@projects = Project.order_by(:created_at.desc)
end
private
def correct_user
@project = current_user.projects.find(params[:id])
redirect_to root_url if @project.nil?
end
end
This code is interesting in that I use InheritedResources. This gem takes on standard CRUD actions for the object. create !, show! - This is a call to the helpers of this gem. I advise you to follow the link, there are detailed examples of how this can be used. Well, before_filter to limit access to create a project and edit it.
In app / views / projects / show.html.slim I display the author’s OpenID
= link_to @project.user.openid_identity, @project.user.openid_identity
Accordingly, if the artist is interested in the project, he can contact the customer through his profile on Facebook, VKontakte or LiveJournal. Here we have such a message board with minimal functionality. We still need the admin panel, feedback and reviews, profiles and much much more. But this may well be the first small step for a large company.
I omitted some edits that I thought were not significant, so you may need the bitbucket.org/nleo/lancemine repository .
Translating the RoR documentation
Ruby on Rails Tutorial: Learning Rails with
InheritedResources Mongoid Examples