Skip to content

Commit

Permalink
codings and whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
RSL committed Oct 20, 2010
1 parent 9a6e884 commit 4fdab4b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 33 deletions.
20 changes: 11 additions & 9 deletions lib/lucky_sneaks/acts_as_url.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
# coding: utf-8

module LuckySneaks
module ActsAsUrl # :nodoc:
def self.included(base)
base.extend ClassMethods
end

module ClassMethods # :doc:
# Creates a callback to automatically create an url-friendly representation
# of the <tt>attribute</tt> argument. Example:
#
#
# act_as_url :title
#
#
# will use the string contents of the <tt>title</tt> attribute
# to create the permalink. <strong>Note:</strong> you can also use a non-database-backed
# method to supply the string contents for the permalink. Just use that method's name
# as the argument as you would an attribute.
#
#
# The default attribute <tt>acts_as_url</tt> uses to save the permalink is <tt>url</tt>
# but this can be changed in the options hash. Available options are:
#
#
# <tt>:url_attribute</tt>:: The name of the attribute to use for storing the generated url string.
# Default is <tt>:url</tt>
# <tt>:scope</tt>:: The name of model attribute to scope unique urls to. There is no default here.
# <tt>:only_when_blank</tt>:: If true, the url generation will only happen when <tt>:url_attribute</tt> is
# <tt>:only_when_blank</tt>:: If true, the url generation will only happen when <tt>:url_attribute</tt> is
# blank. Default is false (meaning url generation will happen always)
# <tt>:sync_url</tt>:: If set to true, the url field will be updated when changes are made to the
# attribute it is based on. Default is false.
Expand All @@ -31,7 +33,7 @@ def acts_as_url(attribute, options = {})
cattr_accessor :url_attribute # The attribute on the DB
cattr_accessor :only_when_blank
cattr_accessor :duplicate_count_separator

if options[:sync_url]
before_validation :ensure_unique_url
else
Expand All @@ -49,7 +51,7 @@ def acts_as_url(attribute, options = {})
# Initialize the url fields for the records that need it. Designed for people who add
# <tt>acts_as_url</tt> support once there's already development/production data they'd
# like to keep around.
#
#
# Note: This method can get very expensive, very fast. If you're planning on using this
# on a large selection, you will get much better results writing your own version with
# using pagination.
Expand All @@ -60,7 +62,7 @@ def initialize_urls
end
end
end

private
def ensure_unique_url
url_attribute = self.class.url_attribute
Expand Down
18 changes: 10 additions & 8 deletions lib/lucky_sneaks/unidecoder.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# coding: utf-8

require "yaml"

module LuckySneaks
Expand All @@ -6,10 +8,10 @@ module Unidecoder
CODEPOINTS = Hash.new { |h, k|
h[k] = YAML::load_file(File.join(File.dirname(__FILE__), "unidecoder_data", "#{k}.yml"))
} unless defined?(CODEPOINTS)

class << self
# Returns string with its UTF-8 characters transliterated to ASCII ones
#
#
# You're probably better off just using the added String#to_ascii
def decode(string)
string.gsub(/[^\x00-\x7f]/u) do |codepoint|
Expand All @@ -22,25 +24,25 @@ def decode(string)
end
end
end

# Returns character for the given Unicode codepoint
def encode(codepoint)
["0x#{codepoint}".to_i(16)].pack("U")
end

# Returns string indicating which file (and line) contains the
# transliteration value for the character
def in_yaml_file(character)
unpacked = character.unpack("U")[0]
"#{code_group(unpacked)}.yml (line #{grouped_point(unpacked) + 2})"
end

private
# Returns the Unicode codepoint grouping for the given character
def code_group(unpacked_character)
"x%02x" % (unpacked_character >> 8)
end

# Returns the index of the given character in the YAML file for its codepoint group
def grouped_point(unpacked_character)
unpacked_character & 255
Expand All @@ -51,8 +53,8 @@ def grouped_point(unpacked_character)

module LuckySneaks
module StringExtensions
# Returns string with its UTF-8 characters transliterated to ASCII ones. Example:
#
# Returns string with its UTF-8 characters transliterated to ASCII ones. Example:
#
# "⠋⠗⠁⠝⠉⠑".to_ascii #=> "braille"
def to_ascii
LuckySneaks::Unidecoder.decode(self)
Expand Down
34 changes: 18 additions & 16 deletions test/acts_as_url_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
require 'rubygems'
gem 'activerecord'
require 'active_record'
RAILS_ROOT = File.dirname(__FILE__)

