├── doc └── README_FOR_APP ├── lib └── tasks │ └── .gitkeep ├── public ├── favicon.ico ├── stylesheets │ ├── .gitkeep │ ├── reset.css │ ├── scaffold.css │ ├── application.css │ └── forms.css ├── images │ ├── social-icons │ │ ├── aol_128.png │ │ ├── aol_256.png │ │ ├── aol_32.png │ │ ├── aol_64.png │ │ ├── github_32.png │ │ ├── github_64.png │ │ ├── google_32.png │ │ ├── openid_32.png │ │ ├── openid_64.png │ │ ├── yahoo_128.png │ │ ├── yahoo_256.png │ │ ├── yahoo_32.png │ │ ├── yahoo_64.png │ │ ├── basecamp_128.png │ │ ├── basecamp_256.png │ │ ├── basecamp_32.png │ │ ├── basecamp_64.png │ │ ├── campfire_128.png │ │ ├── campfire_256.png │ │ ├── campfire_32.png │ │ ├── campfire_64.png │ │ ├── facebook_128.png │ │ ├── facebook_256.png │ │ ├── facebook_32.png │ │ ├── facebook_64.png │ │ ├── github_128.png │ │ ├── github_256.png │ │ ├── google_128.png │ │ ├── google_256.png │ │ ├── linkedin_128.png │ │ ├── linkedin_256.png │ │ ├── linkedin_32.png │ │ ├── linkedin_64.png │ │ ├── myspace_128.png │ │ ├── myspace_256.png │ │ ├── myspace_32.png │ │ ├── myspace_64.png │ │ ├── openid_128.png │ │ ├── openid_256.png │ │ ├── presently_32.png │ │ ├── presently_64.png │ │ ├── twitter_128.png │ │ ├── twitter_256.png │ │ ├── twitter_32.png │ │ ├── twitter_64.png │ │ ├── google_64 copy.png │ │ ├── google_apps_32.png │ │ ├── google_apps_64.png │ │ ├── presently_128.png │ │ ├── presently_256.png │ │ ├── google_128 copy.png │ │ └── google_256 copy.png │ └── user_photos │ │ ├── missing_big.png │ │ ├── missing_mini.png │ │ ├── missing_small.png │ │ ├── missing_thumb.png │ │ └── missing_original.png ├── system │ └── photos │ │ ├── 1 │ │ ├── big │ │ │ ├── pt_normal.jpg │ │ │ └── 48909_614285738_8043_q.jpg │ │ ├── mini │ │ │ ├── pt_normal.jpg │ │ │ └── 48909_614285738_8043_q.jpg │ │ ├── small │ │ │ ├── pt_normal.jpg │ │ │ └── 48909_614285738_8043_q.jpg │ │ ├── thumb │ │ │ ├── pt_normal.jpg │ │ │ └── 48909_614285738_8043_q.jpg │ │ └── original │ │ │ ├── pt_normal.jpg │ │ │ └── 48909_614285738_8043_q.jpg │ │ ├── 2 │ │ ├── big │ │ │ └── pt_normal.jpg │ │ ├── mini │ │ │ └── pt_normal.jpg │ │ ├── small │ │ │ └── pt_normal.jpg │ │ ├── thumb │ │ │ └── pt_normal.jpg │ │ └── original │ │ │ └── pt_normal.jpg │ │ └── 4 │ │ ├── big │ │ └── pt_normal.jpg │ │ ├── mini │ │ └── pt_normal.jpg │ │ ├── small │ │ └── pt_normal.jpg │ │ ├── thumb │ │ └── pt_normal.jpg │ │ └── original │ │ └── pt_normal.jpg ├── javascripts │ ├── application.js │ ├── rails.js │ ├── dragdrop.js │ └── controls.js ├── robots.txt ├── 422.html ├── 404.html └── 500.html ├── vendor └── plugins │ └── .gitkeep ├── .rvmrc ├── .gitignore ├── config ├── initializers │ ├── generators.rb │ ├── mime_types.rb │ ├── inflections.rb │ ├── backtrace_silencers.rb │ ├── session_store.rb │ ├── secret_token.rb │ └── devise.rb ├── boot.rb ├── database.yml ├── environment.rb ├── environments │ ├── development.rb │ ├── test.rb │ └── production.rb ├── locales │ ├── devise.en.yml │ └── en.yml ├── application.rb └── routes.rb ├── spec ├── fixtures │ └── files │ │ └── photo.png ├── controllers │ ├── profiles_controller_spec.rb │ ├── sharings_controller_spec.rb │ └── users │ │ └── omniauth_callbacks_controller_spec.rb ├── builders │ └── fixjour.rb ├── helpers │ └── application_spec.rb ├── spec_helper.rb └── models │ └── user_spec.rb ├── test ├── unit │ ├── helpers │ │ └── monkeys_helper_test.rb │ ├── user_test.rb │ └── monkey_test.rb ├── fixtures │ ├── monkeys.yml │ └── users.yml ├── performance │ └── browsing_test.rb ├── test_helper.rb └── functional │ └── monkeys_controller_test.rb ├── app ├── controllers │ ├── sessions_controller.rb │ ├── profiles_controller.rb │ ├── application_controller.rb │ ├── registrations_controller.rb │ ├── sharings_controller.rb │ ├── redirect_back.rb │ └── users │ │ └── omniauth_callbacks_controller.rb ├── models │ ├── sharing.rb │ ├── nil_user.rb │ ├── omni_auth_populator.rb │ ├── user_token.rb │ └── user.rb ├── views │ ├── sharings │ │ ├── _form.html.erb │ │ ├── new.html.erb │ │ ├── show.html.erb │ │ └── index.html.erb │ ├── devise │ │ ├── shared │ │ │ ├── _forgot_password.erb │ │ │ ├── _social_connected.erb │ │ │ ├── _social_connect_and_unconnect.erb │ │ │ ├── _social_unconnected.erb │ │ │ ├── _social.erb │ │ │ └── _links.erb │ │ ├── mailer │ │ │ ├── confirmation_instructions.html.erb │ │ │ ├── unlock_instructions.html.erb │ │ │ └── reset_password_instructions.html.erb │ │ ├── passwords │ │ │ ├── new.html.erb │ │ │ └── edit.html.erb │ │ ├── unlocks │ │ │ └── new.html.erb │ │ └── confirmations │ │ │ └── new.html.erb │ ├── profiles │ │ └── show.html.erb │ ├── sessions │ │ └── new.html.erb │ ├── registrations │ │ ├── new.html.erb │ │ └── edit.html.erb │ └── layouts │ │ └── application.html.erb └── helpers │ └── application_helper.rb ├── config.ru ├── db ├── migrate │ ├── 20110326070436_add_user_to_sharing.rb │ ├── 20110325233835_add_slugs_to_user.rb │ ├── 20110325235418_add_username_to_provider.rb │ ├── 20110324053313_create_sharings.rb │ ├── 20101106080918_create_user_tokens.rb │ ├── 20110320031947_add_metadata_to_user.rb │ └── 20101105152126_devise_create_users.rb ├── seeds.rb └── schema.rb ├── Rakefile ├── script └── rails ├── Gemfile ├── MIT-LICENSE ├── README.rdoc └── Gemfile.lock /doc/README_FOR_APP: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /lib/tasks/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /vendor/plugins/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /public/stylesheets/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.rvmrc: -------------------------------------------------------------------------------- 1 | rvm use ree-1.8.7-2010.02@devise-example 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .bundle 2 | db/*.sqlite3 3 | log/*.log 4 | tmp/**/* 5 | .idea 6 | -------------------------------------------------------------------------------- /config/initializers/generators.rb: -------------------------------------------------------------------------------- 1 | Rails.application.config.generators do |g| 2 | end 3 | -------------------------------------------------------------------------------- /spec/fixtures/files/photo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/spec/fixtures/files/photo.png -------------------------------------------------------------------------------- /test/unit/helpers/monkeys_helper_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class MonkeysHelperTest < ActionView::TestCase 4 | end 5 | -------------------------------------------------------------------------------- /public/images/social-icons/aol_128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/aol_128.png -------------------------------------------------------------------------------- /public/images/social-icons/aol_256.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/aol_256.png -------------------------------------------------------------------------------- /public/images/social-icons/aol_32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/aol_32.png -------------------------------------------------------------------------------- /public/images/social-icons/aol_64.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/aol_64.png -------------------------------------------------------------------------------- /app/controllers/sessions_controller.rb: -------------------------------------------------------------------------------- 1 | class SessionsController < Devise::RegistrationsController 2 | disable_store_location :new 3 | end 4 | -------------------------------------------------------------------------------- /app/models/sharing.rb: -------------------------------------------------------------------------------- 1 | class Sharing < ActiveRecord::Base 2 | belongs_to :user 3 | validates_length_of :content, :minimum => 5 4 | end 5 | -------------------------------------------------------------------------------- /app/views/sharings/_form.html.erb: -------------------------------------------------------------------------------- 1 | <%= simple_form_for(@sharing) do |f| %> 2 | <%= f.input :content %> 3 | <%= f.submit %> 4 | <% end %> 5 | -------------------------------------------------------------------------------- /public/images/social-icons/github_32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/github_32.png -------------------------------------------------------------------------------- /public/images/social-icons/github_64.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/github_64.png -------------------------------------------------------------------------------- /public/images/social-icons/google_32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/google_32.png -------------------------------------------------------------------------------- /public/images/social-icons/openid_32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/openid_32.png -------------------------------------------------------------------------------- /public/images/social-icons/openid_64.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/openid_64.png -------------------------------------------------------------------------------- /public/images/social-icons/yahoo_128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/yahoo_128.png -------------------------------------------------------------------------------- /public/images/social-icons/yahoo_256.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/yahoo_256.png -------------------------------------------------------------------------------- /public/images/social-icons/yahoo_32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/yahoo_32.png -------------------------------------------------------------------------------- /public/images/social-icons/yahoo_64.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/yahoo_64.png -------------------------------------------------------------------------------- /public/system/photos/1/big/pt_normal.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/system/photos/1/big/pt_normal.jpg -------------------------------------------------------------------------------- /public/system/photos/2/big/pt_normal.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/system/photos/2/big/pt_normal.jpg -------------------------------------------------------------------------------- /public/system/photos/4/big/pt_normal.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/system/photos/4/big/pt_normal.jpg -------------------------------------------------------------------------------- /app/models/nil_user.rb: -------------------------------------------------------------------------------- 1 | class NilUser 2 | def user_tokens 3 | [] 4 | end 5 | 6 | def nil? 7 | true 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /public/images/social-icons/basecamp_128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/basecamp_128.png -------------------------------------------------------------------------------- /public/images/social-icons/basecamp_256.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/basecamp_256.png -------------------------------------------------------------------------------- /public/images/social-icons/basecamp_32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/basecamp_32.png -------------------------------------------------------------------------------- /public/images/social-icons/basecamp_64.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/basecamp_64.png -------------------------------------------------------------------------------- /public/images/social-icons/campfire_128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/campfire_128.png -------------------------------------------------------------------------------- /public/images/social-icons/campfire_256.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/campfire_256.png -------------------------------------------------------------------------------- /public/images/social-icons/campfire_32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/campfire_32.png -------------------------------------------------------------------------------- /public/images/social-icons/campfire_64.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/campfire_64.png -------------------------------------------------------------------------------- /public/images/social-icons/facebook_128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/facebook_128.png -------------------------------------------------------------------------------- /public/images/social-icons/facebook_256.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/facebook_256.png -------------------------------------------------------------------------------- /public/images/social-icons/facebook_32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/facebook_32.png -------------------------------------------------------------------------------- /public/images/social-icons/facebook_64.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/facebook_64.png -------------------------------------------------------------------------------- /public/images/social-icons/github_128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/github_128.png -------------------------------------------------------------------------------- /public/images/social-icons/github_256.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/github_256.png -------------------------------------------------------------------------------- /public/images/social-icons/google_128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/google_128.png -------------------------------------------------------------------------------- /public/images/social-icons/google_256.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/google_256.png -------------------------------------------------------------------------------- /public/images/social-icons/linkedin_128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/linkedin_128.png -------------------------------------------------------------------------------- /public/images/social-icons/linkedin_256.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/linkedin_256.png -------------------------------------------------------------------------------- /public/images/social-icons/linkedin_32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/linkedin_32.png -------------------------------------------------------------------------------- /public/images/social-icons/linkedin_64.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/linkedin_64.png -------------------------------------------------------------------------------- /public/images/social-icons/myspace_128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/myspace_128.png -------------------------------------------------------------------------------- /public/images/social-icons/myspace_256.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/myspace_256.png -------------------------------------------------------------------------------- /public/images/social-icons/myspace_32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/myspace_32.png -------------------------------------------------------------------------------- /public/images/social-icons/myspace_64.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/myspace_64.png -------------------------------------------------------------------------------- /public/images/social-icons/openid_128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/openid_128.png -------------------------------------------------------------------------------- /public/images/social-icons/openid_256.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/openid_256.png -------------------------------------------------------------------------------- /public/images/social-icons/presently_32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/presently_32.png -------------------------------------------------------------------------------- /public/images/social-icons/presently_64.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/presently_64.png -------------------------------------------------------------------------------- /public/images/social-icons/twitter_128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/twitter_128.png -------------------------------------------------------------------------------- /public/images/social-icons/twitter_256.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/twitter_256.png -------------------------------------------------------------------------------- /public/images/social-icons/twitter_32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/twitter_32.png -------------------------------------------------------------------------------- /public/images/social-icons/twitter_64.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/twitter_64.png -------------------------------------------------------------------------------- /public/images/user_photos/missing_big.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/user_photos/missing_big.png -------------------------------------------------------------------------------- /public/images/user_photos/missing_mini.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/user_photos/missing_mini.png -------------------------------------------------------------------------------- /public/images/user_photos/missing_small.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/user_photos/missing_small.png -------------------------------------------------------------------------------- /public/images/user_photos/missing_thumb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/user_photos/missing_thumb.png -------------------------------------------------------------------------------- /public/system/photos/1/mini/pt_normal.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/system/photos/1/mini/pt_normal.jpg -------------------------------------------------------------------------------- /public/system/photos/1/small/pt_normal.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/system/photos/1/small/pt_normal.jpg -------------------------------------------------------------------------------- /public/system/photos/1/thumb/pt_normal.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/system/photos/1/thumb/pt_normal.jpg -------------------------------------------------------------------------------- /public/system/photos/2/mini/pt_normal.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/system/photos/2/mini/pt_normal.jpg -------------------------------------------------------------------------------- /public/system/photos/2/small/pt_normal.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/system/photos/2/small/pt_normal.jpg -------------------------------------------------------------------------------- /public/system/photos/2/thumb/pt_normal.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/system/photos/2/thumb/pt_normal.jpg -------------------------------------------------------------------------------- /public/system/photos/4/mini/pt_normal.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/system/photos/4/mini/pt_normal.jpg -------------------------------------------------------------------------------- /public/system/photos/4/small/pt_normal.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/system/photos/4/small/pt_normal.jpg -------------------------------------------------------------------------------- /public/system/photos/4/thumb/pt_normal.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/system/photos/4/thumb/pt_normal.jpg -------------------------------------------------------------------------------- /public/images/social-icons/google_64 copy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/google_64 copy.png -------------------------------------------------------------------------------- /public/images/social-icons/google_apps_32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/google_apps_32.png -------------------------------------------------------------------------------- /public/images/social-icons/google_apps_64.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/google_apps_64.png -------------------------------------------------------------------------------- /public/images/social-icons/presently_128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/presently_128.png -------------------------------------------------------------------------------- /public/images/social-icons/presently_256.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/presently_256.png -------------------------------------------------------------------------------- /public/system/photos/1/original/pt_normal.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/system/photos/1/original/pt_normal.jpg -------------------------------------------------------------------------------- /public/system/photos/2/original/pt_normal.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/system/photos/2/original/pt_normal.jpg -------------------------------------------------------------------------------- /public/system/photos/4/original/pt_normal.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/system/photos/4/original/pt_normal.jpg -------------------------------------------------------------------------------- /public/images/social-icons/google_128 copy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/google_128 copy.png -------------------------------------------------------------------------------- /public/images/social-icons/google_256 copy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/social-icons/google_256 copy.png -------------------------------------------------------------------------------- /public/images/user_photos/missing_original.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/images/user_photos/missing_original.png -------------------------------------------------------------------------------- /public/system/photos/1/big/48909_614285738_8043_q.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/system/photos/1/big/48909_614285738_8043_q.jpg -------------------------------------------------------------------------------- /public/system/photos/1/mini/48909_614285738_8043_q.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/system/photos/1/mini/48909_614285738_8043_q.jpg -------------------------------------------------------------------------------- /public/system/photos/1/small/48909_614285738_8043_q.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/system/photos/1/small/48909_614285738_8043_q.jpg -------------------------------------------------------------------------------- /public/system/photos/1/thumb/48909_614285738_8043_q.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/system/photos/1/thumb/48909_614285738_8043_q.jpg -------------------------------------------------------------------------------- /public/system/photos/1/original/48909_614285738_8043_q.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pt/devise-omniauth-example/HEAD/public/system/photos/1/original/48909_614285738_8043_q.jpg -------------------------------------------------------------------------------- /public/javascripts/application.js: -------------------------------------------------------------------------------- 1 | // Place your application-specific JavaScript functions and classes here 2 | // This file is automatically included by javascript_include_tag :defaults 3 | -------------------------------------------------------------------------------- /config.ru: -------------------------------------------------------------------------------- 1 | # This file is used by Rack-based servers to start the application. 2 | 3 | require ::File.expand_path('../config/environment', __FILE__) 4 | run OmniauthDeviseExample::Application 5 | -------------------------------------------------------------------------------- /app/views/sharings/new.html.erb: -------------------------------------------------------------------------------- 1 |

Share something to your networks

2 | 3 |
4 |

Post to connected networks!

5 | <%= render :partial => 'form' %> 6 |
-------------------------------------------------------------------------------- /test/unit/user_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class UserTest < ActiveSupport::TestCase 4 | # Replace this with your real tests. 5 | test "the truth" do 6 | assert true 7 | end 8 | end 9 | -------------------------------------------------------------------------------- /test/unit/monkey_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class MonkeyTest < ActiveSupport::TestCase 4 | # Replace this with your real tests. 5 | test "the truth" do 6 | assert true 7 | end 8 | end 9 | -------------------------------------------------------------------------------- /db/migrate/20110326070436_add_user_to_sharing.rb: -------------------------------------------------------------------------------- 1 | class AddUserToSharing < ActiveRecord::Migration 2 | def self.up 3 | add_column :sharings, :user_id, :integer 4 | end 5 | 6 | def self.down 7 | end 8 | end 9 | -------------------------------------------------------------------------------- /test/fixtures/monkeys.yml: -------------------------------------------------------------------------------- 1 | # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html 2 | 3 | one: 4 | name: MyString 5 | desc: MyText 6 | 7 | two: 8 | name: MyString 9 | desc: MyText 10 | -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # See http://www.robotstxt.org/wc/norobots.html for documentation on how to use the robots.txt file 2 | # 3 | # To ban all spiders from the entire site uncomment the next two lines: 4 | # User-Agent: * 5 | # Disallow: / 6 | -------------------------------------------------------------------------------- /app/controllers/profiles_controller.rb: -------------------------------------------------------------------------------- 1 | class ProfilesController < ApplicationController 2 | def show 3 | @user = User.find_by_slug(params[:id]) 4 | 5 | if @user.nil? 6 | redirect_to '/' 7 | end 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /config/initializers/mime_types.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Add new mime types for use in respond_to blocks: 4 | # Mime::Type.register "text/richtext", :rtf 5 | # Mime::Type.register_alias "text/html", :iphone 6 | -------------------------------------------------------------------------------- /db/migrate/20110325233835_add_slugs_to_user.rb: -------------------------------------------------------------------------------- 1 | class AddSlugsToUser < ActiveRecord::Migration 2 | def self.up 3 | add_column :users, :slug, :string 4 | end 5 | 6 | def self.down 7 | remove_column :users, :slug 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /app/models/omni_auth_populator.rb: -------------------------------------------------------------------------------- 1 | module OmniAuthPopulator 2 | def omniauth=(omni) 3 | if omni['provider'] && respond_to?('populate_from_' + omni['provider']) 4 | send(('populate_from_' + omni['provider']).to_sym, omni) 5 | end 6 | end 7 | end -------------------------------------------------------------------------------- /app/views/devise/shared/_forgot_password.erb: -------------------------------------------------------------------------------- 1 |
2 | <%- if devise_mapping.recoverable? && controller_name != 'passwords' %> 3 | <%= link_to "Forgot your password?", new_password_path(resource_name) %> 4 |
5 | <% end -%> 6 |
-------------------------------------------------------------------------------- /db/migrate/20110325235418_add_username_to_provider.rb: -------------------------------------------------------------------------------- 1 | class AddUsernameToProvider < ActiveRecord::Migration 2 | def self.up 3 | add_column :user_tokens, :nickname, :string 4 | end 5 | 6 | def self.down 7 | remove_column :user_tokens, :nickname 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /app/views/devise/mailer/confirmation_instructions.html.erb: -------------------------------------------------------------------------------- 1 |

Welcome <%= @resource.email %>!

2 | 3 |

You can confirm your account through the link below:

4 | 5 |

<%= link_to 'Confirm my account', confirmation_url(@resource, :confirmation_token => @resource.confirmation_token) %>

6 | -------------------------------------------------------------------------------- /test/performance/browsing_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | require 'rails/performance_test_help' 3 | 4 | # Profiling results for each test method are written to tmp/performance. 5 | class BrowsingTest < ActionDispatch::PerformanceTest 6 | def test_homepage 7 | get '/' 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | # Add your own tasks in files placed in lib/tasks ending in .rake, 2 | # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. 3 | 4 | require File.expand_path('../config/application', __FILE__) 5 | require 'rake' 6 | 7 | OmniauthDeviseExample::Application.load_tasks 8 | -------------------------------------------------------------------------------- /db/migrate/20110324053313_create_sharings.rb: -------------------------------------------------------------------------------- 1 | class CreateSharings < ActiveRecord::Migration 2 | def self.up 3 | create_table :sharings do |t| 4 | t.string :content 5 | 6 | t.timestamps 7 | end 8 | end 9 | 10 | def self.down 11 | drop_table :sharings 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /script/rails: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application. 3 | 4 | APP_PATH = File.expand_path('../../config/application', __FILE__) 5 | require File.expand_path('../../config/boot', __FILE__) 6 | require 'rails/commands' 7 | -------------------------------------------------------------------------------- /app/views/devise/shared/_social_connected.erb: -------------------------------------------------------------------------------- 1 |
2 | 7 |
8 |
-------------------------------------------------------------------------------- /app/controllers/application_controller.rb: -------------------------------------------------------------------------------- 1 | class ApplicationController < ActionController::Base 2 | include RedirectBack 3 | 4 | helper_method :resource_class 5 | protect_from_forgery 6 | 7 | def current_user 8 | super || NilUser.new 9 | end 10 | 11 | def user_signed_in? 12 | !current_user.nil? 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /app/views/devise/passwords/new.html.erb: -------------------------------------------------------------------------------- 1 |

Forgot your password?

2 | 3 | <%= simple_form_for(resource, :as => resource_name, :url => password_path(resource_name), :html => { :method => :post }) do |f| %> 4 | <%= devise_error_messages! %> 5 | 6 | <%= f.input :email, :label => "Email*" %> 7 | 8 |

<%= f.submit "Reset Password" %>

9 | <% end %> -------------------------------------------------------------------------------- /app/views/devise/mailer/unlock_instructions.html.erb: -------------------------------------------------------------------------------- 1 |

Hello <%= @resource.email %>!

2 | 3 |

Your account has been locked due to an excessive amount of unsuccessful sign in attempts.

4 | 5 |

Click the link below to unlock your account:

6 | 7 |

<%= link_to 'Unlock my account', unlock_url(@resource, :unlock_token => @resource.unlock_token) %>

