Skip to content

Commit

Permalink
make sure URL does not start with a slash (validation for #59 and #64)
Browse files Browse the repository at this point in the history
  • Loading branch information
jipiboily committed May 26, 2012
1 parent b2b08d9 commit 5c690c1
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
5 changes: 5 additions & 0 deletions app/models/monologue/posts_revision.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ('/')"
5 changes: 4 additions & 1 deletion config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ('/')"
6 changes: 5 additions & 1 deletion spec/models/monologue/posts_revision_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 5c690c1

Please sign in to comment.