Code Monkey home page Code Monkey logo

simple_time_select's Introduction

SIMPLE TIME SELECT PLUGIN

Ever wanted a time select component with only one select field? This simple plugin 
gives you that component and allows you to set minute intervals. If you set your
minute interval to 15, you get options such as:
"6:00 PM", "6:15 PM", "6:30 PM", etc.

If no minute interval is specified, the control defaults to a 15 minute interval. 
As you can see from the sample values above, this control also implements an AM/PM time
format.

Also note that this component is great if you ONLY want the time from a user. As is, the code
prevents the hidden date fields associated with the time_select helper from being created. This means
that a couple lines of code on the controller side are necessary to make this work. See USAGE for details.

USAGE:

To create the time select:

  <%= time_select "event", "time", { :default => Time.now.change(:hour => 21), :simple_time_select => true, :minute_interval => 20, :time_separator => "" } %>

Don't forget to include the time_separator option.  Otherwise you will get an extra colon outside of the select field.

Simple time select also takes a start_hour and end_hour option to be specified in military format (between 0-23).

<%= time_select "event", "time", { :default => Time.now.change(:hour => 21), :simple_time_select => true, :minute_interval => 20, :time_separator => "", :start_hour => 10, :end_hour => 14 } %>

The start hour behaves as you would expect but the end_hour may not.  If you specify the end_hour as 10, your time select will include 10:15, 10:30, 10:45.  So the end_hour sets the last hour that the time select will include. Email me if you don't like this.

When the time is submitted, you will have the value in params on the controller side as shown below:
  
  params[:event][:"time(i)"]

Simply add these lines of code to your handler to play nicely with ActiveRecord:

  params[:event][:time] = Time.parse(params[:event][:"time(i)"])
  params[:event].delete(:"time(i)")

Now the time will be correct, but the date will be the current date.  Here are a couple options for changing the date:

1) Change the date in your controller (replace anything in < > with your own variables):
  
  params[:event][:time] = params[:event][:time].change(:year => <year_var>, :day => <day_var>, :month => <month_var>)
  *I do not use this method, you may need to cast params[:event][:time] to a Time object before applying "change" to it.

2) Change the date in an ActiveRecord callback in your model.  This is my preferred method:

  def before_validation
    # Assuming date is your Date variable
    self.time = self.time.change(:year => date.year, :day => date.day, :month => date.month)
  end

Feel free to send any questions to [email protected]

Author: Tony Amoyal <[email protected]>

Homepage:
http://github.com/tamoyal/simple_time_select/tree/master

simple_time_select's People

Contributors

tamoyal avatar

Watchers

Janmejay Rai (Jay) avatar James Cloos avatar

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.