8 | -------------------------------------------------------------------------------- /app/views/devise/shared/_social_connect_and_unconnect.erb: -------------------------------------------------------------------------------- 1 |
2 | Connect your profile to other networks: 3 | <%= render :partial => "devise/shared/social_unconnected" %> 4 |
5 | 6 |
7 | 8 |
9 |
10 | Currently connected networks: 11 | <%= render :partial => "devise/shared/social_connected" %> 12 |
13 | -------------------------------------------------------------------------------- /db/seeds.rb: -------------------------------------------------------------------------------- 1 | # This file should contain all the record creation needed to seed the database with its default values. 2 | # The data can then be loaded with the rake db:seed (or created alongside the db with db:setup). 3 | # 4 | # Examples: 5 | # 6 | # cities = City.create([{ :name => 'Chicago' }, { :name => 'Copenhagen' }]) 7 | # Mayor.create(:name => 'Daley', :city => cities.first) 8 | -------------------------------------------------------------------------------- /app/views/devise/shared/_social_unconnected.erb: -------------------------------------------------------------------------------- 1 | Us
2 | 7 |
8 |
-------------------------------------------------------------------------------- /config/boot.rb: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | 3 | # Set up gems listed in the Gemfile. 4 | gemfile = File.expand_path('../../Gemfile', __FILE__) 5 | begin 6 | ENV['BUNDLE_GEMFILE'] = gemfile 7 | require 'bundler' 8 | Bundler.setup 9 | rescue Bundler::GemNotFound => e 10 | STDERR.puts e.message 11 | STDERR.puts "Try running `bundle install`." 12 | exit! 13 | end if File.exist?(gemfile) 14 | -------------------------------------------------------------------------------- /spec/controllers/profiles_controller_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | # This spec was generated by rspec-rails when you ran the scaffold generator. 4 | # It demonstrates how one might use RSpec to specify the controller code that 5 | # was generated by the Rails when you ran the scaffold generator. 6 | 7 | describe SharingsController do 8 | 9 | describe "GET show" do 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /test/fixtures/users.yml: -------------------------------------------------------------------------------- 1 | # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html 2 | 3 | # This model initially had no columns defined. If you add columns to the 4 | # model remove the '{}' from the fixture names and add the columns immediately 5 | # below each fixture, per the syntax in the comments below 6 | # 7 | one: {} 8 | # column: value 9 | # 10 | two: {} 11 | # column: value 12 | -------------------------------------------------------------------------------- /app/views/profiles/show.html.erb: -------------------------------------------------------------------------------- 1 |

<%= @user.display_name %>'s Profile

2 | 3 | <%= image_tag @user.photo.url %>
4 | <%= image_tag @user.photo.url(:mini) %>
5 | <%= image_tag @user.photo.url(:thumb) %>
6 | <%= image_tag @user.photo.url(:small) %>
7 | <%= image_tag @user.photo.url(:big) %>
8 | 9 |
10 | Name: <%= @user.name %>
11 | email: <%= @user.email %>
12 |
-------------------------------------------------------------------------------- /db/migrate/20101106080918_create_user_tokens.rb: -------------------------------------------------------------------------------- 1 | class CreateUserTokens < ActiveRecord::Migration 2 | def self.up 3 | create_table :user_tokens do |t| 4 | t.integer :user_id 5 | t.string :provider 6 | t.string :uid 7 | t.string :token 8 | t.string :secret 9 | 10 | t.timestamps 11 | end 12 | end 13 | 14 | def self.down 15 | drop_table :user_tokens 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /config/initializers/inflections.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Add new inflection rules using the following format 4 | # (all these examples are active by default): 5 | # ActiveSupport::Inflector.inflections do |inflect| 6 | # inflect.plural /^(ox)$/i, '\1en' 7 | # inflect.singular /^(ox)en/i, '\1' 8 | # inflect.irregular 'person', 'people' 9 | # inflect.uncountable %w( fish sheep ) 10 | # end 11 | -------------------------------------------------------------------------------- /app/views/devise/unlocks/new.html.erb: -------------------------------------------------------------------------------- 1 |

Resend unlock instructions

2 | 3 | <%= form_for(resource, :as => resource_name, :url => unlock_path(resource_name), :html => { :method => :post }) do |f| %> 4 | <% devise_error_messages! %> 5 | 6 |

<%= f.label :email %>
7 | <%= f.email_field :email %>

8 | 9 |

<%= f.submit "Resend unlock instructions" %>

10 | <% end %> 11 | 12 | <%= render :partial => "devise/shared/links" %> -------------------------------------------------------------------------------- /config/initializers/backtrace_silencers.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces. 4 | # Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ } 5 | 6 | # You can also remove all the silencers if you're trying to debug a problem that might stem from framework code. 7 | # Rails.backtrace_cleaner.remove_silencers! 8 | -------------------------------------------------------------------------------- /spec/builders/fixjour.rb: -------------------------------------------------------------------------------- 1 | require 'fixjour' 2 | 3 | Fixjour do 4 | define_builder(User) do |klass, overrides| 5 | u = klass.new({:email => next_email_address, :name => 'P T', :password => 'password', :password_confirmation => 'password'}.merge(overrides)) 6 | u.confirmed_at = Time.now 7 | u 8 | end 9 | end 10 | 11 | def next_email_address 12 | @@email_addr_cnt ||= 1 13 | @@email_addr_cnt += 1 14 | "x#{@@email_addr_cnt}@example.com" 15 | end -------------------------------------------------------------------------------- /app/views/devise/shared/_social.erb: -------------------------------------------------------------------------------- 1 |
2 | <%- if devise_mapping.omniauthable? %> 3 | 8 |
9 | <% end -%> 10 |
-------------------------------------------------------------------------------- /app/controllers/registrations_controller.rb: -------------------------------------------------------------------------------- 1 | class RegistrationsController < Devise::RegistrationsController 2 | disable_store_location :new 3 | 4 | def edit 5 | super 6 | end 7 | 8 | def update 9 | if resource.update_attributes(params[resource_name]) 10 | set_flash_message :notice, :updated 11 | redirect_to profile_path(resource) 12 | else 13 | clean_up_passwords(resource) 14 | render 'edit' 15 | end 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /app/views/devise/confirmations/new.html.erb: -------------------------------------------------------------------------------- 1 |

Resend confirmation instructions

2 | 3 | <%= form_for(resource, :as => resource_name, :url => confirmation_path(resource_name), :html => { :method => :post }) do |f| %> 4 | <%= devise_error_messages! %> 5 | 6 |

<%= f.label :email %>
7 | <%= f.email_field :email %>

8 | 9 |

<%= f.submit "Resend confirmation instructions" %>

10 | <% end %> 11 | 12 | <%= render :partial => "devise/shared/links" %> -------------------------------------------------------------------------------- /app/views/devise/mailer/reset_password_instructions.html.erb: -------------------------------------------------------------------------------- 1 |

Hello <%= @resource.email %>!

2 | 3 |

Someone has requested a link to change your password, and you can do this through the link below.

4 | 5 |

<%= link_to 'Change my password', edit_password_url(@resource, :reset_password_token => @resource.reset_password_token) %>

6 | 7 |

If you didn't request this, please ignore this email.

8 |

Your password won't change until you access the link above and create a new one.

9 | -------------------------------------------------------------------------------- /app/models/user_token.rb: -------------------------------------------------------------------------------- 1 | class UserToken < ActiveRecord::Base 2 | include OmniAuthPopulator 3 | 4 | belongs_to :user 5 | 6 | def populate_from_twitter(omni) 7 | self.secret = omni['credentials']['secret'] 8 | self.token = omni['credentials']['token'] 9 | self.nickname = omni['user_info']['nickname'] 10 | end 11 | 12 | def populate_from_facebook(omni) 13 | self.token = omni['credentials']['token'] 14 | self.nickname = omni['user_info']['nickname'] 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /config/initializers/session_store.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | OmniauthDeviseExample::Application.config.session_store :cookie_store, :key => '_omniauth-devise-example_session' 4 | 5 | # Use the database for sessions instead of the cookie-based default, 6 | # which shouldn't be used to store highly confidential information 7 | # (create the session table with "rake db:sessions:create") 8 | # OmniauthDeviseExample::Application.config.session_store :active_record_store 9 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'http://rubygems.org' 2 | 3 | gem 'rake' 4 | gem 'rails', '3.0.5' 5 | gem 'sqlite3-ruby', :require => 'sqlite3' 6 | gem 'devise', :git => 'git://github.com/plataformatec/devise', :branch => 'master' 7 | gem 'omniauth', '0.2.0' 8 | gem 'paperclip' 9 | gem "simple_form", "~> 1.2.2" 10 | gem 'twitter_oauth', '0.4.3' 11 | gem "rest-client", "1.6.1", :require => "restclient" 12 | gem "sluggable" 13 | 14 | 15 | group :development, :test do 16 | gem 'rspec-rails' 17 | gem 'fixjour' 18 | end 19 | 20 | -------------------------------------------------------------------------------- /test/test_helper.rb: -------------------------------------------------------------------------------- 1 | ENV["RAILS_ENV"] = "test" 2 | require File.expand_path('../../config/environment', __FILE__) 3 | require 'rails/test_help' 4 | 5 | class ActiveSupport::TestCase 6 | # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order. 7 | # 8 | # Note: You'll currently still have to declare fixtures explicitly in integration tests 9 | # -- they do not yet inherit this setting 10 | fixtures :all 11 | 12 | # Add more helper methods to be used by all tests here... 13 | end 14 | -------------------------------------------------------------------------------- /app/helpers/application_helper.rb: -------------------------------------------------------------------------------- 1 | module ApplicationHelper 2 | 3 | def connected_providers_for(user) 4 | user.user_tokens.collect{|u| u.provider.to_sym } 5 | end 6 | 7 | def unconnected_providers_for(user) 8 | User.omniauth_providers - user.user_tokens.collect{|u| u.provider.to_sym } 9 | end 10 | 11 | def notice_html 12 | "
#{notice}
" unless notice.blank? 13 | end 14 | 15 | def alert_html 16 | "
#{alert}
" unless alert.blank? 17 | end 18 | 19 | end 20 | -------------------------------------------------------------------------------- /config/initializers/secret_token.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Your secret key for verifying the integrity of signed cookies. 4 | # If you change this key, all old signed cookies will become invalid! 5 | # Make sure the secret is at least 30 characters and all random, 6 | # no regular words or you'll be exposed to dictionary attacks. 7 | OmniauthDeviseExample::Application.config.secret_token = '384ed9ce6c937b2f8b25cbb076196b96bbffb6dcb12008a27a13e3b48d62abad1a68ff35fad114b08d706886ac74b1cad0d7e6a527cb930280e73a654abc316e' 8 | -------------------------------------------------------------------------------- /db/migrate/20110320031947_add_metadata_to_user.rb: -------------------------------------------------------------------------------- 1 | class AddMetadataToUser < ActiveRecord::Migration 2 | def self.up 3 | add_column :users, :name, :string 4 | 5 | add_column :users, :photo_file_name, :string # Original filename 6 | add_column :users, :photo_content_type, :string # Mime type 7 | add_column :users, :photo_file_size, :integer # File size in bytes 8 | end 9 | 10 | def self.down 11 | remove_column :users, :photo_file_name 12 | remove_column :users, :photo_content_type 13 | remove_column :users, :photo_file_size 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /config/database.yml: -------------------------------------------------------------------------------- 1 | # SQLite version 3.x 2 | # gem install sqlite3-ruby (not necessary on OS X Leopard) 3 | development: 4 | adapter: sqlite3 5 | database: db/dev.sqlite3 6 | pool: 5 7 | timeout: 5000 8 | 9 | # Warning: The database defined as "test" will be erased and 10 | # re-generated from your development database when you run "rake". 11 | # Do not set this db to the same as development or production. 12 | test: 13 | adapter: sqlite3 14 | database: db/test.sqlite3 15 | pool: 5 16 | timeout: 5000 17 | 18 | production: 19 | adapter: sqlite3 20 | database: db/production.sqlite3 21 | pool: 5 22 | timeout: 5000 23 | -------------------------------------------------------------------------------- /app/views/devise/passwords/edit.html.erb: -------------------------------------------------------------------------------- 1 |

Change your password

2 | 3 | <%= form_for(resource, :as => resource_name, :url => password_path(resource_name), :html => { :method => :put }) do |f| %> 4 | <% devise_error_messages! %> 5 | <%= f.hidden_field :reset_password_token %> 6 | 7 |

<%= f.label :password, "New password" %>
8 | <%= f.password_field :password %>

9 | 10 |

<%= f.label :password_confirmation, "Confirm new password" %>
11 | <%= f.password_field :password_confirmation %>

12 | 13 |

<%= f.submit "Change my password" %>

14 | <% end %> 15 | 16 | <%= render :partial => "devise/shared/links" %> -------------------------------------------------------------------------------- /app/views/sessions/new.html.erb: -------------------------------------------------------------------------------- 1 |

Sign in

2 | 3 | <%= simple_form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %> 4 | 5 | <%= f.input :email, :label => "Email*" %> 6 | <%= f.input :password, :label => "Password*" %> 7 | 8 | <% if devise_mapping.rememberable? -%> 9 | <%= f.input :remember_me, :label => "Always keep me logged in", :as => :boolean %> 10 | <% end -%> 11 | 12 |

<%= f.submit "Sign in" %>

13 | 14 | <% end %> 15 | 16 |

Sign in with

17 | <%= render :partial => "devise/shared/social" %> 18 | 19 | <%= render :partial => "devise/shared/forgot_password" %> 20 | 21 | -------------------------------------------------------------------------------- /app/views/registrations/new.html.erb: -------------------------------------------------------------------------------- 1 |

Sign up with

2 | <%= render :partial => "devise/shared/social" %> 3 | 4 |

The Old Fashioned Way

5 | 6 | <%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :multipart => true }) do |f| %> 7 | <%= devise_error_messages! %> 8 | 9 | <%= f.input :name, :label => "Name" %> 10 | <%= f.input :email, :label => "Email*" %> 11 | <%= f.input :password, :label => "Password*" %> 12 | <%= f.input :password_confirmation, :label => "Password Confirmation*" %> 13 | <%= f.input :photo, :label => "Profile Photo" %> 14 | 15 |

<%= f.submit "Sign up" %>

16 | <% end %> 17 | 18 | -------------------------------------------------------------------------------- /spec/controllers/sharings_controller_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | # This spec was generated by rspec-rails when you ran the scaffold generator. 4 | # It demonstrates how one might use RSpec to specify the controller code that 5 | # was generated by the Rails when you ran the scaffold generator. 6 | 7 | describe SharingsController do 8 | 9 | def mock_sharing(stubs={}) 10 | @mock_sharing ||= mock_model(Sharing, stubs).as_null_object 11 | end 12 | 13 | describe "GET index" do 14 | it "assigns all sharings as @sharings" do 15 | Sharing.stub(:all) { [mock_sharing] } 16 | get :index 17 | assigns(:sharings).should eq([mock_sharing]) 18 | end 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /spec/helpers/application_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe ApplicationHelper do 4 | before do 5 | @user = new_user 6 | @user.apply_omniauth(omniauth_twitter) 7 | @user.apply_omniauth(omniauth_facebook) 8 | end 9 | 10 | describe "#connected_providers_for" do 11 | it "should return providers for which the user has provider tokens" do 12 | helper.connected_providers_for(@user).should include(:facebook) 13 | helper.connected_providers_for(@user).should include(:twitter) 14 | end 15 | 16 | it "should NOT return providers for which the user does not have provider tokens" do 17 | helper.connected_providers_for(@user).should_not include(:google) 18 | end 19 | end 20 | 21 | end -------------------------------------------------------------------------------- /db/migrate/20101105152126_devise_create_users.rb: -------------------------------------------------------------------------------- 1 | class DeviseCreateUsers < ActiveRecord::Migration 2 | def self.up 3 | create_table(:users) do |t| 4 | t.database_authenticatable :null => false 5 | t.recoverable 6 | t.rememberable 7 | t.trackable 8 | 9 | t.confirmable 10 | # t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both 11 | # t.token_authenticatable 12 | 13 | 14 | t.timestamps 15 | end 16 | 17 | add_index :users, :email, :unique => true 18 | add_index :users, :reset_password_token, :unique => true 19 | # add_index :users, :confirmation_token, :unique => true 20 | # add_index :users, :unlock_token, :unique => true 21 | end 22 | 23 | def self.down 24 | drop_table :users 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /public/422.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | The change you wanted was rejected (422) 5 | 17 | 18 | 19 | 20 | 21 |
22 |

The change you wanted was rejected.

23 |

Maybe you tried to change something you didn't have access to.

24 |
25 | 26 | 27 | -------------------------------------------------------------------------------- /public/404.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | The page you were looking for doesn't exist (404) 5 | 17 | 18 | 19 | 20 | 21 |
22 |

The page you were looking for doesn't exist.

23 |

You may have mistyped the address or the page may have moved.

24 |
25 | 26 | 27 | -------------------------------------------------------------------------------- /public/500.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | We're sorry, but something went wrong (500) 5 | 17 | 18 | 19 | 20 | 21 |
22 |

We're sorry, but something went wrong.

23 |

We've been notified about this issue and we'll take a look at it shortly.

24 |
25 | 26 | 27 | -------------------------------------------------------------------------------- /config/environment.rb: -------------------------------------------------------------------------------- 1 | # Load the rails application 2 | DEFAULT_PAPERCLIP_OPTIONS = {} 3 | 4 | DEVISE_MAILER_FROM = "please-change-me@example.com" 5 | LIVE_PERSONS_EMAIL = 'help@example.com' 6 | 7 | FACEBOOK_APP_ID = "213491825331116" 8 | FACEBOOK_APP_SECRET = "3bc4798fec78dc85235d543aa96e5f98" 9 | FACEBOOK_APP_PERMISSIONS = "email,offline_access,publish_stream" 10 | 11 | DEFAULT_FB_SHARE_IMAGE = "http://localhost:3000/images/missing.png" 12 | DEFAULT_FB_POST_NAME = "This Site's Name" 13 | 14 | DEFAULT_PAGE_TITLE = "This Site's Name" 15 | DEFAULT_PAGE_DESCRIPTION = "This Site's Name" 16 | 17 | TWITTER_SECRET_KEY = "L0LJGa5g4TWNUJfK9jmACNt3i2P2ykUw0TVbysQinIg" 18 | TWITTER_CONSUMER_KEY = "D4sOenvRrSaI1GIGTTEeSQ" 19 | 20 | DEFAULT_SHARE_URL = "http://localhost:3000" 21 | 22 | 23 | require File.expand_path('../application', __FILE__) 24 | 25 | # Initialize the rails application 26 | OmniauthDeviseExample::Application.initialize! -------------------------------------------------------------------------------- /app/controllers/sharings_controller.rb: -------------------------------------------------------------------------------- 1 | class SharingsController < ApplicationController 2 | 3 | before_filter :authenticate_user!, :only => [:create, :new] 4 | 5 | def index 6 | @sharings = Sharing.all(:order => "created_at desc") 7 | @sharing = Sharing.new 8 | end 9 | 10 | def show 11 | @sharing = Sharing.find(params[:id]) 12 | end 13 | 14 | def new 15 | @sharing = Sharing.new 16 | end 17 | 18 | def create 19 | @sharing = current_user.sharings.build(params[:sharing]) 20 | 21 | if @sharing.save 22 | if current_user 23 | current_user.tweet!(@sharing.content) if current_user.connected_to?(:twitter) 24 | current_user.fb_post!(@sharing.content) if current_user.connected_to?(:facebook) 25 | end 26 | 27 | redirect_to(:action => "index", :notice => 'Sharing was successfully created.') 28 | else 29 | @sharings = Sharing.all(:order => "created_at desc") 30 | render :action => "new" 31 | end 32 | end 33 | end 34 | -------------------------------------------------------------------------------- /app/views/registrations/edit.html.erb: -------------------------------------------------------------------------------- 1 |

Edit Profile

2 | 3 | <%= image_tag @user.photo.url(:thumb) %>
4 | 5 | <%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %> 6 | <% devise_error_messages! %> 7 | 8 |

About

9 | <%= f.input :name, :label => "Name" %> 10 | <%= f.input :email, :label => "Email" %> 11 | <%= f.input :slug, :label => "Personal URL" %> 12 | 13 |
14 |

Password

15 | <%= f.input :password, :label => "Password", 16 | :hint => "leave blank if you don't want to change it" %> 17 | 18 | <%= f.input :password_confirmation, :label => "Password Confirmation" %> 19 | 20 | <%= f.input :current_password, :label => "Current Password", 21 | :hint => "we need your current password to confirm a password change" %> 22 | 23 | <%= f.input :photo %> 24 | 25 |

<%= f.submit "Update" %>

