Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
cancan
Manage
Activity
Members
Plan
External wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Github Mirror
coopdevs
cancan
Commits
f7a494dc
Commit
f7a494dc
authored
14 years ago
by
Ryan Bates
Browse files
Options
Downloads
Patches
Plain Diff
switching mongoid over to new adapter
parent
f5dce446
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
lib/cancan/model_adapters/mongoid_adapter.rb
+19
-32
19 additions, 32 deletions
lib/cancan/model_adapters/mongoid_adapter.rb
spec/cancan/model_adapters/mongoid_adapter_spec.rb
+8
-1
8 additions, 1 deletion
spec/cancan/model_adapters/mongoid_adapter_spec.rb
with
27 additions
and
33 deletions
lib/cancan/model_adapters/mongoid_adapter.rb
+
19
−
32
View file @
f7a494dc
module
CanCan
module
Ability
# could use alias_method_chain, but it's not worth adding activesupport as a gem dependency
alias_method
:query_without_mongoid_support
,
:query
def
query
(
action
,
subject
)
if
defined?
(
::
Mongoid
)
&&
subject
<=
CanCan
::
MongoidAdditions
query_with_mongoid_support
(
action
,
subject
)
else
query_without_mongoid_support
(
action
,
subject
)
module
ModelAdapters
class
MongoidAdapter
<
AbstractAdapter
def
self
.
for_class?
(
model_class
)
model_class
<=
CanCan
::
MongoidAdditions
# there should be a better class to detect with this
end
end
def
query_with_mongoid_support
(
action
,
subject
)
MongoidQuery
.
new
(
subject
,
relevant_rules_for_query
(
action
,
subject
))
end
end
class
MongoidQuery
def
initialize
(
sanitizer
,
rules
)
@sanitizer
=
sanitizer
@rules
=
rules
end
def
database_records
@model_class
.
where
(
conditions
)
end
def
conditions
if
@rules
.
size
==
0
false_query
else
@rules
.
first
.
instance_variable_get
(
:@conditions
)
def
conditions
if
@rules
.
size
==
0
false_query
else
@rules
.
first
.
conditions
end
end
end
def
false_query
# this query is sure to return no results
{
:_id
=>
{
'$exists'
=>
false
,
'$type'
=>
7
}}
# type 7 is an ObjectID (default for _id)
def
false_query
# this query is sure to return no results
{
:_id
=>
{
'$exists'
=>
false
,
'$type'
=>
7
}}
# type 7 is an ObjectID (default for _id)
end
end
end
...
...
@@ -62,8 +51,6 @@ module CanCan
alias_method
:matches_conditions_hash?
,
:matches_conditions_hash_with_mongoid_subject?
end
module
MongoidAdditions
module
ClassMethods
# Returns a scope which fetches only the records that the passed ability
...
...
@@ -84,8 +71,7 @@ module CanCan
# Here only the articles which the user can update are returned. This
# internally uses Ability#conditions method, see that for more information.
def
accessible_by
(
ability
,
action
=
:read
)
query
=
ability
.
query
(
action
,
self
)
where
(
query
.
conditions
)
ability
.
model_adapter
(
self
,
action
).
database_records
end
end
...
...
@@ -97,6 +83,7 @@ end
# Info on monkeypatching Mongoid :
# http://log.mazniak.org/post/719062325/monkey-patching-activesupport-concern-and-you#footer
# This link is now broken, anyone know what it was referring to?
if
defined?
(
::
Mongoid
)
module
Mongoid
module
Components
...
...
This diff is collapsed.
Click to expand it.
spec/cancan/model_adapters/mongoid_adapter_spec.rb
+
8
−
1
View file @
f7a494dc
...
...
@@ -37,7 +37,7 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
config
.
master
=
Mongo
::
Connection
.
new
(
'127.0.0.1'
,
27017
).
db
(
"cancan_mongoid_spec"
)
end
describe
CanCan
::
Mo
ngoidAdditions
do
describe
CanCan
::
Mo
delAdapters
::
MongoidAdapter
do
context
"Mongoid not defined"
do
before
(
:all
)
do
@mongoid_class
=
Object
.
send
(
:remove_const
,
:Mongoid
)
...
...
@@ -46,6 +46,7 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
after
(
:all
)
do
Object
.
const_set
(
:Mongoid
,
@mongoid_class
)
end
it
"should not raise an error for ActiveRecord models"
do
@model_class
=
Class
.
new
(
Project
)
stub
(
@model_class
).
scoped
{
:scoped_stub
}
...
...
@@ -72,6 +73,12 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
end
.
each
(
&
:drop
)
end
it
"should be for only Mongoid classes"
do
CanCan
::
ModelAdapters
::
MongoidAdapter
.
should_not
be_for_class
(
Object
)
CanCan
::
ModelAdapters
::
MongoidAdapter
.
should
be_for_class
(
@model_class
)
CanCan
::
ModelAdapters
::
AbstractAdapter
.
adapter_class
(
@model_class
).
should
==
CanCan
::
ModelAdapters
::
MongoidAdapter
end
it
"should compare properties on mongoid documents with the conditions hash"
do
model
=
@model_class
.
new
@ability
.
can
:read
,
@model_class
,
:id
=>
model
.
id
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment