ismasan-sluggable_finder 2.0.2 → 2.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +12 -0
- data/lib/sluggable_finder/finder.rb +1 -1
- data/lib/sluggable_finder.rb +11 -1
- data/spec/sluggable_finder_spec.rb +17 -0
- data/tasks/db.rake +10 -3
- metadata +1 -1
data/README.markdown
CHANGED
@@ -63,6 +63,18 @@ The idea is that you keep your controller actions clean and handle Not Found err
|
|
63
63
|
end
|
64
64
|
end
|
65
65
|
|
66
|
+
### Merb and custom NotFound exception
|
67
|
+
|
68
|
+
In merb it would be nice to raise a NotFound exception instead of ActiveRecord's RecordNotFoundm so we don't need to clutter our controller with exception-handling code and we let the framework handle them.
|
69
|
+
|
70
|
+
SluggableFinder.not_found_exception = Merb::ControllerExceptions::NotFound
|
71
|
+
|
72
|
+
You can configure this to raise any exception you want.
|
73
|
+
|
74
|
+
class CustomException < StandardError; end
|
75
|
+
|
76
|
+
SluggableFinder.not_found_exception = CustomException
|
77
|
+
|
66
78
|
### Links
|
67
79
|
|
68
80
|
Link generation remains the same, because the plugin overwrites your model's to_param method
|
@@ -7,7 +7,7 @@ module SluggableFinder
|
|
7
7
|
if (args.first.is_a?(String) and !(args.first =~ /\A\d+\Z/))#only contain digits
|
8
8
|
options = {:conditions => ["#{ sluggable_finder_options[:to]} = ?", args.first]}
|
9
9
|
first(options) or
|
10
|
-
raise
|
10
|
+
raise SluggableFinder.not_found_exception.new("There is no #{sluggable_finder_options[:sluggable_type]} with #{sluggable_finder_options[:to]} '#{args.first}'")
|
11
11
|
else
|
12
12
|
find_without_slug(*args)
|
13
13
|
end
|
data/lib/sluggable_finder.rb
CHANGED
@@ -2,7 +2,17 @@ $:.unshift(File.dirname(__FILE__)) unless
|
|
2
2
|
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
3
|
|
4
4
|
module SluggableFinder
|
5
|
-
VERSION = '2.0.
|
5
|
+
VERSION = '2.0.3'
|
6
|
+
|
7
|
+
@@not_found_exception = nil
|
8
|
+
|
9
|
+
def self.not_found_exception=(ex)
|
10
|
+
@@not_found_exception = ex
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.not_found_exception
|
14
|
+
@@not_found_exception || ActiveRecord::RecordNotFound
|
15
|
+
end
|
6
16
|
|
7
17
|
class << self
|
8
18
|
|
@@ -98,6 +98,23 @@ describe SimpleItem, 'encoding permalinks' do
|
|
98
98
|
end
|
99
99
|
end
|
100
100
|
|
101
|
+
# Raising custom not found exceptions allows us to use this with merb's NotFound exception
|
102
|
+
# or any framework
|
103
|
+
describe "with custom exception" do
|
104
|
+
it "should raise custom exception if configured that way" do
|
105
|
+
class CustomException < StandardError;end
|
106
|
+
|
107
|
+
SluggableFinder.not_found_exception = CustomException
|
108
|
+
lambda {
|
109
|
+
SimpleItem.find 'non-existing-slug'
|
110
|
+
}.should raise_error(CustomException)
|
111
|
+
end
|
112
|
+
|
113
|
+
after(:all) do
|
114
|
+
SluggableFinder.not_found_exception = ActiveRecord::RecordNotFound
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
101
118
|
describe SimpleItem, "with non-english characters" do
|
102
119
|
before(:each) do
|
103
120
|
@item = SimpleItem.create!(:title => "Un ñandú super ñoño I've seen")
|
data/tasks/db.rake
CHANGED
@@ -1,10 +1,15 @@
|
|
1
|
-
|
1
|
+
db = {
|
2
2
|
:adapter=>'sqlite3',
|
3
3
|
:dbfile=> File.join(File.dirname(__FILE__),'..','spec','db','test.db')
|
4
|
-
|
4
|
+
}
|
5
|
+
ActiveRecord::Base.establish_connection( db )
|
5
6
|
# define a migration
|
6
7
|
class TestSchema < ActiveRecord::Migration
|
7
8
|
def self.up
|
9
|
+
create_table :categories do |t|
|
10
|
+
t.string :name
|
11
|
+
t.timestamps
|
12
|
+
end
|
8
13
|
create_table :items do |t|
|
9
14
|
t.string :title
|
10
15
|
t.string :slug
|
@@ -23,8 +28,10 @@ end
|
|
23
28
|
|
24
29
|
namespace :db do
|
25
30
|
desc "Create test schema"
|
26
|
-
task :create
|
31
|
+
task :create do
|
27
32
|
# run the migration
|
33
|
+
File.unlink(db[:dbfile]) if File.exists?(db[:dbfile])
|
34
|
+
ActiveRecord::Base.connection
|
28
35
|
TestSchema.migrate(:up)
|
29
36
|
end
|
30
37
|
|