diff --git a/Gemfile b/Gemfile index 10d5c3c..dccf7ed 100644 --- a/Gemfile +++ b/Gemfile @@ -3,6 +3,7 @@ source 'http://rubygems.org' gem 'rails', '3.0.6' gem 'sqlite3' gem 'haml' +gem 'omniauth' group :development, :test do gem 'rspec-rails' diff --git a/Gemfile.lock b/Gemfile.lock index 9005eb5..7e2ae14 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -29,6 +29,7 @@ GEM activemodel (= 3.0.6) activesupport (= 3.0.6) activesupport (3.0.6) + addressable (2.2.4) arel (2.0.9) autotest (4.4.6) ZenTest (>= 4.4.1) @@ -50,6 +51,10 @@ GEM diff-lcs (1.1.2) erubis (2.6.6) abstract (>= 1.0.0) + faraday (0.6.1) + addressable (~> 2.2.4) + multipart-post (~> 1.1.0) + rack (>= 1.1.0, < 2) ffi (1.0.7) rake (>= 0.8.7) haml (3.0.25) @@ -72,12 +77,56 @@ GEM treetop (~> 1.4.8) mime-types (1.16) mocha (0.9.12) + multi_json (0.0.5) + multipart-post (1.1.0) + net-ldap (0.1.1) nifty-generators (0.4.6) nokogiri (1.4.4) + oa-basic (0.2.4) + oa-core (= 0.2.4) + rest-client (~> 1.6.0) + oa-core (0.2.4) + oa-enterprise (0.2.4) + addressable (= 2.2.4) + net-ldap (~> 0.1.1) + nokogiri (~> 1.4.2) + oa-core (= 0.2.4) + pyu-ruby-sasl (~> 0.0.3.1) + rubyntlm (~> 0.1.1) + oa-more (0.2.4) + multi_json (~> 0.0.2) + oa-core (= 0.2.4) + rest-client (~> 1.6.0) + oa-oauth (0.2.4) + faraday (~> 0.6.1) + multi_json (>= 0.0.5) + nokogiri (~> 1.4.2) + oa-core (= 0.2.4) + oauth (~> 0.4.0) + oauth2 (~> 0.4.1) + oa-openid (0.2.4) + oa-core (= 0.2.4) + rack-openid (~> 1.2.0) + ruby-openid-apps-discovery + oauth (0.4.4) + oauth2 (0.4.1) + faraday (~> 0.6.1) + multi_json (>= 0.0.5) + omniauth (0.2.4) + oa-basic (= 0.2.4) + oa-core (= 0.2.4) + oa-enterprise (= 0.2.4) + oa-more (= 0.2.4) + oa-oauth (= 0.2.4) + oa-openid (= 0.2.4) polyglot (0.3.1) + pyu-ruby-sasl (0.0.3.2) rack (1.2.2) rack-mount (0.6.14) rack (>= 1.0.0) + rack-openid (1.2.0) + rack (>= 1.1.0) + ruby-openid (>= 2.1.8) rack-test (0.5.7) rack (>= 1.0) rails (3.0.6) @@ -109,6 +158,10 @@ GEM activesupport (~> 3.0) railties (~> 3.0) rspec (~> 2.5.0) + ruby-openid (2.1.8) + ruby-openid-apps-discovery (1.2.0) + ruby-openid (>= 2.1.7) + rubyntlm (0.1.1) rubyzip (0.9.4) selenium-webdriver (0.1.4) childprocess (>= 0.1.7) @@ -137,6 +190,7 @@ DEPENDENCIES jquery-rails mocha nifty-generators + omniauth rails (= 3.0.6) rspec-rails sqlite3 diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index c413f9d..aa512d4 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,9 +1,15 @@ class ApplicationController < ActionController::Base protect_from_forgery + helper_method :current_user def find_resource class_name = params[:controller].singularize klass = class_name.camelize.constantize self.instance_variable_set "@" + class_name, klass.find(params[:id]) end + +private + def current_user + @current_user ||= User.find(session[:user_id]) if session[:user_id] + end end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb new file mode 100644 index 0000000..a558659 --- /dev/null +++ b/app/controllers/sessions_controller.rb @@ -0,0 +1,12 @@ +class SessionsController < ApplicationController + def create + user = User.find_or_create_by_auth(request.env["omniauth.auth"]) + session[:user_id] = user.id + redirect_to root_path, :notice => "Logged in as #{current_user.name}" + end + + def destroy + session[:user_id] = nil + redirect_to root_path, :notice => "Goodbye!" + end +end \ No newline at end of file diff --git a/app/helpers/sessions_helper.rb b/app/helpers/sessions_helper.rb new file mode 100644 index 0000000..309f8b2 --- /dev/null +++ b/app/helpers/sessions_helper.rb @@ -0,0 +1,2 @@ +module SessionsHelper +end diff --git a/app/models/company.rb b/app/models/company.rb index 7a7fbba..4c8da27 100644 --- a/app/models/company.rb +++ b/app/models/company.rb @@ -2,4 +2,8 @@ class Company < ActiveRecord::Base attr_accessible :name validates_presence_of :name include Contact + + def to_s + name + end end diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 0000000..b54d906 --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,11 @@ +class User < ActiveRecord::Base + + def self.find_or_create_by_auth(auth_data) + user = self.find_or_create_by_provider_and_uid(auth_data["provider"], auth_data["uid"]) + if user.name != auth_data["user_info"]["name"] + user.name = auth_data["user_info"]["name"] + user.save + end + return user + end +end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index a661a0a..ce52679 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -14,6 +14,14 @@ <%= content_tag :div, msg, :id => "flash_#{name}" %> <% end %> +
+ <% if current_user %> + Welcome, <%= current_user.name %> + <%= link_to "logout", logout_path, :id => "login" %> + <% else %> + <%= link_to "login", login_path, :id => "logout" %> + <% end %> +
<%= content_tag :h1, yield(:title) if show_title? %> <%= yield %> diff --git a/config/initializers/omniauth.rb b/config/initializers/omniauth.rb new file mode 100644 index 0000000..2576a55 --- /dev/null +++ b/config/initializers/omniauth.rb @@ -0,0 +1,3 @@ +Rails.application.config.middleware.use OmniAuth::Builder do + provider :twitter, "EZYxQSqP0j35QWqoV0kUg", "IToKT8jdWZEhEH60wFL94HGf4uoGE1SqFUrZUR34M4" +end diff --git a/config/routes.rb b/config/routes.rb index 04a268a..1c3825e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,66 +1,7 @@ JSContact::Application.routes.draw do - resources :companies - - resources :email_addresses - - resources :phone_numbers - - resources :people - - # The priority is based upon order of creation: - # first created -> highest priority. - - # Sample of regular route: - # match 'products/:id' => 'catalog#view' - # Keep in mind you can assign values other than :controller and :action - - # Sample of named route: - # match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase - # This route can be invoked with purchase_url(:id => product.id) - - # Sample resource route (maps HTTP verbs to controller actions automatically): - # resources :products - - # Sample resource route with options: - # resources :products do - # member do - # get 'short' - # post 'toggle' - # end - # - # collection do - # get 'sold' - # end - # end - - # Sample resource route with sub-resources: - # resources :products do - # resources :comments, :sales - # resource :seller - # end - - # Sample resource route with more complex sub-resources - # resources :products do - # resources :comments - # resources :sales do - # get 'recent', :on => :collection - # end - # end - - # Sample resource route within a namespace: - # namespace :admin do - # # Directs /admin/products/* to Admin::ProductsController - # # (app/controllers/admin/products_controller.rb) - # resources :products - # end - - # You can have the root of your site routed with "root" - # just remember to delete public/index.html. - # root :to => "welcome#index" - - # See how all your routes lay out with "rake routes" - - # 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(/:id(.:format)))' + resources :companies, :email_addresses, :phone_numbers, :people + match '/auth/:provider/callback', :to => 'sessions#create' + match "/login" => redirect("/auth/twitter"), :as => :login + match "/logout" => "sessions#destroy", :as => :logout + root :to => "companies#index" end diff --git a/db/migrate/20110424163240_create_users.rb b/db/migrate/20110424163240_create_users.rb new file mode 100644 index 0000000..1ceae04 --- /dev/null +++ b/db/migrate/20110424163240_create_users.rb @@ -0,0 +1,15 @@ +class CreateUsers < ActiveRecord::Migration + def self.up + create_table :users do |t| + t.string :provider + t.string :uid + t.string :name + + t.timestamps + end + end + + def self.down + drop_table :users + end +end diff --git a/db/schema.rb b/db/schema.rb index eafdf5e..09e4db6 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20110420161636) do +ActiveRecord::Schema.define(:version => 20110424163240) do create_table "companies", :force => true do |t| t.string "name" @@ -41,4 +41,12 @@ t.string "contact_type" end + create_table "users", :force => true do |t| + t.string "provider" + t.string "uid" + t.string "name" + t.datetime "created_at" + t.datetime "updated_at" + end + end diff --git a/public/index.html b/public/index.html deleted file mode 100644 index 75d5edd..0000000 --- a/public/index.html +++ /dev/null @@ -1,239 +0,0 @@ - - - - Ruby on Rails: Welcome aboard - - - - -
- - -
- - - - -
-

