Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 3 commits
  • 30 files changed
  • 0 comments
  • 1 contributor

Showing 30 changed files with 242 additions and 18 deletions. Show diff stats Hide diff stats

  1. +1 0  Gemfile
  2. +2 0  Gemfile.lock
  3. +3 0  app/assets/javascripts/sessions.js.coffee
  4. +3 0  app/assets/javascripts/users.js.coffee
  5. +3 0  app/assets/stylesheets/sessions.css.scss
  6. +3 0  app/assets/stylesheets/users.css.scss
  7. +9 0 app/controllers/application_controller.rb
  8. +6 3 app/controllers/documents_controller.rb
  9. +20 0 app/controllers/sessions_controller.rb
  10. +17 0 app/controllers/users_controller.rb
  11. +2 0  app/helpers/sessions_helper.rb
  12. +2 0  app/helpers/users_helper.rb
  13. +6 0 app/models/category.rb
  14. +6 0 app/models/document.rb
  15. +5 0 app/models/state_change.rb
  16. +7 0 app/models/user.rb
  17. +1 0  app/views/documents/_form.html.haml
  18. +0 2  app/views/documents/index.html.haml
  19. +5 3 app/views/documents/show.html.haml
  20. +9 0 app/views/layouts/_i18n_nav.html.haml
  21. +18 0 app/views/layouts/_user_nav.html.haml
  22. +3 9 app/views/layouts/application.html.haml
  23. +17 0 app/views/sessions/new.html.haml
  24. +10 0 app/views/users/new.html.haml
  25. +26 0 config/locales/en.yml
  26. +26 0 config/locales/pt.yml
  27. +7 0 config/routes.rb
  28. +11 0 db/migrate/20120413231751_create_users.rb
  29. +9 1 db/schema.rb
  30. +5 0 spec/models/user_spec.rb
1  Gemfile
@@ -3,6 +3,7 @@ source 'https://rubygems.org'
3 3 gem 'rails', '3.2.0'
4 4 gem 'pg'
5 5 gem 'jquery-rails'
  6 +gem 'bcrypt-ruby', '~> 3.0.0'
6 7 gem 'state_machine'
7 8 gem 'simple_form', '~> 2.0.0.rc'
8 9
2  Gemfile.lock
@@ -30,6 +30,7 @@ GEM
30 30 multi_json (~> 1.0)
31 31 addressable (2.2.7)
32 32 arel (3.0.2)
  33 + bcrypt-ruby (3.0.1)
33 34 bootstrap-sass (2.0.2)
34 35 builder (3.0.0)
35 36 capybara (1.1.2)
@@ -178,6 +179,7 @@ PLATFORMS
178 179 ruby
179 180
180 181 DEPENDENCIES
  182 + bcrypt-ruby (~> 3.0.0)
181 183 bootstrap-sass (~> 2.0.2)
182 184 capybara
183 185 coffee-rails (~> 3.2.1)
3  app/assets/javascripts/sessions.js.coffee
... ... @@ -0,0 +1,3 @@
  1 +# Place all the behaviors and hooks related to the matching controller here.
  2 +# All this logic will automatically be available in application.js.
  3 +# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
3  app/assets/javascripts/users.js.coffee
... ... @@ -0,0 +1,3 @@
  1 +# Place all the behaviors and hooks related to the matching controller here.
  2 +# All this logic will automatically be available in application.js.
  3 +# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
3  app/assets/stylesheets/sessions.css.scss
... ... @@ -0,0 +1,3 @@
  1 +// Place all the styles related to the sessions controller here.
  2 +// They will automatically be included in application.css.
  3 +// You can use Sass (SCSS) here: http://sass-lang.com/
3  app/assets/stylesheets/users.css.scss
... ... @@ -0,0 +1,3 @@
  1 +// Place all the styles related to the users controller here.
  2 +// They will automatically be included in application.css.
  3 +// You can use Sass (SCSS) here: http://sass-lang.com/
9 app/controllers/application_controller.rb
@@ -15,4 +15,13 @@ def set_locale
15 15 def default_url_options(options = {})
16 16 {locale: I18n.locale}
17 17 end
  18 +
  19 + def current_user
  20 + @current_user ||= User.find(session[:user_id]) if session[:user_id]
  21 + end
  22 + helper_method :current_user
  23 +
  24 + def authorize
  25 + redirect_to login_url, alert: t('flash.alert.unauthorized') if current_user.nil?
  26 + end
18 27 end
9 app/controllers/documents_controller.rb
@@ -44,7 +44,8 @@ def create
44 44
45 45 respond_to do |format|
46 46 if @document.save
47   - format.html { redirect_to @document, notice: 'Document was successfully created.' }
  47 + format.html { redirect_to @document,
  48 + notice: "#{Document.model_name.human} #{ t('flash.notice.create')}" }
48 49 format.json { render json: @document, status: :created, location: @document }
49 50 else
50 51 format.html { render action: "new" }
@@ -60,7 +61,8 @@ def update
60 61
61 62 respond_to do |format|
62 63 if @document.update_attributes(params[:document])
63   - format.html { redirect_to @document, notice: 'Document was successfully updated.' }
  64 + format.html { redirect_to @document,
  65 + notice: "#{Document.model_name.human} #{ t('flash.notice.update')}" }
64 66 format.json { head :no_content }
65 67 else
66 68 format.html { render action: "edit" }
@@ -76,7 +78,8 @@ def destroy
76 78 @document.destroy
77 79
78 80 respond_to do |format|
79   - format.html { redirect_to documents_url }
  81 + format.html { redirect_to documents_url,
  82 + notice: "#{Document.model_name.human} #{ t('flash.notice.destroy')}" }
80 83 format.json { head :no_content }
81 84 end
82 85 end
20 app/controllers/sessions_controller.rb
... ... @@ -0,0 +1,20 @@
  1 +class SessionsController < ApplicationController
  2 + def new
  3 + end
  4 +
  5 + def create
  6 + user = User.find_by_email(params[:email])
  7 + if user && user.authenticate(params[:password])
  8 + session[:user_id] = user.id
  9 + redirect_to root_url, notice: t('flash.notice.logged_in')
  10 + else
  11 + flash.now.alert = t('flash.alert.login_invalid')
  12 + render "new"
  13 + end
  14 + end
  15 +
  16 + def destroy
  17 + session[:user_id] = nil
  18 + redirect_to root_url, notice: t('flash.notice.logged_out')
  19 + end
  20 +end
17 app/controllers/users_controller.rb
... ... @@ -0,0 +1,17 @@
  1 +class UsersController < ApplicationController
  2 +
  3 + def new
  4 + @user = User.new
  5 + end
  6 +
  7 + def create
  8 + @user = User.new(params[:user])
  9 + if @user.save
  10 + session[:user_id] = @user.id
  11 + redirect_to root_url, notice: t('flash.notice.signup')
  12 + else
  13 + render 'new'
  14 + end
  15 + end
  16 +
  17 +end
2  app/helpers/sessions_helper.rb
... ... @@ -0,0 +1,2 @@
  1 +module SessionsHelper
  2 +end
2  app/helpers/users_helper.rb
... ... @@ -0,0 +1,2 @@
  1 +module UsersHelper
  2 +end
6 app/models/category.rb
@@ -6,4 +6,10 @@ class Category < ActiveRecord::Base
6 6
7 7 # Associations
8 8 has_many :documents, dependent: :destroy
  9 +
  10 + # # # # # Public Methods
  11 + # # # # # Instance Methods
  12 + def to_s
  13 + name
  14 + end
9 15 end
6 app/models/document.rb
@@ -7,4 +7,10 @@ class Document < ActiveRecord::Base
7 7 # Associations
8 8 belongs_to :category
9 9 has_many :versions, dependent: :destroy
  10 +
  11 + # # # # # Public Methods
  12 + # # # # # Instance Methods
  13 + def to_s
  14 + name
  15 + end
10 16 end
5 app/models/state_change.rb
@@ -7,4 +7,9 @@ class StateChange < ActiveRecord::Base
7 7 # # # # # Associations
8 8 belongs_to :version
9 9
  10 + # # # # # Public Methods
  11 + # # # # # Instance Methods
  12 + def to_s
  13 + "Set as #{state} by #{user} on #{updated_at}"
  14 + end
10 15 end
7 app/models/user.rb
... ... @@ -0,0 +1,7 @@
  1 +class User < ActiveRecord::Base
  2 + has_secure_password
  3 +
  4 + attr_accessible :email, :password, :password_confirmation
  5 + validates_presence_of :email, :password
  6 + validates_uniqueness_of :email
  7 +end
1  app/views/documents/_form.html.haml
@@ -5,6 +5,7 @@
5 5 = f.input :name, input_html: { class: 'span3' }
6 6 = f.input :title, input_html: { class: 'span5' }
7 7 = f.input :subtitle, input_html: { class: 'span5' }
  8 + = f.association :category, input_html: { class: 'span3' }
8 9 = f.input :description, input_html: { class: 'span5', rows: '5' }
9 10
10 11 .form-actions
2  app/views/documents/index.html.haml
@@ -15,6 +15,4 @@
15 15 = link_to t('actions.edit'), edit_document_path(document), class: 'btn btn-mini'
16 16 = link_to t('actions.destroy'), document, confirm: t('actions.confirm'), method: :delete, class: 'btn btn-mini btn-danger'
17 17
18   -%br
19   -
20 18 = link_to "#{t('actions.new')} #{Document.model_name.human}", new_document_path, class: 'btn btn-primary'
8 app/views/documents/show.html.haml
@@ -3,9 +3,11 @@
3 3 %p
4 4 %h2= @document.title
5 5 %h3= @document.subtitle
6   -%p
7   - %strong Description
8   - %br= @document.description
  6 +%dl
  7 + %dt Category
  8 + %dd= @document.category.to_s
  9 + %dt Description
  10 + %dd= @document.description
9 11
10 12 .form-actions
11 13 = link_to t('actions.back'), documents_path, class: 'btn'
9 app/views/layouts/_i18n_nav.html.haml
... ... @@ -0,0 +1,9 @@
  1 +- if I18n.available_locales.count > 1
  2 + %li.dropdown
  3 + %a.dropdown-toggle{ href: '#', 'data-toggle' => 'dropdown' }
  4 + = "#{I18n.locale.upcase} "
  5 + %b.caret
  6 + %ul.dropdown-menu
  7 + - I18n.available_locales.each do |loc|
  8 + - unless loc == I18n.locale
  9 + %li= link_to loc.upcase, locale: loc
18 app/views/layouts/_user_nav.html.haml
... ... @@ -0,0 +1,18 @@
  1 +- if current_user
  2 + %li.dropdown
  3 + %a.dropdown-toggle{ href: '#', 'data-toggle' => 'dropdown' }
  4 + = "#{ t('session.logged_in_as') } #{ current_user.email }"
  5 + %b.caret
  6 + %ul.dropdown-menu
  7 + %li= link_to "#{ t('actions.edit') } #{ User.model_name.human }", current_user
  8 + %li.divider
  9 + %li= link_to t('actions.logout'), logout_path
  10 +- else
  11 + %li.dropdown
  12 + %a.dropdown-toggle{ href: '#', 'data-toggle' => 'dropdown' }
  13 + = "#{ t('session.logged_out') } "
  14 + %b.caret
  15 + %ul.dropdown-menu
  16 + %li= link_to t('actions.login'), login_path
  17 + %li.divider
  18 + %li= link_to t('actions.signup'), signup_path
12 app/views/layouts/application.html.haml
@@ -22,15 +22,9 @@
22 22 %ul.nav
23 23 %li= link_to t('.documents_link'), documents_path
24 24 %ul.nav.pull-right
25   - - if I18n.available_locales.count > 1
26   - %li.dropdown
27   - %a.dropdown-toggle{ href: '#', 'data-toggle' => 'dropdown' }
28   - = "#{I18n.locale.upcase} "
29   - %b.caret
30   - %ul.dropdown-menu
31   - - I18n.available_locales.each do |loc|
32   - - unless loc == I18n.locale
33   - %li= link_to loc.upcase, locale: loc
  25 + = render 'layouts/user_nav'
  26 + %li.divider-vertical
  27 + = render 'layouts/i18n_nav'
34 28
35 29 .container
36 30 - flash.each do |name, msg|
17 app/views/sessions/new.html.haml
... ... @@ -0,0 +1,17 @@
  1 +
  2 += form_tag sessions_path, class: 'simple_form form-horizontal' do
  3 + %fieldset
  4 + %legend= t 'actions.login'
  5 + .control-group.email.required
  6 + .control-label.email.required
  7 + = label_tag :email
  8 + .controls
  9 + = text_field_tag :email, params[:email]
  10 + .control-group.password.required
  11 + .control-label.password.required
  12 + = label_tag :password
  13 + .controls
  14 + = password_field_tag :password
  15 + .form-actions
  16 + = submit_tag t('actions.login'), class: 'btn btn-primary'
  17 + = link_to t('actions.cancel'), documents_path, class: 'btn'
10 app/views/users/new.html.haml
... ... @@ -0,0 +1,10 @@
  1 += simple_form_for @user, html: {class: 'form-horizontal'} do |f|
  2 + %fieldset
  3 + %legend= "#{t("actions.#{controller.action_name.downcase}")} #{User.model_name.human}"
  4 + = f.input :email
  5 + = f.input :password
  6 + = f.input :password_confirmation
  7 +
  8 + .form-actions
  9 + = f.button :submit, class: 'btn-primary'
  10 + = link_to t('actions.cancel'), documents_path, class: 'btn'