26 | <% end %> 27 | 28 |
29 |
30 | <%= render :partial => "devise/shared/social_connect_and_unconnect" %> -------------------------------------------------------------------------------- /config/environments/development.rb: -------------------------------------------------------------------------------- 1 | OmniauthDeviseExample::Application.configure do 2 | # Settings specified here will take precedence over those in config/environment.rb 3 | 4 | # In the development environment your application's code is reloaded on 5 | # every request. This slows down response time but is perfect for development 6 | # since you don't have to restart the webserver when you make code changes. 7 | config.cache_classes = false 8 | 9 | # Log error messages when you accidentally call methods on nil. 10 | config.whiny_nils = true 11 | 12 | # Show full error reports and disable caching 13 | config.consider_all_requests_local = true 14 | config.action_view.debug_rjs = true 15 | config.action_controller.perform_caching = false 16 | 17 | # Don't care if the mailer can't send 18 | config.action_mailer.raise_delivery_errors = false 19 | 20 | # Print deprecation notices to the Rails logger 21 | config.active_support.deprecation = :log 22 | 23 | # Only use best-standards-support built into browsers 24 | config.action_dispatch.best_standards_support = :builtin 25 | end 26 | 27 | -------------------------------------------------------------------------------- /MIT-LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2009-2011 Holden Thomas. http://holdenthomas.com 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /app/controllers/redirect_back.rb: -------------------------------------------------------------------------------- 1 | module RedirectBack 2 | def self.included(a_controller) 3 | a_controller.module_eval do 4 | extend ClassMethods 5 | include InstanceMethods 6 | 7 | around_filter :store_location 8 | end 9 | end 10 | 11 | module ClassMethods 12 | def actions_not_storing_location 13 | @actions_not_storing_location ||= [] 14 | end 15 | 16 | def disable_store_location(*actions) 17 | actions_not_storing_location.concat(actions.collect(&:to_sym)) 18 | end 19 | end 20 | 21 | module InstanceMethods 22 | def store_location 23 | yield 24 | session["user_return_to"] = request.url if should_store_location? 25 | end 26 | 27 | def should_store_location? 28 | request.get? && !request.xhr? && (response_is_success_or_redirect?) && 29 | !self.class.actions_not_storing_location.include?(action_name.to_sym) 30 | end 31 | 32 | def response_is_success_or_redirect? 33 | response_code = response.status.to_s[0,3].to_i rescue 0 34 | response_code == 200 || (300..399).include?(response_code) 35 | end 36 | 37 | # def redirect_back(default = root_path) 38 | # redirect_to(session && session["user_return_to"] ? session["user_return_to"] : default) 39 | # end 40 | end 41 | end -------------------------------------------------------------------------------- /app/views/devise/shared/_links.erb: -------------------------------------------------------------------------------- 1 | <%- if controller_name != 'sessions' %> 2 | <%= link_to "Sign in", new_session_path(resource_name) %>
3 | <% end -%> 4 | 5 | <%- if devise_mapping.registerable? && controller_name != 'registrations' %> 6 | <%= link_to "Sign up", new_registration_path(resource_name) %>
7 | <% end -%> 8 | 9 | <%- if devise_mapping.confirmable? && controller_name != 'confirmations' %> 10 | <%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %>
11 | <% end -%> 12 | 13 | <%- if devise_mapping.lockable? && resource_class.unlock_strategy_enabled?(:email) && controller_name != 'unlocks' %> 14 | <%= link_to "Didn't receive unlock instructions?", new_unlock_path(resource_name) %>
15 | <% end -%> 16 | 17 |
18 | <%- if devise_mapping.omniauthable? %> 19 | 24 |
25 | <% end -%> 26 |
27 | 28 | <%= render :partial => 'devise/shared/forgot_password' %> -------------------------------------------------------------------------------- /public/stylesheets/reset.css: -------------------------------------------------------------------------------- 1 | /* http://meyerweb.com/eric/tools/css/reset/ 2 | v2.0 | 20110126 3 | License: none (public domain) 4 | */ 5 | 6 | html, body, div, span, applet, object, iframe, 7 | h1, h2, h3, h4, h5, h6, p, blockquote, pre, 8 | a, abbr, acronym, address, big, cite, code, 9 | del, dfn, em, img, ins, kbd, q, s, samp, 10 | small, strike, strong, sub, sup, tt, var, 11 | b, u, i, center, 12 | dl, dt, dd, ol, ul, li, 13 | fieldset, form, label, legend, 14 | table, caption, tbody, tfoot, thead, tr, th, td, 15 | article, aside, canvas, details, embed, 16 | figure, figcaption, footer, header, hgroup, 17 | menu, nav, output, ruby, section, summary, 18 | time, mark, audio, video { 19 | margin: 0; 20 | padding: 0; 21 | border: 0; 22 | font-size: 100%; 23 | font: inherit; 24 | vertical-align: baseline; 25 | } 26 | /* HTML5 display-role reset for older browsers */ 27 | article, aside, details, figcaption, figure, 28 | footer, header, hgroup, menu, nav, section { 29 | display: block; 30 | } 31 | body { 32 | line-height: 1; 33 | } 34 | ol, ul { 35 | list-style: none; 36 | } 37 | blockquote, q { 38 | quotes: none; 39 | } 40 | blockquote:before, blockquote:after, 41 | q:before, q:after { 42 | content: ''; 43 | content: none; 44 | } 45 | table { 46 | border-collapse: collapse; 47 | border-spacing: 0; 48 | } -------------------------------------------------------------------------------- /test/functional/monkeys_controller_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class MonkeysControllerTest < ActionController::TestCase 4 | setup do 5 | @monkey = monkeys(:one) 6 | end 7 | 8 | test "should get index" do 9 | get :index 10 | assert_response :success 11 | assert_not_nil assigns(:monkeys) 12 | end 13 | 14 | test "should get new" do 15 | get :new 16 | assert_response :success 17 | end 18 | 19 | test "should create monkey" do 20 | assert_difference('Monkey.count') do 21 | post :create, :monkey => @monkey.attributes 22 | end 23 | 24 | assert_redirected_to monkey_path(assigns(:monkey)) 25 | end 26 | 27 | test "should show monkey" do 28 | get :show, :id => @monkey.to_param 29 | assert_response :success 30 | end 31 | 32 | test "should get edit" do 33 | get :edit, :id => @monkey.to_param 34 | assert_response :success 35 | end 36 | 37 | test "should update monkey" do 38 | put :update, :id => @monkey.to_param, :monkey => @monkey.attributes 39 | assert_redirected_to monkey_path(assigns(:monkey)) 40 | end 41 | 42 | test "should destroy monkey" do 43 | assert_difference('Monkey.count', -1) do 44 | delete :destroy, :id => @monkey.to_param 45 | end 46 | 47 | assert_redirected_to monkeys_path 48 | end 49 | end 50 | -------------------------------------------------------------------------------- /public/stylesheets/scaffold.css: -------------------------------------------------------------------------------- 1 | /*body { background-color: #fff; color: #333; }*/ 2 | 3 | /*body, p, ol, ul, td {*/ 4 | /*font-family: verdana, arial, helvetica, sans-serif;*/ 5 | /*font-size: 13px;*/ 6 | /*line-height: 18px;*/ 7 | /*}*/ 8 | 9 | /*pre {*/ 10 | /*background-color: #eee;*/ 11 | /*padding: 10px;*/ 12 | /*font-size: 11px;*/ 13 | /*}*/ 14 | 15 | /*a { color: #000; }*/ 16 | /*a:visited { color: #666; }*/ 17 | /*a:hover { color: #fff; background-color:#000; }*/ 18 | 19 | /*div.field, div.actions {*/ 20 | /*margin-bottom: 10px;*/ 21 | /*}*/ 22 | 23 | /*#notice {*/ 24 | /*color: green;*/ 25 | /*}*/ 26 | 27 | /*.field_with_errors {*/ 28 | /*padding: 2px;*/ 29 | /*background-color: red;*/ 30 | /*display: table;*/ 31 | /*}*/ 32 | 33 | /*#error_explanation {*/ 34 | /*width: 450px;*/ 35 | /*border: 2px solid red;*/ 36 | /*padding: 7px;*/ 37 | /*padding-bottom: 0;*/ 38 | /*margin-bottom: 20px;*/ 39 | /*background-color: #f0f0f0;*/ 40 | /*}*/ 41 | 42 | /*#error_explanation h2 {*/ 43 | /*text-align: left;*/ 44 | /*font-weight: bold;*/ 45 | /*padding: 5px 5px 5px 15px;*/ 46 | /*font-size: 12px;*/ 47 | /*margin: -7px;*/ 48 | /*margin-bottom: 0px;*/ 49 | /*background-color: #c00;*/ 50 | /*color: #fff;*/ 51 | /*}*/ 52 | 53 | /*#error_explanation ul li {*/ 54 | /*font-size: 12px;*/ 55 | /*list-style: square;*/ 56 | /*}*/ 57 | -------------------------------------------------------------------------------- /README.rdoc: -------------------------------------------------------------------------------- 1 | The "lived in" version: 2 | 3 | - allows creation of account with no password via omniauth providers 4 | - stores provider auth tokens for future use 5 | - populates name and email from data from providers if blank 6 | - adds photos, extracted from auth'd account(s) 7 | - allows posting to fb and twitter using tokens 8 | - user settable slugs & vanity URLs 9 | - css reset 10 | - profile pages 11 | - added nil user object 12 | - adds friendly display_name to user 13 | - always redirects :back after login/signup 14 | - app always remembers the last location, fixing flows, e.g. connecting an account when logged in redirects back 15 | - don't store locaions on xhrs (pending) 16 | - removes password to edit requirement 17 | - add fb meta tags example 18 | - tweet and like buttons 19 | 20 | Coming "soon": 21 | 22 | - adds basic css 23 | - split out change password from registrations edit 24 | - patch simple_form to show all errors on a field 25 | 26 | ----------- 27 | 28 | == Rails3 Omniauth-Devise supercombo 29 | 30 | As bare as you could get devise-omniauth combo 31 | 32 | I wasn't sure why all the provider callbacks were separated as they can all be handled in the same fashion. 33 | 34 | The Wiki also didn't mention how we should be keeping track of the user tokens in the case we want to allow the user to validate multiple providers. 35 | 36 | So I created a basic model storing the user_id, provider, & uid. 37 | 38 | https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview 39 | 40 | 41 | -------------------------------------------------------------------------------- /app/views/layouts/application.html.erb: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | OmniauthDeviseExample 5 | <%= stylesheet_link_tag 'scaffold' %> 6 | <%= stylesheet_link_tag 'reset' %> 7 | <%= stylesheet_link_tag 'application' %> 8 | <%= stylesheet_link_tag 'forms' %> 9 | <%= javascript_include_tag :defaults %> 10 | <%= csrf_meta_tag %> 11 | 12 | 13 | 14 | 15 | <%= content_for :page_specific_head %> 16 | 17 | 18 | 19 |
20 |
21 | 22 | <%= link_to 'Home', '/' %> 23 | <% if user_signed_in? %> 24 | <%= link_to 'Profile', profile_path(current_user) %> 25 | <%= link_to 'Edit Profile', edit_user_registration_path(current_user) %> 26 | <%= link_to "Sign out", destroy_user_session_path %> 27 | <% else %> 28 | <%= link_to "Sign up", new_user_registration_path %> 29 | <%= link_to "sign in", new_user_session_path %> 30 | <% end %> 31 |
32 | 33 |

Devise/OmniAuth demo

34 | <%= raw notice_html %> 35 | <%= raw alert_html %> 36 | 37 | <%= yield %> 38 | 39 |
40 | 41 | 44 | 45 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /app/views/sharings/show.html.erb: -------------------------------------------------------------------------------- 1 | <% content_for :page_specific_head do %> 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | <% end %> 10 | 11 |

<%= @sharing.user.display_name %> shared this

12 | 13 |
14 | <%= link_to image_tag(@sharing.user.photo.url(:mini), :class => 'share_image'), profile_path(@sharing.user) %> 15 |
16 | <%= @sharing.content %> 17 |
18 |
19 | <%= @sharing.created_at.strftime("%m/%d/%Y %I:%M%p GMT") %> | <%= link_to "permalink", sharing_path(@sharing) %> | 20 | 21 | 26 | 27 | 34 | 35 | 36 | 37 |
38 |
39 | 40 | <%= link_to "Share something yourself", new_sharing_path %> -------------------------------------------------------------------------------- /config/environments/test.rb: -------------------------------------------------------------------------------- 1 | OmniauthDeviseExample::Application.configure do 2 | # Settings specified here will take precedence over those in config/environment.rb 3 | 4 | # The test environment is used exclusively to run your application's 5 | # test suite. You never need to work with it otherwise. Remember that 6 | # your test database is "scratch space" for the test suite and is wiped 7 | # and recreated between test runs. Don't rely on the data there! 8 | config.cache_classes = true 9 | 10 | # Log error messages when you accidentally call methods on nil. 11 | config.whiny_nils = true 12 | 13 | # Show full error reports and disable caching 14 | config.consider_all_requests_local = true 15 | config.action_controller.perform_caching = false 16 | 17 | # Raise exceptions instead of rendering exception templates 18 | config.action_dispatch.show_exceptions = false 19 | 20 | # Disable request forgery protection in test environment 21 | config.action_controller.allow_forgery_protection = false 22 | 23 | # Tell Action Mailer not to deliver emails to the real world. 24 | # The :test delivery method accumulates sent emails in the 25 | # ActionMailer::Base.deliveries array. 26 | config.action_mailer.delivery_method = :test 27 | 28 | # Use SQL instead of Active Record's schema dumper when creating the test database. 29 | # This is necessary if your schema can't be completely dumped by the schema dumper, 30 | # like if you have constraints or database-specific column types 31 | # config.active_record.schema_format = :sql 32 | 33 | # Print deprecation notices to the stderr 34 | config.active_support.deprecation = :stderr 35 | end 36 | -------------------------------------------------------------------------------- /app/views/sharings/index.html.erb: -------------------------------------------------------------------------------- 1 |

Share something to your networks

2 | 3 | <% if !current_user.nil? %> 4 |
5 |

Post to connected networks!

6 | <%= render :partial => 'form' %> 7 |
8 | <% else %> 9 |
10 | <%= link_to "Sign up", new_user_registration_path %> or 11 | <%= link_to "sign in", new_user_session_path %> to share. 12 | <%= render :partial => "devise/shared/social_unconnected" %> 13 |
14 | <% end %> 15 | 16 |

What others have shared

17 | 18 | <% if @sharings.empty? %> 19 |

Nobody has shared yet. Be the first!

