Git Product home page Git Product logo

time_splitter's Introduction

TimeSplitter

Build Status Gem Version Code Climate

Setting DateTimes can be a difficult or ugly thing, especially through a web form. Finding a good DatePicker or TimePicker is easy, but getting them to work on both can be difficult. TimeSplitter automatically generates accessors for date, time, hour, and min on your datetime or time attributes, making it trivial to use different form inputs to set different parts of a datetime field.

This gem is based on SplitDatetime by Michi Huber. TimeSplitter improves on the gem, updating for Rails 4, adding time accessors, and providing a safer and more consistent default setting.

Install

Standalone

$ gem install time_splitter

Gemfile

In your Gemfile:

gem "time_splitter"

After bundling, assuming you have an Event model with a starts_at attribute, add this to your model:

class Event < ActiveRecord::Base
  extend TimeSplitter::Accessors
  split_accessor :starts_at
end

In your view:

<%= simple_form_for @event do |f| %>
  <%= f.input :starts_at_date, as: :string, input_html: { class: 'datepicker' } %>
  <%= f.input :starts_at_hour, collection: 0..24 %>
  <%= f.input :starts_at_min, collection: [0, 15, 30, 45] %>
  <%= f.input :starts_at_time, as: :time
  <%= ... %>
<% end %>

Add your js datepicker and you're good to go. (Of course, this also works with standard Rails form helpers).

If you are using Rails < 4.0 and/or are not using StrongParameters, you must add attr_accessible for any of the split attributes you want to permit mass-assignment. TimeSplitter provides the methods that can be directly accessed, but will not automatically whitelist any of them for mass-assignment.

Options

By default, the read accessors provided by TimeSplitter are as follows:

starts_at_date #=> Date
starts_at_time #=> Time or Timey class used in :default option
starts_at_hour #=> Fixnum
starts_at_min  #=> Fixnum

You can override the default read format for date and time if you so choose, though doing so may not work well with certain form input types.

split_accessor :starts_at, date_format: "%D", time_format: "%I:%M%p"

starts_at_date #=> String "2013-10-13"
starts_at_time #=> String "01:44PM"

See Time#strftime for formats.

You can specify multiple datetime fields to split:

split_accessor :starts_at, :ends_at, :expires_at, format: "%D"

You can specify a default timey object to write. If starts_at is nil, which it would be at the time of a new or create call, TimeSplitter will use the default value as the basepoint for modification.

split_accessor :starts_at, default: -> { DateTime.current }

# model = Model.new(starts_at_time: '09:00')
# model.starts_at
# => Thu, 10 Oct 2013 09:00:00 -0400 # DateTime

The default time object is Time.new(0, 1, 1, 0, 0, 0, '+00:00').

Note that TimeSplitter does not handle seconds at this time, and from testing it appears they are set to zero when modifying them.

time_splitter's People

Contributors

makaio avatar njpearman avatar shekibobo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

time_splitter's Issues

Way to validate date/time components

I have a field called start_at, and I'm using start_at_date in a form. If the user doesn't enter a valid date, it throws an exception "invalid date". Is it possible to use validation instead to prompt the user to enter a valid date?

Specify timezone

Hello,

How can I set timezone?
When I select a date it automatically saves it in UTC format which is wrong here in Sweden.

Thanks!

Allow year, month and day selectors

Allow the splitting up of date with year, month, and day of month.

I'm imagining an API like

start_at_date
start_at_year
start_at_month
start_at_day
...etc.

How to clear a datefield completely?

Hello,

How can I clear the datetime field? Once set I cannot clear it if I submit the form empty. It justs reverts to the old value. The user must be able to remove a date from the form which is not possible right now.

Thankful for all input!

Allow use another field as default

Take the folowing example:

  split_accessor :starts_at, default: -> { Time.current }, time_format: '%H:%M'
  split_accessor :ends_at, default: -> { starts_at }, time_format: '%H:%M'

I want that, if ends_at is empty, TimeSplitter uses starts_at as default value.
Currently this is not allowed since the lambda is executed in Class scope.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.