Skip to content

Conversation

@hjoo
Copy link
Contributor

@hjoo hjoo commented Feb 16, 2015

Created a ':time' option for '#touch' to address issue #18905. Not sure if there is a better method for implementing optional arguments.

@recursive-madman
Copy link
Contributor

Actually there is: keyword arguments:

def touch(*names, time: nil)
  if time
    # ...
  end
end

Alternatively, you can use Array#extract_options!, which active support adds to the core Array class:

def touch(*names)
  options = names.extract_options!
  if options[:time]
    # ...
  end
end

Choose a reason for hiding this comment

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

I think you should be able to use keyword arguments as @recursive-madman commented, something like this:

def touch(*names, time: current_time_from_proper_timezone)

And then change the local var from current_time to this new time one, should allow you to remove the need for the unless condition. What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

Ruby style here should have spaces after the curly braces

time_opt, names = args.partition{ |arg| arg.kind_of?(Hash) && arg.has_key?(:time) }

@hjoo
Copy link
Contributor Author

hjoo commented Feb 16, 2015

Thanks for the feedback! I added time as a keyword argument and made sure the indentation was 2 spaces.

@eileencodes
Copy link
Member

This is looking great @hjoo - we need a couple more items to make this PR complete.

Please add an entry to the Active Record CHANGELOG.md noting that you can now set a time in #touch. This also needs a test using the new argument option. Additionally the documentation above the touch method should show how to use the time argument.

Copy link
Contributor

Choose a reason for hiding this comment

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

why not current_time = time?

Copy link
Contributor

Choose a reason for hiding this comment

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

It's easier to use the time argument and reassigning it to current_time doesn't improve clarity 😉

@eileencodes
Copy link
Member

@hjoo I know we didn't go over this at the Open Academy hackathon - Commit messages should be 50 characters and additional description should be after that. So a better commit message would be:

Add `time` option to `#touch`

Fixes #18905. `#touch` now takes time as an option. Setting the option
saves the record with the updated_at/on attributes set to the current time 
or the time specified. Updated tests and documentation accordingly. 

The idea being that GitHub could go away tomorrow but your commit messages are forever. Updating the changelog doesn't need to be noted, btw. Also, now someone knows what this change is doing without going to github. 😄

To update the commit message you can do:
git commit --amend

After editing and saving the new commit message force push again to your branch.
git push -f origin your-branch

@hjoo hjoo force-pushed the time_option branch 3 times, most recently from 2a01aee to c0809f2 Compare February 18, 2015 15:16
Fixes rails#18905. `#touch` now takes time as an option. Setting the option
saves the record with the updated_at/on attributes set to the current time
or the time specified. Updated tests and documentation accordingly.
eileencodes added a commit that referenced this pull request Feb 18, 2015
Add `time` option to `#touch
@eileencodes eileencodes merged commit 83be869 into rails:master Feb 18, 2015
@jeremy
Copy link
Member

jeremy commented Feb 18, 2015

🎉 Congrats @hjoo !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants