-
Notifications
You must be signed in to change notification settings - Fork 29
DOCSP-44954: scoping #60
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
35c33cf
DOCSP-44954: scoping
rustagir 7d37c4d
add landing page
rustagir d245948
Merge branch 'standardized' of github.com:mongodb/docs-mongoid into D…
rustagir cf1464e
link
rustagir 28e3a9d
vale
rustagir acfb65b
highlighting
rustagir 78f7284
MR PR fixes 1
rustagir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
# start-named-scope-1 | ||
class Band | ||
include Mongoid::Document | ||
|
||
field :country, type: String | ||
field :genres, type: Array | ||
|
||
scope :japanese, ->{ where(country: "Japan") } | ||
scope :rock, ->{ where(:genres.in => [ "rock" ]) } | ||
end | ||
# end-named-scope-1 | ||
|
||
# start-query-named-scope | ||
Band.japanese.rock | ||
# end-query-named-scope | ||
|
||
# start-named-scope-2 | ||
class Band | ||
include Mongoid::Document | ||
|
||
field :name, type: String | ||
field :country, type: String | ||
|
||
scope :based_in, ->(country){ where(country: country) } | ||
end | ||
# end-named-scope-2 | ||
|
||
# start-query-named-scope-2 | ||
Band.based_in("Spain") | ||
# end-query-named-scope-2 | ||
|
||
# start-named-scope-3 | ||
class Band | ||
include Mongoid::Document | ||
|
||
def self.on_tour | ||
true | ||
end | ||
|
||
scope :on_tour, ->{ where(on_tour: true) } | ||
end | ||
# end-named-scope-3 | ||
|
||
# start-default-scope-1 | ||
class Band | ||
include Mongoid::Document | ||
|
||
field :name, type: String | ||
field :active, type: Boolean | ||
|
||
default_scope ->{ where(active: true) } | ||
end | ||
# end-default-scope-1 | ||
|
||
# start-default-scope-2 | ||
class Band | ||
include Mongoid::Document | ||
|
||
field :name, type: String | ||
field :on_tour, type: Boolean, default: true | ||
|
||
default_scope ->{ where(on_tour: false) } | ||
end | ||
|
||
# Creates a new Band instance in which "on_tour" is "false" | ||
Band.new | ||
# end-default-scope-2 | ||
|
||
# start-unscoped | ||
# Inline example | ||
Band.unscoped.where(name: "Depeche Mode") | ||
|
||
# Block example | ||
Band.unscoped do | ||
Band.where(name: "Depeche Mode") | ||
end | ||
# end-unscoped | ||
|
||
# start-scope-association | ||
class Label | ||
include Mongoid::Document | ||
|
||
field :name, type: String | ||
|
||
embeds_many :bands | ||
end | ||
|
||
class Band | ||
include Mongoid::Document | ||
|
||
field :name, type: String | ||
field :active, default: true | ||
|
||
embedded_in :label | ||
default_scope ->{ where(active: true) } | ||
end | ||
# end-scope-association | ||
|
||
# start-scope-association-steps | ||
label = Label.new(name: "Hello World Records") | ||
band = Band.new(name: "Ghost Mountain") | ||
label.bands.push(band) | ||
label.bands # Displays the Band because "active" is "true" | ||
band.update_attribute(:active, false) # Updates "active" to "false" | ||
label.bands # Still displays the Band | ||
label.reload.bands # Won't display the Band after reloading | ||
|
||
# end-scope-association-steps | ||
|
||
# start-scope-query-behavior | ||
class Band | ||
include Mongoid::Document | ||
|
||
field :name | ||
field :touring | ||
field :member_count | ||
|
||
default_scope ->{ where(touring: true) } | ||
end | ||
|
||
# Combines the condition to the default scope with "and" | ||
Band.where(name: 'Infected Mushroom') | ||
# Interpreted query: | ||
# {"touring"=>true, "name"=>"Infected Mushroom"} | ||
|
||
# Combines the first condition to the default scope with "and" | ||
Band.where(name: 'Infected Mushroom').or(member_count: 3) | ||
# Interpreted query: | ||
# {"$or"=>[{"touring"=>true, "name"=>"Infected Mushroom"}, {"member_count"=>3}]} | ||
|
||
# Combines the condition to the default scope with "or" | ||
Band.or(member_count: 3) | ||
# Interpreted query: | ||
# {"$or"=>[{"touring"=>true}, {"member_count"=>3}]} | ||
# end-scope-query-behavior | ||
|
||
# start-override-scope | ||
class Band | ||
include Mongoid::Document | ||
|
||
field :country, type: String | ||
field :genres, type: Array | ||
|
||
scope :mexican, ->{ where(country: "Mexico") } | ||
end | ||
# end-override-scope | ||
|
||
# start-override-scope-block | ||
Band.with_scope(Band.mexican) do | ||
Band.all | ||
end | ||
# end-override-scope-block | ||
|
||
# start-class-methods | ||
class Band | ||
include Mongoid::Document | ||
|
||
field :name, type: String | ||
field :touring, type: Boolean, default: true | ||
|
||
def self.touring | ||
where(touring: true) | ||
end | ||
end | ||
|
||
Band.touring | ||
# end-class-methods |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Q (not familiar with syntax): is
default_scope
a keyword here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is the way to define a default scope. I can make this more clear in the content