Skip to content
This repository was archived by the owner on Oct 24, 2023. It is now read-only.

404 should be rendered when no blog is available #15

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/controllers/spree/blogs/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def search

def show
@post = default_scope.includes(:tags, :images, :products).find_by_path(params[:id]) rescue nil
@title = @post.title if @post && @post.title.present?
return redirect_to archive_posts_path unless @post
end

Expand Down
12 changes: 10 additions & 2 deletions app/models/spree/blog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ def self.find_by_permalink!(path)
end

def self.find_by_permalink(path)
find_by_permalink!(path) rescue ActiveRecord::RecordNotFound
if path.length < 3
raise ActiveRecord::RecordNotFound # pemalink length is at least 3
else
find_by_permalink!(path) rescue ActiveRecord::RecordNotFound
end
end

def self.to_options
Expand All @@ -28,7 +32,11 @@ def self.to_options
def to_param
self.permalink.gsub(/(^\/+)|(\/+$)/, "")
end


def matches?(_path)
( _path.delete("/").match(permalink))
end

private

def permalink_availablity
Expand Down
4 changes: 2 additions & 2 deletions app/models/spree/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class Spree::Post < ActiveRecord::Base
@@per_page = 10

scope :ordered, order("posted_at DESC")
scope :future, where("posted_at > ?", Time.now).order("posted_at ASC")
scope :past, where("posted_at <= ?", Time.now).ordered
scope :future, lambda { where("posted_at > ?", Time.now).order("posted_at ASC") }
scope :past, lambda { where("posted_at <= ?", Time.now).ordered }
scope :live, where(:live => true )

before_validation :create_path, :if => proc{ |record| record.title_changed? }
Expand Down
5 changes: 5 additions & 0 deletions app/overrides/add_blogs_to_menu.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Deface::Override.new(:virtual_path => "spree/shared/_store_menu",
:name => "spree_essential_blog_main_menu_items",
:insert_after => "#home-link[data-hook]",
:partial => "spree/shared/blog_main_menu_items",
:disabled => false)
2 changes: 1 addition & 1 deletion app/views/spree/blogs/admin/blogs/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<%= render :partial => 'spree/admin/shared/configuration_menu' %>

<h1>Listing Blogs</h1>
<h1><%=t "spree.blogs.admin.blogs.index.listing" %></h1>

<table class="index">
<thead>
Expand Down
9 changes: 7 additions & 2 deletions app/views/spree/blogs/admin/posts/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<%= form.field_container :posted_at do %>
<%= form.label :posted_at, t("activerecord.attributes.post.posted_at") %><br />
<%= form.text_field :posted_at, :class => 'text', :value => ((l @post.posted_at, :format => :long) unless @post.posted_at.nil?) %><br />
<%= form.text_field :posted_at, :class => 'text', :value => (@post.posted_at unless @post.posted_at.nil?) %><br />
<%= error_message_on :post, :posted_at %>
<% end %>

Expand All @@ -36,7 +36,12 @@
<script type="text/javascript">
//<![CDATA[
$(document).ready(function() {
$('#post_posted_at').autodate();
$('#post_posted_at').autodate({
date_format: "<%=t 'autodate.date_format' %>",
empty_string: "<%=t 'autodate.empty_string' %>",
input_empty: "<%=t 'autodate.input_empty' %>",
input_invalid: "<%=t 'autodate.input_invalid' %>"
});
$('#post_body').markItUp(markdownSettings);
});
//]]>
Expand Down
8 changes: 4 additions & 4 deletions app/views/spree/blogs/admin/posts/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<div class='toolbar'>
<ul class='actions'>
<li>
<p><%= button_link_to "New Post", new_object_url, :icon => 'add' %></p>
<p><%= button_link_to t("spree.blogs.admin.posts.new.new_post"), new_object_url, :icon => 'add' %></p>
</li>
</ul>
<br class='clear' />
</div>

<h1>Listing Posts</h1>
<h1><%=t "spree.blogs.admin.posts.index.listing" %></h1>

<table class="index">
<thead>
Expand All @@ -24,8 +24,8 @@
<tr id="<%= dom_id post %>">
<td><%= link_to post.title, object_url(post) %></td>
<td><%= post.blog.nil? ? "n/a" : link_to(post.blog.name, admin_posts_path(:search => { :blog_id_equals => post.blog_id })) %></td>
<td><%= l post.posted_at, :format => :date_time %></td>
<td><%= post.live ? 'Yes' : 'No' %></td>
<td><%= l post.posted_at, :format => :short %></td>
<td><%= post.live ? t(:yes) : t(:no) %></td>
<td>
<%= link_to_edit post %> &nbsp;
<%= link_to_delete post %>
Expand Down
6 changes: 3 additions & 3 deletions app/views/spree/blogs/admin/posts/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<%= render :partial => 'spree/blogs/admin/shared/post_tabs', :locals => {:current => "Post Details"} %>
<p>posted in <%= link_to @post.blog.name, edit_admin_blog_path(@post.blog) %></p>
<h4><%= h(l @post.posted_at, :format => :detailed).gsub(/\s0/, ' ') %></h4>
<p><%=t 'spree.blogs.posted_in' %> <%= link_to @post.blog.name, edit_admin_blog_path(@post.blog) %></p>
<h4><%= h(l @post.posted_at, :format => :short).gsub(/\s0/, ' ') %></h4>
<br/>

