Permalink
Browse files

Integrate Sorcery auth gem

Implement Changes display with tests
  • Loading branch information...
mperham committed Nov 10, 2011
1 parent 6d08131 commit 7b6585c87dccc0deead2ab1a003c368212fe1364
Showing with 539 additions and 309 deletions.
  1. +1 −0 Gemfile
  2. +18 −0 Gemfile.lock
  3. BIN app/assets/images/rails.png
  4. +8 −0 app/assets/javascripts/home.js.coffee
  5. +3 −0 app/assets/javascripts/sessions.js.coffee
  6. +3 −0 app/assets/javascripts/users.js.coffee
  7. +3 −0 app/assets/stylesheets/home.css.scss
  8. +3 −0 app/assets/stylesheets/sessions.css.scss
  9. +3 −0 app/assets/stylesheets/users.css.scss
  10. +4 −0 app/controllers/application_controller.rb
  11. +15 −0 app/controllers/home_controller.rb
  12. +16 −0 app/controllers/sessions_controller.rb
  13. +14 −0 app/controllers/users_controller.rb
  14. +2 −0 app/helpers/home_helper.rb
  15. +2 −0 app/helpers/sessions_helper.rb
  16. +2 −0 app/helpers/users_helper.rb
  17. +3 −0 app/models/change.rb
  18. +10 −0 app/models/user.rb
  19. +13 −0 app/views/home/index.html.erb
  20. +20 −2 app/views/layouts/application.html.erb
  21. +15 −0 app/views/sessions/new.html.erb
  22. +25 −0 app/views/users/new.html.erb
  23. +199 −0 config/initializers/sorcery.rb
  24. +7 −53 config/routes.rb
  25. +15 −0 db/migrate/20111110032155_sorcery_core.rb
  26. +7 −0 db/migrate/20111110042615_add_last_read_to_users.rb
  27. +10 −1 db/schema.rb
  28. +0 −241 public/index.html
  29. +0 −9 test/fixtures/changes.yml
  30. +56 −0 test/functional/home_controller_test.rb
  31. +9 −0 test/functional/sessions_controller_test.rb
  32. +9 −0 test/functional/users_controller_test.rb
  33. +1 −0 test/test_helper.rb
  34. +24 −3 test/unit/change_test.rb
  35. +4 −0 test/unit/helpers/home_helper_test.rb
  36. +4 −0 test/unit/helpers/sessions_helper_test.rb
  37. +4 −0 test/unit/helpers/users_helper_test.rb
  38. +7 −0 test/unit/user_test.rb
