Permalink
Browse files

Safer and better testing of asset mp3 tags, names, and generation of …

…permalinks
  • Loading branch information...
sudara committed Apr 24, 2008
1 parent 487b495 commit f37856f18942c18bfef18ec136513f0c1f3c5602
View
@@ -140,15 +140,17 @@ def types_to_conditions(types)
def name
# make sure the title is there, and if not, the filename is used...
- title && !title.empty? ? title : clean_filename
+ (title && !title.strip.blank?) ? title.strip : clean_filename
end
def public_mp3
self.s3_url
end
+ # never allow this to be blank, as permalinks are generated from it
def clean_filename
- self.filename[0..-5].gsub(/-|_/,' ').titleize
+ clean = self.filename[0..-5].gsub(/-|_/,' ').strip.titleize
+ clean.blank? ? 'untitled' : clean
end
def clean_permalink
View
Binary file not shown.
Binary file not shown.
Binary file not shown.
View
@@ -54,10 +54,42 @@
@asset.name.should == 'Old Muppet Men Booing'
end
+ it 'should still work even when tags are empty and the name is weird' do
+ @asset = new_track('_ .mp3')
+ @asset.save
+ @asset.permalink.should == 'untitled'
+ @asset.name.should == 'untitled'
+ end
+
it 'should handle strange charsets / characters in title tags' do
@asset = new_track('japanese-characters.mp3')
+ @asset.title = ''
@asset.save
- @asset.name.should == 'Old Muppet Men Booing'
+ @asset.permalink.should == 'untitled' # name is still 01-\266Ե??\313"
+ end
+
+ it 'should handle empty name' do
+ @asset = new_track('japanese-characters.mp3')
+ @asset.save
+ @asset.permalink.should == "untitled" # name is 01-\266Ե??\313"
+ @asset.title = 'bee'
+ @asset.save
+ @asset.permalink.should == 'bee'
+ end
+
+ it 'should handle non-english filenames and default to untitled' do
+ @asset = new_track('中文測試.mp3')
+ @asset.save.should == true
+ @asset.permalink.should == 'untitled'
+ end
+
+ it 'should handle titles with only ???? and default to untitled' do
+ @asset = new_track('中文測試.mp3')
+ @asset.save.should == true
+ @asset.permalink.should == 'untitled'
+ @asset.title = "中文測試"
+ @asset.save.should == true
+ @asset.permalink.should == 'untitled'
end
it 'should use the mp3 tag1 title as name if present' do
View
@@ -10,6 +10,7 @@
config.use_instantiated_fixtures = false
config.fixture_path = RAILS_ROOT + '/spec/fixtures/'
+
# == Fixtures
#
# You can declare fixtures for each example_group like this:
@@ -1,27 +0,0 @@
-InPlaceEditor
-=============
-
-v0.9
-
-An improved in_place_editor method than that provided in rails.
-
-This plugin is drop-in compatible with the current/old in_place_editor method, but adds the ability to specify any option that the Ajax.InPlaceEditor control accepts.
-
-The current rails version is missing a lot of valid options for the control, but this plugin will accept any current and new parameters for the control.
-
-For example, the current rails version (< 2.0) doesn't allow the setting of onFailure for the control, but with this plugin, simply specify it as follows...
-:on_failure => "function(transport) {alert(\"Error: \" + transport.responseText.stripTags());}"
-
-The option translation simply converts underscored options to camelcase in the JS.
-eg. :on_failure -> onFailure, :rows -> rows, :highlight_color -> highlightColor
-
-* note
-highlightColor is the name of the option in the new rewritten version of the control, highlightcolor (no capital C) is used in the version 1.7 of scriptaculous and previous versions.
-
-Some options require quoting to be valid in the JS, such as :highlight_color. The 2 ways to specify the option are...
-
-1) Manually quote the option
-:highlight_color => "'#000000'"
-
-2) Use the :quoted option
-:quoted => {:highlight_color => '#000000'}
@@ -0,0 +1,22 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the permalink_fu plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the permalink_fu plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'InPlaceEditor'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -6,11 +6,16 @@ class << self
attr_accessor :translation_from
def escape(str)
- s = Iconv.iconv(translation_to, translation_from, str).to_s
+ begin # this can fail when its passed japanese characters
+ s = Iconv.iconv(translation_to, translation_from, str).to_s
+ rescue Iconv::InvalidCharacter
+ s = 'untitled'
+ end
s.gsub!(/\W+/, ' ') # all non-word chars to spaces
s.strip! # ohh la la
s.downcase! #
s.gsub!(/\ +/, '-') # spaces to dashes, preferred separator char everywhere
+ s = (s.blank? ? 'untitled' : s)
s
end
end
Oops, something went wrong.

0 comments on commit f37856f

Please sign in to comment.