RAILS_ROOT = File.dirname(__FILE__)
$: << File.join(File.dirname(__FILE__), '../lib')
end

require File.join(File.dirname(__FILE__), '../init')
# puts Dir.entries(File.join(File.dirname(__FILE__), ".."))
puts File.file?(File.join(File.dirname(__FILE__), '../init.rb'))
require File.join(File.dirname(__FILE__), '../init.rb')

ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => "acts_as_url.sqlite3")

Expand All @@ -28,7 +30,7 @@
create_table :mocuments, :force => true do |t|
t.string :title, :url, :other
end

create_table :permuments, :force => true do |t|
t.string :title, :permalink, :other
end
Expand Down Expand Up @@ -65,7 +67,7 @@ class Permument < ActiveRecord::Base

class Procument < ActiveRecord::Base
acts_as_url :non_attribute_method

def non_attribute_method
"#{title} got massaged"
end
Expand All @@ -84,7 +86,7 @@ def test_should_create_url
@doc = Document.create(:title => "Let's Make a Test Title, <em>Okay</em>?")
assert_equal "lets-make-a-test-title-okay", @doc.url
end

def test_should_create_unique_url
@doc = Document.create!(:title => "Unique")
@other_doc = Document.create!(:title => "Unique")
Expand Down Expand Up @@ -116,54 +118,54 @@ def test_should_create_unique_url_and_not_clobber_if_another_exists
assert_equal "foo", @doc2.other
assert_equal "twonique-1", @other_doc2.url
end

def test_should_create_unique_url_when_partial_url_already_exists
@doc = Document.create!(:title => "House Farms")
@other_doc = Document.create!(:title => "House Farm")

assert_equal "house-farms", @doc.url
assert_equal "house-farm", @other_doc.url
end

def test_should_scope_uniqueness
@moc = Mocument.create!(:title => "Mocumentary", :other => "I dunno why but I don't care if I'm unique")
@other_moc = Mocument.create!(:title => "Mocumentary")
assert_equal @moc.url, @other_moc.url
end

def test_should_still_create_unique_if_in_same_scope
@moc = Mocument.create!(:title => "Mocumentary", :other => "Suddenly, I care if I'm unique")
@other_moc = Mocument.create!(:title => "Mocumentary", :other => "Suddenly, I care if I'm unique")
assert_not_equal @moc.url, @other_moc.url
end

def test_should_use_alternate_field_name
@perm = Permument.create!(:title => "Anything at This Point")
assert_equal "anything-at-this-point", @perm.permalink
end

def test_should_not_update_url_by_default
@doc = Document.create!(:title => "Stable as Stone")
@original_url = @doc.url
@doc.update_attributes :title => "New Unstable Madness"
assert_equal @original_url, @doc.url
end

def test_should_update_url_if_asked
@moc = Mocument.create!(:title => "Original")
@original_url = @moc.url
@moc.update_attributes :title => "New and Improved"
assert_not_equal @original_url, @moc.url
end

def test_should_update_url_only_when_blank_if_asked
@original_url = 'the-url-of-concrete'
@blank = Blankument.create!(:title => "Stable as Stone", :url => @original_url)
assert_equal @original_url, @blank.url
@blank = Blankument.create!(:title => "Stable as Stone")
assert_equal 'stable-as-stone', @blank.url
end

def test_should_mass_initialize_urls
@doc_1 = Document.create!(:title => "Initial")
@doc_2 = Document.create!(:title => "Subsequent")
Expand All @@ -177,7 +179,7 @@ def test_should_mass_initialize_urls
assert_equal "initial", @doc_1.url
assert_equal "subsequent", @doc_2.url
end

def test_should_mass_initialize_urls_with_custom_url_attribute
@doc_1 = Permument.create!(:title => "Initial")
@doc_2 = Permument.create!(:title => "Subsequent")
Expand All @@ -191,7 +193,7 @@ def test_should_mass_initialize_urls_with_custom_url_attribute
assert_equal "initial", @doc_1.permalink
assert_equal "subsequent", @doc_2.permalink
end

def test_should_utilize_block_if_given
@doc = Procument.create!(:title => "Title String")
assert_equal "title-string-got-massaged", @doc.url
Expand Down

0 comments on commit 4fdab4b

Please sign in to comment.