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

Conversation

masonhale
Copy link

Current scope implementation fails if one or more of the scope attribute values is nil.

One may want to use nullable values as scope parameters when using stringex/acts_as_url in combination with a 'soft-delete' solution such as https://github.com/JackDanger/permanent_records

In this case, 'live' records will have a nil value for the deleted_at column, but deleted records will have a specific value. By including the deleted_at column in the acts_as_url scope, one can ensure that url slugs for live records will be unique, while also avoiding having 'deleted' records polluting the available URL space.

@@ -422,4 +434,5 @@ def url_taken?(url)
@doc = Document.create(title: "unique")
assert_equal "unique-3", @doc.url
end

Copy link
Collaborator

Choose a reason for hiding this comment

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

Extra empty line detected at class body end.

@doc = Document.create(title: "Mocumentary", other: "Suddenly, I care if I'm unique",
another: nil)
@other_doc = Document.create(title: "Mocumentary", other: "Suddenly, I care if I'm unique",
another: nil)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Align the elements of a hash literal if they span more than one line.


@doc = Document.create(title: "Mocumentary", other: "Suddenly, I care if I'm unique",
another: nil)
@other_doc = Document.create(title: "Mocumentary", other: "Suddenly, I care if I'm unique",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Line is too long. [95/80]

end

@doc = Document.create(title: "Mocumentary", other: "Suddenly, I care if I'm unique",
another: nil)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Align the elements of a hash literal if they span more than one line.

acts_as_url :title, scope: [:other, :another]
end

@doc = Document.create(title: "Mocumentary", other: "Suddenly, I care if I'm unique",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Line is too long. [89/80]

@@ -177,6 +177,18 @@ 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: [: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.

Use %i or %I for an array of symbols.

@url_owner_conditions << instance.send(scope)
scope_val = instance.send(scope)
cond_sql_operator = scope_val.nil? ? 'IS' : '='
@url_owner_conditions.first << " AND #{scope} #{cond_sql_operator} ?"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Line is too long. [81/80]

@url_owner_conditions.first << " and #{scope} = ?"
@url_owner_conditions << instance.send(scope)
scope_val = instance.send(scope)
cond_sql_operator = scope_val.nil? ? 'IS' : '='
Copy link
Collaborator

Choose a reason for hiding this comment

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

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

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.

acts_as_url :title, scope: %i[other, another]
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.

@@ -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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants