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
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
30 deletions
+45
-30
mongo_adapter.rb
lib/ransack_mongo/mongo_adapter.rb
+20
-8
mongo_adapter_spec.rb
spec/ransack_mongo/mongo_adapter_spec.rb
+25
-22
No files found.
lib/ransack_mongo/mongo_adapter.rb
View file @
bef5ab56
...
@@ -27,23 +27,19 @@ module RansackMongo
...
@@ -27,23 +27,19 @@ module RansackMongo
end
end
def
gt_matcher
(
attr
,
value
)
def
gt_matcher
(
attr
,
value
)
@query
[
attr
]
||=
{}
append_sizeable_matcher
(
'$gt'
,
attr
,
value
)
@query
[
attr
][
'$gt'
]
=
value
.
to_f
end
end
def
lt_matcher
(
attr
,
value
)
def
lt_matcher
(
attr
,
value
)
@query
[
attr
]
||=
{}
append_sizeable_matcher
(
'$lt'
,
attr
,
value
)
@query
[
attr
][
'$lt'
]
=
value
.
to_f
end
end
def
gteq_matcher
(
attr
,
value
)
def
gteq_matcher
(
attr
,
value
)
@query
[
attr
]
||=
{}
append_sizeable_matcher
(
'$gte'
,
attr
,
value
)
@query
[
attr
][
'$gte'
]
=
value
.
to_f
end
end
def
lteq_matcher
(
attr
,
value
)
def
lteq_matcher
(
attr
,
value
)
@query
[
attr
]
||=
{}
append_sizeable_matcher
(
'$lte'
,
attr
,
value
)
@query
[
attr
][
'$lte'
]
=
value
.
to_f
end
end
def
or_op
# or operation
def
or_op
# or operation
...
@@ -63,5 +59,21 @@ module RansackMongo
...
@@ -63,5 +59,21 @@ module RansackMongo
def
self
.
predicates
def
self
.
predicates
PREDICATES
PREDICATES
end
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
end
end
spec/ransack_mongo/mongo_adapter_spec.rb
View file @
bef5ab56
...
@@ -48,35 +48,38 @@ module RansackMongo
...
@@ -48,35 +48,38 @@ module RansackMongo
end
end
end
end
describe
'#gt_matcher'
do
%w[gt lt gteq lteq]
.
each
do
|
m
|
it
'returns the matcher'
do
op_name
=
{
'gteq'
=>
'gte'
,
'lteq'
=>
'lte'
}[
m
]
||
m
subject
.
gt_matcher
(
'quantity'
,
1
)
expect
(
subject
.
to_query
).
to
eq
(
'quantity'
=>
{
'$gt'
=>
1
})
describe
"#
#{
m
}
_matcher"
do
end
it
'returns the matcher'
do
end
subject
.
send
"
#{
m
}
_matcher"
,
'quantity'
,
1
describe
'#lt_matcher'
do
expect
(
subject
.
to_query
).
to
eq
(
'quantity'
=>
{
"$
#{
op_name
}
"
=>
1
})
it
'returns the matcher'
do
end
subject
.
lt_matcher
(
'quantity'
,
1
)
expect
(
subject
.
to_query
).
to
eq
(
'quantity'
=>
{
'$lt'
=>
1
})
it
'accepts time'
do
end
updated_at
=
Time
.
now
end
subject
.
send
"
#{
m
}
_matcher"
,
'updated_at'
,
updated_at
describe
'#gteq_matcher'
do
expect
(
subject
.
to_query
).
to
eq
(
'updated_at'
=>
{
"$
#{
op_name
}
"
=>
updated_at
})
it
'returns the matcher'
do
end
subject
.
gteq_matcher
(
'quantity'
,
1
)
expect
(
subject
.
to_query
).
to
eq
(
'quantity'
=>
{
'$gte'
=>
1
})
it
'accepts time as a string'
do
end
updated_at
=
'2014-10-11 14:48:07 -0300'
end
describe
'#lteq_matcher'
do
subject
.
send
"
#{
m
}
_matcher"
,
'updated_at'
,
updated_at
it
'returns the matcher'
do
subject
.
lteq_matcher
(
'quantity'
,
1
)
expect
(
subject
.
to_query
).
to
eq
(
'updated_at'
=>
{
"$
#{
op_name
}
"
=>
updated_at
})
end
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
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