vanities 0.1.2 → 0.1.3

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 CHANGED
@@ -6,4 +6,7 @@ lib/generators/vanities/templates/vanities_controller.rb
6
6
  lib/generators/vanities/templates/vanity.rb
7
7
  lib/generators/vanities/vanities_generator.rb
8
8
  lib/vanities.rb
9
+ test/unit/vanities_test.rb
10
+ test/unit/vanities_test.sqlite3
11
+ vanities_test.sqlite3
9
12
  Manifest
@@ -1,6 +1,6 @@
1
1
  = Vanities
2
2
 
3
- Vanities is a gem for use with a Rails 3 project that will transform
3
+ Vanities is a gem for use with a Rails 3/3.1 project that will transform
4
4
  URLs like this:
5
5
 
6
6
  http://example.com/users/1
@@ -23,7 +23,7 @@ example.com/users/1 (assuming a user object with an ID of 1 owned the "foo" vani
23
23
  == Common Use Cases
24
24
 
25
25
  Some common use cases for something like vanities would be:
26
- * user profiles (www.example.com/users/22829 => www.example.com/leeroy)
26
+ * user profiles (www.example.com/users/22829 => www.example.com/bob)
27
27
  * specific products in an e-commerce system (www.exampleshop.com/products/19920 => www.exampleshop.com/buythis)
28
28
 
29
29
  == How To Install
@@ -35,19 +35,15 @@ Vanities is pretty simple to install. First, a few requirements and caveats:
35
35
  <b>REST-based models</b> - every model with which you want to use vanities MUST be made
36
36
  a RESTful resource in your routes file (config/routes.rb). This is absolutely required.
37
37
 
38
- <b>Rails 3</b> - vanities is built on Rails 3. It might work with older versions of Rails, but
38
+ <b>Rails 3</b> - vanities is built on Rails 3 or 3.1. It might work with older versions of Rails, but
39
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.
40
+ if using versions of Rails prior to the 2.3.x series. No other versions are supported.
41
41
 
42
42
  <b>ActiveRecord</b> - vanities needs a table in the database to keep track of the individual
43
43
  vanity names. Datamapper and/or -otherorm- isn't/aren't supported out of the box, but the
44
44
  setup itself is so simple that you could probably reverse-engineer this to use another ORM
45
45
  if you wanted to.
46
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
47
  === Installation
52
48
 
53
49
  In your application's Gemfile, add the following line:
@@ -89,21 +85,49 @@ last route in your system.
89
85
 
90
86
  Every model that has a call to 'has_vanity' will have a vanity object associated with it.
91
87
  Each instance of your model will have one - and only one - vanity. Vanities CANNOT be repeated
92
- across different models. For example, let's say you have a user with a vanity called "leeroy", and
93
- a product; you cannot call the product's vanity "leeroy" as well, as that's already taken by
88
+ across different models. For example, let's say you have a user with a vanity called "bob", and
89
+ a product; you cannot call the product's vanity "bob" as well, as that's already taken by
94
90
  the aforementioned user.
95
91
 
96
92
  To set up a vanity for a model, all you need to do is create a vanity. Say you're in the Rails
97
93
  console. This would be as simple as:
98
94
 
99
95
  u = User.first
100
- u.vanity = Vanity.new(:name => "leeroy")
96
+ u.vanity = Vanity.new(:name => "bob")
101
97
 
102
- Finally, you can go to http://localhost:3000/leeroy to be automatically redirected
98
+ Finally, you can go to http://localhost:3000/bob to be automatically redirected
103
99
  to the user's 'show' action.
104
100
 
101
+ == About the Tests
102
+
103
+ Vanities is mostly a convenience gem, nothing more. It makes use of code already tested by Rails, and
104
+ as such, is pretty light on its own tests. As of version 0.1.3, Vanities includes a test to ensure that
105
+ the 'has_vanity' method does work correctly. <b>I fully welcome any well written tests for Vanities.</b>
106
+ Just fork, do your work, and submit a pull request.
107
+
108
+ === Running the tests
109
+
110
+ Download this gem then cd to the right directory:
111
+
112
+ cd /path/to/your/gem/install/vanities
113
+
114
+ Then just use ruby to run the tests:
115
+
116
+ ruby test/unit/vanities_test.rb
117
+
118
+ == What version of ActiveRecord can I use this with?
119
+
120
+ Vanities should work with ActiveRecord 3.0 and 3.1 just fine. It *probably* works with older
121
+ versions, too. In theory, any version fo ActiveRecord that provides polymorphic associations should work,
122
+ as long as the API stays the same. If you have doubts or questions, feel free to run the tests.
123
+
105
124
  == Changelog
106
125
 
107
- 20 March 2011: Slight refactor to make the vanities controller render a 404 if it can't
126
+ *0.1.3* 17 September 2011: Slight refactor based on suggestions from Marc Gayle (@marcgayle) to
127
+ allow for + and - characters in routes. Also added basic test to ensure 'has_vanity' is loaded into
128
+ ActiveRecord as an available method, and that it works to provide the polymorphic association
129
+ necessary to make everything work.
130
+
131
+ *0.1.2* 20 March 2011: Slight refactor to make the vanities controller render a 404 if it can't
108
132
  find a vanity object. Small documentation update about routes (make sure vanities is
109
133
  the LAST route in your routes.rb file).
data/Rakefile CHANGED
@@ -1,9 +1,10 @@
1
1
  require 'rubygems'
2
+ require 'psych'
2
3
  require 'rake'
3
4
  require 'echoe'
4
5
 
5
- Echoe.new('vanities', '0.1.2') do |x|
6
- x.description = "Give each of your models a simple 'vanity' URL.
6
+ Echoe.new('vanities', '0.1.3') do |x|
7
+ x.description = "Give each of your models a simple 'vanity' URL.
7
8
  Makes example.com/users/1 into something like example.com/foobar"
8
9
  x.url = "https://github.com/jaustinhughey/vanities"
9
10
  x.author = "J. Austin Hughey"
@@ -1,30 +1,31 @@
1
1
  class VanitiesGenerator < Rails::Generators::Base
2
2
  source_root File.expand_path('../templates', __FILE__)
3
-
3
+
4
4
  # Copy over the database migration template
5
5
  def generate_vanities_migration
6
6
  copy_file "create_vanities.rb", "db/migrate/#{timestamp}_create_vanities.rb"
7
7
  end
8
-
8
+
9
9
  # Copy over the vanity model
10
10
  def generate_vanity_model
11
11
  copy_file "vanity.rb", "app/models/vanity.rb"
12
12
  end
13
-
13
+
14
14
  # This method actually copies over the vanities controller code
15
15
  # into app/controllers/vanities_controller.rb
16
16
  def generate_vanities_controller
17
17
  copy_file "vanities_controller.rb", "app/controllers/vanities_controller.rb"
18
18
  end
19
-
19
+
20
20
  # Add the route to wire up the vanities controller
21
21
  # Indentation looks a little funky here because I'd strongly prefer
22
22
  # to keep the indentation in the target routes.rb file as it is
23
23
  def inject_new_route
24
- route "
25
- controller :vanities do
26
- match ':vname' => :show, :via => :get, :constraints => {:vname => /[A-Za-z]+/}
27
- end"
24
+ # It seems that the 'route' method has some...strange ways of having
25
+ # some one escape characters...
26
+ route 'controller :vanities do
27
+ match \':vname\' => :show, :via => :get, :constraints => { :vname => /[A-Za-z0-9\-\\\\+]+/ }
28
+ end'
28
29
  end
29
30
 
30
31
  # This method prints some basic setup instructions for the user.
@@ -48,4 +49,4 @@ private
48
49
  def timestamp
49
50
  Time.now.strftime("%Y%m%d%H%M%S")
50
51
  end
51
- end
52
+ end
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'active_record'
4
+ require 'sqlite3'
5
+ require File.expand_path("../../../lib/vanities", __FILE__)
6
+ require File.expand_path("../../../lib/generators/vanities/templates/vanity.rb", __FILE__)
7
+
8
+ # Clean up any existing sqlite3 db if it exists
9
+ old_db_file = "vanities_test.sqlite3"
10
+ if File.exists?(old_db_file)
11
+ File.delete(old_db_file)
12
+ end
13
+
14
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => "vanities_test.sqlite3")
15
+ ActiveRecord::Schema.define :version => 1 do
16
+ create_table :posts do |t|
17
+ t.string :title
18
+ end
19
+ end
20
+
21
+ ActiveRecord::Schema.define :version => 2 do
22
+ create_table :vanities do |t|
23
+ t.string :name
24
+ t.integer :vain_id
25
+ t.string :vain_type
26
+ end
27
+ end
28
+
29
+ class Post < ActiveRecord::Base
30
+ has_vanity
31
+ end
32
+
33
+ class VanitiesTest < Test::Unit::TestCase
34
+ def test_has_vanity_loaded_in_new_ar_object
35
+ # Attempt to create (and thereby save) a new post
36
+ assert (@post = Post.create(:title => "blah"))
37
+
38
+ # Right now the number of vanities that exist should be zero.
39
+ assert Vanity.count == 0
40
+
41
+ # See if that post responds to the "vanity" getter
42
+ assert @post.respond_to?(:vanity)
43
+
44
+ # And see if we can set it to a new vanity
45
+ assert (@post.vanity = Vanity.new(:name => "foo"))
46
+
47
+ # And now that a vanity has been created, we should have exactly 1 vanity.
48
+ assert Vanity.count == 1
49
+
50
+ # Now if we call @post.vanity we should get the one we just created
51
+ assert @post.vanity == Vanity.first
52
+ end
53
+ end
@@ -2,22 +2,23 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{vanities}
5
- s.version = "0.1.2"
5
+ s.version = "0.1.3"
6
6
 
