fix tests + rename and refactor the slugify method (now called permalink)

This commit is contained in:
did 2011-06-21 13:03:24 -07:00
parent 0631b96df9
commit 3c0f7afec0
10 changed files with 83 additions and 42 deletions

View File

@ -33,7 +33,7 @@ module Admin
end
def get_path
page = current_site.pages.build(:parent => current_site.pages.find(params[:parent_id]), :slug => params[:slug].slugify)
page = current_site.pages.build(:parent => current_site.pages.find(params[:parent_id]), :slug => params[:slug].permalink)
render :json => { :url => page_url(page), :slug => page.slug }
end

View File

@ -14,12 +14,14 @@ class ContentInstance
## validations ##
validate :require_highlighted_field
validate :validate_uniqueness_of_slug
validates_presence_of :_slug
## associations ##
embedded_in :content_type, :inverse_of => :contents
## callbacks ##
before_save :set_slug
before_validation :set_slug
before_save :set_visibility
before_create :add_to_list_bottom
after_create :send_notifications
@ -67,8 +69,8 @@ class ContentInstance
protected
def set_slug
_alias = self.highlighted_field_alias
self._slug = self.send(_alias).parameterize('_')
self._slug = self.highlighted_field_value.clone if self._slug.blank? && self.highlighted_field_value.present?
self._slug.permalink! if self._slug.present?
end
def set_visibility
@ -77,7 +79,6 @@ class ContentInstance
end
def add_to_list_bottom
Rails.logger.debug "add_to_list_bottom"
self._position_in_list = self.content_type.contents.size
end
@ -88,6 +89,12 @@ class ContentInstance
end
end
def validate_uniqueness_of_slug
if self._parent.contents.any? { |c| c._id != self._id && c._slug == self._slug }
self.errors.add(:_slug, :taken)
end
end
def highlighted_field_alias
self.content_type.highlighted_field._alias.to_sym
end

View File

@ -131,7 +131,7 @@ class ContentType
def normalize_slug
self.slug = self.name.clone if self.slug.blank? && self.name.present?
self.slug.slugify! if self.slug.present?
self.slug.permalink! if self.slug.present?
end
def remove_uploaded_files # callbacks are not called on each content so we do it manually

View File

@ -97,7 +97,7 @@ class Page
def normalize_slug
self.slug = self.title.clone if self.slug.blank? && self.title.present?
self.slug.slugify!(:without_extension => true) if self.slug.present?
self.slug.permalink! if self.slug.present?
end
def set_default_raw_template

View File

@ -26,7 +26,7 @@ class Snippet
def normalize_slug
# TODO: refactor it
self.slug = self.name.clone if self.slug.blank? && self.name.present?
self.slug.slugify!(:without_extension => true, :downcase => true) if self.slug.present?
self.slug.permalink! if self.slug.present?
end
def update_templates

View File

@ -16,9 +16,11 @@ x remove asset_collections
x assign same _id
x pick up a theme_asset
x pull request locomedia
x refactor slugify method (use parameterize + create a module)
- bushido changes in the master
- SEO: support and support/ should be 2 different pages. Remove trailing slash
- BUG: has_many. Delete an author
- bushido changes in the master
- edit sidebar (inline editor). Unable to reset it
BACKLOG:
@ -35,11 +37,16 @@ BACKLOG:
- icon for redirection page in the pages section (back-office)
- write my first tutorial about locomotive
- upgrade warning if new version of locomotive (maybe based on the commit id)
- deploying workflow:
- roll back a bad update
- conflicts with content types
- dev -> staging -> production
- sync data
- import only theme assets
REFACTORING:
- refactor slugify method (use parameterize + create a module)
- move content_type and content_instances in the CustomFields plugin (much more appropriate)
BUGS:

View File

