Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit df205ab9
authored
Dec 29, 2015
by
Verdier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use has-one scope in getter and setter
1 parent
2c34249d
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
7 deletions
lib/associations/has-one.js
test/integration/associations/scope.test.js
lib/associations/has-one.js
View file @
df205ab
...
@@ -135,6 +135,10 @@ HasOne.prototype.injectGetter = function(instancePrototype) {
...
@@ -135,6 +135,10 @@ HasOne.prototype.injectGetter = function(instancePrototype) {
var
where
=
{};
var
where
=
{};
where
[
association
.
foreignKey
]
=
this
.
get
(
association
.
sourceIdentifier
);
where
[
association
.
foreignKey
]
=
this
.
get
(
association
.
sourceIdentifier
);
if
(
association
.
scope
)
{
_
.
assign
(
where
,
association
.
scope
);
}
options
=
association
.
target
.
__optClone
(
options
)
||
{};
options
=
association
.
target
.
__optClone
(
options
)
||
{};
options
.
where
=
{
options
.
where
=
{
...
@@ -191,7 +195,10 @@ HasOne.prototype.injectSetter = function(instancePrototype) {
...
@@ -191,7 +195,10 @@ HasOne.prototype.injectSetter = function(instancePrototype) {
isNewRecord
:
false
isNewRecord
:
false
});
});
}
}
_
.
assign
(
associatedInstance
,
association
.
scope
);
associatedInstance
.
set
(
association
.
foreignKey
,
instance
.
get
(
association
.
sourceIdentifier
));
associatedInstance
.
set
(
association
.
foreignKey
,
instance
.
get
(
association
.
sourceIdentifier
));
return
associatedInstance
.
save
(
options
);
return
associatedInstance
.
save
(
options
);
}
}
return
null
;
return
null
;
...
...
test/integration/associations/scope.test.js
View file @
df205ab
...
@@ -103,19 +103,34 @@ describe(Support.getTestDialectTeaser('associations'), function() {
...
@@ -103,19 +103,34 @@ describe(Support.getTestDialectTeaser('associations'), function() {
}).
then
(
function
(
comment
)
{
}).
then
(
function
(
comment
)
{
expect
(
comment
.
get
(
'commentable'
)).
to
.
equal
(
'post'
);
expect
(
comment
.
get
(
'commentable'
)).
to
.
equal
(
'post'
);
expect
(
comment
.
get
(
'isMain'
)).
to
.
be
.
false
;
expect
(
comment
.
get
(
'isMain'
)).
to
.
be
.
false
;
return
self
.
Post
.
scope
(
'withMainComment'
).
findById
(
this
.
post
.
get
(
'id'
));
return
this
.
Post
.
scope
(
'withMainComment'
).
findById
(
this
.
post
.
get
(
'id'
));
}).
then
(
function
(
post
)
{
}).
then
(
function
(
post
)
{
expect
(
post
.
mainComment
).
to
.
be
.
null
;
expect
(
post
.
mainComment
).
to
.
be
.
null
;
return
post
.
createMainComment
({
return
post
.
createMainComment
({
title
:
'I am a main post comment'
title
:
'I am a main post comment'
});
});
}).
then
(
function
(
c
omment
)
{
}).
then
(
function
(
mainC
omment
)
{
this
.
mainComment
=
c
omment
;
this
.
mainComment
=
mainC
omment
;
expect
(
c
omment
.
get
(
'commentable'
)).
to
.
equal
(
'post'
);
expect
(
mainC
omment
.
get
(
'commentable'
)).
to
.
equal
(
'post'
);
expect
(
c
omment
.
get
(
'isMain'
)).
to
.
be
.
true
;
expect
(
mainC
omment
.
get
(
'isMain'
)).
to
.
be
.
true
;
return
self
.
Post
.
scope
(
'withMainComment'
).
findById
(
this
.
post
.
id
);
return
this
.
Post
.
scope
(
'withMainComment'
).
findById
(
this
.
post
.
id
);
}).
then
(
function
(
post
)
{
}).
then
(
function
(
post
)
{
expect
(
post
.
mainComment
.
get
(
'id'
)).
to
.
equal
(
this
.
mainComment
.
get
(
'id'
));
expect
(
post
.
mainComment
.
get
(
'id'
)).
to
.
equal
(
this
.
mainComment
.
get
(
'id'
));
return
post
.
getMainComment
();
}).
then
(
function
(
mainComment
,
post
)
{
expect
(
mainComment
.
get
(
'commentable'
)).
to
.
equal
(
'post'
);
expect
(
mainComment
.
get
(
'isMain'
)).
to
.
be
.
true
;
return
this
.
Comment
.
create
({
title
:
'I am a future main comment'
});
}).
then
(
function
(
comment
)
{
return
this
.
post
.
setMainComment
(
comment
);
}).
then
(
function
()
{
return
this
.
post
.
getMainComment
();
}).
then
(
function
(
mainComment
)
{
expect
(
mainComment
.
get
(
'commentable'
)).
to
.
equal
(
'post'
);
expect
(
mainComment
.
get
(
'isMain'
)).
to
.
be
.
true
;
expect
(
mainComment
.
get
(
'title'
)).
to
.
equal
(
'I am a future main comment'
);
});
});
});
});
});
});
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment