Skip to content

Commit

Permalink
refactored tests to make the class changes more transparent
Browse files Browse the repository at this point in the history
  • Loading branch information
rsl committed Dec 21, 2012
1 parent d1c2ad5 commit 170907d
Show file tree
Hide file tree
Showing 7 changed files with 274 additions and 277 deletions.
4 changes: 3 additions & 1 deletion lib/stringex/acts_as_url.rb
Expand Up @@ -30,14 +30,16 @@ module ActsAsUrlClassMethods # :doc:
# unless you are using multiple ORMs in a single project. # unless you are using multiple ORMs in a single project.
# <tt>:allow_slash</tt>:: If true, allow the generated url to contain slashes. Default is false[y]. # <tt>:allow_slash</tt>:: If true, allow the generated url to contain slashes. Default is false[y].
# <tt>:allow_duplicates</tt>:: If true, allow duplicate urls instead of appending numbers to # <tt>:allow_duplicates</tt>:: If true, allow duplicate urls instead of appending numbers to
# differentiate between urls. Default is false[y]. # differentiate between urls. Default is false[y]. See note on <tt>:scope</tt>.
# <tt>:duplicate_count_separator</tt>:: String to use when forcing unique urls from non-unique strings. # <tt>:duplicate_count_separator</tt>:: String to use when forcing unique urls from non-unique strings.
# Default is "-". # Default is "-".
# <tt>:exclude_list</tt>:: List of complete strings that should not be transformed by <tt>acts_as_url</tt>. # <tt>:exclude_list</tt>:: List of complete strings that should not be transformed by <tt>acts_as_url</tt>.
# Default is empty. # Default is empty.
# <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[y] (meaning url generation will happen always). # blank. Default is false[y] (meaning url generation will happen always).
# <tt>:scope</tt>:: The name of model attribute to scope unique urls to. There is no default here. # <tt>:scope</tt>:: The name of model attribute to scope unique urls to. There is no default here.
# <strong>Note:</strong> this will automatically act as if <tt>:allow_duplicates</tt>
# is set to true.
# <tt>:sync_url</tt>:: If set to true, the url field will be updated when changes are made to the # <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[y]. # attribute it is based on. Default is false[y].
# <tt>:url_attribute</tt>:: The name of the attribute to use for storing the generated url string. # <tt>:url_attribute</tt>:: The name of the attribute to use for storing the generated url string.
Expand Down
8 changes: 8 additions & 0 deletions lib/stringex/acts_as_url/adapter/active_record.rb
Expand Up @@ -26,6 +26,14 @@ def add_scoped_url_owner_conditions
@url_owner_conditions << instance.send(settings.scope_for_url) @url_owner_conditions << instance.send(settings.scope_for_url)
end end


def create_callback
if settings.sync_url
klass.before_validation :ensure_unique_url
else
klass.before_validation :ensure_unique_url, :on => :create
end
end

# NOTE: The <tt>instance</tt> here is not the cached instance but a block variable # NOTE: The <tt>instance</tt> here is not the cached instance but a block variable
# passed from <tt>klass_previous_instances</tt>, just to be clear # passed from <tt>klass_previous_instances</tt>, just to be clear
def ensure_unique_url_for!(instance) def ensure_unique_url_for!(instance)
Expand Down
27 changes: 12 additions & 15 deletions lib/stringex/acts_as_url/adapter/base.rb
Expand Up @@ -11,21 +11,9 @@ def initialize(configuration)
end end


def create_callbacks!(klass) def create_callbacks!(klass)
if settings.sync_url self.klass = klass
klass.before_validation :ensure_unique_url create_method_to_callback
else create_callback
if defined?(ActiveModel::Callbacks)
klass.before_validation :ensure_unique_url, :on => :create
else
klass.before_validation_on_create :ensure_unique_url
end
end

klass.class_eval <<-"END"
def #{settings.url_attribute}
acts_as_url_configuration.adapter.url_attribute self
end
END
end end


def ensure_unique_url!(instance) def ensure_unique_url!(instance)
Expand All @@ -44,6 +32,7 @@ def initialize_urls!(klass)
end end


def url_attribute(instance) def url_attribute(instance)
# Retrieve from database record if there are errors on attribute_to_urlify
if !instance.new_record? && instance.errors[settings.attribute_to_urlify].present? if !instance.new_record? && instance.errors[settings.attribute_to_urlify].present?
instance.class.find(instance.id).send settings.url_attribute instance.class.find(instance.id).send settings.url_attribute
else else
Expand All @@ -62,6 +51,14 @@ def self.loadable?


private private


def create_method_to_callback
klass.class_eval <<-"END"
def #{settings.url_attribute}
acts_as_url_configuration.adapter.url_attribute self
end
END
end

def duplicate_for_base_url(n) def duplicate_for_base_url(n)
"#{base_url}#{settings.duplicate_count_separator}#{n}" "#{base_url}#{settings.duplicate_count_separator}#{n}"
end end
Expand Down
8 changes: 8 additions & 0 deletions lib/stringex/acts_as_url/adapter/mongoid.rb
Expand Up @@ -24,6 +24,14 @@ def add_scoped_url_owner_conditions
@url_owner_conditions.merge! settings.scope_for_url => instance.send(settings.scope_for_url) @url_owner_conditions.merge! settings.scope_for_url => instance.send(settings.scope_for_url)
end end


def create_callback
if settings.sync_url
klass.before_validation :ensure_unique_url
else
klass.before_validation :ensure_unique_url, :on => :create
end
end

# NOTE: The <tt>instance</tt> here is not the cached instance but a block variable # NOTE: The <tt>instance</tt> here is not the cached instance but a block variable
# passed from <tt>klass_previous_instances</tt>, just to be clear # passed from <tt>klass_previous_instances</tt>, just to be clear
def ensure_unique_url_for!(instance) def ensure_unique_url_for!(instance)
Expand Down
130 changes: 55 additions & 75 deletions test/acts_as_url/adapter/active_record.rb
Expand Up @@ -14,106 +14,86 @@
ActiveRecord::Migration.verbose = false ActiveRecord::Migration.verbose = false
ActiveRecord::Schema.define do ActiveRecord::Schema.define do
create_table :documents, :force => true do |t| create_table :documents, :force => true do |t|
t.string :title, :url, :other t.string :title, :other, :url
end end


create_table :updatuments, :force => true do |t| # create_table :permuments, :force => true do |t|
t.string :title, :url, :other # t.string :title, :permalink
end # end

create_table :mocuments, :force => true do |t|
t.string :title, :url, :other
end

create_table :permuments, :force => true do |t|
t.string :title, :permalink
end

create_table :procuments, :force => true do |t|
t.string :title, :url
end

create_table :blankuments, :force => true do |t|
t.string :title, :url
end

create_table :duplicatuments, :force => true do |t|
t.string :title, :url
end

create_table :validatuments, :force => true do |t|
t.string :title, :url
end

create_table :ununiquments, :force => true do |t|
t.string :title, :url
end

create_table :limituments, :force => true do |t|
t.string :title, :url
end

create_table :skipuments, :force => true do |t|
t.string :title, :url
end
end end
ActiveRecord::Migration.verbose = true ActiveRecord::Migration.verbose = true


class Document < ActiveRecord::Base class Document < ActiveRecord::Base
acts_as_url :title acts_as_url :title
end end


class Updatument < ActiveRecord::Base # class Updatument < ActiveRecord::Base
acts_as_url :title, :sync_url => true # acts_as_url :title, :sync_url => true
end # end


class Mocument < ActiveRecord::Base # class Mocument < ActiveRecord::Base
acts_as_url :title, :scope => :other, :sync_url => true # acts_as_url :title, :scope => :other, :sync_url => true
end # end


class Permument < ActiveRecord::Base # class Permument < ActiveRecord::Base
acts_as_url :title, :url_attribute => :permalink # acts_as_url :title, :url_attribute => :permalink
end # end


class Procument < ActiveRecord::Base # class Procument < ActiveRecord::Base
acts_as_url :non_attribute_method # acts_as_url :non_attribute_method


def non_attribute_method # def non_attribute_method
"#{title} got massaged" # "#{title} got massaged"
end # end
end # end


