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

Execute default_scope defined by abstract class within the scope of subclass #23666

Merged
merged 1 commit into from
Mar 8, 2016

Conversation

meinac
Copy link
Contributor

@meinac meinac commented Feb 14, 2016

This commit will fix #23413 and #10658. In old behaviour it tries to execute block given by default_scope within the context of abstract class and in this case it can't find the table name. Now it tries to execute the block within subclass and everything works.

@rails-bot
Copy link

r? @pixeltrix

(@rails-bot has picked a reviewer for you, use r? to override)

@meinac
Copy link
Contributor Author

meinac commented Feb 14, 2016

I've fixed the failing tests but travis is not working, maybe we need to close and reopen this.
Also WDYT about this change @sgrif?

@swrobel
Copy link
Contributor

swrobel commented Feb 20, 2016

👏

@swrobel
Copy link
Contributor

swrobel commented Feb 20, 2016

r? @sgrif

@@ -115,7 +115,10 @@ def build_default_scope(base_rel = nil) # :nodoc:
base_rel ||= relation
evaluate_default_scope do
default_scopes.inject(base_rel) do |default_scope, scope|
default_scope.merge(base_rel.scoping { scope.call })
scoped = base_rel.scoping do
scope.is_a?(Proc) ? instance_exec(&scope) : scope.call
Copy link
Member

Choose a reason for hiding this comment

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

Is there any case where scope is not a Proc?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, any kind of object can be used in default scope if the object is callable. I saw this in Rails tests, for example in this case it uses OpenStruct.
But now I think that I have to handle the call method case also in correct context. Change is on the way.

@meinac meinac force-pushed the fix_abstract_default_scope_bug branch from 2add7c0 to 6600bd7 Compare March 1, 2016 18:31
@meinac
Copy link
Contributor Author

meinac commented Mar 1, 2016

@rafaelfranca I've changed the logic. If you have a look at the line you can see that we can use any kind of object which respond to call method as default scope and now we can handle it in correct context.
Thank you for looking into this pr.

@rafaelfranca
Copy link
Member

This looks good to me. r? @sgrif WDYT?

@rails-bot rails-bot assigned sgrif and unassigned pixeltrix Mar 1, 2016
@meinac meinac force-pushed the fix_abstract_default_scope_bug branch from 6600bd7 to 85555ad Compare March 2, 2016 21:32
@@ -115,7 +115,8 @@ def build_default_scope(base_rel = nil) # :nodoc:
base_rel ||= relation
evaluate_default_scope do
default_scopes.inject(base_rel) do |default_scope, scope|
default_scope.merge(base_rel.scoping { scope.call })
scope = scope.is_a?(Proc) ? scope : scope.method(:call)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be respond_to?(:to_proc)?

@meinac meinac force-pushed the fix_abstract_default_scope_bug branch from 85555ad to b5c7dfe Compare March 8, 2016 12:44
@meinac meinac force-pushed the fix_abstract_default_scope_bug branch from b5c7dfe to 0784dcc Compare March 8, 2016 12:56
@meinac
Copy link
Contributor Author

meinac commented Mar 8, 2016

@sgrif rebased & updated this.

sgrif added a commit that referenced this pull request Mar 8, 2016
Execute default_scope defined by abstract class within the scope of subclass
@sgrif sgrif merged commit da1fecc into rails:master Mar 8, 2016
@meinac meinac deleted the fix_abstract_default_scope_bug branch March 8, 2016 20:59
@prathamesh-sonpatki
Copy link
Member

@sgrif Should this be backported to 4-2-stable as per #23413 (comment) ?

rafaelfranca pushed a commit that referenced this pull request May 24, 2016
Execute default_scope defined by abstract class within the scope of subclass
rafaelfranca added a commit that referenced this pull request May 27, 2016
…ope_bug"

This reverts commit bd44967.

Reason: tests are broken after this change.
MichaelSp pushed a commit to MichaelSp/rails that referenced this pull request Nov 2, 2016
…_bug

Execute default_scope defined by abstract class within the scope of subclass
MichaelSp pushed a commit to MichaelSp/rails that referenced this pull request Nov 2, 2016
…lt_scope_bug"

This reverts commit bd44967.

Reason: tests are broken after this change.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Default scopes defined on abstract classes are missing table names when called from subclasses
8 participants