Skip to content

Commit

Permalink
added abstractions to help data mapper adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
rsl committed Jan 23, 2013
1 parent e6c30a3 commit 6e53ebb
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 44 deletions.
35 changes: 30 additions & 5 deletions lib/stringex/acts_as_url/adapter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ def initialize_urls!(klass)

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
if !is_new?(instance) && is_present?(instance.errors[settings.attribute_to_urlify])
self.instance = instance
read_attribute instance_from_db, settings.url_attribute
else
instance.read_attribute settings.url_attribute
read_attribute instance, settings.url_attribute
end
end

Expand Down Expand Up @@ -80,10 +81,26 @@ def handle_duplicate_url!

def handle_url!
self.base_url = instance.send(settings.url_attribute)
modify_base_url if base_url.blank? || !settings.only_when_blank
modify_base_url if is_blank?(base_url) || !settings.only_when_blank
write_url_attribute base_url
end

def instance_from_db
instance.class.find(instance.id)
end

def is_blank?(object)
object.blank?
end

def is_new?(object)
object.new_record?
end

def is_present?(object)
object.present?
end

def loadable?
self.class.loadable?
end
Expand All @@ -93,6 +110,10 @@ def modify_base_url
self.base_url = root.to_url(configuration.string_extensions_settings)
end

def read_attribute(instance, attribute)
instance.read_attribute attribute
end

def url_attribute_for(object)
object.send settings.url_attribute
end
Expand All @@ -107,8 +128,12 @@ def url_owners_class
klass
end

def write_attribute(instance, attribute, value)
instance.send :write_attribute, attribute, value
end

def write_url_attribute(value)
instance.send :write_attribute, settings.url_attribute, value
write_attribute instance, settings.url_attribute, value
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion test/acts_as_url/adapter/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
puts "Running ActsAsUrl tests with ActiveRecord adapter"
puts "-------------------------------------------------"

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

ActiveRecord::Migration.verbose = false
ActiveRecord::Schema.define do
Expand Down Expand Up @@ -65,4 +65,8 @@ def remove_validation_on_document_title
_validators.delete :title
end
end

def adapter_specific_update(instance, hash)
instance.send :update_attributes!, hash
end
end
4 changes: 4 additions & 0 deletions test/acts_as_url/adapter/mongoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,8 @@ def remove_validation_on_document_title
_validators.delete :title
end
end

def adapter_specific_update(instance, hash)
instance.send :update_attributes!, hash
end
end
76 changes: 38 additions & 38 deletions test/acts_as_url_integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@ def test_should_create_url
end

def test_should_create_unique_url
@doc = Document.create!(:title => "Unique")
@other_doc = Document.create!(:title => "Unique")
@doc = Document.create(:title => "Unique")
@other_doc = Document.create(:title => "Unique")
assert_equal "unique", @doc.url
assert_equal "unique-1", @other_doc.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")
@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_not_sync_url_by_default
@doc = Document.create!(:title => "Stable as Stone")
@doc = Document.create(:title => "Stable as Stone")
@original_url = @doc.url
@doc.update_attributes! :title => "New Unstable Madness"
adapter_specific_update @doc, :title => "New Unstable Madness"
assert_equal @original_url, @doc.url
end

Expand All @@ -39,9 +39,9 @@ def test_should_allow_syncing_url
acts_as_url :title, :sync_url => true
end

@doc = Document.create!(:title => "Original")
@doc = Document.create(:title => "Original")
@original_url = @doc.url
@doc.update_attributes! :title => "New and Improved"
adapter_specific_update @doc, :title => "New and Improved"
assert_not_equal @original_url, @doc.url
end

Expand All @@ -62,8 +62,8 @@ def test_should_allow_allowing_duplicate_url
acts_as_url :title, :allow_duplicates => true
end

@doc = Document.create!(:title => "I am not a clone")
@other_doc = Document.create!(:title => "I am not a clone")
@doc = Document.create(:title => "I am not a clone")
@other_doc = Document.create(:title => "I am not a clone")
assert_equal @doc.url, @other_doc.url
end

Expand All @@ -72,8 +72,8 @@ def test_should_allow_scoping_url_uniqueness
acts_as_url :title, :scope => :other
end

@doc = Document.create!(:title => "Mocumentary", :other => "I don't care if I'm unique for some reason")
@other_doc = Document.create!(:title => "Mocumentary", :other => "Me either")
@doc = Document.create(:title => "Mocumentary", :other => "I don't care if I'm unique for some reason")
@other_doc = Document.create(:title => "Mocumentary", :other => "Me either")
assert_equal @doc.url, @other_doc.url
end

Expand All @@ -82,8 +82,8 @@ def test_should_still_create_unique_urls_if_scoped_attribute_is_the_same
acts_as_url :title, :scope => :other
end

@doc = Document.create!(:title => "Mocumentary", :other => "Suddenly, I care if I'm unique")
@other_doc = Document.create!(:title => "Mocumentary", :other => "Suddenly, I care if I'm unique")
@doc = Document.create(:title => "Mocumentary", :other => "Suddenly, I care if I'm unique")
@other_doc = Document.create(:title => "Mocumentary", :other => "Suddenly, I care if I'm unique")
assert_not_equal @doc.url, @other_doc.url
end

Expand All @@ -95,7 +95,7 @@ def test_should_allow_setting_url_attribute
acts_as_url :title, :url_attribute => :other
end