26 config/locales/en.yml
@@ -20,16 +20,42 @@ en:
20 20 confirm: 'Are you sure?'
21 21 cancel: 'Cancel'
22 22 back: 'Back'
  23 + signup: 'Sign Up'
  24 + login: 'Login'
  25 + logout: 'Logout'
  26 +
  27 + session:
  28 + logged_in: 'Logged In'
  29 + logged_in_as: 'Logged In as'
  30 + logged_out: 'Logged Out'
  31 +
  32 + flash:
  33 + notice:
  34 + signup: 'Thank you for signing up!'
  35 + create: 'was successfully created.'
  36 + update: 'was successfully updated.'
  37 + destroy: 'was successfully destroyed.'
  38 + logged_in: 'Logged In!'
  39 + logged_out: 'Logged Out!'
  40 + alert:
  41 + login_invalid: 'Email or password is invalid!'
  42 + unauthorized: 'Not authorized!'
23 43
24 44 activerecord:
25 45 models:
26 46 document: 'Document'
  47 + user: 'User'
27 48 attributes:
28 49 document:
29 50 name: 'Name'
30 51 title: 'Title'
31 52 subtitle: 'Subtitle'
32 53 description: 'Description'
  54 + user:
  55 + email: 'Email'
  56 + name: 'Name'
  57 + password: 'Password'
  58 + password_confirmation: 'Password confirmation'
33 59
34 60 date:
35 61 abbr_day_names:
26 config/locales/pt.yml
@@ -20,16 +20,42 @@ pt:
20 20 confirm: 'Tem a certeza?'
21 21 cancel: 'Cancelar'
22 22 back: 'Voltar'
  23 + signup: 'Registar'
  24 + login: 'Iniciar sessão'
  25 + logout: 'Terminar sessão'
  26 +
  27 + session:
  28 + logged_in: 'Sessão iniciada'
  29 + logged_in_as: 'Sessão iniciada como'
  30 + logged_out: 'Sessão terminada'
  31 +
  32 + flash:
  33 + notice:
  34 + signup: 'Obrigado pelo seu registo!'
  35 + create: 'foi criado com sucesso.'
  36 + update: 'foi actualizado com sucesso.'
  37 + destroy: 'foi destruído com sucesso.'
  38 + logged_in: 'Sessão iniciada com sucesso!'
  39 + logged_out: 'Sessão terminada com sucesso!'
  40 + alert:
  41 + login_invalid: 'Email e/ou password inválidos!'
  42 + unauthorized: 'Não autorizado!'
23 43
24 44 activerecord:
25 45 models:
26 46 document: 'Documento'
  47 + user: 'Utilizador'
27 48 attributes:
28 49 document:
29 50 name: 'Nome'
30 51 title: 'Título'
31 52 subtitle: 'Subtítulo'
32 53 description: 'Descrição'
  54 + user:
  55 + email: 'Email'
  56 + name: 'Nome'
  57 + password: 'Password'
  58 + password_confirmation: 'Confirme password'
33 59
34 60 date:
35 61 abbr_day_names:
7 config/routes.rb
... ... @@ -1,5 +1,12 @@
1 1 Athena::Application.routes.draw do
  2 +
2 3 scope ':locale' do
  4 + get 'signup', to: 'users#new', as: 'signup'
  5 + get 'login', to: 'sessions#new', as: 'login'
  6 + get 'logout', to: 'sessions#destroy', as: 'logout'
  7 +
  8 + resources :sessions
  9 + resources :users
3 10 resources :documents
4 11 root to: 'documents#index'
5 12 end
11 db/migrate/20120413231751_create_users.rb
... ... @@ -0,0 +1,11 @@
  1 +class CreateUsers < ActiveRecord::Migration
  2 + def change
  3 + create_table :users do |t|
  4 + t.string :email
  5 + t.string :name
  6 + t.string :password_digest
  7 +
  8 + t.timestamps
  9 + end
  10 + end
  11 +end
10 db/schema.rb
@@ -11,7 +11,7 @@
11 11 #
12 12 # It's strongly recommended to check this file into your version control system.
13 13
14   -ActiveRecord::Schema.define(:version => 20120412002518) do
  14 +ActiveRecord::Schema.define(:version => 20120413231751) do
15 15
16 16 create_table "categories", :force => true do |t|
17 17 t.string "name"
@@ -40,6 +40,14 @@
40 40 t.datetime "updated_at", :null => false
41 41 end
42 42
  43 + create_table "users", :force => true do |t|
  44 + t.string "email"
  45 + t.string "name"
  46 + t.string "password_digest"
  47 + t.datetime "created_at", :null => false
  48 + t.datetime "updated_at", :null => false
  49 + end
  50 +
43 51 create_table "versions", :force => true do |t|
44 52 t.integer "number"
45 53 t.string "state"
5 spec/models/user_spec.rb
... ... @@ -0,0 +1,5 @@
  1 +require 'spec_helper'
  2 +
  3 +describe User do
  4 + pending "add some examples to (or delete) #{__FILE__}"
  5 +end

No commit comments for this range

Something went wrong with that request. Please try again.