solnic / virtus

[DISCONTINUED ] Attributes on Steroids for Plain Old Ruby Objects
MIT License
3.77k stars 229 forks source link

MultiParameters #158

Closed ismaelga closed 11 years ago

ismaelga commented 11 years ago

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

dkubb commented 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.

ismaelga commented 11 years ago

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)" />
solnic commented 11 years ago

@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)>>
ismaelga commented 11 years ago

@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

solnic commented 11 years ago

@ismaelga I don't have this use case. I'd use a hash like I showed in the example.

solnic commented 11 years ago

closing this one sine this feature isn't needed in virtus

marcgg commented 10 years ago

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.

solnic commented 10 years ago

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.

marcgg commented 10 years ago

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 :)

solnic commented 10 years ago

OK cool! :)

mbj commented 10 years ago

@solnic Morpher could handle these representation mappings. Quite well.

dgilperez commented 9 years ago

@ismaelga how did you eventually approached this?

ismaelga commented 9 years ago

@dgilperez I can't remember but I probably changed the form input to get a string instead.

maxshelley commented 9 years ago

@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?

ismaelga commented 9 years ago

@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

joost commented 9 years ago

Thanks @ismaelga. This gist (Google result #1) did the trick for me: https://gist.github.com/sj26/767b29f11fed8a4d13c5

sj26 commented 9 years ago

I've release a gem based on that gist here:

https://github.com/sj26/virtus-multiparams