Last active
December 16, 2015 22:09
-
-
Save cirdes/5505287 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Concerns::Titleizable | |
extend ActiveSupport::Concern | |
included do | |
before_validation :to_titlecase | |
before_validation :to_downcase | |
class << self | |
attr_accessor :to_titleize | |
attr_accessor :to_be_downcased | |
end | |
end | |
module ClassMethods | |
def titleize(*to_titleize) | |
self.to_titleize = to_titleize | |
end | |
def downcase(*to_downcase) | |
self.to_be_downcased = to_downcase | |
end | |
end | |
def to_titlecase | |
to_titleize = self.class.to_titleize | |
to_titleize.each do | word | | |
if self.send(word) | |
titleized = self.send(word).mb_chars.titlecase.strip.normalize | |
self.send("#{word}=", titleized) | |
end | |
end | |
end | |
def to_downcase | |
to_down = self.class.to_be_downcased | |
to_down.each do | word | | |
if self.send(word) | |
downcased = self.send(word).mb_chars.downcase.strip.normalize | |
self.send("#{word}=", downcased) | |
end | |
end | |
end | |
end | |
require 'spec_helper' | |
class FakeModel | |
def self.before_validation(to_titlecase) | |
end | |
include Concerns::Titleizable | |
titleize :name | |
downcase :title | |
attr_accessor :name, :title | |
def initialize(name, title) | |
@name = name | |
@title = title | |
end | |
def save | |
to_titlecase | |
to_downcase | |
end | |
end | |
describe Concerns::Titleizable do | |
let(:model) { FakeModel.new("mussum ipsum cacilds, vidis litro abertis." , "Me Faiz Elementum Girarzis" ) } | |
subject { model } | |
describe '#save' do | |
before { model.save } | |
its(:name) { should eq("Mussum Ipsum Cacilds, Vidis Litro Abertis.")} | |
its(:title) { should eq("me faiz elementum girarzis")} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment