Closed ismaelga closed 11 years ago
@ismaelga can you be more specific about what you're looking for? I think your request could be interpreted several ways, and I want us to be more clear before discussing too deeply.
Uh. Sorry. What I mean with multi params is when there are many fields in a form that correspond to an attribute. In rails this feature lives in activerecord. I would use this feature to on a Form Object. Currently I can do this using a tableless model but I want to use Virtus so my class does not get polluted by activerecord but this feature is lacking.
Here is an example of the form fields I'm talking about.
<input type="text" name="something(1i)" />
<input type="text" name="something(2s)" />
<input type="text" name="something(3f)" />
@ismaelga virtus supports coercing hash to date/datetime/time
EDIT: here's an example:
User = Class.new { include Virtus }
User.attribute :bday, Date
User.new :bday => { :year => 1983, :month => 11, :day => 18 }
=> #<User:0x007fac89aa0860 @bday=#<Date: 1983-11-18 ((2445657j,0s,0n),+0s,2299161j)>>
@solnic Yes I know that but this is a little different. What you get from params when using Time input is something like this:
{
"start_time(1i)"=>"2013", # year
"start_time(2i)"=>"3", # month
"start_time(3i)"=>"29", # day
"start_time(4i)"=>"18", # hour
"start_time(5i)"=>"00" # minutes
}
What do you guys use for Time inputs if you don't need this feature?
PS: DataMapper has this feature http://www.ruby-doc.org/gems/docs/d/dm-rails-1.2.1/Rails/DataMapper/MultiparameterAttributes.html
@ismaelga I don't have this use case. I'd use a hash like I showed in the example.
closing this one sine this feature isn't needed in virtus
For what it's worth, I'd love to have this feature as well, mainly because Rails date select helper will send data formatted like @ismaelga explained.
A lot of people need functionality that's required by rails - my suggestion is to create virtus-rails with all that stuff. I don't want to have anything rails-specific in main virtus project.
That makes a lot of sense, especially if you don't use Rails yourself.
If the need for such a thing increases in my company, I'll see what I can do. For now the way virtus behaves is already great. Of course it would have been a nice thing to have, but the gem is perfectly usable without it :)
OK cool! :)
@solnic Morpher could handle these representation mappings. Quite well.
@ismaelga how did you eventually approached this?
@dgilperez I can't remember but I probably changed the form input to get a string instead.
@dgilperez I just came across this after needing the same thing. How did you handle this? I wrote a little re-former class, https://gist.github.com/maxshelley/6581d8a3897e81493d39, but I'm not sure this was the best way to deal with this. Would love to hear if you found anything better?
@maxshelley you might want to have a look at https://github.com/datamapper/dm-rails/blob/master/lib/dm-rails/multiparameter_attributes.rb
You will also find a lot of discussion about this feature googling for MultiparameterAttributes
Thanks @ismaelga. This gist (Google result #1) did the trick for me: https://gist.github.com/sj26/767b29f11fed8a4d13c5
I've release a gem based on that gist here:
Is there any way to accept multi parameters for example for date and time?
I haven't read anything about it so I suppose not. If so what do you guys think about adding this as a plugin gem? I have an idea how to implement it but I'm not sure if it will work because I haven't seen much on virtus internals