time_start scope time_end helper - ruby-on-rails

hi im new in ruby on rails
here is my scenario i have two timers time_star and time_end
im just wondering if there is a validator or helper that will change the time of the end time based on the time of the start time in order not to get a negative value upon getting the difference of the two time it is yes possible in jquery but i want to know if rails can also do this
example if i choose 1pm on time_start then time_end will only show 2pm onwards
im also researching it now im just hopping there may be many possible answer here

You can use two different approaches to achieve different goals.
You can write custom validator to prevent time_end that lower than time_start to be stored to the database. Or you can pay attention on standart numericality validator, maybe it will be enough for you.
You can write some helpers, JS or other view related code to allow user choose time_end value only greater than time_start in the html widget.
In my opinion, it's quite a specific task and unlikely you will find something in rails for it, but it can be realized in a few lines of code according to your wishes.

Related

Selecting date and time with a datepicker for the date but with time immediately visible in Rails

I'm using Rails 4, and also Twitter Bootstrap.
I'd like to select the date with a date picker, but have a separately visible component for selecting the hour and minutes.
I've had a look at smalot bootstrap-datetimepicker https://github.com/smalot/bootstrap-datetimepicker , but looking at the Demo Page, they don't demonstrate any ability to show date and time separately. You have to choose date, and then later on choose time, which doesn't feel very intuitive.
I've also looked at Eonasdan bootstrap-datetimepicker https://github.com/Eonasdan/bootstrap-datetimepicker , but the time picking for it, even in inline mode, is not intuitive - will people know they can just click on the hour value to change it?
I'm thinking of just using a date picker for picking the date, and selecting the hour and minute myself, but it kind of feels wrong handing off part of a datetime to a gem/library and handling the rest of it myself.
I came across Separate date and time form fields in Rails , which is asking about this kind of problem, but it's a question from September 2010.
How do I select the date with a date picker, but have simple and immediately visible selection of time?
First, unless you find a plugin that does what you want off the rack, then yes, it's up to you to handle it, and yes, it feels kinda wrong - depending on how you do it.
Not sure what you had in mind, but the way it feels "the most wrong" is if your form has a single "date time" field under the hood, and you use javascript to botch together the date from the plugin and the time from your own setup, and store them in your datetime field. The nice thing about this is your rails app just gets a single datetime field and knows exactly what to do with it.
Here's how I'd approach it:
Keeping in mind that forms don't necessarily have to map 1-to-1 with your models, I'd split it in the controller layer, and conceptually think of "a form with two fields: date, and time", and then in your controller (or a form object, which is probably better for this situation) you'd stitch the date and time together, before saving them to your model. This approach means you can have separate validation on each field, which is probably also what you want (because I'd assume it's possible for users to input a valid date, but an invalid time or vica versa).
In terms of handling the date with a plugin and the time yourself, that's now fine - they're two completely separate fields from the perspective of your form, so there's nothing dirty about it. It just means you need the extra logic in your controller layer to split the datetime when you display the form, and merge the date and time back into one when you save the form.
Edit: if you haven't heard of form objects, check out https://github.com/apotonick/reform and http://railscasts.com/episodes/416-form-objects

ActiveScaffold search date range

I am developing a Rails application using ActiveScaffold.
The thing is that I need to filter a list of results by a date range.
(SQL equivalent 'BETWEEN ? AND ?')
I know that ActiveScaffold has a feature that already does that, but it has too many options.
(Past, Future, Range, =, ...., Between). In this case, I only need the Between option in that combo.
I tried overriding the field using the Helper, but the closest I got was to display 3 different combos (one for year, one for month, one for day).
Is there any way to override Active Scaffold so that only displays the "Between" option?
EDIT
Active Scaffold already does the search part well.
I am trying to change the visual part so it doesn't display so many options.
EDIT 2
The calendar plugins for rails are dated from 2009 or they aren't under maintenance.
You can use a range as a parameter in AR.
Model.where(:date => from..to)
Also, I'm not sure if ActiveScaffold has something to do with it. Normally, all the tasks like this one can be perfectly solved within plain ActiveRecord.
EDIT:
As it turns out, author also needs to get the user's input for the boundaries.
This is a common task that can be solved with one of the thouhands js plugins for datepickers.
I would recommend you not to stick to ActiveScaffold for that purpose.
Try this simple Jquery datepicker, and it will turn normal text field into that drop-down calendar. You will only need several lines of javascript then.
If you need further advise, just ask.

Weekly scheduling in Rails

I want to allow teachers to be able to login to my Rails 3.2 app and be able to set when they are available. So instead of having two datetime fields where an actual date is stored is stored for starts_at and ends_at, I'd like for them just to say I'm available on "Mondays between 4:00pm and 5:00pm" with all three values being dropdowns.
The orignal way I approached this was having a string for day and using the time_select method in my form for my starts_at and ends_at. Unfortunately, time_select still comes with the date.
I'm just looking for the cleanest way to allow weekly scheduling. Is this possible? If it is, is there an easier way to do this? Thanks in advance for your tips.
take a look at
https://github.com/mzararagoza/rails-fullcalendar-icecube
its a small appointment app that i think that you can take allot out of it.

Rails Telephone field like a date field?

I using devise to allow users to register on my website. I have field for them to put in their telephone number when registering.
I however want to split the telephone field into 3 parts so you put in different parts of the number, kind of like a date.
Is it possible in rails to do something similar like you would with a date? When you have a date select on a form it gives the field names:
model[date(1i)]
model[date(2i)]
model[date(3i)]
Is this possible with other fields?
Cheers
I do not believe it is possible to cajole the date_select/select_date family into doing what you want.
You could try to mirror the DateTimeSelector class for your purposes: https://github.com/rails/rails/blob/master/actionpack/lib/action_view/helpers/date_helper.rb
But why not just deal with this in the controller? You can slice and dice the input any way you want. If it's a one-time thing, I would do it that way. Otherwise, it might be worth your time to solve the problem in general, in which case you can make a gem for the world to use... although I think these 'i' suffixes are only useful for dates and times as far as ActiveRecord is concerned.

Is there a time_from_now gem?

I'm looking for something that given a date, will display the time from now in words. Example:
if today is 4/28/2012, I want:
4/29 to display "Tomorrow"
4/30 to display "Next Monday"
4/31 to display "Next Tuesday"
Is there a gem for this already or do I have to write it myself? If I have to write it what's the cleanest way to do it? I haven't written anything like this before in rails. Thanks!
I believe you're looking for the distance_of_time_in_words helper, which is already available in Rails - it's basically the opposite of time_ago_in_words, in the sense that it tells you the amount of time between two times, as opposed to how long since a certain time has passed vs now, and can therefore be used to measure into the future instead of into the past.
It won't give you days of the week like "Next monday", but if you click the Source: show link at the bottom of the helper's documentation, it will provide you the code you'd need to create a modified version of the helper that could display the day of week information pretty easily. Combine that with the code in this answer regarding calculating the day of the week, and you're pretty much done.

Resources