-
Notifications
You must be signed in to change notification settings - Fork 92
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
Filter tomatoes by creation date #255
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,12 +5,8 @@ class TomatoesController < BaseController | |
|
||
def index | ||
@tomatoes = current_user.tomatoes | ||
if from = Time.zone.parse(params[:from].to_s) | ||
@tomatoes = @tomatoes.after(from) | ||
end | ||
if to = Time.zone.parse(params[:to].to_s) | ||
@tomatoes = @tomatoes.before(to) | ||
end | ||
@tomatoes = @tomatoes.after(from) if from | ||
@tomatoes = @tomatoes.before(to) if to | ||
@tomatoes = @tomatoes.order_by([[:created_at, :desc], [:_id, :desc]]).page params[:page] | ||
|
||
render json: Presenter::Tomatoes.new(@tomatoes) | ||
|
@@ -46,6 +42,14 @@ def destroy | |
|
||
private | ||
|
||
def from | ||
@from ||= Time.zone.parse(params[:from].to_s) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does it handle nil? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should work! :) 2.3.3 :001 > nil.to_s
=> ""
2.3.3 :002 > Time.zone.parse ''
=> nil There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that's how. |
||
end | ||
|
||
def to | ||
@to ||= Time.zone.parse(params[:to].to_s) | ||
end | ||
|
||
def find_tomato | ||
@tomato = current_user.tomatoes.find(params[:id]) | ||
end | ||
|
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.
Why don't you create a scope for sorting tomatoes?