just_open_id 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1,56 @@
1
+ == Overview
2
+
3
+ One of the things I was most looking forward to in rails 3 was the plugin / engine architecture. Recently, I sat down to figure out how to create my first engine and package it up as a gem and it took me awhile of time just to get the structure of the engine setup. It's missing a lot of the "rails" you get in a normal rails app.
4
+
5
+ In it's simplest form engines are quite easy, however for a full-featured engine (such as creating a forum) there are a lot of extras that you're going to want. These are the things I spent time figuring out that I've packaged up into an easy starting point:
6
+
7
+ * Namespacing models & controllers so they don't collide with those in the main app
8
+ * Creating a global layout within your engine that gets nested within your application layout
9
+ * Generating migrations from your engine
10
+ * Creating an "acts_as_[plugin]" declaration for use inside your main app's models
11
+ * Easy plugin configuration file editable from main app directory
12
+ * Rake tasks within engine
13
+ * Writing tests for models complete with fixtures
14
+ * Serving static assets within engine
15
+ * Packaging and distributing as a gem
16
+ * Code is here - I've created an engine stub that has all the things setup for you already. I want to see a lot more rails 3 engines get created, I hope this helps! I'd love to hear feedback from you if you try it out.
17
+
18
+ Here’s how you get ready to create your first gem by using this starting point:
19
+
20
+ * git clone http://github.com/krschacht/rails_3_engine_demo.git
21
+ * cd rails_3_engine_demo
22
+ * [edit test/database.yml]
23
+ * rake test (this will initialize your test database and run the basic test suite)
24
+
25
+ Now, create a plain rails app and set it up to use your engine. FYI: even though the engine's directory is 'rails_3_engine_demo', internally the engine is named 'just_open_id'
26
+
27
+ * cd .. [ this is to take you outside the 'rails_3_engine_demo' directory that was created above' ]
28
+ * rails new demo_app_to_use_gem -d mysql
29
+ * cd demo_app_to_use_gem
30
+ * [edit config/database.yml]
31
+ * [edit Gemfile, add line: ] gem ‘just_open_id’, :path => "../rails_3_engine_demo"
32
+ * rails generate just_open_id
33
+ * [examine config/initializers/just_open_id.rb to see basic config parameters]
34
+ * rake db:create
35
+ * rake db:migrate (one of the migrations that you'll see run came from the engine)
36
+
37
+ You have now setup a empty rails app that utilizes your engine. To test out the functionality, startup the demo app’s webserver:
38
+
39
+ * rails server
40
+ * Then visit: http://localhost:3000/just_open_id (this is a controller included within the engine)
41
+ * Watch the server logs as you're viewing this page and you'll see some output which is coming from an application before_filter which is executing from inside the engine (in lib/application_controller.rb)
42
+ * rake just_open_id:report (this is a a rake task that is included inside the engine)
43
+
44
+ Lastly, let's package up your engine as a real gem. You’ll need Jeweler installed for this:
45
+
46
+ * cd rails_3_engine_demo
47
+ * sudo gem install jeweler
48
+ * rake gemspec
49
+ * rake build
50
+ * rake install (you have now installed your engine as a gem locally)
51
+ * [ At this point if you wanted your demo app to use your installed gem, edit the Gemfile in your demo app and remove the 'path' option from your gem line. It should look like this: ] gem ‘just_open_id’
52
+ * rake gemcutter:release (this pushes your gem up to Gemcutter, a public gem repository)
53
+
54
+ Now you’re ready to start customizing this engine for your own purposes. Do a global find in your engine directory and replace every instance of "just_open_id" and "JustOpenId" with your engine name (be sure to preserve capitalization). Likewise, rename every directory and file that’s named "just_open_id" with your engine name. I should really automate this but I haven’t figured out how to create a rake task that I can run from within the engine directory itself.
55
+
56
+ P.S. Special thanks to Chrispy for helping figure out the application_controller includes!
@@ -0,0 +1,22 @@
1
+ module JustOpenId
2
+ class SessionsController < ApplicationController
3
+
4
+ unloadable
5
+
6
+ layout 'just_open_id' # this allows you to have a gem-wide layout
7
+
8
+ def create
9
+ auth = request.env["omniauth.auth"]
10
+ user = User.find_by_provider_and_uid(auth["provider"], auth["uid"]) || User.create_with_omniauth(auth)
11
+ session[:user_id] = user.id
12
+ redirect_to root_url, :notice => "Signed in!"
13
+ end
14
+
15
+ def destroy
16
+ session[:user_id] = nil
17
+ redirect_to root_url, :notice => "Signed out!"
18
+ end
19
+
20
+ end
21
+ end
22
+
@@ -0,0 +1,14 @@
1
+ module JustOpenId
2
+ class User < ActiveRecord::Base
3
+ set_table_name "just_open_id_users"
4
+
5
+ def self.create_with_omniauth(auth)
6
+ create! do |user|
7
+ user.provider = auth["provider"]
8
+ user.uid = auth["uid"]
9
+ user.name = auth["user_info"]["name"]
10
+ end
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,10 @@
1
+ # require 'omniauth/openid'
2
+ # require 'openid/store/filesystem'
3
+ #
4
+ # use Rack::Session::Cookie
5
+ # use OmniAuth::Strategies::OpenID, OpenID::Store::Filesystem.new('/tmp')
6
+
7
+ require 'openid/store/filesystem'
8
+ Rails.application.config.middleware.use OmniAuth::Builder do
9
+ provider :open_id, OpenID::Store::Filesystem.new('/tmp'), :identifier => JustOpenId::Engine.config.openid_url
10
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,9 @@
1
+ Rails.application.routes.draw do |map|
2
+
3
+ # to login you go to:
4
+ # /auth/open_id
5
+ # it is provided by omniauth rack middleware declared in initializers/omniauth.rb
6
+ match "/auth/:provider/callback" => "JustOpenId::Sessions#create"
7
+ match "/signout" => "JustOpenId::Sessions#destroy", :as => :signout
8
+
9
+ end
data/lib/engine.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'just_open_id'
2
+ require 'rails'
3
+ require 'action_controller'
4
+
5
+ module JustOpenId
6
+ class Engine < Rails::Engine
7
+
8
+ config.openid_url = 'www.myopenid.com/'
9
+
10
+ end
11
+ end
@@ -0,0 +1,28 @@
1
+ class ActionController::Base
2
+ def self.require_user(options = {})
3
+ raise Exception, "require_user cannot be called on ActionController::Base. Only it's subclasses" if self == ActionController::Base
4
+ prepend_before_filter :require_user, options
5
+ end
6
+
7
+ helper_method :current_user
8
+
9
+ protected
10
+
11
+ # Filters
12
+ def require_user
13
+ current_user.present? || deny_access
14
+ end
15
+
16
+ def store_location
17
+ session[:return_to] = request.fullpath
18
+ end
19
+
20
+ def deny_access
21
+ store_location
22
+ redirect_to login_path
23
+ end
24
+
25
+ def current_user
26
+ @current_user ||= User.find(session[:user_id]) if session[:user_id]
27
+ end
28
+ end
@@ -0,0 +1,14 @@
1
+ module JustOpenId
2
+ require 'engine' if defined?(Rails) && Rails::VERSION::MAJOR == 3
3
+ require 'extensions/action_controller/base'
4
+
5
+ # This engine is intended to run as a gem.
6
+ # The lib below will be installed via gem dependencies.
7
+ begin
8
+ require "omniauth"
9
+ rescue LoadError => e
10
+ warn "Could not load 'omniauth'. Please ensure you have the omniauth gem installed and listed in your Gemfile."
11
+ raise
12
+ end
13
+ end
14
+
@@ -0,0 +1,65 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ class JustOpenIdGenerator < Rails::Generators::Base
5
+ include Rails::Generators::Migration
6
+
7
+ def self.source_root
8
+ File.join(File.dirname(__FILE__), 'templates')
9
+ end
10
+
11
+ def self.next_migration_number(dirname) #:nodoc:
12
+ if ActiveRecord::Base.timestamped_migrations
13
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
14
+ else
15
+ "%.3d" % (current_migration_number(dirname) + 1)
16
+ end
17
+ end
18
+
19
+
20
+ # Every method that is declared below will be automatically executed when the generator is run
21
+
22
+ def create_migration_file
23
+ f = File.open File.join(File.dirname(__FILE__), 'templates', 'schema.rb')
24
+ schema = f.read; f.close
25
+
26
+ schema.gsub!(/ActiveRecord::Schema.*\n/, '')
27
+ schema.gsub!(/^end\n*$/, '')
28
+
29
+ f = File.open File.join(File.dirname(__FILE__), 'templates', 'migration.rb')
30
+ migration = f.read; f.close
31
+ migration.gsub!(/SCHEMA_AUTO_INSERTED_HERE/, schema)
32
+
33
+ tmp = File.open "tmp/~migration_ready.rb", "w"
34
+ tmp.write migration
35
+ tmp.close
36
+
37
+ migration_template '../../../tmp/~migration_ready.rb',
38
+ 'db/migrate/create_just_open_id_tables.rb'
39
+ remove_file 'tmp/~migration_ready.rb'
40
+ end
41
+
42
+ def copy_initializer_file
43
+ copy_file 'initializer.rb', 'config/initializers/just_open_id.rb'
44
+ end
45
+
46
+ def add_user_model
47
+ create_file 'app/models/user.rb' do
48
+ "class User < JustOpenId::User\nend"
49
+ end
50
+ end
51
+
52
+ def welcome
53
+ puts '1. Run your migrations for your namespaced User model.
54
+ 2. Ensure you have a root path.
55
+ 3. Add this to a template.
56
+
57
+ <% if current_user %>
58
+ Welcome <%= current_user.name %>!
59
+ <%= link_to "Sign Out", signout_path %>
60
+ <% else %>
61
+ <%= link_to "Sign in with Twitter", "/auth/twitter" %>
62
+ <% end %>'
63
+ end
64
+
65
+ end
@@ -0,0 +1,5 @@
1
+ module JustOpenId
2
+ class Engine < Rails::Engine
3
+ config.openid_url = 'www.myopenid.com/'
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ class CreateJustOpenIdTables < ActiveRecord::Migration
2
+ def self.up
3
+ SCHEMA_AUTO_INSERTED_HERE
4
+ end
5
+
6
+ def self.down
7
+ drop_table :just_open_id_users
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+
3
+ create_table :just_open_id_users, :force => true do |t|
4
+ t.string :provider
5
+ t.string :uid
6
+ t.string :name
7
+
8
+ t.timestamps
9
+ end
10
+
11
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: just_open_id
3
+ version: !ruby/object:Gem::Version
4
+ hash: 31
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 0
10
+ version: 0.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Jody Alkema
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-09 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ prerelease: false
23
+ type: :runtime
24
+ name: rails
25
+ version_requirements: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ hash: 3
31
+ segments:
32
+ - 0
33
+ version: "0"
34
+ requirement: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ prerelease: false
37
+ type: :runtime
38
+ name: omniauth
39
+ version_requirements: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 3
45
+ segments:
46
+ - 0
47
+ version: "0"
48
+ requirement: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ prerelease: false
51
+ type: :runtime
52
+ name: openid
53
+ version_requirements: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ requirement: *id003
63
+ - !ruby/object:Gem::Dependency
64
+ prerelease: false
65
+ type: :runtime
66
+ name: omniauth
67
+ version_requirements: &id004 !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ requirement: *id004
77
+ description:
78
+ email: jody@alkema.ca
79
+ executables: []
80
+
81
+ extensions: []
82
+
83
+ extra_rdoc_files:
84
+ - README.rdoc
85
+ files:
86
+ - app/controllers/just_open_id/sessions_controller.rb
87
+ - app/models/just_open_id/user.rb
88
+ - config/initializers/omniauth.rb
89
+ - config/routes.rb
90
+ - lib/engine.rb
91
+ - lib/extensions/action_controller/base.rb
92
+ - lib/just_open_id.rb
93
+ - lib/rails/generators/just_open_id/just_open_id_generator.rb
94
+ - lib/rails/generators/just_open_id/templates/initializer.rb
95
+ - lib/rails/generators/just_open_id/templates/migration.rb
96
+ - lib/rails/generators/just_open_id/templates/schema.rb
97
+ - README.rdoc
98
+ has_rdoc: true
99
+ homepage:
100
+ licenses: []
101
+
102
+ post_install_message:
103
+ rdoc_options: []
104
+
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ hash: 3
113
+ segments:
114
+ - 0
115
+ version: "0"
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ none: false
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ hash: 3
122
+ segments:
123
+ - 0
124
+ version: "0"
125
+ requirements: []
126
+
127
+ rubyforge_project:
128
+ rubygems_version: 1.3.7
129
+ signing_key:
130
+ specification_version: 3
131
+ summary: A Rails 3 engine just OpenID logins using OmniAuth
132
+ test_files: []
133
+