Rails Telephone field like a date field? - ruby-on-rails

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.

Related

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.

Generating values for dropdown ONLY for 'C' of CRUD

When choosing 'Add' in CRUD, how best to generate a list of choices to pick from a dropdown?
For U/update - just display what's there...
The field contents starts with a letter, followed by five numeric digits:{A-I,K-N,Z}#####
Each letter has a different 'max' value for the numeric part.
So when adding a new record, I'd like to offer a listbox with one of each letter and that letter's highest numeric value + 10.
So, if the max 'A' as A00120, and max 'B' B00030 (etc) the listbox would have A00130 and B00040.. etc
Save the user having to figure out which is 'next' when generating a new record.
? Thanks,
Mark
This time I'll not be able to come up with ready to use solution, but I must say - everything is possible with ATK4. You just have to customize and extend it to fit your needs :)
Speaking about your question above - I guess you have to split it in multiple parts.
First part is about how to show select box on Create and readonly or disabled field on Update. I guess you can make some custom Form field or pin some action to existing Form Field hook. Not sure exactly what's better in this case.
Second one is about data structure. I believe that this field actually should be 2 fields in DB and maybe (only maybe) merged together in ATK model with addExpression() just for user interface needs to display these 2 fields as one field easily in UI. Maybe such concatenated field will be useful also for searching, but definitely not as only one field stored in DB. Imagine how hard it'll be for DB engine to find max value if such field. Store it like type = letter, num = number and then search like SELECT max(num)+10 FROM t WHERE type='A'
Finally Third part is about how to generate this next number. I read your question 3 times and came to conclusion that actually you don't have to show this +10 numeric value in UI at all if it's hardly predefined anyway. Actually that'll not work correctly if this will be multi-user system which I guess it will. Instead just show simple select box with letters {A-I,K-N,Z} and calculate this next value exactly before inserting data in DB. That can be done using models insert hook. This will be much more appropriate solution and will be better for UI and also more stable because calculated in model not incorrectly in UI part.

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.

How to use Symfony validators on an array of items (dates, emails, etc.)?

I have a form where users add dates to a list. I can easily validate a single date, but how do is use sfValidateDate to validate an array/collection of dates?
sfValidateDate returns an error for obvious reasons.
I've experimented with this:
$this->setValidator('dates_list', new sfValidatorSchemaForEach(new sfValidatorDate(),2));
... but I've hardcoded the number 2 as the number of times the validator should repeat. I'm not sure if this is even remotely correct, but it seems to work in practice for 2 dates. However, the user of the form can add any number of dates, so it needs to be dynamic.
Any suggestions would be much appreciated.
Why not use $this->getValues()? Then you can simply get dates count submitted by user.

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