Permalink
Browse files

refactored tests to make the class changes more transparent

  • Loading branch information...
1 parent d1c2ad5 commit 170907dc9508eb5a8e07527f34b395a07230c529 @rsl committed Dec 21, 2012
@@ -30,14 +30,16 @@ module ActsAsUrlClassMethods # :doc:
# 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_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.
# Default is "-".
# <tt>:exclude_list</tt>:: List of complete strings that should not be transformed by <tt>acts_as_url</tt>.
# Default is empty.
# <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).
# <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
# 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.
@@ -26,6 +26,14 @@ def add_scoped_url_owner_conditions
@url_owner_conditions << instance.send(settings.scope_for_url)
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
# passed from <tt>klass_previous_instances</tt>, just to be clear
def ensure_unique_url_for!(instance)
@@ -11,21 +11,9 @@ def initialize(configuration)
end
def create_callbacks!(klass)
- if settings.sync_url
- klass.before_validation :ensure_unique_url
- else
- 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
+ self.klass = klass
+ create_method_to_callback
+ create_callback
end
def ensure_unique_url!(instance)
@@ -44,6 +32,7 @@ def initialize_urls!(klass)
end
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?
instance.class.find(instance.id).send settings.url_attribute
else
@@ -62,6 +51,14 @@ def self.loadable?
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)
"#{base_url}#{settings.duplicate_count_separator}#{n}"
end
@@ -24,6 +24,14 @@ def add_scoped_url_owner_conditions
@url_owner_conditions.merge! settings.scope_for_url => instance.send(settings.scope_for_url)
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
# passed from <tt>klass_previous_instances</tt>, just to be clear
def ensure_unique_url_for!(instance)
@@ -14,106 +14,86 @@
ActiveRecord::Migration.verbose = false
ActiveRecord::Schema.define do
create_table :documents, :force => true do |t|
- t.string :title, :url, :other
+ t.string :title, :other, :url
end
- create_table :updatuments, :force => true do |t|
- t.string :title, :url, :other
- 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
+ # create_table :permuments, :force => true do |t|
+ # t.string :title, :permalink
+ # end
end
ActiveRecord::Migration.verbose = true
class Document < ActiveRecord::Base
acts_as_url :title
end
-class Updatument < ActiveRecord::Base
- acts_as_url :title, :sync_url => true
-end
+# class Updatument < ActiveRecord::Base
+# acts_as_url :title, :sync_url => true
+# end
-class Mocument < ActiveRecord::Base
- acts_as_url :title, :scope => :other, :sync_url => true
-end
+# class Mocument < ActiveRecord::Base
+# acts_as_url :title, :scope => :other, :sync_url => true
+# end
-class Permument < ActiveRecord::Base
- acts_as_url :title, :url_attribute => :permalink
-end
+# class Permument < ActiveRecord::Base
+# acts_as_url :title, :url_attribute => :permalink
+# end
-class Procument < ActiveRecord::Base
- acts_as_url :non_attribute_method
+# class Procument < ActiveRecord::Base
+# acts_as_url :non_attribute_method
- def non_attribute_method
- "#{title} got massaged"
- end
-end
+# def non_attribute_method
+# "#{title} got massaged"
+# end
+# end
-class Blankument < ActiveRecord::Base
- acts_as_url :title, :only_when_blank => true
-end
+# class Blankument < ActiveRecord::Base
+# acts_as_url :title, :only_when_blank => true
+# end
-class Duplicatument < ActiveRecord::Base
- acts_as_url :title, :duplicate_count_separator => "---"
-end
+# class Duplicatument < ActiveRecord::Base
+# acts_as_url :title, :duplicate_count_separator => "---"
+# end
-class Validatument < ActiveRecord::Base
- acts_as_url :title, :sync_url => true
- validates_presence_of :title
-end
+# class Validatument < ActiveRecord::Base
+# acts_as_url :title, :sync_url => true
+# validates_presence_of :title
+# end
-class Ununiqument < ActiveRecord::Base
- acts_as_url :title, :allow_duplicates => true
-end
+# class Ununiqument < ActiveRecord::Base
+# acts_as_url :title, :allow_duplicates => true
+# end
-class Limitument < ActiveRecord::Base
- acts_as_url :title, :limit => 13
-end
+# class Limitument < ActiveRecord::Base
+# acts_as_url :title, :limit => 13
+# end
-class Skipument < ActiveRecord::Base
- acts_as_url :title, :exclude => ["_So_Fucking_Special"]
-end
+# class Skipument < ActiveRecord::Base
+# acts_as_url :title, :exclude => ["_So_Fucking_Special"]
+# end
module AdapterSpecificTestBehaviors
def setup
# No setup tasks at present
end
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
@@ -22,101 +22,28 @@ class Document
acts_as_url :title
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
def setup
# No setup tasks at present
end
def teardown
- Mongoid.default_session.collections.each do |collection|
- collection.drop
+ 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
Oops, something went wrong.

0 comments on commit 170907d

Please sign in to comment.