7
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{2011-03-20}
10
- s.description = %q{Give each of your models a simple 'vanity' URL.
8
+ s.authors = [%q{J. Austin Hughey}]
9
+ s.date = %q{2011-09-18}
10
+ s.description = %q{Give each of your models a simple 'vanity' URL.
11
11
  Makes example.com/users/1 into something like example.com/foobar}
12
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"]
13
+ s.extra_rdoc_files = [%q{README.rdoc}, %q{lib/generators/vanities/USAGE}, %q{lib/generators/vanities/templates/create_vanities.rb}, %q{lib/generators/vanities/templates/vanities_controller.rb}, %q{lib/generators/vanities/templates/vanity.rb}, %q{lib/generators/vanities/vanities_generator.rb}, %q{lib/vanities.rb}]
14
+ s.files = [%q{README.rdoc}, %q{Rakefile}, %q{lib/generators/vanities/USAGE}, %q{lib/generators/vanities/templates/create_vanities.rb}, %q{lib/generators/vanities/templates/vanities_controller.rb}, %q{lib/generators/vanities/templates/vanity.rb}, %q{lib/generators/vanities/vanities_generator.rb}, %q{lib/vanities.rb}, %q{test/unit/vanities_test.rb}, %q{test/unit/vanities_test.sqlite3}, %q{vanities_test.sqlite3}, %q{Manifest}, %q{vanities.gemspec}]
15
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"]
16
+ s.rdoc_options = [%q{--line-numbers}, %q{--inline-source}, %q{--title}, %q{Vanities}, %q{--main}, %q{README.rdoc}]
17
+ s.require_paths = [%q{lib}]
18
18
  s.rubyforge_project = %q{vanities}
19
- s.rubygems_version = %q{1.6.0}
20
- s.summary = %q{Give each of your models a simple 'vanity' URL. Makes example.com/users/1 into something like example.com/foobar}
19
+ s.rubygems_version = %q{1.8.9}
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
+ s.test_files = [%q{test/unit/vanities_test.rb}]
21
22
 
22
23
  if s.respond_to? :specification_version then
23
24
  s.specification_version = 3
Binary file
metadata CHANGED
@@ -1,28 +1,22 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: vanities
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
4
5
  prerelease:
5
- version: 0.1.2
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - J. Austin Hughey
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2011-03-20 00:00:00 -06:00
14
- default_executable:
12
+ date: 2011-09-18 00:00:00.000000000Z
15
13
  dependencies: []
16
-
17
- description: |-
18
- Give each of your models a simple 'vanity' URL.
19
- Makes example.com/users/1 into something like example.com/foobar
14
+ description: ! "Give each of your models a simple 'vanity' URL.\n Makes
15
+ example.com/users/1 into something like example.com/foobar"
20
16
  email: jaustinhughey@gmail.com
21
17
  executables: []
22
-
23
18
  extensions: []
24
-
25
- extra_rdoc_files:
19
+ extra_rdoc_files:
26
20
  - README.rdoc
27
21
  - lib/generators/vanities/USAGE
28
22
  - lib/generators/vanities/templates/create_vanities.rb
@@ -30,7 +24,7 @@ extra_rdoc_files:
30
24
  - lib/generators/vanities/templates/vanity.rb
31
25
  - lib/generators/vanities/vanities_generator.rb
32
26
  - lib/vanities.rb
33
- files:
27
+ files:
34
28
  - README.rdoc
35
29
  - Rakefile
36
30
  - lib/generators/vanities/USAGE
@@ -39,40 +33,41 @@ files:
39
33
  - lib/generators/vanities/templates/vanity.rb
40
34
  - lib/generators/vanities/vanities_generator.rb
41
35
  - lib/vanities.rb
36
+ - test/unit/vanities_test.rb
37
+ - test/unit/vanities_test.sqlite3
38
+ - vanities_test.sqlite3
42
39
  - Manifest
43
40
  - vanities.gemspec
44
- has_rdoc: true
45
41
  homepage: https://github.com/jaustinhughey/vanities
46
42
  licenses: []
47
-
48
43
  post_install_message:
49
- rdoc_options:
44
+ rdoc_options:
50
45
  - --line-numbers
51
46
  - --inline-source
52
47
  - --title
53
48
  - Vanities
54
49
  - --main
55
50
  - README.rdoc
56
- require_paths:
51
+ require_paths:
57
52
  - lib
58
- required_ruby_version: !ruby/object:Gem::Requirement
53
+ required_ruby_version: !ruby/object:Gem::Requirement
59
54
  none: false
60
- requirements:
61
- - - ">="
62
- - !ruby/object:Gem::Version
63
- version: "0"
64
- required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
60
  none: false
66
- requirements:
67
- - - ">="
68
- - !ruby/object:Gem::Version
69
- version: "1.2"
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '1.2'
70
65
  requirements: []
71
-
72
66
  rubyforge_project: vanities
73
- rubygems_version: 1.6.0
67
+ rubygems_version: 1.8.9
74
68
  signing_key:
75
69
  specification_version: 3
76
- summary: Give each of your models a simple 'vanity' URL. Makes example.com/users/1 into something like example.com/foobar
77
- test_files: []
78
-
70
+ summary: Give each of your models a simple 'vanity' URL. Makes example.com/users/1
71
+ into something like example.com/foobar
72
+ test_files:
73
+ - test/unit/vanities_test.rb