View
@@ -5,6 +5,7 @@ gem 'rails', '3.1.1'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
+gem 'sorcery'
gem 'sqlite3'
View
@@ -29,8 +29,10 @@ GEM
activesupport (= 3.1.1)
activesupport (3.1.1)
multi_json (~> 1.0)
+ addressable (2.2.6)
ansi (1.4.1)
arel (2.2.1)
+ bcrypt-ruby (3.0.1)
builder (3.0.0)
coffee-rails (3.1.1)
coffee-script (>= 2.2.0)
@@ -42,6 +44,10 @@ GEM
erubis (2.7.0)
execjs (1.2.9)
multi_json (~> 1.0)
+ faraday (0.6.1)
+ addressable (~> 2.2.4)
+ multipart-post (~> 1.1.0)
+ rack (>= 1.1.0, < 2)
hike (1.2.1)
i18n (0.6.0)
jquery-rails (1.0.17)
@@ -54,6 +60,11 @@ GEM
treetop (~> 1.4.8)
mime-types (1.17.2)
multi_json (1.0.3)
+ multipart-post (1.1.3)
+ oauth (0.4.5)
+ oauth2 (0.4.1)
+ faraday (~> 0.6.1)
+ multi_json (>= 0.0.5)
polyglot (0.3.3)
rack (1.3.5)
rack-cache (1.1)
@@ -89,6 +100,12 @@ GEM
sass (>= 3.1.4)
sprockets (~> 2.0.0)
tilt (~> 1.3.2)
+ sorcery (0.7.4)
+ bcrypt-ruby (~> 3.0.0)
+ oauth (~> 0.4.4)
+ oauth (~> 0.4.4)
+ oauth2 (~> 0.4.1)
+ oauth2 (~> 0.4.1)
sprockets (2.0.3)
hike (~> 1.2)
rack (~> 1.0)
@@ -114,6 +131,7 @@ DEPENDENCIES
jquery-rails
rails (= 3.1.1)
sass-rails (~> 3.1.4)
+ sorcery
sqlite3
turn
uglifier (>= 1.0.3)
View
Binary file not shown.
@@ -0,0 +1,8 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
+
+$ ->
+ $('#mark_as_read').click ->
+ $.post '/home/mark_as_read', ->
+ $('#news').toggle()
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
@@ -0,0 +1,3 @@
+// Place all the styles related to the Home controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
@@ -0,0 +1,3 @@
+// Place all the styles related to the sessions controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
@@ -0,0 +1,3 @@
+// Place all the styles related to the users controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
@@ -1,3 +1,7 @@
class ApplicationController < ActionController::Base
protect_from_forgery
+
+ def not_authenticated
+ redirect_to login_url, :alert => "Please login to access this page."
+ end
end
@@ -0,0 +1,15 @@
+class HomeController < ApplicationController
+ before_filter :require_login
+
+ START_OF_TIME = Time.utc(2000, 1, 1)
+
+ def index
+ @changes = Change.since(current_user.last_read_at || START_OF_TIME)
+ end
+
+ def mark_as_read
+ current_user.update_attribute(:last_read_at, Time.now)
+ render :nothing => true
+ end
+
+end
@@ -0,0 +1,16 @@
+class SessionsController < ApplicationController
+ def create
+ user = login(params[:email], params[:password], params[:remember_me])
+ if user
+ redirect_back_or_to root_url, :notice => "Logged in!"
+ else
+ flash.now.alert = "Email or password was invalid"
+ render :new
+ end
+ end
+
+ def destroy
+ logout
+ redirect_to root_url, :notice => "Logged out!"
+ end
+end
@@ -0,0 +1,14 @@
+class UsersController < ApplicationController
+ def new
+ @user = User.new
+ end
+
+ def create
+ @user = User.new(params[:user])
+ if @user.save
+ redirect_to root_url, :notice => "Signed up!"
+ else
+ render :new
+ end
+ end
+end
@@ -0,0 +1,2 @@
+module HomeHelper
+end
@@ -0,0 +1,2 @@
+module SessionsHelper
+end
@@ -0,0 +1,2 @@
+module UsersHelper
+end
View
@@ -1,2 +1,5 @@
class Change < ActiveRecord::Base
+ def self.since(time)
+ where('created_at > ?', time).order('created_at DESC').limit(5)
+ end
end
View
@@ -0,0 +1,10 @@
+class User < ActiveRecord::Base
+ authenticates_with_sorcery!
+
+ attr_accessible :email, :password, :password_confirmation
+
+ validates_confirmation_of :password
+ validates_presence_of :password, :on => :create
+ validates_presence_of :email
+ validates_uniqueness_of :email
+end
@@ -0,0 +1,13 @@
+<h1>Welcome Home!</h1>
+
+<% if @changes.present? %>
+ <div id="news">
+ <span class="label success">News!</span>
+ <ul>
+ <% @changes.each do |change| %>
+ <li><%= change.title %> (<%= link_to 'Read More', change.link %>)</li>
+ <% end %>
+ </ul>
+ <%= link_to 'Mark as Read', '#', :id => 'mark_as_read' %>
+ </div>
+<% end %>
@@ -2,13 +2,31 @@
<html>
<head>
<title>NewsFlash</title>
+ <link rel="stylesheet" href="http://twitter.github.com/bootstrap/1.4.0/bootstrap.min.css">
<%= stylesheet_link_tag "application" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
-<body>
+<body style="padding-top: 50px;">
+ <div class="topbar">
+ <div class="topbar-inner">
+ <div class="container">
+ <a class="brand" href="/">News Flash</a>
+ <ul class="nav">
+ <% if current_user %>
+ <li><%= link_to "Log out", logout_path %></li>
+ <% else %>
+ <li><%= link_to "Sign up", signup_path %></li>
+ <li><%= link_to "Log in", login_path %></li>
+ <% end %>
+ </ul>
+ </div>
+ </div>
+ </div>
-<%= yield %>
+ <div class="container">
+ <%= yield %>
+ </div>
</body>
</html>
@@ -0,0 +1,15 @@
+<%= form_tag sessions_path do %>
+ <div class="field">
+ <%= label_tag :email %>
+ <%= text_field_tag :email, params[:email] %>
+ </div>
+ <div class="field">
+ <%= label_tag :password %>
+ <%= password_field_tag :password %>
+ </div>
+ <div class="field">
+ <%= check_box_tag :remember_me, 1, params[:remember_me] %>
+ <%= label_tag :remember_me %>
+ </div>
+ <div class="actions"><%= submit_tag "Log in", :class => 'btn primary' %></div>
+<% end %>
@@ -0,0 +1,25 @@
+<%= form_for @user do |f| %>
+ <% if @user.errors.any? %>
+ <div class="error_messages">
+ <h2>Form is invalid</h2>
+ <ul>
+ <% @user.errors.full_messages.each do |message| %>
+ <li><%= message %></li>
+ <% end %>
+ </ul>
+ </div>
+ <% end %>
+ <div class="field">
+ <%= f.label :email %>
+ <%= f.text_field :email %>
+ </div>
+ <div class="field">
+ <%= f.label :password %>
+ <%= f.password_field :password %>
+ </div>
+ <div class="field">
+ <%= f.label :password_confirmation %>
+ <%= f.password_field :password_confirmation %>
+ </div>
+ <div class="actions"><%= f.submit %></div>
+<% end %>
Oops, something went wrong.

0 comments on commit 7b6585c

Please sign in to comment.