Getting started

-

Here’s how to get rolling:

- -
    -
  1. -

    Use rails generate to create your models and controllers

    -

    To see all available options, run it without parameters.

    -
  2. - -
  3. -

    Set up a default route and remove or rename this file

    -

    Routes are set up in config/routes.rb.

    -
  4. - -
  5. -

    Create your database

    -

    Run rake db:migrate to create your database. If you're not using SQLite (the default), edit config/database.yml with your username and password.

    -
  6. -
-
-
- - -
- - diff --git a/spec/controllers/sessions_controller_spec.rb b/spec/controllers/sessions_controller_spec.rb new file mode 100644 index 0000000..702c559 --- /dev/null +++ b/spec/controllers/sessions_controller_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe SessionsController do + +end diff --git a/spec/helpers/sessions_helper_spec.rb b/spec/helpers/sessions_helper_spec.rb new file mode 100644 index 0000000..c14cfa5 --- /dev/null +++ b/spec/helpers/sessions_helper_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +# Specs in this file have access to a helper object that includes +# the SessionsHelper. For example: +# +# describe SessionsHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# helper.concat_strings("this","that").should == "this that" +# end +# end +# end +describe SessionsHelper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb new file mode 100644 index 0000000..44032b4 --- /dev/null +++ b/spec/models/user_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe User do + pending "add some examples to (or delete) #{__FILE__}" +end