@doc = Document.create!(:title => "Anything at This Point")
@doc = Document.create(:title => "Anything at This Point")
assert_equal "anything-at-this-point", @doc.other
assert_nil @doc.url
ensure
Expand All @@ -111,17 +111,17 @@ def test_should_allow_updating_url_only_when_blank
end

@string = 'the-url-of-concrete'
@doc = Document.create!(:title => "Stable as Stone", :url => @string)
@doc = Document.create(:title => "Stable as Stone", :url => @string)
assert_equal @string, @doc.url
@other_doc = Document.create!(:title => "Stable as Stone")
@other_doc = Document.create(:title => "Stable as Stone")
assert_equal 'stable-as-stone', @other_doc.url
end

def test_should_mass_initialize_urls
@doc = Document.create!(:title => "Initial")
@other_doc = Document.create!(:title => "Subsequent")
@doc.update_attributes! :url => nil
@other_doc.update_attributes! :url => nil
@doc = Document.create(:title => "Initial")
@other_doc = Document.create(:title => "Subsequent")
adapter_specific_update @doc, :url => nil
adapter_specific_update @other_doc, :url => nil
# Just making sure this got unset before the real test
assert_nil @doc.url
assert_nil @other_doc.url
Expand All @@ -142,10 +142,10 @@ def test_should_mass_initialize_urls_with_custom_url_attribute
acts_as_url :title, :url_attribute => :other
end

@doc = Document.create!(:title => "Initial")
@other_doc = Document.create!(:title => "Subsequent")
@doc.update_attributes! :other => nil
@other_doc.update_attributes! :other => nil
@doc = Document.create(:title => "Initial")
@other_doc = Document.create(:title => "Subsequent")
adapter_specific_update @doc, :other => nil
adapter_specific_update @other_doc, :other => nil
# Just making sure this got unset before the real test
assert_nil @doc.other
assert_nil @other_doc.other
Expand All @@ -172,7 +172,7 @@ def non_attribute_method
end
end

@doc = Document.create!(:title => "Title String")
@doc = Document.create(:title => "Title String")
assert_equal "title-string-got-massaged", @doc.url
ensure
Document.class_eval do
Expand All @@ -186,8 +186,8 @@ def test_should_allow_customizing_duplicate_count_separator
acts_as_url :title, :duplicate_count_separator => "---"
end

@doc = Document.create!(:title => "Unique")
@other_doc = Document.create!(:title => "Unique")
@doc = Document.create(:title => "Unique")
@other_doc = Document.create(:title => "Unique")
assert_equal "unique", @doc.url
assert_equal "unique---1", @other_doc.url
end
Expand All @@ -198,7 +198,7 @@ def test_should_only_update_url_if_url_attribute_is_valid
end
add_validation_on_document_title

@doc = Document.create!(:title => "Valid Record", :other => "Present")
@doc = Document.create(:title => "Valid Record", :other => "Present")
assert_equal "valid-record", @doc.url
@doc.title = nil
assert_equal false, @doc.valid?
Expand All @@ -212,7 +212,7 @@ def test_should_allow_customizing_url_limit
acts_as_url :title, :limit => 13
end

@doc = Document.create!(:title => "I am much too long")
@doc = Document.create(:title => "I am much too long")
assert_equal "i-am-much-too", @doc.url
end

Expand All @@ -221,9 +221,9 @@ def test_handling_duplicate_urls_with_limits
acts_as_url :title, :limit => 13
end

@doc = Document.create!(:title => "I am much too long and also duplicated")
@doc = Document.create(:title => "I am much too long and also duplicated")
assert_equal "i-am-much-too", @doc.url
@other_doc = Document.create!(:title => "I am much too long and also duplicated")
@other_doc = Document.create(:title => "I am much too long and also duplicated")
assert_equal "i-am-much-too-1", @other_doc.url
end

Expand All @@ -232,9 +232,9 @@ def test_should_allow_excluding_specific_values_from_being_run_through_to_url
acts_as_url :title, :exclude => ["_So_Fucking_Special"]
end

@doc = Document.create!(:title => "_So_Fucking_Special")
@doc = Document.create(:title => "_So_Fucking_Special")
assert_equal "_So_Fucking_Special", @doc.url
@doc_2 = Document.create!(:title => "But I'm a creep")
@doc_2 = Document.create(:title => "But I'm a creep")
assert_equal "but-im-a-creep", @doc_2.url
end

Expand All @@ -243,7 +243,7 @@ def test_should_allow_not_forcing_downcasing
acts_as_url :title, :force_downcase => false
end

@doc = Document.create!(:title => "I have CAPS!")
@doc = Document.create(:title => "I have CAPS!")
assert_equal "I-have-CAPS", @doc.url
end

Expand All @@ -252,7 +252,7 @@ def test_should_allow_alternate_whitespace_replacements
acts_as_url :title, :replace_whitespace_with => "~"
end

@doc = Document.create!(:title => "now with tildes")
@doc = Document.create(:title => "now with tildes")
assert_equal "now~with~tildes", @doc.url
end

Expand All @@ -261,9 +261,9 @@ def test_should_allow_enforcing_uniqueness_on_sti_base_class
acts_as_url :title, :enforce_uniqueness_on_sti_base_class => true
end

@doc = STIChildDocument.create!(:title => "Unique")
@doc = STIChildDocument.create(:title => "Unique")
assert_equal "unique", @doc.url
@doc_2 = AnotherSTIChildDocument.create!(:title => "Unique")
@doc_2 = AnotherSTIChildDocument.create(:title => "Unique")
assert_equal "unique-1", @doc_2.url
end
end

0 comments on commit 6e53ebb

Please sign in to comment.