Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for nil scope values (such as deleted_at), with unit test #198

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/stringex/acts_as_url/adapter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ def add_new_record_url_owner_conditions

def add_scoped_url_owner_conditions
[settings.scope_for_url].flatten.compact.each do |scope|
@url_owner_conditions.first << " and #{scope} = ?"
@url_owner_conditions << instance.send(scope)
scope_val = instance.send(scope)
sql_operator = scope_val.nil? ? "IS" : "="
@url_owner_conditions.first << " AND #{scope} #{sql_operator} ?"
@url_owner_conditions << scope_val
end
end

Expand Down
16 changes: 16 additions & 0 deletions test/unit/acts_as_url_integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,22 @@ def test_should_only_create_unique_urls_for_multiple_scopes_if_both_attributes_a
assert_not_equal @doc.url, @other_doc.url
end

def test_should_create_uniuque_urls_for_nil_scope_values
Document.class_eval do
acts_as_url :title, scope: %i[other, another]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Within %i/%I, ':' and ',' are unnecessary and may be unwanted in the resulting symbols.

end

@doc = Document.create(title: "Soft Deleted Document",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing whitespace detected.

other: "scope key",
another: nil)

@other_doc = Document.create(title: "Soft Deleted Document",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing whitespace detected.

other: "scope key",
another: nil)

assert_not_equal @doc.url, @other_doc.url
end

def test_should_allow_setting_url_attribute
Document.class_eval do
# Manually undefining the url method on Document which, in a real class not reused for tests,
Expand Down