Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

make sure URL does not start with a slash (validation for #59 and #64)

  • Loading branch information...
commit 5c690c1fd85bd7b6cc0a620d062524b02c8f9e4b 1 parent b2b08d9
jipiboily authored
View
5 app/models/monologue/posts_revision.rb
@@ -13,6 +13,7 @@ class PostsRevision < ActiveRecord::Base
validates :title, :presence => true
validates :content, :presence => true
validates :url, :presence => true
+ validate :url_do_not_start_with_slash
validates :user_id, :presence => true
# validates :post_id, :presence => true # TODO: do something about this validation on the first creation of a POST
validates :published_at, :presence => true
@@ -27,6 +28,10 @@ def full_url
"#{Monologue::Engine.routes.url_helpers.root_path}#{self.url}"
end
+ def url_do_not_start_with_slash
+ errors.add(:url, I18n.t("activerecord.errors.models.monologue/posts_revision.attributes.url.start_with_slash")) if self.url.start_with?("/")
+ end
+
private
def generate_url
View
5 config/locales/en.yml
@@ -109,4 +109,7 @@ en:
attributes:
published_at:
blank:
- "'Published at' is required"
+ "'Published at' is required"
+ url:
+ start_with_slash:
+ "URL can't start with a slash ('/')"
View
5 config/locales/fr.yml
@@ -109,4 +109,7 @@ fr:
attributes:
published_at:
blank:
- "'Publié le' est requis"
+ "'Publié le' est requis"
+ url:
+ start_with_slash:
+ "L'adresse URL ne peut débuter par un slash ('/')"
View
6 spec/models/monologue/posts_revision_spec.rb
@@ -28,9 +28,13 @@
post.url.should == "2012/this-is-a-great-title"
end
+ it "should not let you create a post with a url starting with a '/'" do
+ expect { Factory(:posts_revision, :url => "/whatever") }.to raise_error(ActiveRecord::RecordInvalid)
+ end
+
it { validate_presence_of(:title) }
it { validate_presence_of(:content) }
it { validate_presence_of(:user_id) }
it { validate_presence_of(:published_at) }
-end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.