Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
ta_ransack_mongo
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open-source
ta_ransack_mongo
Commits
bef5ab56
Commit
bef5ab56
authored
Oct 11, 2014
by
Pablo Cantero
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix lt gt comparison with Date & Time
parent
cd210689
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
41 additions
and
26 deletions
+41
-26
mongo_adapter.rb
lib/ransack_mongo/mongo_adapter.rb
+20
-8
mongo_adapter_spec.rb
spec/ransack_mongo/mongo_adapter_spec.rb
+21
-18
No files found.
lib/ransack_mongo/mongo_adapter.rb
View file @
bef5ab56
...
...
@@ -27,23 +27,19 @@ module RansackMongo
end
def
gt_matcher
(
attr
,
value
)
@query
[
attr
]
||=
{}
@query
[
attr
][
'$gt'
]
=
value
.
to_f
append_sizeable_matcher
(
'$gt'
,
attr
,
value
)
end
def
lt_matcher
(
attr
,
value
)
@query
[
attr
]
||=
{}
@query
[
attr
][
'$lt'
]
=
value
.
to_f
append_sizeable_matcher
(
'$lt'
,
attr
,
value
)
end
def
gteq_matcher
(
attr
,
value
)
@query
[
attr
]
||=
{}
@query
[
attr
][
'$gte'
]
=
value
.
to_f
append_sizeable_matcher
(
'$gte'
,
attr
,
value
)
end
def
lteq_matcher
(
attr
,
value
)
@query
[
attr
]
||=
{}
@query
[
attr
][
'$lte'
]
=
value
.
to_f
append_sizeable_matcher
(
'$lte'
,
attr
,
value
)
end
def
or_op
# or operation
...
...
@@ -63,5 +59,21 @@ module RansackMongo
def
self
.
predicates
PREDICATES
end
private
def
append_sizeable_matcher
(
m
,
attr
,
value
)
@query
[
attr
]
||=
{}
@query
[
attr
][
m
]
=
parse_sizeable_value
(
value
)
end
def
parse_sizeable_value
(
value
)
case
value
when
Date
,
Time
value
else
Float
(
value
)
rescue
value
end
end
end
end
spec/ransack_mongo/mongo_adapter_spec.rb
View file @
bef5ab56
...
...
@@ -48,35 +48,38 @@ module RansackMongo
end
end
describe
'#gt_matcher'
do
%w[gt lt gteq lteq]
.
each
do
|
m
|
op_name
=
{
'gteq'
=>
'gte'
,
'lteq'
=>
'lte'
}[
m
]
||
m
describe
"#
#{
m
}
_matcher"
do
it
'returns the matcher'
do
subject
.
gt_matcher
(
'quantity'
,
1
)
subject
.
send
"
#{
m
}
_matcher"
,
'quantity'
,
1
expect
(
subject
.
to_query
).
to
eq
(
'quantity'
=>
{
'$gt'
=>
1
})
end
expect
(
subject
.
to_query
).
to
eq
(
'quantity'
=>
{
"$
#{
op_name
}
"
=>
1
})
end
describe
'#lt_matcher
'
do
it
'returns the matcher'
do
subject
.
lt_matcher
(
'quantity'
,
1
)
it
'accepts time
'
do
updated_at
=
Time
.
now
subject
.
send
"
#{
m
}
_matcher"
,
'updated_at'
,
updated_at
expect
(
subject
.
to_query
).
to
eq
(
'quantity'
=>
{
'$lt'
=>
1
})
end
expect
(
subject
.
to_query
).
to
eq
(
'updated_at'
=>
{
"$
#{
op_name
}
"
=>
updated_at
})
end
describe
'#gteq_matcher'
do
it
'returns the matcher'
do
subject
.
gteq_matcher
(
'quantity'
,
1
)
it
'accepts time as a string'
do
updated_at
=
'2014-10-11 14:48:07 -0300'
expect
(
subject
.
to_query
).
to
eq
(
'quantity'
=>
{
'$gte'
=>
1
})
end
subject
.
send
"
#{
m
}
_matcher"
,
'updated_at'
,
updated_at
expect
(
subject
.
to_query
).
to
eq
(
'updated_at'
=>
{
"$
#{
op_name
}
"
=>
updated_at
})
end
describe
'#lteq_matcher'
do
it
'returns the matcher'
do
subject
.
lteq_matcher
(
'quantity'
,
1
)
it
'accepts date'
do
updated_at
=
Date
.
new
expect
(
subject
.
to_query
).
to
eq
(
'quantity'
=>
{
'$lte'
=>
1
})
subject
.
send
"
#{
m
}
_matcher"
,
'updated_at'
,
updated_at
expect
(
subject
.
to_query
).
to
eq
(
'updated_at'
=>
{
"$
#{
op_name
}
"
=>
updated_at
})
end
end
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
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 comment