20 | <% end %> 21 | 22 | <% @sharings.each do |sharing| %> 23 |
24 | <%= link_to image_tag(sharing.user.photo.url(:mini), :class => 'share_image'), profile_path(sharing.user) %> 25 |
26 | <%= sharing.content %> 27 |
28 |
29 | <%= sharing.created_at.strftime("%m/%d/%Y %I:%M%p GMT") %> | <%= link_to "permalink", sharing_path(sharing) %> | 30 | 31 | 36 | 37 | 38 | 45 | 46 | 47 |
48 |
49 | 50 | <% end %> 51 | -------------------------------------------------------------------------------- /config/environments/production.rb: -------------------------------------------------------------------------------- 1 | OmniauthDeviseExample::Application.configure do 2 | # Settings specified here will take precedence over those in config/environment.rb 3 | 4 | # The production environment is meant for finished, "live" apps. 5 | # Code is not reloaded between requests 6 | config.cache_classes = true 7 | 8 | # Full error reports are disabled and caching is turned on 9 | config.consider_all_requests_local = false 10 | config.action_controller.perform_caching = true 11 | 12 | # Specifies the header that your server uses for sending files 13 | config.action_dispatch.x_sendfile_header = "X-Sendfile" 14 | 15 | # For nginx: 16 | # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' 17 | 18 | # If you have no front-end server that supports something like X-Sendfile, 19 | # just comment this out and Rails will serve the files 20 | 21 | # See everything in the log (default is :info) 22 | # config.log_level = :debug 23 | 24 | # Use a different logger for distributed setups 25 | # config.logger = SyslogLogger.new 26 | 27 | # Use a different cache store in production 28 | # config.cache_store = :mem_cache_store 29 | 30 | # Disable Rails's static asset server 31 | # In production, Apache or nginx will already do this 32 | config.serve_static_assets = false 33 | 34 | # Enable serving of images, stylesheets, and javascripts from an asset server 35 | # config.action_controller.asset_host = "http://assets.example.com" 36 | 37 | # Disable delivery errors, bad email addresses will be ignored 38 | # config.action_mailer.raise_delivery_errors = false 39 | 40 | # Enable threaded mode 41 | # config.threadsafe! 42 | 43 | # Enable locale fallbacks for I18n (makes lookups for any locale fall back to 44 | # the I18n.default_locale when a translation can not be found) 45 | config.i18n.fallbacks = true 46 | 47 | # Send deprecation notices to registered listeners 48 | config.active_support.deprecation = :notify 49 | end 50 | -------------------------------------------------------------------------------- /config/locales/devise.en.yml: -------------------------------------------------------------------------------- 1 | en: 2 | errors: 3 | messages: 4 | not_found: "not found" 5 | already_confirmed: "was already confirmed" 6 | not_locked: "was not locked" 7 | 8 | devise: 9 | failure: 10 | unauthenticated: 'You need to sign in or sign up before continuing.' 11 | unconfirmed: 'You have to confirm your account before continuing.' 12 | locked: 'Your account is locked.' 13 | invalid: 'Invalid email or password.' 14 | invalid_token: 'Invalid authentication token.' 15 | timeout: 'Your session expired, please sign in again to continue.' 16 | inactive: 'Your account was not activated yet.' 17 | sessions: 18 | signed_in: 'Signed in successfully.' 19 | signed_out: 'Signed out successfully.' 20 | passwords: 21 | send_instructions: 'You will receive an email with instructions about how to reset your password in a few minutes.' 22 | updated: 'Your password was changed successfully. You are now signed in.' 23 | confirmations: 24 | send_instructions: 'You will receive an email with instructions about how to confirm your account in a few minutes.' 25 | confirmed: 'Your account was successfully confirmed. You are now signed in.' 26 | registrations: 27 | signed_up: 'You have signed up successfully. If enabled, a confirmation was sent to your e-mail.' 28 | updated: 'You updated your account successfully.' 29 | destroyed: 'Bye! Your account was successfully cancelled. We hope to see you again soon.' 30 | unlocks: 31 | send_instructions: 'You will receive an email with instructions about how to unlock your account in a few minutes.' 32 | unlocked: 'Your account was successfully unlocked. You are now signed in.' 33 | mailer: 34 | confirmation_instructions: 35 | subject: 'Confirmation instructions' 36 | reset_password_instructions: 37 | subject: 'Reset password instructions' 38 | unlock_instructions: 39 | subject: 'Unlock Instructions' 40 | -------------------------------------------------------------------------------- /config/application.rb: -------------------------------------------------------------------------------- 1 | require File.expand_path('../boot', __FILE__) 2 | 3 | require 'rails/all' 4 | 5 | # If you have a Gemfile, require the gems listed there, including any gems 6 | # you've limited to :test, :development, or :production. 7 | Bundler.require(:default, Rails.env) if defined?(Bundler) 8 | 9 | module OmniauthDeviseExample 10 | class Application < Rails::Application 11 | # Settings in config/environments/* take precedence over those specified here. 12 | # Application configuration should go into files in config/initializers 13 | # -- all .rb files in that directory are automatically loaded. 14 | 15 | # Custom directories with classes and modules you want to be autoloadable. 16 | # config.autoload_paths += %W(#{config.root}/extras) 17 | 18 | # Only load the plugins named here, in the order given (default is alphabetical). 19 | # :all can be used as a placeholder for all plugins not explicitly named. 20 | # config.plugins = [ :exception_notification, :ssl_requirement, :all ] 21 | 22 | # Activate observers that should always be running. 23 | # config.active_record.observers = :cacher, :garbage_collector, :forum_observer 24 | 25 | # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. 26 | # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. 27 | # config.time_zone = 'Central Time (US & Canada)' 28 | 29 | # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. 30 | # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] 31 | # config.i18n.default_locale = :de 32 | 33 | # JavaScript files you want as :defaults (application.js is always included). 34 | # config.action_view.javascript_expansions[:defaults] = %w(jquery rails) 35 | 36 | # Configure the default encoding used in templates for Ruby 1.9. 37 | config.encoding = "utf-8" 38 | 39 | # Configure sensitive parameters which will be filtered from the log file. 40 | config.filter_parameters += [:password] 41 | end 42 | end 43 | -------------------------------------------------------------------------------- /config/routes.rb: -------------------------------------------------------------------------------- 1 | OmniauthDeviseExample::Application.routes.draw do 2 | resources :sharings 3 | devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks", :registrations => 'registrations'} 4 | 5 | # The priority is based upon order of creation: 6 | # first created -> highest priority. 7 | 8 | # Sample of regular route: 9 | # match 'products/:id' => 'catalog#view' 10 | # Keep in mind you can assign values other than :controller and :action 11 | 12 | # Sample of named route: 13 | # match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase 14 | # This route can be invoked with purchase_url(:id => product.id) 15 | 16 | # Sample resource route (maps HTTP verbs to controller actions automatically): 17 | # resources :products 18 | 19 | # Sample resource route with options: 20 | # resources :products do 21 | # member do 22 | # get 'short' 23 | # post 'toggle' 24 | # end 25 | # 26 | # collection do 27 | # get 'sold' 28 | # end 29 | # end 30 | 31 | # Sample resource route with sub-resources: 32 | # resources :products do 33 | # resources :comments, :sales 34 | # resource :seller 35 | # end 36 | 37 | # Sample resource route with more complex sub-resources 38 | # resources :products do 39 | # resources :comments 40 | # resources :sales do 41 | # get 'recent', :on => :collection 42 | # end 43 | # end 44 | 45 | # Sample resource route within a namespace: 46 | # namespace :admin do 47 | # # Directs /admin/products/* to Admin::ProductsController 48 | # # (app/controllers/admin/products_controller.rb) 49 | # resources :products 50 | # end 51 | 52 | # You can have the root of your site routed with "root" 53 | # just remember to delete public/index.html. 54 | root :to => "sharings#index" 55 | 56 | # See how all your routes lay out with "rake routes" 57 | 58 | # This is a legacy wild controller route that's not recommended for RESTful applications. 59 | # Note: This route will make all actions in every controller accessible via GET requests. 60 | # match ':controller(/:action(/:id(.:format)))' 61 | 62 | match '/:id', :controller => 'profiles', :action => 'show', :as => 'profile', :via => :get 63 | end 64 | -------------------------------------------------------------------------------- /public/stylesheets/application.css: -------------------------------------------------------------------------------- 1 | html, body { 2 | height: 100%; 3 | font-family:"lucida grande",tahoma,verdana,arial,sans-serif; 4 | } 5 | 6 | body { 7 | background-color: #EEE; 8 | } 9 | 10 | a { 11 | text-decoration: none; 12 | color: blue; 13 | } 14 | 15 | a:visited { 16 | color: green; 17 | } 18 | 19 | a:visited { 20 | color: green; 21 | } 22 | h1 { 23 | font-size: 40px; 24 | } 25 | 26 | h2 { 27 | font-size: 30px; 28 | margin: 20px 0; 29 | } 30 | 31 | h3 { 32 | font-size: 20px; 33 | margin: 10px 0; 34 | } 35 | 36 | #user_nav { 37 | text-align: right; 38 | padding-right: 15px; 39 | padding-bottom: 10px; 40 | } 41 | 42 | .page_content { 43 | width: 960px; 44 | min-height: 100%; 45 | height: auto !important; 46 | height: 100%; 47 | margin: 0 auto -45px; /* the bottom margin is the negative value of the footer's height */ 48 | background: white; 49 | border-left: 1px solid #ccc; 50 | border-right: 1px solid #ccc; 51 | padding: 10px 20px 0 20px; 52 | } 53 | 54 | .footer { 55 | width: 960px; 56 | margin: 0 auto; 57 | height: 45px; 58 | text-align: center; 59 | } 60 | 61 | 62 | 63 | ul#social-icons li { 64 | display: block; 65 | float: left; 66 | 67 | } 68 | 69 | .spacer { 70 | margin: 10px 0; 71 | font-size: 16px; 72 | font-style:italic; 73 | } 74 | 75 | .forgot-password { 76 | margin-top: 10px; 77 | clear: both; 78 | } 79 | 80 | h1.site-title { 81 | font-size: 50px; 82 | font-weight: bold; 83 | margin: 20px 0 60px 0; 84 | } 85 | 86 | div.notice, div.alert{ 87 | padding: 20px 20px; 88 | width: 600px; 89 | font-size: 20px; 90 | text-align: center; 91 | margin: 10px auto; 92 | color: black; 93 | } 94 | 95 | div.notice { 96 | background-color:aliceblue; 97 | } 98 | 99 | div.alert { 100 | background-color:lightsalmon; 101 | } 102 | 103 | div.share_text { 104 | float: left; 105 | margin: 0 0 0 10px; 106 | width: 600px; 107 | } 108 | 109 | div.sharing { 110 | padding-bottom: 40px; 111 | overflow: hidden; 112 | } 113 | 114 | .share_image { 115 | float: left; 116 | } 117 | div.sharing_form { 118 | background-color: #eee; 119 | padding: 15px; 120 | margin: 25px 0; 121 | } 122 | 123 | .sharing_signup_call_to_action { 124 | padding: 50px 0; 125 | font-size: 28px; 126 | } -------------------------------------------------------------------------------- /db/schema.rb: -------------------------------------------------------------------------------- 1 | # This file is auto-generated from the current state of the database. Instead 2 | # of editing this file, please use the migrations feature of Active Record to 3 | # incrementally modify your database, and then regenerate this schema definition. 4 | # 5 | # Note that this schema.rb definition is the authoritative source for your 6 | # database schema. If you need to create the application database on another 7 | # system, you should be using db:schema:load, not running all the migrations 8 | # from scratch. The latter is a flawed and unsustainable approach (the more migrations 9 | # you'll amass, the slower it'll run and the greater likelihood for issues). 10 | # 11 | # It's strongly recommended to check this file into your version control system. 12 | 13 | ActiveRecord::Schema.define(:version => 20110326070436) do 14 | 15 | create_table "sharings", :force => true do |t| 16 | t.string "content" 17 | t.datetime "created_at" 18 | t.datetime "updated_at" 19 | t.integer "user_id" 20 | end 21 | 22 | create_table "user_tokens", :force => true do |t| 23 | t.integer "user_id" 24 | t.string "provider" 25 | t.string "uid" 26 | t.string "token" 27 | t.string "secret" 28 | t.datetime "created_at" 29 | t.datetime "updated_at" 30 | t.string "nickname" 31 | end 32 | 33 | create_table "users", :force => true do |t| 34 | t.string "email", :default => "", :null => false 35 | t.string "encrypted_password", :limit => 128, :default => "", :null => false 36 | t.string "reset_password_token" 37 | t.string "remember_token" 38 | t.datetime "remember_created_at" 39 | t.integer "sign_in_count", :default => 0 40 | t.datetime "current_sign_in_at" 41 | t.datetime "last_sign_in_at" 42 | t.string "current_sign_in_ip" 43 | t.string "last_sign_in_ip" 44 | t.string "confirmation_token" 45 | t.datetime "confirmed_at" 46 | t.datetime "confirmation_sent_at" 47 | t.datetime "created_at" 48 | t.datetime "updated_at" 49 | t.string "name" 50 | t.string "photo_file_name" 51 | t.string "photo_content_type" 52 | t.integer "photo_file_size" 53 | t.string "slug" 54 | end 55 | 56 | add_index "users", ["email"], :name => "index_users_on_email", :unique => true 57 | add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true 58 | 59 | end 60 | -------------------------------------------------------------------------------- /app/controllers/users/omniauth_callbacks_controller.rb: -------------------------------------------------------------------------------- 1 | class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController 2 | attr_accessor :omniauth_data 3 | attr_accessor :preexisting_authorization_token 4 | 5 | before_filter :set_omniauth_data 6 | 7 | def method_missing(provider) 8 | return super unless valid_provider?(provider) 9 | omniauthorize_additional_account || omniauth_sign_in || omniauth_sign_up 10 | end 11 | 12 | def omniauth_sign_in 13 | #todo merge by email if signing in with a new account for which we already have a user (match on email) 14 | return false unless preexisting_authorization_token 15 | 16 | flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => omniauth_data['provider'] 17 | sign_in_and_redirect(:user, preexisting_authorization_token.user) 18 | true 19 | end 20 | 21 | def omniauth_sign_up 22 | unless omniauth_data.recursive_find_by_key("email").blank? 23 | user = User.find_or_initialize_by_email(:email => omniauth_data.recursive_find_by_key("email")) 24 | else 25 | user = User.new 26 | end 27 | 28 | user.apply_omniauth(omniauth_data) 29 | 30 | if user.save 31 | flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => omniauth_data['provider'] 32 | sign_in_and_redirect(:user, user) 33 | else 34 | session[:omniauth] = omniauth_data.except('extra') 35 | redirect_to new_user_registration_url 36 | end 37 | end 38 | 39 | def omniauthorize_additional_account 40 | return false if current_user.nil? 41 | 42 | #todo signin not necessary, may mess up last sign in dates 43 | if preexisting_authorization_token && preexisting_authorization_token != current_user 44 | flash[:alert] = "You have created two accounts and they can't be merged automatically. Email #{LIVE_PERSONS_EMAIL} for help." 45 | sign_in_and_redirect(:user, current_user) 46 | else 47 | 48 | current_user.apply_omniauth(omniauth_data) 49 | current_user.save 50 | 51 | flash[:notice] = "Account connected" 52 | sign_in_and_redirect(:user, current_user) 53 | end 54 | end 55 | 56 | def set_omniauth_data 57 | self.omniauth_data = env["omniauth.auth"] 58 | self.preexisting_authorization_token = UserToken.find_by_provider_and_uid(omniauth_data['provider'], omniauth_data['uid']) 59 | end 60 | 61 | def valid_provider?(provider) 62 | !User.omniauth_providers.index(provider).nil? 63 | end 64 | end -------------------------------------------------------------------------------- /config/locales/en.yml: -------------------------------------------------------------------------------- 1 | # Additional translations at http://github.com/plataformatec/devise/wiki/I18n 2 | 3 | en: 4 | errors: 5 | messages: 6 | not_found: "not found" 7 | already_confirmed: "was already confirmed, please try signing in" 8 | not_locked: "was not locked" 9 | not_saved: 10 | one: "1 error prohibited this %{resource} from being saved:" 11 | other: "%{count} errors prohibited this %{resource} from being saved:" 12 | 13 | devise: 14 | failure: 15 | unauthenticated: 'You need to sign in or sign up before continuing.' 16 | unconfirmed: 'You have to confirm your account before continuing.' 17 | locked: 'Your account is locked.' 18 | invalid: 'Invalid email or password.' 19 | invalid_token: 'Invalid authentication token.' 20 | timeout: 'Your session expired, please sign in again to continue.' 21 | inactive: 'Your account was not activated yet.' 22 | sessions: 23 | signed_in: 'Signed in successfully.' 24 | signed_out: 'Signed out successfully.' 25 | passwords: 26 | send_instructions: 'You will receive an email with instructions about how to reset your password in a few minutes.' 27 | updated: 'Your password was changed successfully. You are now signed in.' 28 | confirmations: 29 | send_instructions: 'You will receive an email with instructions about how to confirm your account in a few minutes.' 30 | confirmed: 'Your account was successfully confirmed. You are now signed in.' 31 | registrations: 32 | signed_up: 'Welcome! You have signed up successfully.' 33 | inactive_signed_up: 'You have signed up successfully. However, we could not sign you in because your account is %{reason}.' 34 | updated: 'You updated your account successfully.' 35 | destroyed: 'Bye! Your account was successfully cancelled. We hope to see you again soon.' 36 | unlocks: 37 | send_instructions: 'You will receive an email with instructions about how to unlock your account in a few minutes.' 38 | unlocked: 'Your account was successfully unlocked. You are now signed in.' 39 | omniauth_callbacks: 40 | success: 'Successfully authorized from %{kind} account.' 41 | failure: 'Could not authorize you from %{kind} because "%{reason}".' 42 | mailer: 43 | confirmation_instructions: 44 | subject: 'Confirmation instructions' 45 | reset_password_instructions: 46 | subject: 'Reset password instructions' 47 | unlock_instructions: 48 | subject: 'Unlock Instructions' -------------------------------------------------------------------------------- /public/stylesheets/forms.css: -------------------------------------------------------------------------------- 1 | /* forms */ 2 | form { 3 | margin: 10px 0; 4 | display: block; 5 | } 6 | 7 | form > div { 8 | overflow: hidden; 9 | padding-bottom: 8px; 10 | } 11 | 12 | .simple_form { 13 | width: 400px; 14 | } 15 | input.string, input.password, textarea { 16 | border-width: 1px; 17 | border-style: solid; 18 | border-top-color: #999999; 19 | border-left-color: #999999; 20 | border-bottom-color: #e5e5e5; 21 | border-right-color: #e5e5e5; 22 | padding: 4px 2px 3px 2px; 23 | background-color: #f9f9f9; 24 | width: 200px; 25 | float:right; 26 | } 27 | 28 | .input.boolean input.boolean { 29 | width: 20px; 30 | float: left; 31 | margin-left: 150px; 32 | 33 | } 34 | 35 | .input.boolean label.boolean { 36 | float: left; 37 | width: 160px; 38 | margin: 6px 0 0 0; 39 | text-align: left; 40 | } 41 | 42 | input[tupe=submit] { 43 | display: block; 44 | } 45 | 46 | label { 47 | display: inline; /* IE hack */ 48 | float: left; 49 | font-family: Arial, Helvetica, sans-serif; 50 | font-size: 13px; 51 | line-height: 22px; 52 | color: #333333; 53 | width: 140px; 54 | padding-right: 20px; 55 | padding-top: 5px; 56 | text-align: right; 57 | } 58 | label abbr { 59 | display: none; 60 | } 61 | 62 | .input { 63 | margin-bottom: 5px; 64 | } 65 | .input input, .input span.hint { 66 | display: block; 67 | float: left; 68 | width: 200px; 69 | } 70 | 71 | .input .hint, .input .error { 72 | float: left; 73 | font-family: Arial, Helvetica, sans-serif; 74 | font-size: 12px; 75 | line-height: 14px; 76 | padding: 5px 0 0 0; 77 | margin: 0px 0; 78 | } 79 | 80 | #error_explanation { 81 | color: #df644c; 82 | font-family: Arial, sans-serif; 83 | font-size: 13px; 84 | font-weight: bold; 85 | } 86 | 87 | .field_with_errors { 88 | padding-bottom: 0; 89 | margin-bottom: 15px; 90 | } 91 | 92 | .field_with_errors.select { 93 | padding-bottom: 18px; 94 | } 95 | 96 | .field_with_errors label { 97 | color: #df644c; 98 | } 99 | 100 | .field_with_errors > span { 101 | display: inline; /* IE hack */ 102 | float: left; 103 | color: #ee3333; 104 | white-space: nowrap; 105 | } 106 | 107 | .field_with_errors > span { 108 | margin-left: 175px; 109 | } 110 | 111 | .field_with_errors.select > span { 112 | margin-left: 10px; 113 | } 114 | 115 | .field_with_errors input, 116 | .field_with_errors textarea { 117 | border-color: #df644c; 118 | } 119 | 120 | 121 | #user_remember_me { 122 | width: 30px; 123 | margin-top: 15px; 124 | float: left; 125 | } 126 | 127 | label[for=user_remember_me] { 128 | width: 300px; 129 | margin-top: 10px; 130 | float: left; 131 | } 132 | 133 | .forgot-password { 134 | margin-top: 100px; 135 | } 136 | 137 | .or-spacer { 138 | margin: 50px 0; 139 | font-style: italic; 140 | font-size: 24px; 141 | width: 200px; 142 | text-align: center; 143 | } -------------------------------------------------------------------------------- /spec/controllers/users/omniauth_callbacks_controller_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe Users::OmniauthCallbacksController do 4 | 5 | before do 6 | request.env["devise.mapping"] = Devise.mappings[:user] 7 | end 8 | 9 | describe "/auth/:action/callback" do 10 | describe "when user is logging in with a previously authed account" do 11 | before do 12 | @user = create_user(:name => "", :email => 'existing@example.com') 13 | @twitter_data = omniauth_twitter 14 | @user.apply_omniauth(@twitter_data) 15 | @user.save! 16 | 17 | set_omniauth_credentials :twitter, @twitter_data 18 | end 19 | 20 | 21 | it "should lookup the correct user" do 22 | get :twitter 23 | controller.current_user.should == @user 24 | end 25 | 26 | it "should not create new provider token" do 27 | get :twitter 28 | @user.reload 29 | @user.user_tokens.size.should == 1 30 | end 31 | 32 | it "should redirect :back if :location set" do 33 | session["user_return_to"] = "/?remember_the_alamo" 34 | get :twitter 35 | response.should redirect_to("/?remember_the_alamo") 36 | end 37 | 38 | it "should redirect to / if :back not set" do 39 | get :twitter 40 | response.should redirect_to('/') 41 | end 42 | end 43 | 44 | describe "when user is already logged in and adding a provider account associated with another user" do 45 | it "should tell the user to email for help" do 46 | @other_acct = create_user(:name => "", :email => 'existing@example.com') 47 | @twitter_data = omniauth_twitter 48 | @other_acct.apply_omniauth(@twitter_data) 49 | @other_acct.save! 50 | 51 | 52 | @user = create_user(:name => "") 53 | sign_in :user, @user 54 | 55 | set_omniauth_credentials :twitter, @twitter_data 56 | 57 | get :twitter 58 | flash[:alert].should_not be_nil 59 | flash[:alert].should =~ /help/ 60 | end 61 | end 62 | 63 | describe "when user is already logged in and adding an additional provider we haven't seen" do 64 | before do 65 | @user = create_user(:name => "", :email => 'existing@example.com') 66 | sign_in :user, @user 67 | 68 | @twitter_data = omniauth_twitter 69 | set_omniauth_credentials :twitter, @twitter_data 70 | end 71 | 72 | it "should not create new user" do 73 | get :twitter 74 | controller.current_user.should == @user 75 | end 76 | 77 | it "should add missing user metadata" do 78 | get :twitter 79 | controller.current_user.name.should == @twitter_data['user_info']['name'] 80 | end 81 | 82 | it "should redirect :back if :back set" do 83 | session["user_return_to"] = "/?remember_the_alamo" 84 | get :twitter 85 | response.should redirect_to("/?remember_the_alamo") 86 | end 87 | 88 | it "should redirect to profile if :back not set" 89 | # do 90 | # get :twitter 91 | # response.should redirect_to("/") 92 | # end 93 | end 94 | 95 | 96 | describe "when user is new and signing up/in" do 97 | before do 98 | @twitter_data = omniauth_twitter 99 | set_omniauth_credentials :twitter, @twitter_data 100 | end 101 | 102 | it "should create user" do 103 | get :twitter 104 | @user = controller.current_user 105 | 106 | @user.should_not be_nil 107 | @user.should_not be_new_record 108 | end 109 | 110 | it "should create provider token" do 111 | get :twitter 112 | @user = controller.current_user 113 | 114 | @user.user_tokens.twitter.token.should == @twitter_data["credentials"]["token"] 115 | @user.user_tokens.twitter.secret.should == @twitter_data["credentials"]["secret"] 116 | end 117 | 118 | it "should redirect :back if :location set" do 119 | session["user_return_to"] = "/?remember_the_alamo" 120 | get :twitter 121 | response.should redirect_to("/?remember_the_alamo") 122 | end 123 | 124 | it "should redirect to / if :back not set" do 125 | get :twitter 126 | response.should redirect_to('/') 127 | end 128 | end 129 | end 130 | end -------------------------------------------------------------------------------- /app/models/user.rb: -------------------------------------------------------------------------------- 1 | class User < ActiveRecord::Base 2 | include OmniAuthPopulator 3 | include Sluggable 4 | 5 | before_validation :generate_slug, :on => :create 6 | 7 | validates_uniqueness_of :slug 8 | validates_length_of :slug, :minimum => 1 9 | 10 | has_many :user_tokens do 11 | def facebook 12 | target.detect{|t| t.provider == 'facebook'} 13 | end 14 | 15 | def twitter 16 | target.detect{|t| t.provider == 'twitter'} 17 | end 18 | end 19 | 20 | has_many :sharings 21 | 22 | has_attached_file :photo, 23 | :styles => { 24 | :mini => "40x40#", 25 | :thumb => "80x80#", 26 | :small => "100x100#", 27 | :big => "150x150#" 28 | }, 29 | :default_url => "/images/user_photos/missing_:style.png" 30 | 31 | # Include default devise modules. Others available are: 32 | # :token_authenticatable, :confirmable, :lockable and :timeoutable 33 | devise :database_authenticatable, :registerable, 34 | :recoverable, :rememberable, :trackable, :omniauthable, :validatable #:flexible_devise_validatable 35 | 36 | # Setup accessible (or protected) attributes for your model 37 | attr_accessible :email, :password, :password_confirmation, :remember_me, :name, :slug 38 | 39 | def self.new_with_session(params, session) 40 | super.tap do |user| 41 | if data = session[:omniauth] 42 | user.user_tokens.build(:provider => data['provider'], :uid => data['uid']) 43 | end 44 | end 45 | end 46 | 47 | def slug_source 48 | :raw_slug_text 49 | end 50 | 51 | def slug_scope 52 | [] 53 | end 54 | 55 | def raw_slug_text 56 | return slug unless slug.nil? 57 | return name unless name.blank? 58 | return 'user' 59 | end 60 | 61 | def to_param 62 | slug 63 | end 64 | 65 | def apply_omniauth(omniauth) 66 | self.omniauth = omniauth 67 | user_tokens.build(:provider => omniauth['provider'], :uid => omniauth['uid'], :omniauth => omniauth) 68 | 69 | populate_photo_from_url(omniauth['user_info']['image']) unless photo.exists? || omniauth['user_info']['image'].blank? 70 | end 71 | 72 | def populate_from_twitter(omni) 73 | self.name = omni['user_info']['name'] if self.name.blank? 74 | end 75 | 76 | def populate_from_google_apps(omni) 77 | self.name = omni['user_info']['name'] if self.name.blank? 78 | end 79 | 80 | def populate_from_facebook(omni) 81 | self.name = omni['user_info']['name'] if self.name.blank? 82 | self.email = omni['user_info']['email'] if self.email.blank? 83 | end 84 | 85 | #allows for account creation from twitter & fb 86 | #allows saves w/o password 87 | def password_required? 88 | (!persisted? && user_tokens.empty?) || password.present? || password_confirmation.present? 89 | end 90 | 91 | #allows for account creation from twitter 92 | def email_required? 93 | user_tokens.empty? 94 | end 95 | 96 | def remember_me 97 | super.nil? ? false : true 98 | end 99 | 100 | def tweet!(message, url=DEFAULT_SHARE_URL) 101 | twitter_client.update truncated_message_with_url(message, url) 102 | end 103 | 104 | def fb_post!(message, name=DEFAULT_FB_POST_NAME, 105 | description="TODO", url=DEFAULT_SHARE_URL, img=DEFAULT_FB_SHARE_IMAGE) 106 | options = {'access_token' => user_tokens.facebook.token, 107 | 'message' => message, 108 | 'link' => url, 109 | 'picture' => img, 110 | 'name' => name, 111 | 'caption' => url, 112 | 'description' => description 113 | } 114 | RestClient.post(URI.escape("https://graph.facebook.com/me/feed/"), options) 115 | end 116 | 117 | def connected_to?(provider) 118 | user_tokens.detect{|t| t.provider == provider.to_s} != nil 119 | end 120 | 121 | def display_name 122 | name || email 123 | end 124 | 125 | private 126 | 127 | def populate_photo_from_url(image_url) 128 | require 'open-uri' 129 | io = open(URI.parse(image_url)) 130 | 131 | def io.original_filename; 132 | base_uri.path.split('/').last; 133 | end 134 | 135 | self.photo = io.original_filename.blank? ? nil : io 136 | #todo for now throw, not sire the error cases 137 | end 138 | 139 | def twitter_client 140 | client = TwitterOAuth::Client.new( 141 | :consumer_key => ::TWITTER_CONSUMER_KEY, 142 | :consumer_secret => ::TWITTER_SECRET_KEY, 143 | :token => user_tokens.twitter.token, 144 | :secret => user_tokens.twitter.secret 145 | ) 146 | end 147 | 148 | def truncated_message_with_url(message="", url="", length=140) 149 | if message.size + url.size > 140 150 | share = message[0..(136-url.size)] + "..." + url 151 | else 152 | share = message + " " + url 153 | end 154 | share 155 | end 156 | end 157 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | GIT 2 | remote: git://github.com/plataformatec/devise 3 | revision: b23e2e807a3dc350f724b0f23320394ff006857c 4 | branch: master 5 | specs: 6 | devise (1.2.rc2) 7 | bcrypt-ruby (~> 2.1.2) 8 | orm_adapter (~> 0.0.3) 9 | warden (~> 1.0.3) 10 | 11 | GEM 12 | remote: http://rubygems.org/ 13 | specs: 14 | abstract (1.0.0) 15 | actionmailer (3.0.5) 16 | actionpack (= 3.0.5) 17 | mail (~> 2.2.15) 18 | actionpack (3.0.5) 19 | activemodel (= 3.0.5) 20 | activesupport (= 3.0.5) 21 | builder (~> 2.1.2) 22 | erubis (~> 2.6.6) 23 | i18n (~> 0.4) 24 | rack (~> 1.2.1) 25 | rack-mount (~> 0.6.13) 26 | rack-test (~> 0.5.7) 27 | tzinfo (~> 0.3.23) 28 | activemodel (3.0.5) 29 | activesupport (= 3.0.5) 30 | builder (~> 2.1.2) 31 | i18n (~> 0.4) 32 | activerecord (3.0.5) 33 | activemodel (= 3.0.5) 34 | activesupport (= 3.0.5) 35 | arel (~> 2.0.2) 36 | tzinfo (~> 0.3.23) 37 | activeresource (3.0.5) 38 | activemodel (= 3.0.5) 39 | activesupport (= 3.0.5) 40 | activesupport (3.0.5) 41 | addressable (2.2.4) 42 | arel (2.0.9) 43 | bcrypt-ruby (2.1.4) 44 | builder (2.1.2) 45 | diff-lcs (1.1.2) 46 | erubis (2.6.6) 47 | abstract (>= 1.0.0) 48 | faraday (0.5.7) 49 | addressable (~> 2.2.4) 50 | multipart-post (~> 1.1.0) 51 | rack (>= 1.1.0, < 2) 52 | fixjour (0.5.3) 53 | activerecord 54 | i18n (0.5.0) 55 | json (1.5.1) 56 | mail (2.2.15) 57 | activesupport (>= 2.3.6) 58 | i18n (>= 0.4.0) 59 | mime-types (~> 1.16) 60 | treetop (~> 1.4.8) 61 | mime-types (1.16) 62 | multi_json (0.0.5) 63 | multipart-post (1.1.0) 64 | net-ldap (0.1.1) 65 | nokogiri (1.4.4) 66 | oa-basic (0.2.0) 67 | multi_json (~> 0.0.2) 68 | nokogiri (~> 1.4.2) 69 | oa-core (= 0.2.0) 70 | rest-client (~> 1.6.0) 71 | oa-core (0.2.0) 72 | rack (~> 1.1) 73 | oa-enterprise (0.2.0) 74 | net-ldap (~> 0.1.1) 75 | nokogiri (~> 1.4.2) 76 | oa-core (= 0.2.0) 77 | pyu-ruby-sasl (~> 0.0.3.1) 78 | rubyntlm (~> 0.1.1) 79 | oa-more (0.2.0) 80 | multi_json (~> 0.0.2) 81 | oa-core (= 0.2.0) 82 | rest-client (~> 1.6.0) 83 | oa-oauth (0.2.0) 84 | multi_json (~> 0.0.2) 85 | nokogiri (~> 1.4.2) 86 | oa-core (= 0.2.0) 87 | oauth (~> 0.4.0) 88 | oauth2 (~> 0.1.1) 89 | oa-openid (0.2.0) 90 | oa-core (= 0.2.0) 91 | rack-openid (~> 1.2.0) 92 | ruby-openid-apps-discovery 93 | oauth (0.4.4) 94 | oauth2 (0.1.1) 95 | faraday (~> 0.5.0) 96 | multi_json (~> 0.0.4) 97 | omniauth (0.2.0) 98 | oa-basic (= 0.2.0) 99 | oa-core (= 0.2.0) 100 | oa-enterprise (= 0.2.0) 101 | oa-more (= 0.2.0) 102 | oa-oauth (= 0.2.0) 103 | oa-openid (= 0.2.0) 104 | orm_adapter (0.0.4) 105 | paperclip (2.3.8) 106 | activerecord 107 | activesupport 108 | polyglot (0.3.1) 109 | pyu-ruby-sasl (0.0.3.2) 110 | rack (1.2.1) 111 | rack-mount (0.6.13) 112 | rack (>= 1.0.0) 113 | rack-openid (1.2.0) 114 | rack (>= 1.1.0) 115 | ruby-openid (>= 2.1.8) 116 | rack-test (0.5.7) 117 | rack (>= 1.0) 118 | rails (3.0.5) 119 | actionmailer (= 3.0.5) 120 | actionpack (= 3.0.5) 121 | activerecord (= 3.0.5) 122 | activeresource (= 3.0.5) 123 | activesupport (= 3.0.5) 124 | bundler (~> 1.0) 125 | railties (= 3.0.5) 126 | railties (3.0.5) 127 | actionpack (= 3.0.5) 128 | activesupport (= 3.0.5) 129 | rake (>= 0.8.7) 130 | thor (~> 0.14.4) 131 | rake (0.8.7) 132 | rest-client (1.6.1) 133 | mime-types (>= 1.16) 134 | rspec (2.5.0) 135 | rspec-core (~> 2.5.0) 136 | rspec-expectations (~> 2.5.0) 137 | rspec-mocks (~> 2.5.0) 138 | rspec-core (2.5.1) 139 | rspec-expectations (2.5.0) 140 | diff-lcs (~> 1.1.2) 141 | rspec-mocks (2.5.0) 142 | rspec-rails (2.5.0) 143 | actionpack (~> 3.0) 144 | activesupport (~> 3.0) 145 | railties (~> 3.0) 146 | rspec (~> 2.5.0) 147 | ruby-openid (2.1.8) 148 | ruby-openid-apps-discovery (1.2.0) 149 | ruby-openid (>= 2.1.7) 150 | rubyntlm (0.1.1) 151 | simple_form (1.2.2) 152 | sluggable (0.1.1) 153 | activerecord (>= 2.1.1) 154 | activesupport (>= 2.1.1) 155 | sqlite3 (1.3.3) 156 | sqlite3-ruby (1.3.3) 157 | sqlite3 (>= 1.3.3) 158 | thor (0.14.6) 159 | treetop (1.4.9) 160 | polyglot (>= 0.3.1) 161 | twitter_oauth (0.4.3) 162 | json (>= 1.1.9) 163 | mime-types (>= 1.16) 164 | oauth (>= 0.4.1) 165 | tzinfo (0.3.25) 166 | warden (1.0.3) 167 | rack (>= 1.0.0) 168 | 169 | PLATFORMS 170 | ruby 171 | 172 | DEPENDENCIES 173 | devise! 174 | fixjour 175 | omniauth (= 0.2.0) 176 | paperclip 177 | rails (= 3.0.5) 178 | rake 179 | rest-client (= 1.6.1) 180 | rspec-rails 181 | simple_form (~> 1.2.2) 182 | sluggable 183 | sqlite3-ruby 184 | twitter_oauth (= 0.4.3) 185 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | ENV["RAILS_ENV"] ||= 'test' 2 | require File.expand_path("../../config/environment", __FILE__) 3 | require 'rspec' 4 | require 'rspec/rails' 5 | require File.expand_path(File.dirname(__FILE__) +'/builders/fixjour.rb') 6 | 7 | Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} 8 | Dir[File.expand_path(File.join(File.dirname(__FILE__), 'shared_examples', '**', '*.rb'))].each { |f| require f } 9 | 10 | RSpec.configure do |config| 11 | # == Mock Framework 12 | # 13 | # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line: 14 | # 15 | # config.mock_with :mocha 16 | # config.mock_with :flexmock 17 | # config.mock_with :rr 18 | config.mock_with :rspec 19 | 20 | config.fixture_path = "#{::Rails.root}/spec/fixtures" 21 | 22 | # If you're not using ActiveRecord, or you'd prefer not to run each of your 23 | # examples within a transaction, comment the following line or assign false 24 | # instead of true. 25 | config.use_transactional_fixtures = true 26 | config.include(Fixjour) 27 | config.include Devise::TestHelpers, :type => :controller 28 | config.include ActionDispatch::TestProcess 29 | 30 | end 31 | OmniAuth.config.test_mode = true 32 | 33 | def set_omniauth_credentials(provider, value) 34 | # OmniAuth.config.mock_auth[provider] = value 35 | controller.stub(:env){ {"omniauth.auth" => @twitter_data } } 36 | end 37 | 38 | def omniauth_twitter(uid="111") 39 | {"user_info"=> 40 | {"name"=>"Parker Twhompson", 41 | "location"=>"iPhone: 37.774284,-122.276520", 42 | "urls"=>{"Website"=>"http://parkerthompson.org", 43 | "Twitter"=>"http://twitter.com/pt"}, 44 | "nickname"=>"pt", 45 | "description"=>"I live in San Francisco.", 46 | "image"=>"http://a3.twimg.com/profile_images/1270199748/pt_normal.jpg" 47 | }, 48 | "uid"=>uid, 49 | "credentials"=>{"token"=>"xx", 50 | "secret"=>"xx" 51 | }, 52 | "extra"=>{"user_hash"=>{"name"=>"Parker TW Thompson", 53 | "profile_sidebar_border_color"=>"C0DEED", 54 | "profile_background_tile"=>false, 55 | "profile_sidebar_fill_color"=>"DDEEF6", 56 | "created_at"=>"Sun Oct 21 01:50:51 +0000 2007", 57 | "location"=>"iPhone: 37.774284,-122.276520", 58 | "profile_image_url"=>"http://a3.twimg.com/profile_images/1270199748/pt_normal.jpg", 59 | "follow_request_sent"=>false, 60 | "profile_link_color"=>"0084B4", 61 | "is_translator"=>false, 62 | "id_str"=>"9571702", 63 | "contributors_enabled"=>false, 64 | "url"=>"http://parkerthompson.org", 65 | "favourites_count"=>0, 66 | "id"=>9571702, 67 | "listed_count"=>19, 68 | "protected"=>false, 69 | "lang"=>"en", 70 | "followers_count"=>699, 71 | "notifications"=>false, 72 | "description"=>"I live in San Francisco..", 73 | "statuses_count"=>1265, 74 | "friends_count"=>240, 75 | "status"=>{"coordinates"=>nil, 76 | "created_at"=>"Sat Mar 19 18:15:16 +0000 2011", 77 | "text"=>"@jpignata xxx.", 78 | "id"=>49172100509990912, 79 | }, 80 | "profile_background_image_url"=>"http://a3.twimg.com/a/1299876209/images/themes/theme1/bg.png", 81 | "screen_name"=>"pt", 82 | "show_all_inline_media"=>true, 83 | "following"=>false}, 84 | "access_token"=>nil 85 | }, 86 | "provider"=>"twitter" 87 | } 88 | end 89 | 90 | def omniauth_facebook(uid="222") 91 | {"user_info"=> 92 | {"name"=>"Parker FBompson", 93 | "urls"=>{"Facebook"=>"http://www.facebook.com/m.parker.thompson", 94 | "Website"=>nil}, 95 | "nickname"=>"m.parker.thompson", 96 | "last_name"=>"Thompson", 97 | "image"=>"http://graph.facebook.com/614285738/picture?type=square", 98 | "first_name"=>"Parker", 99 | "email"=>"parkert@example.com"}, 100 | "uid"=>uid, 101 | "credentials"=>{"token"=>"ffffffffffffff"}, 102 | "extra"=>{ 103 | "user_hash"=>{ 104 | "name"=>"Parker FB Thompson", 105 | "location"=>{ 106 | "name"=>"San Francisco, California", 107 | "id"=>"114952118516947" 108 | }, 109 | "username"=>"m.parker.thompson", 110 | "timezone"=>-5, 111 | "gender"=>"male", 112 | "id"=>"1", 113 | "last_name"=>"Thompson", 114 | "updated_time"=>"2011-03-19T19:49:54+0000", 115 | "verified"=>true, 116 | "locale"=>"en_US", 117 | "bio"=>"On the web at http://parkerthompson.org.", 118 | "hometown"=>{"name"=>"Olympia, Washington", 119 | "id"=>"108374975857409"}, 120 | "link"=>"http://www.facebook.com/m.parker.thompson", 121 | "email"=>"parkert@example.com", 122 | "education"=>[{"school"=>{"name"=>"Timberline High School", 123 | "id"=>"109394095745350"}, 124 | "type"=>"High School", 125 | "year"=>{"name"=>"1917", 126 | "id"=>"131821060195210"}} 127 | ], 128 | "work"=>[{"start_date"=>"0000-00", "location"=>{"name"=>"San Francisco, California", "id"=>"114952118516947"}, 129 | "employer"=>{"name"=>"Pivotal Labs, Inc.", "id"=>"107708552595423"}, "end_date"=>"0000-00"}], "first_name"=>"Parker"}}, 130 | "provider"=>"facebook"} 131 | end 132 | 133 | -------------------------------------------------------------------------------- /public/javascripts/rails.js: -------------------------------------------------------------------------------- 1 | (function() { 2 | // Technique from Juriy Zaytsev 3 | // http://thinkweb2.com/projects/prototype/detecting-event-support-without-browser-sniffing/ 4 | function isEventSupported(eventName) { 5 | var el = document.createElement('div'); 6 | eventName = 'on' + eventName; 7 | var isSupported = (eventName in el); 8 | if (!isSupported) { 9 | el.setAttribute(eventName, 'return;'); 10 | isSupported = typeof el[eventName] == 'function'; 11 | } 12 | el = null; 13 | return isSupported; 14 | } 15 | 16 | function isForm(element) { 17 | return Object.isElement(element) && element.nodeName.toUpperCase() == 'FORM' 18 | } 19 | 20 | function isInput(element) { 21 | if (Object.isElement(element)) { 22 | var name = element.nodeName.toUpperCase() 23 | return name == 'INPUT' || name == 'SELECT' || name == 'TEXTAREA' 24 | } 25 | else return false 26 | } 27 | 28 | var submitBubbles = isEventSupported('submit'), 29 | changeBubbles = isEventSupported('change') 30 | 31 | if (!submitBubbles || !changeBubbles) { 32 | // augment the Event.Handler class to observe custom events when needed 33 | Event.Handler.prototype.initialize = Event.Handler.prototype.initialize.wrap( 34 | function(init, element, eventName, selector, callback) { 35 | init(element, eventName, selector, callback) 36 | // is the handler being attached to an element that doesn't support this event? 37 | if ( (!submitBubbles && this.eventName == 'submit' && !isForm(this.element)) || 38 | (!changeBubbles && this.eventName == 'change' && !isInput(this.element)) ) { 39 | // "submit" => "emulated:submit" 40 | this.eventName = 'emulated:' + this.eventName 41 | } 42 | } 43 | ) 44 | } 45 | 46 | if (!submitBubbles) { 47 | // discover forms on the page by observing focus events which always bubble 48 | document.on('focusin', 'form', function(focusEvent, form) { 49 | // special handler for the real "submit" event (one-time operation) 50 | if (!form.retrieve('emulated:submit')) { 51 | form.on('submit', function(submitEvent) { 52 | var emulated = form.fire('emulated:submit', submitEvent, true) 53 | // if custom event received preventDefault, cancel the real one too 54 | if (emulated.returnValue === false) submitEvent.preventDefault() 55 | }) 56 | form.store('emulated:submit', true) 57 | } 58 | }) 59 | } 60 | 61 | if (!changeBubbles) { 62 | // discover form inputs on the page 63 | document.on('focusin', 'input, select, texarea', function(focusEvent, input) { 64 | // special handler for real "change" events 65 | if (!input.retrieve('emulated:change')) { 66 | input.on('change', function(changeEvent) { 67 | input.fire('emulated:change', changeEvent, true) 68 | }) 69 | input.store('emulated:change', true) 70 | } 71 | }) 72 | } 73 | 74 | function handleRemote(element) { 75 | var method, url, params; 76 | 77 | var event = element.fire("ajax:before"); 78 | if (event.stopped) return false; 79 | 80 | if (element.tagName.toLowerCase() === 'form') { 81 | method = element.readAttribute('method') || 'post'; 82 | url = element.readAttribute('action'); 83 | params = element.serialize(); 84 | } else { 85 | method = element.readAttribute('data-method') || 'get'; 86 | url = element.readAttribute('href'); 87 | params = {}; 88 | } 89 | 90 | new Ajax.Request(url, { 91 | method: method, 92 | parameters: params, 93 | evalScripts: true, 94 | 95 | onComplete: function(request) { element.fire("ajax:complete", request); }, 96 | onSuccess: function(request) { element.fire("ajax:success", request); }, 97 | onFailure: function(request) { element.fire("ajax:failure", request); } 98 | }); 99 | 100 | element.fire("ajax:after"); 101 | } 102 | 103 | function handleMethod(element) { 104 | var method = element.readAttribute('data-method'), 105 | url = element.readAttribute('href'), 106 | csrf_param = $$('meta[name=csrf-param]')[0], 107 | csrf_token = $$('meta[name=csrf-token]')[0]; 108 | 109 | var form = new Element('form', { method: "POST", action: url, style: "display: none;" }); 110 | element.parentNode.insert(form); 111 | 112 | if (method !== 'post') { 113 | var field = new Element('input', { type: 'hidden', name: '_method', value: method }); 114 | form.insert(field); 115 | } 116 | 117 | if (csrf_param) { 118 | var param = csrf_param.readAttribute('content'), 119 | token = csrf_token.readAttribute('content'), 120 | field = new Element('input', { type: 'hidden', name: param, value: token }); 121 | form.insert(field); 122 | } 123 | 124 | form.submit(); 125 | } 126 | 127 | 128 | document.on("click", "*[data-confirm]", function(event, element) { 129 | var message = element.readAttribute('data-confirm'); 130 | if (!confirm(message)) event.stop(); 131 | }); 132 | 133 | document.on("click", "a[data-remote]", function(event, element) { 134 | if (event.stopped) return; 135 | handleRemote(element); 136 | event.stop(); 137 | }); 138 | 139 | document.on("click", "a[data-method]", function(event, element) { 140 | if (event.stopped) return; 141 | handleMethod(element); 142 | event.stop(); 143 | }); 144 | 145 | document.on("submit", function(event) { 146 | var element = event.findElement(), 147 | message = element.readAttribute('data-confirm'); 148 | if (message && !confirm(message)) { 149 | event.stop(); 150 | return false; 151 | } 152 | 153 | var inputs = element.select("input[type=submit][data-disable-with]"); 154 | inputs.each(function(input) { 155 | input.disabled = true; 156 | input.writeAttribute('data-original-value', input.value); 157 | input.value = input.readAttribute('data-disable-with'); 158 | }); 159 | 160 | var element = event.findElement("form[data-remote]"); 161 | if (element) { 162 | handleRemote(element); 163 | event.stop(); 164 | } 165 | }); 166 | 167 | document.on("ajax:after", "form", function(event, element) { 168 | var inputs = element.select("input[type=submit][disabled=true][data-disable-with]"); 169 | inputs.each(function(input) { 170 | input.value = input.readAttribute('data-original-value'); 171 | input.removeAttribute('data-original-value'); 172 | input.disabled = false; 173 | }); 174 | }); 175 | })(); 176 | -------------------------------------------------------------------------------- /config/initializers/devise.rb: -------------------------------------------------------------------------------- 1 | # Use this hook to configure devise mailer, warden hooks and so forth. The first 2 | # four configuration values can also be set straight in your models. 3 | 4 | require 'openid/store/filesystem' 5 | 6 | Devise.setup do |config| 7 | # ==> Mailer Configuration 8 | # Configure the e-mail address which will be shown in DeviseMailer. 9 | config.mailer_sender = DEVISE_MAILER_FROM 10 | 11 | # Configure the class responsible to send e-mails. 12 | # config.mailer = "Devise::Mailer" 13 | 14 | # ==> ORM configuration 15 | # Load and configure the ORM. Supports :active_record (default) and 16 | # :mongoid (bson_ext recommended) by default. Other ORMs may be 17 | # available as additional gems. 18 | require 'devise/orm/active_record' 19 | 20 | # ==> Configuration for any authentication mechanism 21 | # Configure which keys are used when authenticating an user. By default is 22 | # just :email. You can configure it to use [:username, :subdomain], so for 23 | # authenticating an user, both parameters are required. Remember that those 24 | # parameters are used only when authenticating and not when retrieving from 25 | # session. If you need permissions, you should implement that in a before filter. 26 | # config.authentication_keys = [ :email ] 27 | 28 | # Tell if authentication through request.params is enabled. True by default. 29 | # config.params_authenticatable = true 30 | 31 | # Tell if authentication through HTTP Basic Auth is enabled. True by default. 32 | # config.http_authenticatable = true 33 | 34 | # Set this to true to use Basic Auth for AJAX requests. True by default. 35 | # config.http_authenticatable_on_xhr = true 36 | 37 | # The realm used in Http Basic Authentication 38 | # config.http_authentication_realm = "Application" 39 | 40 | # ==> Configuration for :database_authenticatable 41 | # For bcrypt, this is the cost for hashing the password and defaults to 10. If 42 | # using other encryptors, it sets how many times you want the password re-encrypted. 43 | config.stretches = 10 44 | 45 | # Define which will be the encryption algorithm. Devise also supports encryptors 46 | # from others authentication tools as :clearance_sha1, :authlogic_sha512 (then 47 | # you should set stretches above to 20 for default behavior) and :restful_authentication_sha1 48 | # (then you should set stretches to 10, and copy REST_AUTH_SITE_KEY to pepper) 49 | #config.encryptor = :bcrypt 50 | 51 | # Setup a pepper to generate the encrypted password. 52 | config.pepper = "439ecfc4b47fe22c733da4ac682d1a315d78f3f8a69307e61bad501978e8d5f1db516f1cc96f3a2fcf8f87f63e1860eb27be5208894bf2b5e9b91481febe52ab" 53 | 54 | # ==> Configuration for :confirmable 55 | # The time you want to give your user to confirm his account. During this time 56 | # he will be able to access your application without confirming. Default is nil. 57 | # When confirm_within is zero, the user won't be able to sign in without confirming. 58 | # You can use this to let your user access some features of your application 59 | # without confirming the account, but blocking it after a certain period 60 | # (ie 2 days). 61 | # config.confirm_within = 2.days 62 | 63 | # ==> Configuration for :rememberable 64 | # The time the user will be remembered without asking for credentials again. 65 | # config.remember_for = 2.weeks 66 | 67 | # If true, a valid remember token can be re-used between multiple browsers. 68 | # config.remember_across_browsers = true 69 | 70 | # If true, extends the user's remember period when remembered via cookie. 71 | # config.extend_remember_period = false 72 | 73 | # ==> Configuration for :validatable 74 | # Range for password length 75 | # config.password_length = 6..20 76 | 77 | # Regex to use to validate the email address 78 | # config.email_regexp = /^([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})$/i 79 | 80 | # ==> Configuration for :timeoutable 81 | # The time you want to timeout the user session without activity. After this 82 | # time the user will be asked for credentials again. 83 | # config.timeout_in = 10.minutes 84 | 85 | # ==> Configuration for :lockable 86 | # Defines which strategy will be used to lock an account. 87 | # :failed_attempts = Locks an account after a number of failed attempts to sign in. 88 | # :none = No lock strategy. You should handle locking by yourself. 89 | # config.lock_strategy = :failed_attempts 90 | 91 | # Defines which strategy will be used to unlock an account. 92 | # :email = Sends an unlock link to the user email 93 | # :time = Re-enables login after a certain amount of time (see :unlock_in below) 94 | # :both = Enables both strategies 95 | # :none = No unlock strategy. You should handle unlocking by yourself. 96 | # config.unlock_strategy = :both 97 | 98 | # Number of authentication tries before locking an account if lock_strategy 99 | # is failed attempts. 100 | # config.maximum_attempts = 20 101 | 102 | # Time interval to unlock the account if :time is enabled as unlock_strategy. 103 | # config.unlock_in = 1.hour 104 | 105 | # ==> Configuration for :token_authenticatable 106 | # Defines name of the authentication token params key 107 | # config.token_authentication_key = :auth_token 108 | 109 | # ==> Scopes configuration 110 | # Turn scoped views on. Before rendering "sessions/new", it will first check for 111 | # "users/sessions/new". It's turned off by default because it's slower if you 112 | # are using only default views. 113 | # config.scoped_views = true 114 | 115 | # Configure the default scope given to Warden. By default it's the first 116 | # devise role declared in your routes. 117 | # config.default_scope = :user 118 | 119 | # Configure sign_out behavior. 120 | # By default sign_out is scoped (i.e. /users/sign_out affects only :user scope). 121 | # In case of sign_out_all_scopes set to true any logout action will sign out all active scopes. 122 | # config.sign_out_all_scopes = false 123 | 124 | # ==> Navigation configuration 125 | # Lists the formats that should be treated as navigational. Formats like 126 | # :html, should redirect to the sign in page when the user does not have 127 | # access, but formats like :xml or :json, should return 401. 128 | # If you have any extra navigational formats, like :iphone or :mobile, you 129 | # should add them to the navigational formats lists. Default is [:html] 130 | # config.navigational_formats = [:html, :iphone] 131 | 132 | # ==> Warden configuration 133 | # If you want to use other strategies, that are not (yet) supported by Devise, 134 | # you can configure them inside the config.warden block. The example below 135 | # allows you to setup OAuth, using http://github.com/roman/warden_oauth 136 | 137 | #config.omniauth :facebook, "APP_ID", "APP_SECRET" 138 | config.omniauth :facebook, FACEBOOK_APP_ID, FACEBOOK_APP_SECRET, :scope => FACEBOOK_APP_PERMISSIONS 139 | # config.omniauth :twitter, TWITTER_SECRET_KEY, TWITTER_CONSUMER_KEY 140 | config.omniauth :twitter, TWITTER_CONSUMER_KEY, TWITTER_SECRET_KEY 141 | config.omniauth :google_apps, OpenID::Store::Filesystem.new('/tmp'), :domain => 'gmail.com' 142 | # 143 | # config.warden do |manager| 144 | # manager.oauth(:twitter) do |twitter| 145 | # twitter.consumer_secret = 146 | # twitter.consumer_key = 147 | # twitter.options :site => 'http://twitter.com' 148 | # end 149 | # manager.default_strategies(:scope => :user).unshift :twitter_oauth 150 | # end 151 | 152 | #monkey patch 153 | 154 | require 'openid/store/nonce' 155 | require 'openid/store/interface' 156 | module OpenID 157 | module Store 158 | class Memcache < Interface 159 | def use_nonce(server_url, timestamp, salt) 160 | return false if (timestamp - Time.now.to_i).abs > Nonce.skew 161 | ts = timestamp.to_s # base 10 seconds since epoch 162 | nonce_key = key_prefix + 'N' + server_url + '|' + ts + '|' + salt 163 | result = @cache_client.add(nonce_key, '', expiry(Nonce.skew + 5)) 164 | 165 | return result #== true (edited 10/25/10) 166 | # return !!(result =~ /^STORED/) 167 | end 168 | end 169 | end 170 | end 171 | 172 | class Hash 173 | def recursive_find_by_key(key) 174 | # Create a stack of hashes to search through for the needle which 175 | # is initially this hash 176 | stack = [ self ] 177 | 178 | # So long as there are more haystacks to search... 179 | while (to_search = stack.pop) 180 | # ...keep searching for this particular key... 181 | to_search.each do |k, v| 182 | # ...and return the corresponding value if it is found. 183 | return v if (k == key) 184 | 185 | # If this value can be recursively searched... 186 | if (v.respond_to?(:recursive_find_by_key)) 187 | # ...push that on to the list of places to search. 188 | stack << v 189 | end 190 | end 191 | end 192 | end 193 | end 194 | end 195 | -------------------------------------------------------------------------------- /spec/models/user_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe User do 4 | before do 5 | @user = new_user 6 | end 7 | 8 | describe "#password_required?" do 9 | 10 | context "new users" do 11 | before do 12 | @user = User.new 13 | end 14 | 15 | it "requires a password when no user_token present" do 16 | @user.should be_password_required 17 | end 18 | it "should not require password when they have an auth token" do 19 | @user.apply_omniauth(omniauth_twitter) 20 | @user.should_not be_password_required 21 | end 22 | end 23 | 24 | context "existing users" do 25 | before do 26 | @user.save! 27 | @user = User.find(@user.id) 28 | end 29 | it "requires a password when password is present" do 30 | @user.password = 'mistyped' 31 | @user.should be_password_required 32 | end 33 | 34 | it "requires a correct password when password_comfirmation is present" do 35 | @user.password_confirmation = 'xx' 36 | @user.should be_password_required 37 | end 38 | 39 | it "should not require password no password or confirmation is present" do 40 | @user.reload.should_not be_password_required 41 | end 42 | end 43 | 44 | end 45 | 46 | describe "slugs" do 47 | context "new records" do 48 | it "should be set on create" do 49 | @user.slug.should be_nil 50 | @user.save! 51 | @user.slug.should_not be_nil 52 | end 53 | 54 | it "should deal gracefully with colliding slugs" do 55 | @user.slug = 'p-t' 56 | @user.save! 57 | 58 | @dup = new_user 59 | @dup.apply_omniauth(omniauth_twitter) 60 | @dup.should be_valid 61 | @dup.slug.should == 'p-t-2' 62 | end 63 | end 64 | 65 | context "existing records" do 66 | before do 67 | @slug = 'hi' 68 | @user = new_user(:slug => @slug) 69 | @user.slug = @slug 70 | @user.save! 71 | end 72 | 73 | it "should not automatically generate slug once a slug set" do 74 | @user.save! 75 | @user.reload 76 | @user.slug.should == @slug 77 | end 78 | 79 | it "should allow the user to change slug" do 80 | @user.slug = 'bye' 81 | @user.save! 82 | @user.reload 83 | @user.slug = 'bye' 84 | end 85 | 86 | it "should not allow duplicate slugs" do 87 | @dup = create_user 88 | @dup.slug = @slug 89 | @dup.should_not be_valid 90 | 91 | @dup.errors[:slug].should_not be_empty 92 | end 93 | 94 | it "should be invalid with a blank slug" do 95 | @user = new_user 96 | @user.slug = "" 97 | @user.valid? 98 | @user.errors[:slug].should_not be_empty 99 | end 100 | end 101 | end 102 | 103 | describe "validations" do 104 | describe "for omniauthed users" do 105 | it "should be valid with a token" do 106 | @user = User.new 107 | @user.apply_omniauth(omniauth_twitter) 108 | @user.should be_valid 109 | end 110 | end 111 | 112 | describe "for new users" do 113 | 114 | describe "for on-site registered users" do 115 | it "should require password & match confirmation" do 116 | @user.password = nil 117 | @user.password_confirmation = nil 118 | 119 | @user.should_not be_valid 120 | 121 | @user.password = "cool" 122 | @user.password_confirmation = nil 123 | 124 | @user.should_not be_valid 125 | 126 | @user.password = "cool" 127 | @user.password_confirmation = "hot" 128 | 129 | @user.should_not be_valid 130 | end 131 | 132 | it "should require email" do 133 | @user.email = nil 134 | @user.should_not be_valid 135 | end 136 | end 137 | end 138 | end 139 | 140 | describe "#apply_omniauth" do 141 | context "twitter" do 142 | it "should create provider token and add twitter credentials" do 143 | @user.user_tokens.should be_empty 144 | 145 | @user.apply_omniauth(omniauth_twitter) 146 | 147 | token = @user.user_tokens.first 148 | token.secret.should_not be_nil 149 | token.token.should_not be_nil 150 | end 151 | end 152 | 153 | context "facebook" do 154 | it "should create provider token and add credentials" do 155 | @user.user_tokens.should be_empty 156 | 157 | @user.apply_omniauth(omniauth_facebook) 158 | 159 | token = @user.user_tokens.first 160 | token.token.should_not be_nil 161 | end 162 | end 163 | 164 | it "should assign photo is image url is present and photo is empty" do 165 | @user.user_tokens.should be_empty 166 | @omni = omniauth_facebook 167 | @omni['user_info']['image'] = nil 168 | @user.apply_omniauth(@omni) 169 | @user.photo.should_not be_exists 170 | end 171 | 172 | #todo file fixtues not found. wtf? 173 | # it "should NOT assign photo and photo is set" do 174 | # @user = new_user 175 | # @user.photo = fixture_file_upload('/files/photo.png', 'image/png') 176 | # @photo = @user.photo 177 | # @user.user_tokens.should be_empty 178 | # @omni = omniauth_facebook 179 | # 180 | # @user.apply_omniauth(@omni) 181 | # @user.photo.should be_exists 182 | # @user.photo.should == @user.photo 183 | # end 184 | # 185 | # it "should NOT assign photo and photo is empty and no image_url is present" 186 | end 187 | 188 | describe "#populate_from_twitter" do 189 | 190 | it "should populate the user's name" do 191 | @user.name = nil 192 | 193 | @user.apply_omniauth(omniauth_twitter) 194 | 195 | @user.name.should_not be_blank 196 | @user.name.should == omniauth_twitter['user_info']['name'] 197 | end 198 | 199 | context "when name is already set" do 200 | it "should not override an existing name" do 201 | @user.name = "Bob" 202 | @user.apply_omniauth(omniauth_twitter) 203 | @user.name.should == 'Bob' 204 | end 205 | end 206 | 207 | end 208 | 209 | describe "#connected_to?" do 210 | before do 211 | @user.apply_omniauth(omniauth_twitter) 212 | end 213 | 214 | it "returns true when user has been authed to a network" do 215 | @user.connected_to?(:twitter).should == true 216 | end 217 | 218 | it "returns false when user has NOT been authed to a network" do 219 | @user.connected_to?(:facebook).should == false 220 | end 221 | end 222 | 223 | describe "#populate_from_facebook" do 224 | before do 225 | @user = new_user(:name => nil, :email => nil) 226 | end 227 | 228 | it "should populate the user's name" do 229 | 230 | @user.apply_omniauth(omniauth_facebook) 231 | 232 | @user.name.should_not be_blank 233 | @user.name.should == omniauth_facebook['user_info']['name'] 234 | end 235 | 236 | it "should populate the user's email" do 237 | @user.apply_omniauth(omniauth_facebook) 238 | @user.email.should_not be_blank 239 | @user.email.should == omniauth_facebook['user_info']['email'] 240 | end 241 | 242 | context "when name is already set" do 243 | before do 244 | #todo fix fixjour not overriding name 245 | @user = new_user(:name => 'Bob', :email => 'bob@example.com') 246 | @user.name = 'Bob' 247 | @user.apply_omniauth(omniauth_facebook) 248 | end 249 | 250 | it "should not override an existing name" do 251 | @user.name.should == 'Bob' 252 | end 253 | 254 | it "should not override an existing email" do 255 | @user.email.should == 'bob@example.com' 256 | end 257 | end 258 | 259 | end 260 | 261 | describe "#fb_post!" do 262 | before do 263 | @client = RestClient 264 | @user.apply_omniauth(omniauth_facebook) 265 | end 266 | 267 | describe "when facebook credentials are no longer valid" do 268 | it "should raise" 269 | end 270 | 271 | describe "when no credentials are present" do 272 | it "should raise" 273 | end 274 | 275 | it "should make post" do 276 | @client.should_receive(:post) 277 | @user.fb_post!("hi") 278 | end 279 | end 280 | 281 | describe "#tweet!" do 282 | before do 283 | @client = mock("client") 284 | @user.apply_omniauth(omniauth_twitter) 285 | @user.stub(:twitter_client) { @client } 286 | end 287 | 288 | describe "when twitter credentials are no longer valid" do 289 | it "should raise" 290 | end 291 | 292 | describe "when no twitter credentials are present" do 293 | it "should raise" 294 | end 295 | 296 | it "should send tweet" do 297 | @client.should_receive(:update) 298 | @user.tweet!("hi") 299 | end 300 | 301 | it "should truncate tweets that are too long" do 302 | tweet_message = "this is a tweet that is just much much much much much much much much much much much much much much much much too long for its own good." 303 | tweet_url = "http://foo.com/username" 304 | truncated = "this is a tweet that is just much much much much much much much much much much much much much much much much too l...http://foo.com/username" 305 | 306 | @client.should_receive(:update).with(truncated) 307 | @user.tweet!(tweet_message, tweet_url) 308 | end 309 | 310 | it "should concat message and url" do 311 | tweet_message = "this is a tweet." 312 | tweet_url = "http://foo.com/username" 313 | sent = "this is a tweet. http://foo.com/username" 314 | 315 | @client.should_receive(:update).with(sent) 316 | @user.tweet!(tweet_message, tweet_url) 317 | end 318 | end 319 | end 320 | -------------------------------------------------------------------------------- /public/javascripts/dragdrop.js: -------------------------------------------------------------------------------- 1 | // script.aculo.us dragdrop.js v1.8.3, Thu Oct 08 11:23:33 +0200 2009 2 | 3 | // Copyright (c) 2005-2009 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us) 4 | // 5 | // script.aculo.us is freely distributable under the terms of an MIT-style license. 6 | // For details, see the script.aculo.us web site: http://script.aculo.us/ 7 | 8 | if(Object.isUndefined(Effect)) 9 | throw("dragdrop.js requires including script.aculo.us' effects.js library"); 10 | 11 | var Droppables = { 12 | drops: [], 13 | 14 | remove: function(element) { 15 | this.drops = this.drops.reject(function(d) { return d.element==$(element) }); 16 | }, 17 | 18 | add: function(element) { 19 | element = $(element); 20 | var options = Object.extend({ 21 | greedy: true, 22 | hoverclass: null, 23 | tree: false 24 | }, arguments[1] || { }); 25 | 26 | // cache containers 27 | if(options.containment) { 28 | options._containers = []; 29 | var containment = options.containment; 30 | if(Object.isArray(containment)) { 31 | containment.each( function(c) { options._containers.push($(c)) }); 32 | } else { 33 | options._containers.push($(containment)); 34 | } 35 | } 36 | 37 | if(options.accept) options.accept = [options.accept].flatten(); 38 | 39 | Element.makePositioned(element); // fix IE 40 | options.element = element; 41 | 42 | this.drops.push(options); 43 | }, 44 | 45 | findDeepestChild: function(drops) { 46 | deepest = drops[0]; 47 | 48 | for (i = 1; i < drops.length; ++i) 49 | if (Element.isParent(drops[i].element, deepest.element)) 50 | deepest = drops[i]; 51 | 52 | return deepest; 53 | }, 54 | 55 | isContained: function(element, drop) { 56 | var containmentNode; 57 | if(drop.tree) { 58 | containmentNode = element.treeNode; 59 | } else { 60 | containmentNode = element.parentNode; 61 | } 62 | return drop._containers.detect(function(c) { return containmentNode == c }); 63 | }, 64 | 65 | isAffected: function(point, element, drop) { 66 | return ( 67 | (drop.element!=element) && 68 | ((!drop._containers) || 69 | this.isContained(element, drop)) && 70 | ((!drop.accept) || 71 | (Element.classNames(element).detect( 72 | function(v) { return drop.accept.include(v) } ) )) && 73 | Position.within(drop.element, point[0], point[1]) ); 74 | }, 75 | 76 | deactivate: function(drop) { 77 | if(drop.hoverclass) 78 | Element.removeClassName(drop.element, drop.hoverclass); 79 | this.last_active = null; 80 | }, 81 | 82 | activate: function(drop) { 83 | if(drop.hoverclass) 84 | Element.addClassName(drop.element, drop.hoverclass); 85 | this.last_active = drop; 86 | }, 87 | 88 | show: function(point, element) { 89 | if(!this.drops.length) return; 90 | var drop, affected = []; 91 | 92 | this.drops.each( function(drop) { 93 | if(Droppables.isAffected(point, element, drop)) 94 | affected.push(drop); 95 | }); 96 | 97 | if(affected.length>0) 98 | drop = Droppables.findDeepestChild(affected); 99 | 100 | if(this.last_active && this.last_active != drop) this.deactivate(this.last_active); 101 | if (drop) { 102 | Position.within(drop.element, point[0], point[1]); 103 | if(drop.onHover) 104 | drop.onHover(element, drop.element, Position.overlap(drop.overlap, drop.element)); 105 | 106 | if (drop != this.last_active) Droppables.activate(drop); 107 | } 108 | }, 109 | 110 | fire: function(event, element) { 111 | if(!this.last_active) return; 112 | Position.prepare(); 113 | 114 | if (this.isAffected([Event.pointerX(event), Event.pointerY(event)], element, this.last_active)) 115 | if (this.last_active.onDrop) { 116 | this.last_active.onDrop(element, this.last_active.element, event); 117 | return true; 118 | } 119 | }, 120 | 121 | reset: function() { 122 | if(this.last_active) 123 | this.deactivate(this.last_active); 124 | } 125 | }; 126 | 127 | var Draggables = { 128 | drags: [], 129 | observers: [], 130 | 131 | register: function(draggable) { 132 | if(this.drags.length == 0) { 133 | this.eventMouseUp = this.endDrag.bindAsEventListener(this); 134 | this.eventMouseMove = this.updateDrag.bindAsEventListener(this); 135 | this.eventKeypress = this.keyPress.bindAsEventListener(this); 136 | 137 | Event.observe(document, "mouseup", this.eventMouseUp); 138 | Event.observe(document, "mousemove", this.eventMouseMove); 139 | Event.observe(document, "keypress", this.eventKeypress); 140 | } 141 | this.drags.push(draggable); 142 | }, 143 | 144 | unregister: function(draggable) { 145 | this.drags = this.drags.reject(function(d) { return d==draggable }); 146 | if(this.drags.length == 0) { 147 | Event.stopObserving(document, "mouseup", this.eventMouseUp); 148 | Event.stopObserving(document, "mousemove", this.eventMouseMove); 149 | Event.stopObserving(document, "keypress", this.eventKeypress); 150 | } 151 | }, 152 | 153 | activate: function(draggable) { 154 | if(draggable.options.delay) { 155 | this._timeout = setTimeout(function() { 156 | Draggables._timeout = null; 157 | window.focus(); 158 | Draggables.activeDraggable = draggable; 159 | }.bind(this), draggable.options.delay); 160 | } else { 161 | window.focus(); // allows keypress events if window isn't currently focused, fails for Safari 162 | this.activeDraggable = draggable; 163 | } 164 | }, 165 | 166 | deactivate: function() { 167 | this.activeDraggable = null; 168 | }, 169 | 170 | updateDrag: function(event) { 171 | if(!this.activeDraggable) return; 172 | var pointer = [Event.pointerX(event), Event.pointerY(event)]; 173 | // Mozilla-based browsers fire successive mousemove events with 174 | // the same coordinates, prevent needless redrawing (moz bug?) 175 | if(this._lastPointer && (this._lastPointer.inspect() == pointer.inspect())) return; 176 | this._lastPointer = pointer; 177 | 178 | this.activeDraggable.updateDrag(event, pointer); 179 | }, 180 | 181 | endDrag: function(event) { 182 | if(this._timeout) { 183 | clearTimeout(this._timeout); 184 | this._timeout = null; 185 | } 186 | if(!this.activeDraggable) return; 187 | this._lastPointer = null; 188 | this.activeDraggable.endDrag(event); 189 | this.activeDraggable = null; 190 | }, 191 | 192 | keyPress: function(event) { 193 | if(this.activeDraggable) 194 | this.activeDraggable.keyPress(event); 195 | }, 196 | 197 | addObserver: function(observer) { 198 | this.observers.push(observer); 199 | this._cacheObserverCallbacks(); 200 | }, 201 | 202 | removeObserver: function(element) { // element instead of observer fixes mem leaks 203 | this.observers = this.observers.reject( function(o) { return o.element==element }); 204 | this._cacheObserverCallbacks(); 205 | }, 206 | 207 | notify: function(eventName, draggable, event) { // 'onStart', 'onEnd', 'onDrag' 208 | if(this[eventName+'Count'] > 0) 209 | this.observers.each( function(o) { 210 | if(o[eventName]) o[eventName](eventName, draggable, event); 211 | }); 212 | if(draggable.options[eventName]) draggable.options[eventName](draggable, event); 213 | }, 214 | 215 | _cacheObserverCallbacks: function() { 216 | ['onStart','onEnd','onDrag'].each( function(eventName) { 217 | Draggables[eventName+'Count'] = Draggables.observers.select( 218 | function(o) { return o[eventName]; } 219 | ).length; 220 | }); 221 | } 222 | }; 223 | 224 | /*--------------------------------------------------------------------------*/ 225 | 226 | var Draggable = Class.create({ 227 | initialize: function(element) { 228 | var defaults = { 229 | handle: false, 230 | reverteffect: function(element, top_offset, left_offset) { 231 | var dur = Math.sqrt(Math.abs(top_offset^2)+Math.abs(left_offset^2))*0.02; 232 | new Effect.Move(element, { x: -left_offset, y: -top_offset, duration: dur, 233 | queue: {scope:'_draggable', position:'end'} 234 | }); 235 | }, 236 | endeffect: function(element) { 237 | var toOpacity = Object.isNumber(element._opacity) ? element._opacity : 1.0; 238 | new Effect.Opacity(element, {duration:0.2, from:0.7, to:toOpacity, 239 | queue: {scope:'_draggable', position:'end'}, 240 | afterFinish: function(){ 241 | Draggable._dragging[element] = false 242 | } 243 | }); 244 | }, 245 | zindex: 1000, 246 | revert: false, 247 | quiet: false, 248 | scroll: false, 249 | scrollSensitivity: 20, 250 | scrollSpeed: 15, 251 | snap: false, // false, or xy or [x,y] or function(x,y){ return [x,y] } 252 | delay: 0 253 | }; 254 | 255 | if(!arguments[1] || Object.isUndefined(arguments[1].endeffect)) 256 | Object.extend(defaults, { 257 | starteffect: function(element) { 258 | element._opacity = Element.getOpacity(element); 259 | Draggable._dragging[element] = true; 260 | new Effect.Opacity(element, {duration:0.2, from:element._opacity, to:0.7}); 261 | } 262 | }); 263 | 264 | var options = Object.extend(defaults, arguments[1] || { }); 265 | 266 | this.element = $(element); 267 | 268 | if(options.handle && Object.isString(options.handle)) 269 | this.handle = this.element.down('.'+options.handle, 0); 270 | 271 | if(!this.handle) this.handle = $(options.handle); 272 | if(!this.handle) this.handle = this.element; 273 | 274 | if(options.scroll && !options.scroll.scrollTo && !options.scroll.outerHTML) { 275 | options.scroll = $(options.scroll); 276 | this._isScrollChild = Element.childOf(this.element, options.scroll); 277 | } 278 | 279 | Element.makePositioned(this.element); // fix IE 280 | 281 | this.options = options; 282 | this.dragging = false; 283 | 284 | this.eventMouseDown = this.initDrag.bindAsEventListener(this); 285 | Event.observe(this.handle, "mousedown", this.eventMouseDown); 286 | 287 | Draggables.register(this); 288 | }, 289 | 290 | destroy: function() { 291 | Event.stopObserving(this.handle, "mousedown", this.eventMouseDown); 292 | Draggables.unregister(this); 293 | }, 294 | 295 | currentDelta: function() { 296 | return([ 297 | parseInt(Element.getStyle(this.element,'left') || '0'), 298 | parseInt(Element.getStyle(this.element,'top') || '0')]); 299 | }, 300 | 301 | initDrag: function(event) { 302 | if(!Object.isUndefined(Draggable._dragging[this.element]) && 303 | Draggable._dragging[this.element]) return; 304 | if(Event.isLeftClick(event)) { 305 | // abort on form elements, fixes a Firefox issue 306 | var src = Event.element(event); 307 | if((tag_name = src.tagName.toUpperCase()) && ( 308 | tag_name=='INPUT' || 309 | tag_name=='SELECT' || 310 | tag_name=='OPTION' || 311 | tag_name=='BUTTON' || 312 | tag_name=='TEXTAREA')) return; 313 | 314 | var pointer = [Event.pointerX(event), Event.pointerY(event)]; 315 | var pos = this.element.cumulativeOffset(); 316 | this.offset = [0,1].map( function(i) { return (pointer[i] - pos[i]) }); 317 | 318 | Draggables.activate(this); 319 | Event.stop(event); 320 | } 321 | }, 322 | 323 | startDrag: function(event) { 324 | this.dragging = true; 325 | if(!this.delta) 326 | this.delta = this.currentDelta(); 327 | 328 | if(this.options.zindex) { 329 | this.originalZ = parseInt(Element.getStyle(this.element,'z-index') || 0); 330 | this.element.style.zIndex = this.options.zindex; 331 | } 332 | 333 | if(this.options.ghosting) { 334 | this._clone = this.element.cloneNode(true); 335 | this._originallyAbsolute = (this.element.getStyle('position') == 'absolute'); 336 | if (!this._originallyAbsolute) 337 | Position.absolutize(this.element); 338 | this.element.parentNode.insertBefore(this._clone, this.element); 339 | } 340 | 341 | if(this.options.scroll) { 342 | if (this.options.scroll == window) { 343 | var where = this._getWindowScroll(this.options.scroll); 344 | this.originalScrollLeft = where.left; 345 | this.originalScrollTop = where.top; 346 | } else { 347 | this.originalScrollLeft = this.options.scroll.scrollLeft; 348 | this.originalScrollTop = this.options.scroll.scrollTop; 349 | } 350 | } 351 | 352 | Draggables.notify('onStart', this, event); 353 | 354 | if(this.options.starteffect) this.options.starteffect(this.element); 355 | }, 356 | 357 | updateDrag: function(event, pointer) { 358 | if(!this.dragging) this.startDrag(event); 359 | 360 | if(!this.options.quiet){ 361 | Position.prepare(); 362 | Droppables.show(pointer, this.element); 363 | } 364 | 365 | Draggables.notify('onDrag', this, event); 366 | 367 | this.draw(pointer); 368 | if(this.options.change) this.options.change(this); 369 | 370 | if(this.options.scroll) { 371 | this.stopScrolling(); 372 | 373 | var p; 374 | if (this.options.scroll == window) { 375 | with(this._getWindowScroll(this.options.scroll)) { p = [ left, top, left+width, top+height ]; } 376 | } else { 377 | p = Position.page(this.options.scroll); 378 | p[0] += this.options.scroll.scrollLeft + Position.deltaX; 379 | p[1] += this.options.scroll.scrollTop + Position.deltaY; 380 | p.push(p[0]+this.options.scroll.offsetWidth); 381 | p.push(p[1]+this.options.scroll.offsetHeight); 382 | } 383 | var speed = [0,0]; 384 | if(pointer[0] < (p[0]+this.options.scrollSensitivity)) speed[0] = pointer[0]-(p[0]+this.options.scrollSensitivity); 385 | if(pointer[1] < (p[1]+this.options.scrollSensitivity)) speed[1] = pointer[1]-(p[1]+this.options.scrollSensitivity); 386 | if(pointer[0] > (p[2]-this.options.scrollSensitivity)) speed[0] = pointer[0]-(p[2]-this.options.scrollSensitivity); 387 | if(pointer[1] > (p[3]-this.options.scrollSensitivity)) speed[1] = pointer[1]-(p[3]-this.options.scrollSensitivity); 388 | this.startScrolling(speed); 389 | } 390 | 391 | // fix AppleWebKit rendering 392 | if(Prototype.Browser.WebKit) window.scrollBy(0,0); 393 | 394 | Event.stop(event); 395 | }, 396 | 397 | finishDrag: function(event, success) { 398 | this.dragging = false; 399 | 400 | if(this.options.quiet){ 401 | Position.prepare(); 402 | var pointer = [Event.pointerX(event), Event.pointerY(event)]; 403 | Droppables.show(pointer, this.element); 404 | } 405 | 406 | if(this.options.ghosting) { 407 | if (!this._originallyAbsolute) 408 | Position.relativize(this.element); 409 | delete this._originallyAbsolute; 410 | Element.remove(this._clone); 411 | this._clone = null; 412 | } 413 | 414 | var dropped = false; 415 | if(success) { 416 | dropped = Droppables.fire(event, this.element); 417 | if (!dropped) dropped = false; 418 | } 419 | if(dropped && this.options.onDropped) this.options.onDropped(this.element); 420 | Draggables.notify('onEnd', this, event); 421 | 422 | var revert = this.options.revert; 423 | if(revert && Object.isFunction(revert)) revert = revert(this.element); 424 | 425 | var d = this.currentDelta(); 426 | if(revert && this.options.reverteffect) { 427 | if (dropped == 0 || revert != 'failure') 428 | this.options.reverteffect(this.element, 429 | d[1]-this.delta[1], d[0]-this.delta[0]); 430 | } else { 431 | this.delta = d; 432 | } 433 | 434 | if(this.options.zindex) 435 | this.element.style.zIndex = this.originalZ; 436 | 437 | if(this.options.endeffect) 438 | this.options.endeffect(this.element); 439 | 440 | Draggables.deactivate(this); 441 | Droppables.reset(); 442 | }, 443 | 444 | keyPress: function(event) { 445 | if(event.keyCode!=Event.KEY_ESC) return; 446 | this.finishDrag(event, false); 447 | Event.stop(event); 448 | }, 449 | 450 | endDrag: function(event) { 451 | if(!this.dragging) return; 452 | this.stopScrolling(); 453 | this.finishDrag(event, true); 454 | Event.stop(event); 455 | }, 456 | 457 | draw: function(point) { 458 | var pos = this.element.cumulativeOffset(); 459 | if(this.options.ghosting) { 460 | var r = Position.realOffset(this.element); 461 | pos[0] += r[0] - Position.deltaX; pos[1] += r[1] - Position.deltaY; 462 | } 463 | 464 | var d = this.currentDelta(); 465 | pos[0] -= d[0]; pos[1] -= d[1]; 466 | 467 | if(this.options.scroll && (this.options.scroll != window && this._isScrollChild)) { 468 | pos[0] -= this.options.scroll.scrollLeft-this.originalScrollLeft; 469 | pos[1] -= this.options.scroll.scrollTop-this.originalScrollTop; 470 | } 471 | 472 | var p = [0,1].map(function(i){ 473 | return (point[i]-pos[i]-this.offset[i]) 474 | }.bind(this)); 475 | 476 | if(this.options.snap) { 477 | if(Object.isFunction(this.options.snap)) { 478 | p = this.options.snap(p[0],p[1],this); 479 | } else { 480 | if(Object.isArray(this.options.snap)) { 481 | p = p.map( function(v, i) { 482 | return (v/this.options.snap[i]).round()*this.options.snap[i] }.bind(this)); 483 | } else { 484 | p = p.map( function(v) { 485 | return (v/this.options.snap).round()*this.options.snap }.bind(this)); 486 | } 487 | }} 488 | 489 | var style = this.element.style; 490 | if((!this.options.constraint) || (this.options.constraint=='horizontal')) 491 | style.left = p[0] + "px"; 492 | if((!this.options.constraint) || (this.options.constraint=='vertical')) 493 | style.top = p[1] + "px"; 494 | 495 | if(style.visibility=="hidden") style.visibility = ""; // fix gecko rendering 496 | }, 497 | 498 | stopScrolling: function() { 499 | if(this.scrollInterval) { 500 | clearInterval(this.scrollInterval); 501 | this.scrollInterval = null; 502 | Draggables._lastScrollPointer = null; 503 | } 504 | }, 505 | 506 | startScrolling: function(speed) { 507 | if(!(speed[0] || speed[1])) return; 508 | this.scrollSpeed = [speed[0]*this.options.scrollSpeed,speed[1]*this.options.scrollSpeed]; 509 | this.lastScrolled = new Date(); 510 | this.scrollInterval = setInterval(this.scroll.bind(this), 10); 511 | }, 512 | 513 | scroll: function() { 514 | var current = new Date(); 515 | var delta = current - this.lastScrolled; 516 | this.lastScrolled = current; 517 | if(this.options.scroll == window) { 518 | with (this._getWindowScroll(this.options.scroll)) { 519 | if (this.scrollSpeed[0] || this.scrollSpeed[1]) { 520 | var d = delta / 1000; 521 | this.options.scroll.scrollTo( left + d*this.scrollSpeed[0], top + d*this.scrollSpeed[1] ); 522 | } 523 | } 524 | } else { 525 | this.options.scroll.scrollLeft += this.scrollSpeed[0] * delta / 1000; 526 | this.options.scroll.scrollTop += this.scrollSpeed[1] * delta / 1000; 527 | } 528 | 529 | Position.prepare(); 530 | Droppables.show(Draggables._lastPointer, this.element); 531 | Draggables.notify('onDrag', this); 532 | if (this._isScrollChild) { 533 | Draggables._lastScrollPointer = Draggables._lastScrollPointer || $A(Draggables._lastPointer); 534 | Draggables._lastScrollPointer[0] += this.scrollSpeed[0] * delta / 1000; 535 | Draggables._lastScrollPointer[1] += this.scrollSpeed[1] * delta / 1000; 536 | if (Draggables._lastScrollPointer[0] < 0) 537 | Draggables._lastScrollPointer[0] = 0; 538 | if (Draggables._lastScrollPointer[1] < 0) 539 | Draggables._lastScrollPointer[1] = 0; 540 | this.draw(Draggables._lastScrollPointer); 541 | } 542 | 543 | if(this.options.change) this.options.change(this); 544 | }, 545 | 546 | _getWindowScroll: function(w) { 547 | var T, L, W, H; 548 | with (w.document) { 549 | if (w.document.documentElement && documentElement.scrollTop) { 550 | T = documentElement.scrollTop; 551 | L = documentElement.scrollLeft; 552 | } else if (w.document.body) { 553 | T = body.scrollTop; 554 | L = body.scrollLeft; 555 | } 556 | if (w.innerWidth) { 557 | W = w.innerWidth; 558 | H = w.innerHeight; 559 | } else if (w.document.documentElement && documentElement.clientWidth) { 560 | W = documentElement.clientWidth; 561 | H = documentElement.clientHeight; 562 | } else { 563 | W = body.offsetWidth; 564 | H = body.offsetHeight; 565 | } 566 | } 567 | return { top: T, left: L, width: W, height: H }; 568 | } 569 | }); 570 | 571 | Draggable._dragging = { }; 572 | 573 | /*--------------------------------------------------------------------------*/ 574 | 575 | var SortableObserver = Class.create({ 576 | initialize: function(element, observer) { 577 | this.element = $(element); 578 | this.observer = observer; 579 | this.lastValue = Sortable.serialize(this.element); 580 | }, 581 | 582 | onStart: function() { 583 | this.lastValue = Sortable.serialize(this.element); 584 | }, 585 | 586 | onEnd: function() { 587 | Sortable.unmark(); 588 | if(this.lastValue != Sortable.serialize(this.element)) 589 | this.observer(this.element) 590 | } 591 | }); 592 | 593 | var Sortable = { 594 | SERIALIZE_RULE: /^[^_\-](?:[A-Za-z0-9\-\_]*)[_](.*)$/, 595 | 596 | sortables: { }, 597 | 598 | _findRootElement: function(element) { 599 | while (element.tagName.toUpperCase() != "BODY") { 600 | if(element.id && Sortable.sortables[element.id]) return element; 601 | element = element.parentNode; 602 | } 603 | }, 604 | 605 | options: function(element) { 606 | element = Sortable._findRootElement($(element)); 607 | if(!element) return; 608 | return Sortable.sortables[element.id]; 609 | }, 610 | 611 | destroy: function(element){ 612 | element = $(element); 613 | var s = Sortable.sortables[element.id]; 614 | 615 | if(s) { 616 | Draggables.removeObserver(s.element); 617 | s.droppables.each(function(d){ Droppables.remove(d) }); 618 | s.draggables.invoke('destroy'); 619 | 620 | delete Sortable.sortables[s.element.id]; 621 | } 622 | }, 623 | 624 | create: function(element) { 625 | element = $(element); 626 | var options = Object.extend({ 627 | element: element, 628 | tag: 'li', // assumes li children, override with tag: 'tagname' 629 | dropOnEmpty: false, 630 | tree: false, 631 | treeTag: 'ul', 632 | overlap: 'vertical', // one of 'vertical', 'horizontal' 633 | constraint: 'vertical', // one of 'vertical', 'horizontal', false 634 | containment: element, // also takes array of elements (or id's); or false 635 | handle: false, // or a CSS class 636 | only: false, 637 | delay: 0, 638 | hoverclass: null, 639 | ghosting: false, 640 | quiet: false, 641 | scroll: false, 642 | scrollSensitivity: 20, 643 | scrollSpeed: 15, 644 | format: this.SERIALIZE_RULE, 645 | 646 | // these take arrays of elements or ids and can be 647 | // used for better initialization performance 648 | elements: false, 649 | handles: false, 650 | 651 | onChange: Prototype.emptyFunction, 652 | onUpdate: Prototype.emptyFunction 653 | }, arguments[1] || { }); 654 | 655 | // clear any old sortable with same element 656 | this.destroy(element); 657 | 658 | // build options for the draggables 659 | var options_for_draggable = { 660 | revert: true, 661 | quiet: options.quiet, 662 | scroll: options.scroll, 663 | scrollSpeed: options.scrollSpeed, 664 | scrollSensitivity: options.scrollSensitivity, 665 | delay: options.delay, 666 | ghosting: options.ghosting, 667 | constraint: options.constraint, 668 | handle: options.handle }; 669 | 670 | if(options.starteffect) 671 | options_for_draggable.starteffect = options.starteffect; 672 | 673 | if(options.reverteffect) 674 | options_for_draggable.reverteffect = options.reverteffect; 675 | else 676 | if(options.ghosting) options_for_draggable.reverteffect = function(element) { 677 | element.style.top = 0; 678 | element.style.left = 0; 679 | }; 680 | 681 | if(options.endeffect) 682 | options_for_draggable.endeffect = options.endeffect; 683 | 684 | if(options.zindex) 685 | options_for_draggable.zindex = options.zindex; 686 | 687 | // build options for the droppables 688 | var options_for_droppable = { 689 | overlap: options.overlap, 690 | containment: options.containment, 691 | tree: options.tree, 692 | hoverclass: options.hoverclass, 693 | onHover: Sortable.onHover 694 | }; 695 | 696 | var options_for_tree = { 697 | onHover: Sortable.onEmptyHover, 698 | overlap: options.overlap, 699 | containment: options.containment, 700 | hoverclass: options.hoverclass 701 | }; 702 | 703 | // fix for gecko engine 704 | Element.cleanWhitespace(element); 705 | 706 | options.draggables = []; 707 | options.droppables = []; 708 | 709 | // drop on empty handling 710 | if(options.dropOnEmpty || options.tree) { 711 | Droppables.add(element, options_for_tree); 712 | options.droppables.push(element); 713 | } 714 | 715 | (options.elements || this.findElements(element, options) || []).each( function(e,i) { 716 | var handle = options.handles ? $(options.handles[i]) : 717 | (options.handle ? $(e).select('.' + options.handle)[0] : e); 718 | options.draggables.push( 719 | new Draggable(e, Object.extend(options_for_draggable, { handle: handle }))); 720 | Droppables.add(e, options_for_droppable); 721 | if(options.tree) e.treeNode = element; 722 | options.droppables.push(e); 723 | }); 724 | 725 | if(options.tree) { 726 | (Sortable.findTreeElements(element, options) || []).each( function(e) { 727 | Droppables.add(e, options_for_tree); 728 | e.treeNode = element; 729 | options.droppables.push(e); 730 | }); 731 | } 732 | 733 | // keep reference 734 | this.sortables[element.identify()] = options; 735 | 736 | // for onupdate 737 | Draggables.addObserver(new SortableObserver(element, options.onUpdate)); 738 | 739 | }, 740 | 741 | // return all suitable-for-sortable elements in a guaranteed order 742 | findElements: function(element, options) { 743 | return Element.findChildren( 744 | element, options.only, options.tree ? true : false, options.tag); 745 | }, 746 | 747 | findTreeElements: function(element, options) { 748 | return Element.findChildren( 749 | element, options.only, options.tree ? true : false, options.treeTag); 750 | }, 751 | 752 | onHover: function(element, dropon, overlap) { 753 | if(Element.isParent(dropon, element)) return; 754 | 755 | if(overlap > .33 && overlap < .66 && Sortable.options(dropon).tree) { 756 | return; 757 | } else if(overlap>0.5) { 758 | Sortable.mark(dropon, 'before'); 759 | if(dropon.previousSibling != element) { 760 | var oldParentNode = element.parentNode; 761 | element.style.visibility = "hidden"; // fix gecko rendering 762 | dropon.parentNode.insertBefore(element, dropon); 763 | if(dropon.parentNode!=oldParentNode) 764 | Sortable.options(oldParentNode).onChange(element); 765 | Sortable.options(dropon.parentNode).onChange(element); 766 | } 767 | } else { 768 | Sortable.mark(dropon, 'after'); 769 | var nextElement = dropon.nextSibling || null; 770 | if(nextElement != element) { 771 | var oldParentNode = element.parentNode; 772 | element.style.visibility = "hidden"; // fix gecko rendering 773 | dropon.parentNode.insertBefore(element, nextElement); 774 | if(dropon.parentNode!=oldParentNode) 775 | Sortable.options(oldParentNode).onChange(element); 776 | Sortable.options(dropon.parentNode).onChange(element); 777 | } 778 | } 779 | }, 780 | 781 | onEmptyHover: function(element, dropon, overlap) { 782 | var oldParentNode = element.parentNode; 783 | var droponOptions = Sortable.options(dropon); 784 | 785 | if(!Element.isParent(dropon, element)) { 786 | var index; 787 | 788 | var children = Sortable.findElements(dropon, {tag: droponOptions.tag, only: droponOptions.only}); 789 | var child = null; 790 | 791 | if(children) { 792 | var offset = Element.offsetSize(dropon, droponOptions.overlap) * (1.0 - overlap); 793 | 794 | for (index = 0; index < children.length; index += 1) { 795 | if (offset - Element.offsetSize (children[index], droponOptions.overlap) >= 0) { 796 | offset -= Element.offsetSize (children[index], droponOptions.overlap); 797 | } else if (offset - (Element.offsetSize (children[index], droponOptions.overlap) / 2) >= 0) { 798 | child = index + 1 < children.length ? children[index + 1] : null; 799 | break; 800 | } else { 801 | child = children[index]; 802 | break; 803 | } 804 | } 805 | } 806 | 807 | dropon.insertBefore(element, child); 808 | 809 | Sortable.options(oldParentNode).onChange(element); 810 | droponOptions.onChange(element); 811 | } 812 | }, 813 | 814 | unmark: function() { 815 | if(Sortable._marker) Sortable._marker.hide(); 816 | }, 817 | 818 | mark: function(dropon, position) { 819 | // mark on ghosting only 820 | var sortable = Sortable.options(dropon.parentNode); 821 | if(sortable && !sortable.ghosting) return; 822 | 823 | if(!Sortable._marker) { 824 | Sortable._marker = 825 | ($('dropmarker') || Element.extend(document.createElement('DIV'))). 826 | hide().addClassName('dropmarker').setStyle({position:'absolute'}); 827 | document.getElementsByTagName("body").item(0).appendChild(Sortable._marker); 828 | } 829 | var offsets = dropon.cumulativeOffset(); 830 | Sortable._marker.setStyle({left: offsets[0]+'px', top: offsets[1] + 'px'}); 831 | 832 | if(position=='after') 833 | if(sortable.overlap == 'horizontal') 834 | Sortable._marker.setStyle({left: (offsets[0]+dropon.clientWidth) + 'px'}); 835 | else 836 | Sortable._marker.setStyle({top: (offsets[1]+dropon.clientHeight) + 'px'}); 837 | 838 | Sortable._marker.show(); 839 | }, 840 | 841 | _tree: function(element, options, parent) { 842 | var children = Sortable.findElements(element, options) || []; 843 | 844 | for (var i = 0; i < children.length; ++i) { 845 | var match = children[i].id.match(options.format); 846 | 847 | if (!match) continue; 848 | 849 | var child = { 850 | id: encodeURIComponent(match ? match[1] : null), 851 | element: element, 852 | parent: parent, 853 | children: [], 854 | position: parent.children.length, 855 | container: $(children[i]).down(options.treeTag) 856 | }; 857 | 858 | /* Get the element containing the children and recurse over it */ 859 | if (child.container) 860 | this._tree(child.container, options, child); 861 | 862 | parent.children.push (child); 863 | } 864 | 865 | return parent; 866 | }, 867 | 868 | tree: function(element) { 869 | element = $(element); 870 | var sortableOptions = this.options(element); 871 | var options = Object.extend({ 872 | tag: sortableOptions.tag, 873 | treeTag: sortableOptions.treeTag, 874 | only: sortableOptions.only, 875 | name: element.id, 876 | format: sortableOptions.format 877 | }, arguments[1] || { }); 878 | 879 | var root = { 880 | id: null, 881 | parent: null, 882 | children: [], 883 | container: element, 884 | position: 0 885 | }; 886 | 887 | return Sortable._tree(element, options, root); 888 | }, 889 | 890 | /* Construct a [i] index for a particular node */ 891 | _constructIndex: function(node) { 892 | var index = ''; 893 | do { 894 | if (node.id) index = '[' + node.position + ']' + index; 895 | } while ((node = node.parent) != null); 896 | return index; 897 | }, 898 | 899 | sequence: function(element) { 900 | element = $(element); 901 | var options = Object.extend(this.options(element), arguments[1] || { }); 902 | 903 | return $(this.findElements(element, options) || []).map( function(item) { 904 | return item.id.match(options.format) ? item.id.match(options.format)[1] : ''; 905 | }); 906 | }, 907 | 908 | setSequence: function(element, new_sequence) { 909 | element = $(element); 910 | var options = Object.extend(this.options(element), arguments[2] || { }); 911 | 912 | var nodeMap = { }; 913 | this.findElements(element, options).each( function(n) { 914 | if (n.id.match(options.format)) 915 | nodeMap[n.id.match(options.format)[1]] = [n, n.parentNode]; 916 | n.parentNode.removeChild(n); 917 | }); 918 | 919 | new_sequence.each(function(ident) { 920 | var n = nodeMap[ident]; 921 | if (n) { 922 | n[1].appendChild(n[0]); 923 | delete nodeMap[ident]; 924 | } 925 | }); 926 | }, 927 | 928 | serialize: function(element) { 929 | element = $(element); 930 | var options = Object.extend(Sortable.options(element), arguments[1] || { }); 931 | var name = encodeURIComponent( 932 | (arguments[1] && arguments[1].name) ? arguments[1].name : element.id); 933 | 934 | if (options.tree) { 935 | return Sortable.tree(element, arguments[1]).children.map( function (item) { 936 | return [name + Sortable._constructIndex(item) + "[id]=" + 937 | encodeURIComponent(item.id)].concat(item.children.map(arguments.callee)); 938 | }).flatten().join('&'); 939 | } else { 940 | return Sortable.sequence(element, arguments[1]).map( function(item) { 941 | return name + "[]=" + encodeURIComponent(item); 942 | }).join('&'); 943 | } 944 | } 945 | }; 946 | 947 | // Returns true if child is contained within element 948 | Element.isParent = function(child, element) { 949 | if (!child.parentNode || child == element) return false; 950 | if (child.parentNode == element) return true; 951 | return Element.isParent(child.parentNode, element); 952 | }; 953 | 954 | Element.findChildren = function(element, only, recursive, tagName) { 955 | if(!element.hasChildNodes()) return null; 956 | tagName = tagName.toUpperCase(); 957 | if(only) only = [only].flatten(); 958 | var elements = []; 959 | $A(element.childNodes).each( function(e) { 960 | if(e.tagName && e.tagName.toUpperCase()==tagName && 961 | (!only || (Element.classNames(e).detect(function(v) { return only.include(v) })))) 962 | elements.push(e); 963 | if(recursive) { 964 | var grandchildren = Element.findChildren(e, only, recursive, tagName); 965 | if(grandchildren) elements.push(grandchildren); 966 | } 967 | }); 968 | 969 | return (elements.length>0 ? elements.flatten() : []); 970 | }; 971 | 972 | Element.offsetSize = function (element, type) { 973 | return element['offset' + ((type=='vertical' || type=='height') ? 'Height' : 'Width')]; 974 | }; -------------------------------------------------------------------------------- /public/javascripts/controls.js: -------------------------------------------------------------------------------- 1 | // script.aculo.us controls.js v1.8.3, Thu Oct 08 11:23:33 +0200 2009 2 | 3 | // Copyright (c) 2005-2009 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us) 4 | // (c) 2005-2009 Ivan Krstic (http://blogs.law.harvard.edu/ivan) 5 | // (c) 2005-2009 Jon Tirsen (http://www.tirsen.com) 6 | // Contributors: 7 | // Richard Livsey 8 | // Rahul Bhargava 9 | // Rob Wills 10 | // 11 | // script.aculo.us is freely distributable under the terms of an MIT-style license. 12 | // For details, see the script.aculo.us web site: http://script.aculo.us/ 13 | 14 | // Autocompleter.Base handles all the autocompletion functionality 15 | // that's independent of the data source for autocompletion. This 16 | // includes drawing the autocompletion menu, observing keyboard 17 | // and mouse events, and similar. 18 | // 19 | // Specific autocompleters need to provide, at the very least, 20 | // a getUpdatedChoices function that will be invoked every time 21 | // the text inside the monitored textbox changes. This method 22 | // should get the text for which to provide autocompletion by 23 | // invoking this.getToken(), NOT by directly accessing 24 | // this.element.value. This is to allow incremental tokenized 25 | // autocompletion. Specific auto-completion logic (AJAX, etc) 26 | // belongs in getUpdatedChoices. 27 | // 28 | // Tokenized incremental autocompletion is enabled automatically 29 | // when an autocompleter is instantiated with the 'tokens' option 30 | // in the options parameter, e.g.: 31 | // new Ajax.Autocompleter('id','upd', '/url/', { tokens: ',' }); 32 | // will incrementally autocomplete with a comma as the token. 33 | // Additionally, ',' in the above example can be replaced with 34 | // a token array, e.g. { tokens: [',', '\n'] } which 35 | // enables autocompletion on multiple tokens. This is most 36 | // useful when one of the tokens is \n (a newline), as it 37 | // allows smart autocompletion after linebreaks. 38 | 39 | if(typeof Effect == 'undefined') 40 | throw("controls.js requires including script.aculo.us' effects.js library"); 41 | 42 | var Autocompleter = { }; 43 | Autocompleter.Base = Class.create({ 44 | baseInitialize: function(element, update, options) { 45 | element = $(element); 46 | this.element = element; 47 | this.update = $(update); 48 | this.hasFocus = false; 49 | this.changed = false; 50 | this.active = false; 51 | this.index = 0; 52 | this.entryCount = 0; 53 | this.oldElementValue = this.element.value; 54 | 55 | if(this.setOptions) 56 | this.setOptions(options); 57 | else 58 | this.options = options || { }; 59 | 60 | this.options.paramName = this.options.paramName || this.element.name; 61 | this.options.tokens = this.options.tokens || []; 62 | this.options.frequency = this.options.frequency || 0.4; 63 | this.options.minChars = this.options.minChars || 1; 64 | this.options.onShow = this.options.onShow || 65 | function(element, update){ 66 | if(!update.style.position || update.style.position=='absolute') { 67 | update.style.position = 'absolute'; 68 | Position.clone(element, update, { 69 | setHeight: false, 70 | offsetTop: element.offsetHeight 71 | }); 72 | } 73 | Effect.Appear(update,{duration:0.15}); 74 | }; 75 | this.options.onHide = this.options.onHide || 76 | function(element, update){ new Effect.Fade(update,{duration:0.15}) }; 77 | 78 | if(typeof(this.options.tokens) == 'string') 79 | this.options.tokens = new Array(this.options.tokens); 80 | // Force carriage returns as token delimiters anyway 81 | if (!this.options.tokens.include('\n')) 82 | this.options.tokens.push('\n'); 83 | 84 | this.observer = null; 85 | 86 | this.element.setAttribute('autocomplete','off'); 87 | 88 | Element.hide(this.update); 89 | 90 | Event.observe(this.element, 'blur', this.onBlur.bindAsEventListener(this)); 91 | Event.observe(this.element, 'keydown', this.onKeyPress.bindAsEventListener(this)); 92 | }, 93 | 94 | show: function() { 95 | if(Element.getStyle(this.update, 'display')=='none') this.options.onShow(this.element, this.update); 96 | if(!this.iefix && 97 | (Prototype.Browser.IE) && 98 | (Element.getStyle(this.update, 'position')=='absolute')) { 99 | new Insertion.After(this.update, 100 | ''); 103 | this.iefix = $(this.update.id+'_iefix'); 104 | } 105 | if(this.iefix) setTimeout(this.fixIEOverlapping.bind(this), 50); 106 | }, 107 | 108 | fixIEOverlapping: function() { 109 | Position.clone(this.update, this.iefix, {setTop:(!this.update.style.height)}); 110 | this.iefix.style.zIndex = 1; 111 | this.update.style.zIndex = 2; 112 | Element.show(this.iefix); 113 | }, 114 | 115 | hide: function() { 116 | this.stopIndicator(); 117 | if(Element.getStyle(this.update, 'display')!='none') this.options.onHide(this.element, this.update); 118 | if(this.iefix) Element.hide(this.iefix); 119 | }, 120 | 121 | startIndicator: function() { 122 | if(this.options.indicator) Element.show(this.options.indicator); 123 | }, 124 | 125 | stopIndicator: function() { 126 | if(this.options.indicator) Element.hide(this.options.indicator); 127 | }, 128 | 129 | onKeyPress: function(event) { 130 | if(this.active) 131 | switch(event.keyCode) { 132 | case Event.KEY_TAB: 133 | case Event.KEY_RETURN: 134 | this.selectEntry(); 135 | Event.stop(event); 136 | case Event.KEY_ESC: 137 | this.hide(); 138 | this.active = false; 139 | Event.stop(event); 140 | return; 141 | case Event.KEY_LEFT: 142 | case Event.KEY_RIGHT: 143 | return; 144 | case Event.KEY_UP: 145 | this.markPrevious(); 146 | this.render(); 147 | Event.stop(event); 148 | return; 149 | case Event.KEY_DOWN: 150 | this.markNext(); 151 | this.render(); 152 | Event.stop(event); 153 | return; 154 | } 155 | else 156 | if(event.keyCode==Event.KEY_TAB || event.keyCode==Event.KEY_RETURN || 157 | (Prototype.Browser.WebKit > 0 && event.keyCode == 0)) return; 158 | 159 | this.changed = true; 160 | this.hasFocus = true; 161 | 162 | if(this.observer) clearTimeout(this.observer); 163 | this.observer = 164 | setTimeout(this.onObserverEvent.bind(this), this.options.frequency*1000); 165 | }, 166 | 167 | activate: function() { 168 | this.changed = false; 169 | this.hasFocus = true; 170 | this.getUpdatedChoices(); 171 | }, 172 | 173 | onHover: function(event) { 174 | var element = Event.findElement(event, 'LI'); 175 | if(this.index != element.autocompleteIndex) 176 | { 177 | this.index = element.autocompleteIndex; 178 | this.render(); 179 | } 180 | Event.stop(event); 181 | }, 182 | 183 | onClick: function(event) { 184 | var element = Event.findElement(event, 'LI'); 185 | this.index = element.autocompleteIndex; 186 | this.selectEntry(); 187 | this.hide(); 188 | }, 189 | 190 | onBlur: function(event) { 191 | // needed to make click events working 192 | setTimeout(this.hide.bind(this), 250); 193 | this.hasFocus = false; 194 | this.active = false; 195 | }, 196 | 197 | render: function() { 198 | if(this.entryCount > 0) { 199 | for (var i = 0; i < this.entryCount; i++) 200 | this.index==i ? 201 | Element.addClassName(this.getEntry(i),"selected") : 202 | Element.removeClassName(this.getEntry(i),"selected"); 203 | if(this.hasFocus) { 204 | this.show(); 205 | this.active = true; 206 | } 207 | } else { 208 | this.active = false; 209 | this.hide(); 210 | } 211 | }, 212 | 213 | markPrevious: function() { 214 | if(this.index > 0) this.index--; 215 | else this.index = this.entryCount-1; 216 | this.getEntry(this.index).scrollIntoView(true); 217 | }, 218 | 219 | markNext: function() { 220 | if(this.index < this.entryCount-1) this.index++; 221 | else this.index = 0; 222 | this.getEntry(this.index).scrollIntoView(false); 223 | }, 224 | 225 | getEntry: function(index) { 226 | return this.update.firstChild.childNodes[index]; 227 | }, 228 | 229 | getCurrentEntry: function() { 230 | return this.getEntry(this.index); 231 | }, 232 | 233 | selectEntry: function() { 234 | this.active = false; 235 | this.updateElement(this.getCurrentEntry()); 236 | }, 237 | 238 | updateElement: function(selectedElement) { 239 | if (this.options.updateElement) { 240 | this.options.updateElement(selectedElement); 241 | return; 242 | } 243 | var value = ''; 244 | if (this.options.select) { 245 | var nodes = $(selectedElement).select('.' + this.options.select) || []; 246 | if(nodes.length>0) value = Element.collectTextNodes(nodes[0], this.options.select); 247 | } else 248 | value = Element.collectTextNodesIgnoreClass(selectedElement, 'informal'); 249 | 250 | var bounds = this.getTokenBounds(); 251 | if (bounds[0] != -1) { 252 | var newValue = this.element.value.substr(0, bounds[0]); 253 | var whitespace = this.element.value.substr(bounds[0]).match(/^\s+/); 254 | if (whitespace) 255 | newValue += whitespace[0]; 256 | this.element.value = newValue + value + this.element.value.substr(bounds[1]); 257 | } else { 258 | this.element.value = value; 259 | } 260 | this.oldElementValue = this.element.value; 261 | this.element.focus(); 262 | 263 | if (this.options.afterUpdateElement) 264 | this.options.afterUpdateElement(this.element, selectedElement); 265 | }, 266 | 267 | updateChoices: function(choices) { 268 | if(!this.changed && this.hasFocus) { 269 | this.update.innerHTML = choices; 270 | Element.cleanWhitespace(this.update); 271 | Element.cleanWhitespace(this.update.down()); 272 | 273 | if(this.update.firstChild && this.update.down().childNodes) { 274 | this.entryCount = 275 | this.update.down().childNodes.length; 276 | for (var i = 0; i < this.entryCount; i++) { 277 | var entry = this.getEntry(i); 278 | entry.autocompleteIndex = i; 279 | this.addObservers(entry); 280 | } 281 | } else { 282 | this.entryCount = 0; 283 | } 284 | 285 | this.stopIndicator(); 286 | this.index = 0; 287 | 288 | if(this.entryCount==1 && this.options.autoSelect) { 289 | this.selectEntry(); 290 | this.hide(); 291 | } else { 292 | this.render(); 293 | } 294 | } 295 | }, 296 | 297 | addObservers: function(element) { 298 | Event.observe(element, "mouseover", this.onHover.bindAsEventListener(this)); 299 | Event.observe(element, "click", this.onClick.bindAsEventListener(this)); 300 | }, 301 | 302 | onObserverEvent: function() { 303 | this.changed = false; 304 | this.tokenBounds = null; 305 | if(this.getToken().length>=this.options.minChars) { 306 | this.getUpdatedChoices(); 307 | } else { 308 | this.active = false; 309 | this.hide(); 310 | } 311 | this.oldElementValue = this.element.value; 312 | }, 313 | 314 | getToken: function() { 315 | var bounds = this.getTokenBounds(); 316 | return this.element.value.substring(bounds[0], bounds[1]).strip(); 317 | }, 318 | 319 | getTokenBounds: function() { 320 | if (null != this.tokenBounds) return this.tokenBounds; 321 | var value = this.element.value; 322 | if (value.strip().empty()) return [-1, 0]; 323 | var diff = arguments.callee.getFirstDifferencePos(value, this.oldElementValue); 324 | var offset = (diff == this.oldElementValue.length ? 1 : 0); 325 | var prevTokenPos = -1, nextTokenPos = value.length; 326 | var tp; 327 | for (var index = 0, l = this.options.tokens.length; index < l; ++index) { 328 | tp = value.lastIndexOf(this.options.tokens[index], diff + offset - 1); 329 | if (tp > prevTokenPos) prevTokenPos = tp; 330 | tp = value.indexOf(this.options.tokens[index], diff + offset); 331 | if (-1 != tp && tp < nextTokenPos) nextTokenPos = tp; 332 | } 333 | return (this.tokenBounds = [prevTokenPos + 1, nextTokenPos]); 334 | } 335 | }); 336 | 337 | Autocompleter.Base.prototype.getTokenBounds.getFirstDifferencePos = function(newS, oldS) { 338 | var boundary = Math.min(newS.length, oldS.length); 339 | for (var index = 0; index < boundary; ++index) 340 | if (newS[index] != oldS[index]) 341 | return index; 342 | return boundary; 343 | }; 344 | 345 | Ajax.Autocompleter = Class.create(Autocompleter.Base, { 346 | initialize: function(element, update, url, options) { 347 | this.baseInitialize(element, update, options); 348 | this.options.asynchronous = true; 349 | this.options.onComplete = this.onComplete.bind(this); 350 | this.options.defaultParams = this.options.parameters || null; 351 | this.url = url; 352 | }, 353 | 354 | getUpdatedChoices: function() { 355 | this.startIndicator(); 356 | 357 | var entry = encodeURIComponent(this.options.paramName) + '=' + 358 | encodeURIComponent(this.getToken()); 359 | 360 | this.options.parameters = this.options.callback ? 361 | this.options.callback(this.element, entry) : entry; 362 | 363 | if(this.options.defaultParams) 364 | this.options.parameters += '&' + this.options.defaultParams; 365 | 366 | new Ajax.Request(this.url, this.options); 367 | }, 368 | 369 | onComplete: function(request) { 370 | this.updateChoices(request.responseText); 371 | } 372 | }); 373 | 374 | // The local array autocompleter. Used when you'd prefer to 375 | // inject an array of autocompletion options into the page, rather 376 | // than sending out Ajax queries, which can be quite slow sometimes. 377 | // 378 | // The constructor takes four parameters. The first two are, as usual, 379 | // the id of the monitored textbox, and id of the autocompletion menu. 380 | // The third is the array you want to autocomplete from, and the fourth 381 | // is the options block. 382 | // 383 | // Extra local autocompletion options: 384 | // - choices - How many autocompletion choices to offer 385 | // 386 | // - partialSearch - If false, the autocompleter will match entered 387 | // text only at the beginning of strings in the 388 | // autocomplete array. Defaults to true, which will 389 | // match text at the beginning of any *word* in the 390 | // strings in the autocomplete array. If you want to 391 | // search anywhere in the string, additionally set 392 | // the option fullSearch to true (default: off). 393 | // 394 | // - fullSsearch - Search anywhere in autocomplete array strings. 395 | // 396 | // - partialChars - How many characters to enter before triggering 397 | // a partial match (unlike minChars, which defines 398 | // how many characters are required to do any match 399 | // at all). Defaults to 2. 400 | // 401 | // - ignoreCase - Whether to ignore case when autocompleting. 402 | // Defaults to true. 403 | // 404 | // It's possible to pass in a custom function as the 'selector' 405 | // option, if you prefer to write your own autocompletion logic. 406 | // In that case, the other options above will not apply unless 407 | // you support them. 408 | 409 | Autocompleter.Local = Class.create(Autocompleter.Base, { 410 | initialize: function(element, update, array, options) { 411 | this.baseInitialize(element, update, options); 412 | this.options.array = array; 413 | }, 414 | 415 | getUpdatedChoices: function() { 416 | this.updateChoices(this.options.selector(this)); 417 | }, 418 | 419 | setOptions: function(options) { 420 | this.options = Object.extend({ 421 | choices: 10, 422 | partialSearch: true, 423 | partialChars: 2, 424 | ignoreCase: true, 425 | fullSearch: false, 426 | selector: function(instance) { 427 | var ret = []; // Beginning matches 428 | var partial = []; // Inside matches 429 | var entry = instance.getToken(); 430 | var count = 0; 431 | 432 | for (var i = 0; i < instance.options.array.length && 433 | ret.length < instance.options.choices ; i++) { 434 | 435 | var elem = instance.options.array[i]; 436 | var foundPos = instance.options.ignoreCase ? 437 | elem.toLowerCase().indexOf(entry.toLowerCase()) : 438 | elem.indexOf(entry); 439 | 440 | while (foundPos != -1) { 441 | if (foundPos == 0 && elem.length != entry.length) { 442 | ret.push("
  • " + elem.substr(0, entry.length) + "" + 443 | elem.substr(entry.length) + "
  • "); 444 | break; 445 | } else if (entry.length >= instance.options.partialChars && 446 | instance.options.partialSearch && foundPos != -1) { 447 | if (instance.options.fullSearch || /\s/.test(elem.substr(foundPos-1,1))) { 448 | partial.push("
  • " + elem.substr(0, foundPos) + "" + 449 | elem.substr(foundPos, entry.length) + "" + elem.substr( 450 | foundPos + entry.length) + "
  • "); 451 | break; 452 | } 453 | } 454 | 455 | foundPos = instance.options.ignoreCase ? 456 | elem.toLowerCase().indexOf(entry.toLowerCase(), foundPos + 1) : 457 | elem.indexOf(entry, foundPos + 1); 458 | 459 | } 460 | } 461 | if (partial.length) 462 | ret = ret.concat(partial.slice(0, instance.options.choices - ret.length)); 463 | return ""; 464 | } 465 | }, options || { }); 466 | } 467 | }); 468 | 469 | // AJAX in-place editor and collection editor 470 | // Full rewrite by Christophe Porteneuve (April 2007). 471 | 472 | // Use this if you notice weird scrolling problems on some browsers, 473 | // the DOM might be a bit confused when this gets called so do this 474 | // waits 1 ms (with setTimeout) until it does the activation 475 | Field.scrollFreeActivate = function(field) { 476 | setTimeout(function() { 477 | Field.activate(field); 478 | }, 1); 479 | }; 480 | 481 | Ajax.InPlaceEditor = Class.create({ 482 | initialize: function(element, url, options) { 483 | this.url = url; 484 | this.element = element = $(element); 485 | this.prepareOptions(); 486 | this._controls = { }; 487 | arguments.callee.dealWithDeprecatedOptions(options); // DEPRECATION LAYER!!! 488 | Object.extend(this.options, options || { }); 489 | if (!this.options.formId && this.element.id) { 490 | this.options.formId = this.element.id + '-inplaceeditor'; 491 | if ($(this.options.formId)) 492 | this.options.formId = ''; 493 | } 494 | if (this.options.externalControl) 495 | this.options.externalControl = $(this.options.externalControl); 496 | if (!this.options.externalControl) 497 | this.options.externalControlOnly = false; 498 | this._originalBackground = this.element.getStyle('background-color') || 'transparent'; 499 | this.element.title = this.options.clickToEditText; 500 | this._boundCancelHandler = this.handleFormCancellation.bind(this); 501 | this._boundComplete = (this.options.onComplete || Prototype.emptyFunction).bind(this); 502 | this._boundFailureHandler = this.handleAJAXFailure.bind(this); 503 | this._boundSubmitHandler = this.handleFormSubmission.bind(this); 504 | this._boundWrapperHandler = this.wrapUp.bind(this); 505 | this.registerListeners(); 506 | }, 507 | checkForEscapeOrReturn: function(e) { 508 | if (!this._editing || e.ctrlKey || e.altKey || e.shiftKey) return; 509 | if (Event.KEY_ESC == e.keyCode) 510 | this.handleFormCancellation(e); 511 | else if (Event.KEY_RETURN == e.keyCode) 512 | this.handleFormSubmission(e); 513 | }, 514 | createControl: function(mode, handler, extraClasses) { 515 | var control = this.options[mode + 'Control']; 516 | var text = this.options[mode + 'Text']; 517 | if ('button' == control) { 518 | var btn = document.createElement('input'); 519 | btn.type = 'submit'; 520 | btn.value = text; 521 | btn.className = 'editor_' + mode + '_button'; 522 | if ('cancel' == mode) 523 | btn.onclick = this._boundCancelHandler; 524 | this._form.appendChild(btn); 525 | this._controls[mode] = btn; 526 | } else if ('link' == control) { 527 | var link = document.createElement('a'); 528 | link.href = '#'; 529 | link.appendChild(document.createTextNode(text)); 530 | link.onclick = 'cancel' == mode ? this._boundCancelHandler : this._boundSubmitHandler; 531 | link.className = 'editor_' + mode + '_link'; 532 | if (extraClasses) 533 | link.className += ' ' + extraClasses; 534 | this._form.appendChild(link); 535 | this._controls[mode] = link; 536 | } 537 | }, 538 | createEditField: function() { 539 | var text = (this.options.loadTextURL ? this.options.loadingText : this.getText()); 540 | var fld; 541 | if (1 >= this.options.rows && !/\r|\n/.test(this.getText())) { 542 | fld = document.createElement('input'); 543 | fld.type = 'text'; 544 | var size = this.options.size || this.options.cols || 0; 545 | if (0 < size) fld.size = size; 546 | } else { 547 | fld = document.createElement('textarea'); 548 | fld.rows = (1 >= this.options.rows ? this.options.autoRows : this.options.rows); 549 | fld.cols = this.options.cols || 40; 550 | } 551 | fld.name = this.options.paramName; 552 | fld.value = text; // No HTML breaks conversion anymore 553 | fld.className = 'editor_field'; 554 | if (this.options.submitOnBlur) 555 | fld.onblur = this._boundSubmitHandler; 556 | this._controls.editor = fld; 557 | if (this.options.loadTextURL) 558 | this.loadExternalText(); 559 | this._form.appendChild(this._controls.editor); 560 | }, 561 | createForm: function() { 562 | var ipe = this; 563 | function addText(mode, condition) { 564 | var text = ipe.options['text' + mode + 'Controls']; 565 | if (!text || condition === false) return; 566 | ipe._form.appendChild(document.createTextNode(text)); 567 | }; 568 | this._form = $(document.createElement('form')); 569 | this._form.id = this.options.formId; 570 | this._form.addClassName(this.options.formClassName); 571 | this._form.onsubmit = this._boundSubmitHandler; 572 | this.createEditField(); 573 | if ('textarea' == this._controls.editor.tagName.toLowerCase()) 574 | this._form.appendChild(document.createElement('br')); 575 | if (this.options.onFormCustomization) 576 | this.options.onFormCustomization(this, this._form); 577 | addText('Before', this.options.okControl || this.options.cancelControl); 578 | this.createControl('ok', this._boundSubmitHandler); 579 | addText('Between', this.options.okControl && this.options.cancelControl); 580 | this.createControl('cancel', this._boundCancelHandler, 'editor_cancel'); 581 | addText('After', this.options.okControl || this.options.cancelControl); 582 | }, 583 | destroy: function() { 584 | if (this._oldInnerHTML) 585 | this.element.innerHTML = this._oldInnerHTML; 586 | this.leaveEditMode(); 587 | this.unregisterListeners(); 588 | }, 589 | enterEditMode: function(e) { 590 | if (this._saving || this._editing) return; 591 | this._editing = true; 592 | this.triggerCallback('onEnterEditMode'); 593 | if (this.options.externalControl) 594 | this.options.externalControl.hide(); 595 | this.element.hide(); 596 | this.createForm(); 597 | this.element.parentNode.insertBefore(this._form, this.element); 598 | if (!this.options.loadTextURL) 599 | this.postProcessEditField(); 600 | if (e) Event.stop(e); 601 | }, 602 | enterHover: function(e) { 603 | if (this.options.hoverClassName) 604 | this.element.addClassName(this.options.hoverClassName); 605 | if (this._saving) return; 606 | this.triggerCallback('onEnterHover'); 607 | }, 608 | getText: function() { 609 | return this.element.innerHTML.unescapeHTML(); 610 | }, 611 | handleAJAXFailure: function(transport) { 612 | this.triggerCallback('onFailure', transport); 613 | if (this._oldInnerHTML) { 614 | this.element.innerHTML = this._oldInnerHTML; 615 | this._oldInnerHTML = null; 616 | } 617 | }, 618 | handleFormCancellation: function(e) { 619 | this.wrapUp(); 620 | if (e) Event.stop(e); 621 | }, 622 | handleFormSubmission: function(e) { 623 | var form = this._form; 624 | var value = $F(this._controls.editor); 625 | this.prepareSubmission(); 626 | var params = this.options.callback(form, value) || ''; 627 | if (Object.isString(params)) 628 | params = params.toQueryParams(); 629 | params.editorId = this.element.id; 630 | if (this.options.htmlResponse) { 631 | var options = Object.extend({ evalScripts: true }, this.options.ajaxOptions); 632 | Object.extend(options, { 633 | parameters: params, 634 | onComplete: this._boundWrapperHandler, 635 | onFailure: this._boundFailureHandler 636 | }); 637 | new Ajax.Updater({ success: this.element }, this.url, options); 638 | } else { 639 | var options = Object.extend({ method: 'get' }, this.options.ajaxOptions); 640 | Object.extend(options, { 641 | parameters: params, 642 | onComplete: this._boundWrapperHandler, 643 | onFailure: this._boundFailureHandler 644 | }); 645 | new Ajax.Request(this.url, options); 646 | } 647 | if (e) Event.stop(e); 648 | }, 649 | leaveEditMode: function() { 650 | this.element.removeClassName(this.options.savingClassName); 651 | this.removeForm(); 652 | this.leaveHover(); 653 | this.element.style.backgroundColor = this._originalBackground; 654 | this.element.show(); 655 | if (this.options.externalControl) 656 | this.options.externalControl.show(); 657 | this._saving = false; 658 | this._editing = false; 659 | this._oldInnerHTML = null; 660 | this.triggerCallback('onLeaveEditMode'); 661 | }, 662 | leaveHover: function(e) { 663 | if (this.options.hoverClassName) 664 | this.element.removeClassName(this.options.hoverClassName); 665 | if (this._saving) return; 666 | this.triggerCallback('onLeaveHover'); 667 | }, 668 | loadExternalText: function() { 669 | this._form.addClassName(this.options.loadingClassName); 670 | this._controls.editor.disabled = true; 671 | var options = Object.extend({ method: 'get' }, this.options.ajaxOptions); 672 | Object.extend(options, { 673 | parameters: 'editorId=' + encodeURIComponent(this.element.id), 674 | onComplete: Prototype.emptyFunction, 675 | onSuccess: function(transport) { 676 | this._form.removeClassName(this.options.loadingClassName); 677 | var text = transport.responseText; 678 | if (this.options.stripLoadedTextTags) 679 | text = text.stripTags(); 680 | this._controls.editor.value = text; 681 | this._controls.editor.disabled = false; 682 | this.postProcessEditField(); 683 | }.bind(this), 684 | onFailure: this._boundFailureHandler 685 | }); 686 | new Ajax.Request(this.options.loadTextURL, options); 687 | }, 688 | postProcessEditField: function() { 689 | var fpc = this.options.fieldPostCreation; 690 | if (fpc) 691 | $(this._controls.editor)['focus' == fpc ? 'focus' : 'activate'](); 692 | }, 693 | prepareOptions: function() { 694 | this.options = Object.clone(Ajax.InPlaceEditor.DefaultOptions); 695 | Object.extend(this.options, Ajax.InPlaceEditor.DefaultCallbacks); 696 | [this._extraDefaultOptions].flatten().compact().each(function(defs) { 697 | Object.extend(this.options, defs); 698 | }.bind(this)); 699 | }, 700 | prepareSubmission: function() { 701 | this._saving = true; 702 | this.removeForm(); 703 | this.leaveHover(); 704 | this.showSaving(); 705 | }, 706 | registerListeners: function() { 707 | this._listeners = { }; 708 | var listener; 709 | $H(Ajax.InPlaceEditor.Listeners).each(function(pair) { 710 | listener = this[pair.value].bind(this); 711 | this._listeners[pair.key] = listener; 712 | if (!this.options.externalControlOnly) 713 | this.element.observe(pair.key, listener); 714 | if (this.options.externalControl) 715 | this.options.externalControl.observe(pair.key, listener); 716 | }.bind(this)); 717 | }, 718 | removeForm: function() { 719 | if (!this._form) return; 720 | this._form.remove(); 721 | this._form = null; 722 | this._controls = { }; 723 | }, 724 | showSaving: function() { 725 | this._oldInnerHTML = this.element.innerHTML; 726 | this.element.innerHTML = this.options.savingText; 727 | this.element.addClassName(this.options.savingClassName); 728 | this.element.style.backgroundColor = this._originalBackground; 729 | this.element.show(); 730 | }, 731 | triggerCallback: function(cbName, arg) { 732 | if ('function' == typeof this.options[cbName]) { 733 | this.options[cbName](this, arg); 734 | } 735 | }, 736 | unregisterListeners: function() { 737 | $H(this._listeners).each(function(pair) { 738 | if (!this.options.externalControlOnly) 739 | this.element.stopObserving(pair.key, pair.value); 740 | if (this.options.externalControl) 741 | this.options.externalControl.stopObserving(pair.key, pair.value); 742 | }.bind(this)); 743 | }, 744 | wrapUp: function(transport) { 745 | this.leaveEditMode(); 746 | // Can't use triggerCallback due to backward compatibility: requires 747 | // binding + direct element 748 | this._boundComplete(transport, this.element); 749 | } 750 | }); 751 | 752 | Object.extend(Ajax.InPlaceEditor.prototype, { 753 | dispose: Ajax.InPlaceEditor.prototype.destroy 754 | }); 755 | 756 | Ajax.InPlaceCollectionEditor = Class.create(Ajax.InPlaceEditor, { 757 | initialize: function($super, element, url, options) { 758 | this._extraDefaultOptions = Ajax.InPlaceCollectionEditor.DefaultOptions; 759 | $super(element, url, options); 760 | }, 761 | 762 | createEditField: function() { 763 | var list = document.createElement('select'); 764 | list.name = this.options.paramName; 765 | list.size = 1; 766 | this._controls.editor = list; 767 | this._collection = this.options.collection || []; 768 | if (this.options.loadCollectionURL) 769 | this.loadCollection(); 770 | else 771 | this.checkForExternalText(); 772 | this._form.appendChild(this._controls.editor); 773 | }, 774 | 775 | loadCollection: function() { 776 | this._form.addClassName(this.options.loadingClassName); 777 | this.showLoadingText(this.options.loadingCollectionText); 778 | var options = Object.extend({ method: 'get' }, this.options.ajaxOptions); 779 | Object.extend(options, { 780 | parameters: 'editorId=' + encodeURIComponent(this.element.id), 781 | onComplete: Prototype.emptyFunction, 782 | onSuccess: function(transport) { 783 | var js = transport.responseText.strip(); 784 | if (!/^\[.*\]$/.test(js)) // TODO: improve sanity check 785 | throw('Server returned an invalid collection representation.'); 786 | this._collection = eval(js); 787 | this.checkForExternalText(); 788 | }.bind(this), 789 | onFailure: this.onFailure 790 | }); 791 | new Ajax.Request(this.options.loadCollectionURL, options); 792 | }, 793 | 794 | showLoadingText: function(text) { 795 | this._controls.editor.disabled = true; 796 | var tempOption = this._controls.editor.firstChild; 797 | if (!tempOption) { 798 | tempOption = document.createElement('option'); 799 | tempOption.value = ''; 800 | this._controls.editor.appendChild(tempOption); 801 | tempOption.selected = true; 802 | } 803 | tempOption.update((text || '').stripScripts().stripTags()); 804 | }, 805 | 806 | checkForExternalText: function() { 807 | this._text = this.getText(); 808 | if (this.options.loadTextURL) 809 | this.loadExternalText(); 810 | else 811 | this.buildOptionList(); 812 | }, 813 | 814 | loadExternalText: function() { 815 | this.showLoadingText(this.options.loadingText); 816 | var options = Object.extend({ method: 'get' }, this.options.ajaxOptions); 817 | Object.extend(options, { 818 | parameters: 'editorId=' + encodeURIComponent(this.element.id), 819 | onComplete: Prototype.emptyFunction, 820 | onSuccess: function(transport) { 821 | this._text = transport.responseText.strip(); 822 | this.buildOptionList(); 823 | }.bind(this), 824 | onFailure: this.onFailure 825 | }); 826 | new Ajax.Request(this.options.loadTextURL, options); 827 | }, 828 | 829 | buildOptionList: function() { 830 | this._form.removeClassName(this.options.loadingClassName); 831 | this._collection = this._collection.map(function(entry) { 832 | return 2 === entry.length ? entry : [entry, entry].flatten(); 833 | }); 834 | var marker = ('value' in this.options) ? this.options.value : this._text; 835 | var textFound = this._collection.any(function(entry) { 836 | return entry[0] == marker; 837 | }.bind(this)); 838 | this._controls.editor.update(''); 839 | var option; 840 | this._collection.each(function(entry, index) { 841 | option = document.createElement('option'); 842 | option.value = entry[0]; 843 | option.selected = textFound ? entry[0] == marker : 0 == index; 844 | option.appendChild(document.createTextNode(entry[1])); 845 | this._controls.editor.appendChild(option); 846 | }.bind(this)); 847 | this._controls.editor.disabled = false; 848 | Field.scrollFreeActivate(this._controls.editor); 849 | } 850 | }); 851 | 852 | //**** DEPRECATION LAYER FOR InPlace[Collection]Editor! **** 853 | //**** This only exists for a while, in order to let **** 854 | //**** users adapt to the new API. Read up on the new **** 855 | //**** API and convert your code to it ASAP! **** 856 | 857 | Ajax.InPlaceEditor.prototype.initialize.dealWithDeprecatedOptions = function(options) { 858 | if (!options) return; 859 | function fallback(name, expr) { 860 | if (name in options || expr === undefined) return; 861 | options[name] = expr; 862 | }; 863 | fallback('cancelControl', (options.cancelLink ? 'link' : (options.cancelButton ? 'button' : 864 | options.cancelLink == options.cancelButton == false ? false : undefined))); 865 | fallback('okControl', (options.okLink ? 'link' : (options.okButton ? 'button' : 866 | options.okLink == options.okButton == false ? false : undefined))); 867 | fallback('highlightColor', options.highlightcolor); 868 | fallback('highlightEndColor', options.highlightendcolor); 869 | }; 870 | 871 | Object.extend(Ajax.InPlaceEditor, { 872 | DefaultOptions: { 873 | ajaxOptions: { }, 874 | autoRows: 3, // Use when multi-line w/ rows == 1 875 | cancelControl: 'link', // 'link'|'button'|false 876 | cancelText: 'cancel', 877 | clickToEditText: 'Click to edit', 878 | externalControl: null, // id|elt 879 | externalControlOnly: false, 880 | fieldPostCreation: 'activate', // 'activate'|'focus'|false 881 | formClassName: 'inplaceeditor-form', 882 | formId: null, // id|elt 883 | highlightColor: '#ffff99', 884 | highlightEndColor: '#ffffff', 885 | hoverClassName: '', 886 | htmlResponse: true, 887 | loadingClassName: 'inplaceeditor-loading', 888 | loadingText: 'Loading...', 889 | okControl: 'button', // 'link'|'button'|false 890 | okText: 'ok', 891 | paramName: 'value', 892 | rows: 1, // If 1 and multi-line, uses autoRows 893 | savingClassName: 'inplaceeditor-saving', 894 | savingText: 'Saving...', 895 | size: 0, 896 | stripLoadedTextTags: false, 897 | submitOnBlur: false, 898 | textAfterControls: '', 899 | textBeforeControls: '', 900 | textBetweenControls: '' 901 | }, 902 | DefaultCallbacks: { 903 | callback: function(form) { 904 | return Form.serialize(form); 905 | }, 906 | onComplete: function(transport, element) { 907 | // For backward compatibility, this one is bound to the IPE, and passes 908 | // the element directly. It was too often customized, so we don't break it. 909 | new Effect.Highlight(element, { 910 | startcolor: this.options.highlightColor, keepBackgroundImage: true }); 911 | }, 912 | onEnterEditMode: null, 913 | onEnterHover: function(ipe) { 914 | ipe.element.style.backgroundColor = ipe.options.highlightColor; 915 | if (ipe._effect) 916 | ipe._effect.cancel(); 917 | }, 918 | onFailure: function(transport, ipe) { 919 | alert('Error communication with the server: ' + transport.responseText.stripTags()); 920 | }, 921 | onFormCustomization: null, // Takes the IPE and its generated form, after editor, before controls. 922 | onLeaveEditMode: null, 923 | onLeaveHover: function(ipe) { 924 | ipe._effect = new Effect.Highlight(ipe.element, { 925 | startcolor: ipe.options.highlightColor, endcolor: ipe.options.highlightEndColor, 926 | restorecolor: ipe._originalBackground, keepBackgroundImage: true 927 | }); 928 | } 929 | }, 930 | Listeners: { 931 | click: 'enterEditMode', 932 | keydown: 'checkForEscapeOrReturn', 933 | mouseover: 'enterHover', 934 | mouseout: 'leaveHover' 935 | } 936 | }); 937 | 938 | Ajax.InPlaceCollectionEditor.DefaultOptions = { 939 | loadingCollectionText: 'Loading options...' 940 | }; 941 | 942 | // Delayed observer, like Form.Element.Observer, 943 | // but waits for delay after last key input 944 | // Ideal for live-search fields 945 | 946 | Form.Element.DelayedObserver = Class.create({ 947 | initialize: function(element, delay, callback) { 948 | this.delay = delay || 0.5; 949 | this.element = $(element); 950 | this.callback = callback; 951 | this.timer = null; 952 | this.lastValue = $F(this.element); 953 | Event.observe(this.element,'keyup',this.delayedListener.bindAsEventListener(this)); 954 | }, 955 | delayedListener: function(event) { 956 | if(this.lastValue == $F(this.element)) return; 957 | if(this.timer) clearTimeout(this.timer); 958 | this.timer = setTimeout(this.onTimerEvent.bind(this), this.delay * 1000); 959 | this.lastValue = $F(this.element); 960 | }, 961 | onTimerEvent: function() { 962 | this.timer = null; 963 | this.callback(this.element, $F(this.element)); 964 | } 965 | }); --------------------------------------------------------------------------------