Add support for nil scope values (such as deleted_at), with unit test#198
Add support for nil scope values (such as deleted_at), with unit test#198masonhale wants to merge 3 commits intorsl:masterfrom
Conversation
| @doc = Document.create(title: "unique") | ||
| assert_equal "unique-3", @doc.url | ||
| end | ||
|
|
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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", |
There was a problem hiding this comment.
Line is too long. [95/80]
| end | ||
|
|
||
| @doc = Document.create(title: "Mocumentary", other: "Suddenly, I care if I'm unique", | ||
| another: nil) |
There was a problem hiding this comment.
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", |
There was a problem hiding this comment.
Line is too long. [89/80]
|
|
||
| def test_should_create_uniuque_urls_for_nil_scope_values | ||
| Document.class_eval do | ||
| acts_as_url :title, scope: [:other, :another] |
There was a problem hiding this comment.
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} ?" |
There was a problem hiding this comment.
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' : '=' |
There was a problem hiding this comment.
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", |
There was a problem hiding this comment.
Trailing whitespace detected.
| acts_as_url :title, scope: %i[other, another] | ||
| end | ||
|
|
||
| @doc = Document.create(title: "Soft Deleted Document", |
There was a problem hiding this comment.
Trailing whitespace detected.
|
|
||
| def test_should_create_uniuque_urls_for_nil_scope_values | ||
| Document.class_eval do | ||
| acts_as_url :title, scope: %i[other, another] |
There was a problem hiding this comment.
Within %i/%I, ':' and ',' are unnecessary and may be unwanted in the resulting symbols.
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.