Skip to content
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

Merged
merged 4 commits into from
Feb 5, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix rubocop offenses
potomak committed Feb 5, 2017
commit 36a38c79bae76a51314df5f9f03f02f93d45ffbd
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@ Lint/UnderscorePrefixedVariableName:

# Offense count: 6
Metrics/AbcSize:
Max: 33
Max: 33.56

# Offense count: 1
# Configuration parameters: CountComments.
16 changes: 10 additions & 6 deletions app/controllers/api/tomatoes_controller.rb
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]
Copy link
Member

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?


render json: Presenter::Tomatoes.new(@tomatoes)
@@ -46,6 +42,14 @@ def destroy

private

def from
@from ||= Time.zone.parse(params[:from].to_s)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does it handle nil?

Copy link
Member

Choose a reason for hiding this comment

The 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 

Copy link
Member Author

@potomak potomak Feb 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's how. #parse should work on any string, it returns nil in case of parsing failure. #to_s will convert any params[:from] to a string.

end

def to
@to ||= Time.zone.parse(params[:to].to_s)
end

def find_tomato
@tomato = current_user.tomatoes.find(params[:id])
end
2 changes: 1 addition & 1 deletion test/functional/api/tomatoes_controller_test.rb
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ class TomatoesControllerTest < ActionController::TestCase
@tomato1.created_at = 3.hours.ago
@tomato1.save!
@tomato2 = @user.tomatoes.build(tag_list: 'one, two')
@tomato2.created_at = 2.hour.ago
@tomato2.created_at = 2.hours.ago
@tomato2.save!
@tomato3 = @user.tomatoes.build(tag_list: 'three, four')
@tomato3.created_at = 1.hour.ago