ActiveScaffold search date range - ruby-on-rails

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.

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

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.

time_start scope time_end helper

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.

Highcharts on Rails 3

I am quite new to Rails, and I need to work on a report generating plugin using Rails and Highcharts for the output. My main issue is that my user is meant to be able to define a time range using a jQuery datepicker. How can I get the highcharts graph on the x axis to reflect the time range based on the users selection, and how do I tie it to the other fields in the rails model.
Eg. If I have a table with a created_by field which the user is supposed to pick his to and from dates from... and the table also has user_id and the users select a specific language for each application. I want the graph to show for each day the count of the different languages different users selected during their application.
Hope my question is clear enough. Thanks
You should look at: http://railscasts.com/episodes/223-charts

How can I get Rails to interpret a text field as a datetime

My database has a datetime field, and I want to be able to create new entries. Obviously the Rails datetime_select helper isn't the most user friendly thing to have in your form.
I'd rather have a text field for the datetime (or one for the date, and one for the time) and interpret the inputs like PHP strtotime can.
I might just be searching the wrong keywords. Surely this has been discussed in great depth somewhere.
Thanks
:0)
Check out Railscast #32, I've used this method a few times and it works pretty well.

Resources