Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Article slugs #155

Merged
merged 4 commits into from Aug 18, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/admin/articles_controller.rb
Expand Up @@ -47,7 +47,7 @@ def destroy
private

def find_article
@article = Article.find(params[:id])
@article = Article[params[:id]]
end
end
end
12 changes: 7 additions & 5 deletions app/controllers/articles_controller.rb
@@ -1,5 +1,6 @@
class ArticlesController < ApplicationController
before_filter :find_article, :only => [:show, :edit, :update, :share]
before_filter :redirect_to_slug, :only => [:show]
before_filter :create_visit, :only => [:show]

skip_before_filter :authenticate, :only => [:shared, :samples]
Expand Down Expand Up @@ -80,11 +81,7 @@ def random
private

def find_article
if params[:volume] && params[:issue]
@article = Article.find_by_issue_number("#{params[:volume]}.#{params[:issue]}")
else
@article = Article.find_by_id(params[:id])
end
@article = Article[params[:id]]

render_http_error(404) unless @article
end
Expand All @@ -108,4 +105,9 @@ def create_visit
end
end

def redirect_to_slug
return unless @article.slug.present?

redirect_to article_path(@article.slug) unless params[:id] == @article.slug
end
end
15 changes: 14 additions & 1 deletion app/models/article.rb
Expand Up @@ -3,7 +3,8 @@ class Article < ActiveRecord::Base
belongs_to :volume
belongs_to :collection

validates_presence_of :issue_number
validates_presence_of :issue_number
validates_uniqueness_of :slug, :allow_blank => true

def self.in_volume(number)
includes(:volume)
Expand All @@ -18,6 +19,18 @@ def self.drafts
where(:status => "draft")
end

def self.[](key)
find_by_slug(key) || find_by_id(key)
end

def to_param
if slug.present?
slug
else
id.to_s
end
end

def full_subject
"Issue #{issue_number}: #{subject}"
end
Expand Down
4 changes: 4 additions & 0 deletions app/views/admin/articles/_form.html.haml
Expand Up @@ -4,6 +4,10 @@
= f.label :subject
%br
= f.text_field :subject
%p
= f.label :slug
%br
= f.text_field :slug
%p
= f.label :published_time
= f.date_select :published_time
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20130816174433_add_slug_to_articles.rb
@@ -0,0 +1,5 @@
class AddSlugToArticles < ActiveRecord::Migration
def change
add_column :articles, :slug, :string
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20130215174537) do
ActiveRecord::Schema.define(:version => 20130816174433) do

create_table "announcements", :force => true do |t|
t.text "title"
Expand Down Expand Up @@ -43,6 +43,7 @@
t.text "issue_number"
t.integer "volume_id"
t.integer "collection_id"
t.string "slug"
end

create_table "authorization_links", :force => true do |t|
Expand Down
35 changes: 35 additions & 0 deletions test/integration/article_routing_test.rb
@@ -0,0 +1,35 @@
require_relative "../test_helper"

class ArticleRoutingTest < ActionDispatch::IntegrationTest
setup do
@article = FactoryGirl.create(:article, :slug => "awesome-article")
simulated_user { register Support::SimulatedUser.default }
end

test "by slug" do
visit "/articles/awesome-article"

assert_content @article.subject
end

test "by id" do
id = @article.id
visit "/articles/#{id}"

assert_content @article.subject

assert_current_path "/articles/awesome-article"
end

test "with invalid slug" do
visit "/articles/i-do-no-exist"

assert_equal 404, page.status_code
end

test "with invalid id" do
visit "/articles/99999"

assert_equal 404, page.status_code
end
end
27 changes: 27 additions & 0 deletions test/integration/edit_article_test.rb
@@ -0,0 +1,27 @@
require_relative '../test_helper'

class EditArticleTest < ActionDispatch::IntegrationTest
setup do
simulated_user { register Support::SimulatedUser.default }
@article = FactoryGirl.create(:article, :slug => "awesome-article")
User.first.update_attribute(:admin, true)
end

test "can edit articles with slugs" do
visit edit_admin_article_path(@article.slug)

click_button "Update Article"

assert_current_path article_path(@article)
end

test "can edit articles without slugs" do
@article.update_attribute(:slug, nil)

visit edit_admin_article_path(@article.id)

click_button "Update Article"

assert_current_path article_path(@article)
end
end