vanities 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Manifest ADDED
@@ -0,0 +1,9 @@
1
+ README.rdoc
2
+ Rakefile
3
+ lib/generators/vanities/USAGE
4
+ lib/generators/vanities/templates/create_vanities.rb
5
+ lib/generators/vanities/templates/vanities_controller.rb
6
+ lib/generators/vanities/templates/vanity.rb
7
+ lib/generators/vanities/vanities_generator.rb
8
+ lib/vanities.rb
9
+ Manifest
data/README.rdoc ADDED
@@ -0,0 +1,92 @@
1
+ = Vanities
2
+
3
+ Vanities is a gem for use with a Rails 3 project that will transform
4
+ URLs like this:
5
+
6
+ http://example.com/users/1
7
+
8
+ Into something cool, like this:
9
+
10
+ http://example.com/foo
11
+
12
+ == The Vanity URL Explained
13
+
14
+ Vanities implements the concept of the "vanity URL". Similar to a vanity
15
+ license plate, it's a unique name or ID chosen by the user that is specific to them.
16
+ However, this doesn't necessarily have to be for a user - in theory, any model in your
17
+ Rails 3 application can use a vanity URL.
18
+
19
+ This gem works by redirecting requests for a vanity name - say "foo" - to the object
20
+ that owns it. So for example, visiting example.com/foo would perform a redirect to
21
+ example.com/users/1 (assuming a user object with an ID of 1 owned the "foo" vanity).
22
+
23
+ == Common Use Cases
24
+
25
+ Some common use cases for something like vanities would be:
26
+ * user profiles (www.example.com/users/22829 => www.example.com/leeroy)
27
+ * specific products in an e-commerce system (www.exampleshop.com/products/19920 => www.exampleshop.com/buythis)
28
+
29
+ == How To Install
30
+
31
+ Vanities is pretty simple to install. First, a few requirements and caveats:
32
+
33
+ === Requirements
34
+
35
+ <b>REST-based models</b> - every model with which you want to use vanities MUST be made
36
+ a RESTful resource in your routes file (config/routes.rb). This is absolutely required.
37
+
38
+ <b>Rails 3</b> - vanities is built on Rails 3. It might work with older versions of Rails, but
39
+ only if you go back and reverse engineer some of the routing stuff, and possibly other things
40
+ if using versions of Rails prior to the 2.3.x series.
41
+
42
+ <b>ActiveRecord</b> - vanities needs a table in the database to keep track of the individual
43
+ vanity names. Datamapper and/or -otherorm- isn't/aren't supported out of the box, but the
44
+ setup itself is so simple that you could probably reverse-engineer this to use another ORM
45
+ if you wanted to.
46
+
47
+ <b>Letters-Only for Vanity URLs</b> - Vanity URLs can contain letters only - no numbers. If you
48
+ don't like this functionality, you can manually remove the regular expression call in the
49
+ route for the vanities controller.
50
+
51
+ === Installation
52
+
53
+ In your application's Gemfile, add the following line:
54
+
55
+ gem 'vanities'
56
+
57
+ Then in your console/terminal, do:
58
+
59
+ bundle install
60
+
61
+ Next, while still in your terminal/console:
62
+
63
+ rails g vanities
64
+
65
+ You'll see a list of instructions that it'll print out. Next you'll want to
66
+ open up your model that you want to have a vanity URL (say a User model) and add this
67
+ line to it to substitute the polymorphic association that does all the magic:
68
+
69
+ has_vanity
70
+
71
+ Finally, do:
72
+
73
+ rake db:migrate
74
+
75
+ And you're all set!
76
+
77
+ == Using vanities
78
+
79
+ Every model that has a call to 'has_vanity' will have a vanity object associated with it.
80
+ Each instance of your model will have one - and only one - vanity. Vanities CANNOT be repeated
81
+ across different models. For example, let's say you have a user with a vanity called "leeroy", and
82
+ a product; you cannot call the product's vanity "leeroy" as well, as that's already taken by
83
+ the aforementioned user.
84
+
85
+ To set up a vanity for a model, all you need to do is create a vanity. Say you're in the Rails
86
+ console. This would be as simple as:
87
+
88
+ u = User.first
89
+ u.vanity = Vanity.new(:name => "leeroy")
90
+
91
+ Finally, you can go to http://localhost:3000/leeroy to be automatically redirected
92
+ to the user's 'show' action.
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('vanities', '0.1.1') do |x|
6
+ x.description = "Give each of your models a simple 'vanity' URL.
7
+ Makes example.com/users/1 into something like example.com/foobar"
8
+ x.url = "https://github.com/jaustinhughey/vanities"
9
+ x.author = "J. Austin Hughey"
10
+ x.email = "jaustinhughey@gmail.com"
11
+ x.ignore_pattern = ["tmp/*", "script/*"]
12
+ x.development_dependencies = []
13
+ end
14
+
15
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
File without changes
@@ -0,0 +1,20 @@
1
+ class CreateVanities < ActiveRecord::Migration
2
+ def self.up
3
+ # Create the vanities table
4
+ create_table :vanities do |t|
5
+ t.string :name
6
+ t.integer :vain_id
7
+ t.string :vain_type
8
+ end
9
+
10
+ # Add some indexes
11
+ add_index :vanities, :name, :unique => true
12
+ add_index :vanities, :vain_id
13
+ add_index :vanities, :vain_type
14
+
15
+ end
16
+
17
+ def self.down
18
+ drop_table :vanities
19
+ end
20
+ end
@@ -0,0 +1,8 @@
1
+ class VanitiesController < ApplicationController
2
+ def show
3
+ v = Vanity.find_by_name params[:vname]
4
+ redirect_to v.vain
5
+ # Note that in this case, any model using the vanities system
6
+ # MUST be set up via REST.
7
+ end
8
+ end
@@ -0,0 +1,16 @@
1
+ class Vanity < ActiveRecord::Base
2
+ # The "Vanity" class has a polymorphic association called "vain".
3
+ # In any model that has a vanity entry, you can reference this association like so:
4
+ #
5
+ # has_one :vanity, :as => :vain
6
+ #
7
+ # For your convenience, there's a simpler method called 'has_vanity'. Just call it inside
8
+ # any model that should have a vanity:
9
+ #
10
+ # has_vanity
11
+ #
12
+ # And that's it! See the vanities README file for more information.
13
+
14
+ belongs_to :vain, :polymorphic => true
15
+ validates :name, :presence => true, :uniqueness => true
16
+ end
@@ -0,0 +1,51 @@
1
+ class VanitiesGenerator < Rails::Generators::Base
2
+ source_root File.expand_path('../templates', __FILE__)
3
+
4
+ # Copy over the database migration template
5
+ def generate_vanities_migration
6
+ copy_file "create_vanities.rb", "db/migrate/#{timestamp}_create_vanities.rb"
7
+ end
8
+
9
+ # Copy over the vanity model
10
+ def generate_vanity_model
11
+ copy_file "vanity.rb", "app/models/vanity.rb"
12
+ end
13
+
14
+ # This method actually copies over the vanities controller code
15
+ # into app/controllers/vanities_controller.rb
16
+ def generate_vanities_controller
17
+ copy_file "vanities_controller.rb", "app/controllers/vanities_controller.rb"
18
+ end
19
+
20
+ # Add the route to wire up the vanities controller
21
+ # Indentation looks a little funky here because I'd strongly prefer
22
+ # to keep the indentation in the target routes.rb file as it is
23
+ def inject_new_route
24
+ route "
25
+ controller :vanities do
26
+ match ':vname' => :show, :via => :get, :constraints => {:vname => /[A-Za-z]+/}
27
+ end"
28
+ end
29
+
30
+ # This method prints some basic setup instructions for the user.
31
+ def talk_to_user
32
+ puts "****************************************"
33
+ puts " Thanks for installing vanities!"
34
+ puts "****************************************"
35
+ puts "Now that you've run the generator, all you have left to do are the following tasks:"
36
+ puts "1) rake db:migrate"
37
+ puts "2) In any model you want to have a vanity URL, add the line:"
38
+ puts " has_vanity"
39
+ puts " Inside the model, just like you would an association (this is in fact what it does)"
40
+ puts "3) Make sure that the model in question is set up as a RESTful resource in your"
41
+ puts " project's routes file. If it isn't, this won't work."
42
+ puts ""
43
+ puts "Check out this project's GitHub page or README.rdoc for more information."
44
+ puts ""
45
+ end
46
+
47
+ private
48
+ def timestamp
49
+ Time.now.strftime("%Y%m%d%H%M%S")
50
+ end
51
+ end
data/lib/vanities.rb ADDED
@@ -0,0 +1,17 @@
1
+ module Vanities
2
+ def self.included(base)
3
+ base.extend ClassMethods
4
+ end
5
+
6
+ module ClassMethods
7
+ def has_vanity
8
+ has_one :vanity, :as => :vain
9
+ end
10
+ end
11
+
12
+
13
+ end
14
+
15
+ class ActiveRecord::Base
16
+ include Vanities
17
+ end
data/vanities.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{vanities}
5
+ s.version = "0.1.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["J. Austin Hughey"]
9
+ s.date = %q{2010-11-07}
10
+ s.description = %q{Give each of your models a simple 'vanity' URL.
11
+ Makes example.com/users/1 into something like example.com/foobar}
12
+ s.email = %q{jaustinhughey@gmail.com}
13
+ s.extra_rdoc_files = ["README.rdoc", "lib/generators/vanities/USAGE", "lib/generators/vanities/templates/create_vanities.rb", "lib/generators/vanities/templates/vanities_controller.rb", "lib/generators/vanities/templates/vanity.rb", "lib/generators/vanities/vanities_generator.rb", "lib/vanities.rb"]
14
+ s.files = ["README.rdoc", "Rakefile", "lib/generators/vanities/USAGE", "lib/generators/vanities/templates/create_vanities.rb", "lib/generators/vanities/templates/vanities_controller.rb", "lib/generators/vanities/templates/vanity.rb", "lib/generators/vanities/vanities_generator.rb", "lib/vanities.rb", "Manifest", "vanities.gemspec"]
15
+ s.homepage = %q{https://github.com/jaustinhughey/vanities}
16
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Vanities", "--main", "README.rdoc"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{vanities}
19
+ s.rubygems_version = %q{1.3.7}
20
+ s.summary = %q{Give each of your models a simple 'vanity' URL. Makes example.com/users/1 into something like example.com/foobar}
21
+
22
+ if s.respond_to? :specification_version then
23
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
+ s.specification_version = 3
25
+
26
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
27
+ else
28
+ end
29
+ else
30
+ end
31
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vanities
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 1
10
+ version: 0.1.1
11
+ platform: ruby
12
+ authors:
13
+ - J. Austin Hughey
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-11-07 01:00:00 -06:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: |-
23
+ Give each of your models a simple 'vanity' URL.
24
+ Makes example.com/users/1 into something like example.com/foobar
25
+ email: jaustinhughey@gmail.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - README.rdoc
32
+ - lib/generators/vanities/USAGE
33
+ - lib/generators/vanities/templates/create_vanities.rb
34
+ - lib/generators/vanities/templates/vanities_controller.rb
35
+ - lib/generators/vanities/templates/vanity.rb
36
+ - lib/generators/vanities/vanities_generator.rb
37
+ - lib/vanities.rb
38
+ files:
39
+ - README.rdoc
40
+ - Rakefile
41
+ - lib/generators/vanities/USAGE
42
+ - lib/generators/vanities/templates/create_vanities.rb
43
+ - lib/generators/vanities/templates/vanities_controller.rb
44
+ - lib/generators/vanities/templates/vanity.rb
45
+ - lib/generators/vanities/vanities_generator.rb
46
+ - lib/vanities.rb
47
+ - Manifest
48
+ - vanities.gemspec
49
+ has_rdoc: true
50
+ homepage: https://github.com/jaustinhughey/vanities
51
+ licenses: []
52
+
53
+ post_install_message:
54
+ rdoc_options:
55
+ - --line-numbers
56
+ - --inline-source
57
+ - --title
58
+ - Vanities
59
+ - --main
60
+ - README.rdoc
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ hash: 3
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 11
78
+ segments:
79
+ - 1
80
+ - 2
81
+ version: "1.2"
82
+ requirements: []
83
+
84
+ rubyforge_project: vanities
85
+ rubygems_version: 1.3.7
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: Give each of your models a simple 'vanity' URL. Makes example.com/users/1 into something like example.com/foobar
89
+ test_files: []
90
+