Browse files

add loging and localization

  • Loading branch information...
1 parent 9f4afdf commit 9c7ed858d2f46a3533342c043968faa7b868e027 @Alkagar committed Sep 1, 2012
View
21 app/controllers/application_controller.rb
@@ -1,10 +1,21 @@
class ApplicationController < ActionController::Base
- protect_from_forgery
+ protect_from_forgery
- def index
- end
+ before_filter :set_locale
- def help
- end
+ def set_locale
+ I18n.locale = params[:locale] || I18n.default_locale
+ end
+
+ def default_url_options(options={})
+ logger.debug "default_url_options is passed options: #{options.inspect}\n"
+ { :locale => I18n.locale }
+ end
+
+ def index
+ end
+
+ def help
+ end
end
View
46 app/controllers/users_controller.rb
@@ -1,21 +1,37 @@
class UsersController < ApplicationController
- def login
- end
+ def login
+ if !session[:current_user].nil?
+ redirect_to(root_path)
+ end
+ if request.post?
+ @user = User.authenticate(params[:user][:login], params[:user][:password])
+ if @user.nil?
+ flash.now[:error] = 'You need to log in!'
+ else
+ session[:current_user] = @user
+ redirect_to(root_path)
+ end
+ end
+ @user = User.new
+ end
- # here show form
- def new
- @user = User.new
- end
+ # here show form
+ def new
+ @user = User.new
+ end
- # here add user to db
- def create
- @user = User.new params[:user]
- if @user.valid?
- redirect_to({:action => 'login'}) if @user.save
- end
- end
+ # here add user to db
+ def create
+ @user = User.new params[:user]
+ if @user.valid? :on => :create
+ flash[:notice] = 'Account created, now you can login using your password.'
+ redirect_to(login_users_path) if @user.save
+ end
+ end
- def logout
- end
+ def logout
+ session[:current_user] = nil
+ redirect_to(root_path)
+ end
end
View
30 app/models/user.rb
@@ -1,27 +1,27 @@
class User < ActiveRecord::Base
attr_accessible :email, :login, :name, :password, :password_confirmation, :autologin
- validates :name, :password, :email, :presence => true
- validates :name, :length => { :in => 5..40 }
- validates :login, :length => { :in => 5..40 }
- validates :password, :length => { :in => 8..40 }, :confirmation => true
+ validates :name, :length => { :in => 5..40 }, :presence => true
+
+ validates :login, :length => { :in => 5..40 }, :presence => true
+
+ validates :password, :length => { :in => 8..40 }, :presence => true
+ validates :password, :confirmation => true
validates :password_confirmation, :presence => true
- validates :email, :uniqueness => true
- validates :email, :format => { :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i }
+
+ validates :email, :format => { :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i }, :uniqueness => true, :presence => true
+
def self.encrypt(password)
Digest::SHA1.hexdigest(password)
end
- def hashed_password=(password)
- self.password = User.encrypt(password)
- end
-
def self.authenticate(login, password)
- user = find(:first, :conditions=>["login = ?", login])
- return nil if user.nil?
- return user if User.encrypt(password) == user.password
- nil
+ user = User.where(:login => login, :password => password)
+ if user.nil?
+ return user
+ else
+ return user.first
+ end
end
end
-
View
7 app/views/application/index.html.erb
@@ -1,7 +1,2 @@
<h2>hello ruby dude!</h2>
-You can
-<%= link_to 'login', url_for(:action => 'login', :controller => 'users') %>
-,
-<%= link_to 'create', :new_user %>
-account or read
-<%= link_to 'help', url_for(:action => 'help', :controller => 'application') %>
+
View
3 app/views/layouts/_flash.html.erb
@@ -0,0 +1,3 @@
+<% if flash[:notice] %>
+ <div class='notice'> <%= flash[:notice] %> </div>
+<% end %>
View
17 app/views/layouts/application.html.erb
@@ -7,8 +7,21 @@
<%= csrf_meta_tags %>
</head>
<body>
-
+ <div>
+ <%= link_to t(:general_home), root_path %>,
+ <% if session[:current_user].nil? %>
+ <%= link_to t(:general_login), login_users_path %>,
+ <%= link_to t(:general_create), new_user_path %>,
+ <% else %>
+ <%= link_to "#{session[:current_user].name} (#{t(:general_logout)})", logout_users_path %>,
+ <% end %>
+ <%= link_to t(:general_help), help_path %>,
+ (<%= link_to t(:general_en), root_path(:locale => 'en') %>,
+ <%= link_to t(:general_pl), root_path(:locale => 'pl') %>)
+ </div>
<%= yield %>
-
+<div>
+ <%= params.inspect %>
+</div>
</body>
</html>
View
10 app/views/users/_form_create.html.erb
@@ -1,10 +1,18 @@
<%= form_for @user do |f| %>
<div><%= f.error_messages %></div>
+ <div><%= f.label :name %></div>
<div><%= f.text_field :name %></div>
+ <div><%= f.label :login %></div>
<div><%= f.text_field :login %></div>
+ <div><%= f.label :password %></div>
<div><%= f.password_field :password %></div>
+ <div><%= f.label :password_confirmation %></div>
<div><%= f.password_field :password_confirmation %></div>
+ <div><%= f.label :email %></div>
<div><%= f.text_field :email %></div>
- <div><%= f.check_box :autologin %></div>
+ <div>
+ <%= f.label :autologin %>
+ <%= f.check_box :autologin %>
+ </div>
<div><%= f.submit %></div>
<% end %>
View
12 app/views/users/_form_login.html.erb
@@ -0,0 +1,12 @@
+<%= form_for @user, :url => { :action => 'login' } do |f| %>
+ <div><%= f.error_messages %></div>
+ <div><%= f.label :login %></div>
+ <div><%= f.text_field :login %></div>
+ <div><%= f.label :password %></div>
+ <div><%= f.password_field :password %></div>
+ <div>
+ <%= f.label :autologin %>
+ <%= f.check_box :autologin %>
+ </div>
+ <div><%= f.submit 'login' %></div>
+<% end %>
View
10 app/views/users/login.html.erb
@@ -1 +1,11 @@
<h2>login</h2>
+<% #render 'layouts/flash', :locals => {:flash => flash} %>
+
+<% if flash[:notice] %>
+ <div class='notice'> <%= flash[:notice] %> </div>
+<% end %>
+<% if flash[:error] %>
+ <div class='error'> <%= flash[:error] %> </div>
+<% end %>
+
+<%= render 'form_login' %>
View
19 config/locales/en.yml
@@ -2,4 +2,21 @@
# See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
en:
- hello: "Hello world"
+ hello: "Hello world"
+ general_home: "home"
+ general_login: 'login'
+ general_create: 'create'
+ general_help: 'help'
+ general_logout: 'logout'
+ general_en: 'en'
+ general_pl: 'pl'
+
+pl:
+ general_home: 'główna'
+ general_login: 'login'
+ general_create: 'załóż konto'
+ general_help: 'pomoc'
+ general_logout: 'wyloguj'
+ general_en: 'ang'
+ general_pl: 'pol'
+
View
25 config/routes.rb
@@ -1,13 +1,22 @@
ACalendar::Application.routes.draw do
- # You can have the root of your site routed with "root"
- # just remember to delete public/index.html.
- root :to => 'application#index'
+ # You can have the root of your site routed with "root"
+ # just remember to delete public/index.html.
- match 'help' => 'application#help'
- resources :users
+ scope "(/:locale)", :locale => /pl|en/ do
+ match 'help' => 'application#help'
+ resources :users do
+ collection do
+ match 'login'
+ match 'logout'
+ end
+ end
+ end
- # This is a legacy wild controller route that's not recommended for RESTful applications.
- # Note: This route will make all actions in every controller accessible via GET requests.
- match ':controller(/:action(/*args))(.:format)', :args => /[a-zA-Z0-9\/-]{0,}/
+ root :to => 'application#index'
+ match '/:locale' => 'application#index'
+
+ # This is a legacy wild controller route that's not recommended for RESTful applications.
+ # Note: This route will make all actions in every controller accessible via GET requests.
+ #match ':controller(/:action(/*args))(.:format)', :args => /[a-zA-Z0-9\/-]{0,}/
end

0 comments on commit 9c7ed85

Please sign in to comment.