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:
- Ruby on Rails 3.2.8
- Slim
- Mongoid
- Loginza
Project creation
rails new lancemine -O -G -T
-O refuse to use ActiveRecord (Mongoid instead)
-G discard git (I use Mercurial)
-T refuse tests
We rule the Gemfile by bringing 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 necessary gems and specify the libraries for JS compilation depending on the platform. therubyracer is considered faster, but there are problems with its compilation under win, so we use the Mozilla engine.
bundle
We generate config for MongoDB
rails g mongoid: config
Mercurial
If you want to use a version control system, then I recommend Mercurial. In my opinion, it makes everything the same as Git, only easier and more logical.
We need to create a .hgignore file in the root to exclude temporary and frequently changing files from under control:
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 control
-m that the commit comment is passed on the command line.
User Model
rails g model User openid_identity openid_data: hash status
This will create us a model app / models / user.rb, we will 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 AR created_at and updated_at familiar to us from AR
Projects
rails g scaffod Project title body body budget time
Add the following 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 register as such, only login via OpenID via Loginza. To do this, we will
enable 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’ll use the following link:
loginza.ru/api/widget?token_url=#{u 'http://localhost:3000/signin'}&providers_set=vkontakte,facebook,livejournal
localhost : 3000 / signin is the address where logins will redirect the user after login
providers_set - list of allowed providers for login.
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=
Here, using the net / http library, we request user data using a token that Loginza POST sent us with a request for / signin.
TODO: it is necessary to do error handling (Loginza may return an error, and 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 recorded in the session.
Otherwise, 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 the addition of its methods in 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 because I use
InheritedResources This gem takes standard CRUD actions for an object. create !, show! - this is a call to the helpers of this heme. I advise you to follow the link, there are detailed examples of how this can be used. Well, before_filter to restrict access to creating a project and editing 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 get in touch with the customer through his profile on Facebook, VKontakte or LiveJournal.
Here is a bulletin board with minimal functionality. We still need admin panel, feedback and feedback, profiles and 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
Translation of RoR documentationRuby on Rails Tutorial: Learning Rails on ExamplesInheritedResourcesMongoid