Permalink
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...
1 parent 0e31465 commit 18d64b4d3a2b15348e0a1a57a9b2842a66a86994 @pat committed Feb 17, 2014
@@ -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
@@ -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'
@@ -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
@@ -1,5 +1,5 @@
class Gutentag::TagName
- def self.normalise(name)
+ def self.call(name)
new(name).to_s
end
@@ -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'
@@ -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
@@ -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')

0 comments on commit 18d64b4

Please sign in to comment.