dterror-resource_identifier 0.1.0
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/LICENSE +20 -0
- data/Manifest +8 -0
- data/README.md +25 -0
- data/Rakefile +21 -0
- data/init.rb +1 -0
- data/lib/resource_identifier.rb +58 -0
- data/resource_identifier.gemspec +31 -0
- data/spec/resource_id_spec.rb +75 -0
- data/spec/spec_helper.rb +24 -0
- metadata +69 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Diogo Terror
|
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.
|
data/Manifest
ADDED
data/README.md
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
#resource_identifier
|
2
|
+
|
3
|
+
This plugin allows you to specify a different table identifier (AR's default is the :id field) so that it affects
|
4
|
+
how find classmethod works and also the to_param instance method so that your model behaves like having a different identifier (a slug field, for instance).
|
5
|
+
|
6
|
+
This is great if you want to have really beautiful urls :)
|
7
|
+
|
8
|
+
take a look at some code:
|
9
|
+
|
10
|
+
class Article < ActiveRecord::Base
|
11
|
+
resource_identifier :slug
|
12
|
+
end
|
13
|
+
|
14
|
+
# then...
|
15
|
+
@article = Article.find('hello-world')
|
16
|
+
|
17
|
+
# and if you have routes like...
|
18
|
+
map.resouces :articles
|
19
|
+
|
20
|
+
# then article_path(@article) will be...
|
21
|
+
'/articles/hello-world/'
|
22
|
+
|
23
|
+
## Install
|
24
|
+
|
25
|
+
sudo gem install dterror-resource_identifier
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require "spec/rake/spectask"
|
3
|
+
require 'echoe'
|
4
|
+
|
5
|
+
desc 'Default: run the spec'
|
6
|
+
task :default => :spec
|
7
|
+
|
8
|
+
Echoe.new('resource_identifier', '0.1.0') do |p|
|
9
|
+
p.summary = "Have trully beautiful URLS in your Rails RESTful routes"
|
10
|
+
p.description = "An ActiveRecord plugin that allows you to specify a different table identifier (AR's default is the :id field)."
|
11
|
+
p.url = 'http://github.com/dterror/resource_identifier/'
|
12
|
+
p.author = 'Diogo Terror'
|
13
|
+
p.email = 'ranccis@gmail.com'
|
14
|
+
p.development_dependencies = []
|
15
|
+
end
|
16
|
+
|
17
|
+
desc "Run all specs"
|
18
|
+
Spec::Rake::SpecTask.new('spec') do |t|
|
19
|
+
t.spec_files = FileList['spec/**/*.rb']
|
20
|
+
t.spec_opts = ["-c", "-fs"]
|
21
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/lib/resource_identifier'
|
@@ -0,0 +1,58 @@
|
|
1
|
+
|
2
|
+
module ResourceIdentifier
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.class_eval do
|
6
|
+
extend ClassMethods
|
7
|
+
include InstanceMethods
|
8
|
+
|
9
|
+
alias_method_chain :to_param, :identifier
|
10
|
+
class << self
|
11
|
+
alias_method_chain :find, :identifier
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
module ClassMethods
|
17
|
+
def resource_identifier(symbol, opts_hash={})
|
18
|
+
self.instance_eval do
|
19
|
+
@resource_id = symbol
|
20
|
+
@allow_default_id = (opts_hash[:allow_default_id] || false)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def get_resource_id
|
25
|
+
self.instance_variable_get("@resource_id")
|
26
|
+
end
|
27
|
+
|
28
|
+
def allow_default?
|
29
|
+
self.instance_variable_get("@allow_default_id")
|
30
|
+
end
|
31
|
+
|
32
|
+
def find_with_identifier(*args)
|
33
|
+
case args.first
|
34
|
+
when String
|
35
|
+
res = find_without_identifier(:first, :conditions => { (self.get_resource_id || :id) => args.first })
|
36
|
+
raise ActiveRecord::RecordNotFound, "Couldn't find #{self.class} with Identifier=#{args.first}" if res.nil?
|
37
|
+
return res
|
38
|
+
when Fixnum
|
39
|
+
if self.get_resource_id.nil? || (self.get_resource_id and self.allow_default?)
|
40
|
+
find_without_identifier(*args)
|
41
|
+
else
|
42
|
+
raise ActiveRecord::RecordNotFound, "The given identifier is not a valid format of resource identifier"
|
43
|
+
end
|
44
|
+
else
|
45
|
+
find_without_identifier(*args)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
module InstanceMethods
|
51
|
+
def to_param_with_identifier
|
52
|
+
send self.class.get_resource_id
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
ActiveRecord::Base.send :include, ResourceIdentifier
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{resource_identifier}
|
5
|
+
s.version = "0.1.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Diogo Terror"]
|
9
|
+
s.date = %q{2009-08-14}
|
10
|
+
s.description = %q{An ActiveRecord plugin that allows you to specify a different table identifier (AR's default is the :id field).}
|
11
|
+
s.email = %q{ranccis@gmail.com}
|
12
|
+
s.extra_rdoc_files = ["lib/resource_identifier.rb", "LICENSE", "README.md"]
|
13
|
+
s.files = ["init.rb", "lib/resource_identifier.rb", "LICENSE", "Rakefile", "README.md", "spec/resource_id_spec.rb", "spec/spec_helper.rb", "Manifest", "resource_identifier.gemspec"]
|
14
|
+
s.has_rdoc = true
|
15
|
+
s.homepage = %q{http://www.github.com/dterror/resource_identifier}
|
16
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Resource_identifier", "--main", "README.md"]
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
s.rubyforge_project = %q{resource_identifier}
|
19
|
+
s.rubygems_version = %q{1.3.2}
|
20
|
+
s.summary = %q{Have trully beautiful URLS in your Rails RESTful routes}
|
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::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
27
|
+
else
|
28
|
+
end
|
29
|
+
else
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe "A model Resource Ided" do
|
4
|
+
|
5
|
+
before(:all) do
|
6
|
+
setup_db
|
7
|
+
class Person < ActiveRecord::Base;resource_identifier(:slug);end
|
8
|
+
class Comment < ActiveRecord::Base;end
|
9
|
+
class Article < ActiveRecord::Base;resource_identifier(:slug, :allow_default_id => true);end
|
10
|
+
end
|
11
|
+
|
12
|
+
before(:each) do
|
13
|
+
Person.create(:name => "John Locke", :slug => "john-locke")
|
14
|
+
Comment.create(:name => "John Locke", :comment => "Come on...")
|
15
|
+
Article.create(:title => "My Article", :slug => 'my-article')
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should only start working when the resource_identifier method is called" do
|
19
|
+
Comment.get_resource_id.should be_nil
|
20
|
+
comm = Comment.find(1)
|
21
|
+
comm.name.should == 'John Locke'
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should populate resource_id field" do
|
25
|
+
Person.get_resource_id.should == :slug
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should find with the specified resource_identifier" do
|
29
|
+
john = Person.find('john-locke')
|
30
|
+
john.name.should == 'John Locke'
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should respond with ActiveRecord::RecordNotFound instea of a plain nil when not found" do
|
34
|
+
a = lambda { Person.find('jack-shephard') }
|
35
|
+
a.should raise_error(ActiveRecord::RecordNotFound)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should not find with id by default" do
|
39
|
+
a = lambda { Person.find(1) }
|
40
|
+
a.should raise_error(ActiveRecord::RecordNotFound)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should not find with id by default even when its stringified" do
|
44
|
+
a = lambda { Person.find('1') }
|
45
|
+
a.should raise_error(ActiveRecord::RecordNotFound)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should return to_param with the identifier field" do
|
49
|
+
john = Person.find('john-locke')
|
50
|
+
john.to_param.should == 'john-locke'
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should work with option allow_default_id" do
|
54
|
+
art = Article.find('my-article')
|
55
|
+
same_art = Article.find(1)
|
56
|
+
art.title.should == "My Article"
|
57
|
+
same_art.title.should == 'My Article'
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
$LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib/'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'spec'
|
4
|
+
require 'active_record'
|
5
|
+
require 'resource_identifier'
|
6
|
+
|
7
|
+
ActiveRecord::Base.establish_connection(
|
8
|
+
:adapter => 'sqlite3',
|
9
|
+
:database => ':memory:'
|
10
|
+
)
|
11
|
+
|
12
|
+
def setup_db
|
13
|
+
ActiveRecord::Schema.define do
|
14
|
+
create_table :people, :force => true do |t|
|
15
|
+
t.string :name, :slug
|
16
|
+
end
|
17
|
+
create_table :comments, :force => true do |t|
|
18
|
+
t.string :name, :comment
|
19
|
+
end
|
20
|
+
create_table :articles, :force => true do |t|
|
21
|
+
t.string :title, :slug
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dterror-resource_identifier
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Diogo Terror
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-08-14 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: An ActiveRecord plugin that allows you to specify a different table identifier (AR's default is the :id field).
|
17
|
+
email: ranccis@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- lib/resource_identifier.rb
|
24
|
+
- LICENSE
|
25
|
+
- README.md
|
26
|
+
files:
|
27
|
+
- init.rb
|
28
|
+
- lib/resource_identifier.rb
|
29
|
+
- LICENSE
|
30
|
+
- Rakefile
|
31
|
+
- README.md
|
32
|
+
- spec/resource_id_spec.rb
|
33
|
+
- spec/spec_helper.rb
|
34
|
+
- Manifest
|
35
|
+
- resource_identifier.gemspec
|
36
|
+
has_rdoc: true
|
37
|
+
homepage: http://www.github.com/dterror/resource_identifier
|
38
|
+
licenses:
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options:
|
41
|
+
- --line-numbers
|
42
|
+
- --inline-source
|
43
|
+
- --title
|
44
|
+
- Resource_identifier
|
45
|
+
- --main
|
46
|
+
- README.md
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: "1.2"
|
60
|
+
version:
|
61
|
+
requirements: []
|
62
|
+
|
63
|
+
rubyforge_project: resource_identifier
|
64
|
+
rubygems_version: 1.3.5
|
65
|
+
signing_key:
|
66
|
+
specification_version: 3
|
67
|
+
summary: Have trully beautiful URLS in your Rails RESTful routes
|
68
|
+
test_files: []
|
69
|
+
|