@ -1,37 +1,15 @@
## String
class String
# def perma_string(sep = '_')
# ActiveSupport::Inflector.parameterize(self, sep).to_s
# end
def slugify(options = {})
options = { :sep => '_', :without_extension => false, :downcase => false, :underscore => false }.merge(options)
# replace accented chars with ther ascii equivalents
s = ActiveSupport::Inflector.transliterate(self).to_s
# No more than one slash in a row
s.gsub!(/(\/[\/]+)/, '/')
# Remove leading or trailing space
s.strip!
# Remove leading or trailing slash
s.gsub! /(^[\/]+)|([\/]+$)/, ''
# Remove extensions
s.gsub! /(\.[a-zA-Z]{2,})/, '' if options[:without_extension]
# Downcase
s.downcase! if options[:downcase]
# Turn unwanted chars into the seperator
s.gsub!(/[^a-zA-Z0-9\-_\+\/]+/i, options[:sep])
# Underscore
s.gsub!(/[\-]/i, '_') if options[:underscore]
s
def permalink
self.parameterize('-')
end
def slugify!(options = {})
replace(self.slugify(options))
def permalink!
replace(self.permalink)
end
def parameterize!(sep = '_')
replace(self.parameterize(sep))
end
alias :parameterize! :permalink!
end

View File

@ -49,6 +49,7 @@ var TinyMceDefaultSettings = {
theme : 'advanced',
skin : 'locomotive',
plugins: 'safari,inlinepopups,locomedia',
extended_valid_elements: 'iframe[width|height|frameborder|allowfullscreen|src|title]',
theme_advanced_buttons1 : 'code,|,bold,italic,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist,|,outdent,indent,blockquote,|,link,unlink',
theme_advanced_buttons2 : 'formatselect,fontselect,fontsizeselect,|,locomedia',
theme_advanced_buttons3 : '',

View File

@ -1,3 +1,5 @@
# encoding: utf-8
require 'spec_helper'
describe ContentInstance do
@ -17,14 +19,60 @@ describe ContentInstance do
build_content.should be_valid
end
# Validations ##
## Validations ##
it 'requires presence of title' do
it 'requires the presence of title' do
content = build_content :title => nil
content.should_not be_valid
content.errors[:title].should == ["can't be blank"]
end
it 'requires the presence of the permalink (_slug)' do
content = build_content :title => nil
content.should_not be_valid
content.errors[:_slug].should == ["can't be blank"]
end
it 'has an unique permalink' do
build_content.save; @content_type = ContentType.find(@content_type._id)
content = build_content
content.should_not be_valid
content.errors[:_slug].should == ["is already taken"]
end
end
describe '#permalink' do
before(:each) do
@content = build_content
end
it 'has a default value based on the highlighted field' do
@content.send(:set_slug)
@content._permalink.should == 'locomotive'
end
it 'is empty if no value for the highlighted field is provided' do
@content.title = nil; @content.send(:set_slug)
@content._permalink.should be_nil
end
it 'includes dashes instead of white spaces' do
@content.title = 'my content instance'; @content.send(:set_slug)
@content._permalink.should == 'my-content-instance'
end
it 'removes accentued characters' do
@content.title = "une chèvre dans le pré"; @content.send(:set_slug)
@content._permalink.should == 'une-chevre-dans-le-pre'
end
it 'removes dots' do
@content.title = "my.test"; @content.send(:set_slug)
@content._permalink.should == 'my-test'
end
end
describe '#visibility' do
@ -93,7 +141,7 @@ describe ContentInstance do
end
end
describe '#site' do
it 'delegates to the content type' do
@content_type.expects(:site)

View File

@ -70,11 +70,11 @@ describe Page do
it 'should have normalized slug' do
page = Factory.build(:page, :slug => ' Valid ité.html ')
page.valid?
page.slug.should == 'Valid_ite'
page.slug.should == 'valid-ite-html'
page = Factory.build(:page, :title => ' Valid ité.html ', :slug => nil, :site => page.site)
page.should be_valid
page.slug.should == 'Valid_ite'
page.slug.should == 'valid-ite-html'
end
it 'has no cache strategy' do