<% for image in @post.images do %>
Expand All @@ -12,6 +12,6 @@
<br class="clear" />

<p>
<%= link_to_edit @user %> &nbsp;
<%= link_to_edit @post %> &nbsp;
<%= link_to t('back'), collection_url %>
</p>
2 changes: 1 addition & 1 deletion app/views/spree/blogs/admin/shared/_post_tabs.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<% content_for :sidebar do %>

<h3 class="name"><%= @post.title %><span class='sku'><%= l (@post.posted_at || Time.now), :format => :date_only %></span></h3>
<h3 class="name"><%= @post.title %><span class='sku'><%= l (@post.posted_at || Time.now), :format => :short %></span></h3>
<br clear="clear" />

<ul class="sidebar post-menu">
Expand Down
5 changes: 5 additions & 0 deletions app/views/spree/shared/_blog_main_menu_items.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<% unless @blogs.blank? %>
<% @blogs.each do |blog| %>
<%= content_tag(:li, link_to(blog.name, blog_posts_path(blog)), :class => blog.matches?(request.path) ? "active" : nil) %>
<% end %>
<% end %>
13 changes: 13 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ en:
# Spree Hax! Missing translation...
image_settings_description: "Image Settings"

'yes': "Yes"
'no': "No"

activerecord:
attributes:
blog:
Expand Down Expand Up @@ -33,10 +36,13 @@ en:
new:
new_blog: New Blog
index:
listing: Listing Blogs
new_blog: New Blog


posts:
index:
listing: Listing Posts
new:
new_post: New Post
post_products:
Expand All @@ -61,6 +67,7 @@ en:
title: Recent Posts
archive: Blog Archive
no_posts: No posts found!
posted_in: posted in
posts:
index:
blog: Blog
Expand Down Expand Up @@ -104,3 +111,9 @@ en:
date:
formats:
month_year: "%B %Y"

autodate:
date_format: "dddd, MMMM d, yyyy h:mm:ss tt"
empty_string: "Type a date above"
input_empty: "Enter any date here.."
input_invalid: "Please enter a valid date."
4 changes: 2 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace :admin do

resources :blogs, :constraints => { :id => /[a-z0-9\-\_\/]{3,}/ }
resources :blogs, :constraints => { :id => /[a-z0-9\-\_\/]{0,}/ }

resources :posts do
resources :images, :controller => "post_images" do
Expand All @@ -20,7 +20,7 @@

end

constraints :blog_id => /([a-z0-9\-\_\/]{3,})/ do
constraints :blog_id => /(.*)/ do

constraints(
:year => /\d{4}/,
Expand Down
6 changes: 5 additions & 1 deletion lib/spree_essential_blog/posts_controller_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ def get_tags
end

def get_blog
@blog = Spree::Blog.find_by_permalink!(params[:blog_id])
begin
@blog = Spree::Blog.find_by_permalink!(params[:blog_id])
rescue ActiveRecord::RecordNotFound => e
render_404(e)
end
end

end
43 changes: 43 additions & 0 deletions test/integration/spree/blogs/blog_integration_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#! /usr/bin/env ruby
# encoding: UTF-8

require 'test_helper'


class Spree::Blogs::BlogIntegrationTest < SpreeEssentials::IntegrationCase

def setup
Spree::Blog.destroy_all
end

context "with some blogs" do

setup do
3.times{ |i| Factory.create(:spree_blog, :name => "My blog #{i}") }
end

should "render 404 if blog is not found" do
with_driver(:rack_test) do
visit spree.blog_posts_path("does_not_exist")
assert page.status_code == 404, "Expected response to be 404"
end
end

should "render 404 if blog name is shorter than 3 characters" do
Factory.create(:spree_blog, :name => "abc")

with_driver(:rack_test) do
visit spree.blog_posts_path("a")
assert page.status_code == 404, "Expected response to be 404"

visit spree.blog_posts_path("ab")
assert page.status_code == 404, "Expected response to be 404"

visit spree.blog_posts_path("abc")
assert page.status_code == 200, "Expected response to be 200"
end
end

end

end
6 changes: 6 additions & 0 deletions test/support/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ def setup_action_controller_behaviour(controller_class)
@routes = Spree::Core::Engine.routes
@controller = controller_class.new
end

def with_driver(driver)
Capybara.current_driver = driver
yield
Capybara.use_default_driver
end