Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Eliminate Article#find_by_slug -- does not provide extra clarity

  • Loading branch information...
commit 947fc2cd4f3a9d96a61ae3d2be2efd16e7649fe0 1 parent b963419
@brandur authored
View
8 lib/hekla/models/article.rb
@@ -1,14 +1,6 @@
class Article < Sequel::Model
plugin :validation_helpers
- def self.find_by_slug(s)
- filter(slug: s).first
- end
-
- def self.find_by_slug!(s)
- find_by_slug(s) || raise(Sinatra::NotFound)
- end
-
def self.ordered
reverse_order(:published_at)
end
View
4 lib/hekla/modules/api.rb
@@ -42,7 +42,7 @@ class API < Sinatra::Base
put "/articles/:id" do |id|
authorized!
- @article = Article.find_by_slug!(id)
+ @article = Article.first(slug: id) || raise(Sinatra::NotFound)
@article.update(article_params)
cache_clear
[200, encode_json(@article.v1_attributes)]
@@ -50,7 +50,7 @@ class API < Sinatra::Base
delete "/articles/:id" do |id|
authorized!
- @article = Article.find_by_slug!(id)
+ @article = Article.first(slug: id) || raise(Sinatra::NotFound)
@article.destroy
cache_clear
[200, encode_json(@article.v1_attributes)]
View
10 lib/hekla/modules/web.rb
@@ -58,13 +58,13 @@ class Web < Sinatra::Base
end
get "/:id.:format" do |id, format|
- redirect to("/#{id}") if Article.find_by_slug(id)
- raise(Sinatra::NotFound)
+ Article.first(slug: id) || raise(Sinatra::NotFound)
+ redirect to("/#{id}")
end
get "/:id" do |id|
cache do
- @article = Article.find_by_slug!(id)
+ @article = Article.first(slug: id) || raise(Sinatra::NotFound)
@title = @article.title
slim :show, layout: !pjax?
end
@@ -73,8 +73,8 @@ class Web < Sinatra::Base
# redirect old style permalinks
get "/articles/:id" do |id|
log :get_article, old_permalink: true
- redirect to("/#{id}") if Article.find_by_slug(id)
- raise(Sinatra::NotFound)
+ Article.first(slug: id) || raise(Sinatra::NotFound)
+ redirect to("/#{id}")
end
get "/a/:id" do |id|
View
2  test/modules/api_test.rb
@@ -81,7 +81,7 @@ def app
it "fails to update an article" do
authorize "", "KEY"
- mock(Article).find_by_slug!("about") { article }
+ mock(Article).first(slug: "about") { article }
mock(article).update.with_any_args {
raise(Sequel::ValidationFailed.new([]))
}
Please sign in to comment.
Something went wrong with that request. Please try again.