class Blankument < ActiveRecord::Base # class Blankument < ActiveRecord::Base
acts_as_url :title, :only_when_blank => true # acts_as_url :title, :only_when_blank => true
end # end


class Duplicatument < ActiveRecord::Base # class Duplicatument < ActiveRecord::Base
acts_as_url :title, :duplicate_count_separator => "---" # acts_as_url :title, :duplicate_count_separator => "---"
end # end


class Validatument < ActiveRecord::Base # class Validatument < ActiveRecord::Base
acts_as_url :title, :sync_url => true # acts_as_url :title, :sync_url => true
validates_presence_of :title # validates_presence_of :title
end # end


class Ununiqument < ActiveRecord::Base # class Ununiqument < ActiveRecord::Base
acts_as_url :title, :allow_duplicates => true # acts_as_url :title, :allow_duplicates => true
end # end


class Limitument < ActiveRecord::Base # class Limitument < ActiveRecord::Base
acts_as_url :title, :limit => 13 # acts_as_url :title, :limit => 13
end # end


class Skipument < ActiveRecord::Base # class Skipument < ActiveRecord::Base
acts_as_url :title, :exclude => ["_So_Fucking_Special"] # acts_as_url :title, :exclude => ["_So_Fucking_Special"]
end # end


module AdapterSpecificTestBehaviors module AdapterSpecificTestBehaviors
def setup def setup
# No setup tasks at present # No setup tasks at present
end end


def teardown def teardown
# No teardown tasks at present Document.delete_all
# Reset behavior to default
Document.class_eval do
acts_as_url :title
end
end

def add_validation_on_document_title
Document.class_eval do
validates_presence_of :title
end
end

def remove_validation_on_document_title
Document.class_eval do
_validators.delete :title
end
end end
end end
105 changes: 16 additions & 89 deletions test/acts_as_url/adapter/mongoid.rb
Expand Up @@ -22,101 +22,28 @@ class Document
acts_as_url :title acts_as_url :title
end end


class Updatument
include Mongoid::Document
field :title, :type => String
field :other, :type => String
field :url, :type => String

acts_as_url :title, :sync_url => true
end

class Mocument
include Mongoid::Document
field :title, :type => String
field :other, :type => String
field :url, :type => String

acts_as_url :title, :scope => :other, :sync_url => true
end

class Permument
include Mongoid::Document
field :title, :type => String
field :url, :type => String

acts_as_url :title, :url_attribute => :permalink
end

class Procument
include Mongoid::Document
field :title, :type => String
field :url, :type => String

acts_as_url :non_attribute_method

def non_attribute_method
"#{title} got massaged"
end
end

class Blankument
include Mongoid::Document
field :title, :type => String
field :url, :type => String

acts_as_url :title, :only_when_blank => true
end

class Duplicatument
include Mongoid::Document
field :title, :type => String
field :url, :type => String

acts_as_url :title, :duplicate_count_separator => "---"
end

class Validatument
include Mongoid::Document
field :title, :type => String
field :url, :type => String

acts_as_url :title, :sync_url => true
validates_presence_of :title
end

class Ununiqument
include Mongoid::Document
field :title, :type => String
field :url, :type => String

acts_as_url :title, :allow_duplicates => true
end

class Limitument
include Mongoid::Document
field :title, :type => String
field :url, :type => String

acts_as_url :title, :limit => 13
end

class Skipument
include Mongoid::Document
field :title, :type => String
field :url, :type => String

acts_as_url :title, :exclude => ["_So_Fucking_Special"]
end

module AdapterSpecificTestBehaviors module AdapterSpecificTestBehaviors
def setup def setup
# No setup tasks at present # No setup tasks at present
end end


def teardown def teardown
Mongoid.default_session.collections.each do |collection| Document.delete_all
collection.drop # Reset behavior to default
Document.class_eval do
acts_as_url :title
end
end

def add_validation_on_document_title
Document.class_eval do
validates_presence_of :title
end
end

def remove_validation_on_document_title
Document.class_eval do
_validators.delete :title
end end
end end
end end

0 comments on commit 170907d

Please sign in to comment.