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

Filter tomatoes by creation date #255

merged 4 commits into from
Feb 5, 2017

Conversation

potomak
Copy link
Member

@potomak potomak commented Feb 5, 2017

Closes #241.

@potomak potomak added this to the 0.14 milestone Feb 5, 2017
@potomak potomak requested review from dalpo and bugant February 5, 2017 05:31
@@ -39,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.

Copy link
Member

@dalpo dalpo left a comment

Choose a reason for hiding this comment

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

For me is ok!

The only one thing I'd like to change is creating a common scope for sorting tomatoes.

The common use case is sorting tomatoes by creation:
So we can create something like this:

scope :sorted_desc, -> { order_by([[:created_at, :desc], [:_id, :desc]]) }

@@ -39,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.

It should work! :)

2.3.3 :001 > nil.to_s
 => ""
2.3.3 :002 > Time.zone.parse ''
 => nil 

@tomatoes = current_user.tomatoes
@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?

@@ -75,7 +75,7 @@ def create
if @tomato.save
format.js do
@highlight = @tomato
@tomatoes = current_user.tomatoes.after(Time.zone.now.beginning_of_day)
@tomatoes = current_user.tomatoes.after(Time.zone.now.beginning_of_day).order_by([[:created_at, :desc]])
Copy link
Member

Choose a reason for hiding this comment

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

If you create a scope for sorting, you can reuse it here too :)

@potomak
Copy link
Member Author

potomak commented Feb 5, 2017

Yes @dalpo, I wanted to create such a scope, but I didn't find a good name for it. sorted_desc is not descriptive enough. I thought of reverse_chronological_order, but it's too long. I though of latest_first or freshest_first, but they have first in them that seem like a selection of the first element. I thought of created_at_desc, but it wouldn't include the second sorting condition on _id.

@dalpo
Copy link
Member

dalpo commented Feb 5, 2017

I like sorted by #freshest tomatoes 😄
...but It's not a good name.

Maybe simply: Tomato.descendants? I think it's self explanatory enough.

@potomak
Copy link
Member Author

potomak commented Feb 5, 2017

@dalpo naming is too hard! I'll leave this task for another PR. I created a proposal where we can discuss on an appropriate name for the scope: #256.

@potomak potomak merged commit d11177a into master Feb 5, 2017
@potomak potomak deleted the filter-tomatoes-by-date branch February 5, 2017 19:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants