Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow for custom tag name normalisers.

The default is lowercased, but you can customise it:

    Gutentag.normaliser = lambda { |name| name.upcase }

As long as the setting responds to `call`, you should be fine.
  • Loading branch information...
commit 18d64b4d3a2b15348e0a1a57a9b2842a66a86994 1 parent 0e31465
@pat authored
View
4 app/models/gutentag/tag.rb
@@ -13,7 +13,7 @@ class Gutentag::Tag < ActiveRecord::Base
before_validation :normalise_name
def self.find_by_name(name)
- where(:name => Gutentag::TagName.normalise(name)).first
+ where(:name => Gutentag.normaliser.call(name)).first
end
def self.find_or_create(name)
@@ -23,6 +23,6 @@ def self.find_or_create(name)
private
def normalise_name
- self.name = Gutentag::TagName.normalise name
+ self.name = Gutentag.normaliser.call name
end
end
View
8 lib/gutentag.rb
@@ -1,5 +1,11 @@
module Gutentag
- #
+ def self.normaliser
+ @normaliser ||= Gutentag::TagName
+ end
+
+ def self.normaliser=(normaliser)
+ @normaliser = normaliser
+ end
end
require 'gutentag/active_record'
View
2  lib/gutentag/persistence.rb
@@ -40,6 +40,6 @@ def tagger
end
def normaliser
- @normaliser ||= Proc.new { |name| Gutentag::TagName.normalise(name) }
+ @normaliser ||= Proc.new { |name| Gutentag.normaliser.call(name) }
end
end
View
2  lib/gutentag/tag_name.rb
@@ -1,5 +1,5 @@
class Gutentag::TagName
- def self.normalise(name)
+ def self.call(name)
new(name).to_s
end
View
8 spec/acceptance/tag_names_spec.rb
@@ -18,6 +18,14 @@
article.tags.collect(&:name).should == ['melbourne']
end
+ it "allows for different tag normalisation" do
+ Gutentag.normaliser = lambda { |name| name.upcase }
+
+ Gutentag::Tag.create(:name => 'melbourne').name.should == 'MELBOURNE'
+
+ Gutentag.normaliser = nil
+ end
+
it "doesn't complain when adding an existing tag" do
article.tag_names << 'melbourne'
article.tag_names << 'melbourne'
View
4 spec/gutentag/tag_name_spec.rb
@@ -1,9 +1,9 @@
require 'spec_helper'
describe Gutentag::TagName do
- describe '.normalise' do
+ describe '.call' do
it "downcases the provided name" do
- Gutentag::TagName.normalise('Tasty Pancakes').should == 'tasty pancakes'
+ Gutentag::TagName.call('Tasty Pancakes').should == 'tasty pancakes'
end
end
end
View
4 spec/models/gutentag/tag_spec.rb
@@ -39,11 +39,11 @@
describe '#name' do
before :each do
- Gutentag::TagName.stub :normalise => 'waffles'
+ Gutentag::TagName.stub :call => 'waffles'
end
it "normalises the provided name" do
- Gutentag::TagName.should_receive(:normalise).with('Pancakes').
+ Gutentag::TagName.should_receive(:call).with('Pancakes').
and_return('waffles')
Gutentag::Tag.create!(:name => 'Pancakes')
Please sign in to comment.
Something went wrong with that request. Please try again.