madeintandem / hstore_accessor

Adds typed hstore-backed field support to ActiveRecord models.
MIT License
242 stars 47 forks source link

Extend column_for_attribute to work with form helpers (simple_form, etc) #26

Open peck opened 10 years ago

peck commented 10 years ago

Any interest in adding this to the gem itself? I find myself using using the following function in my models that I pass into simple_form builders to have the fields show up as the correct type, and not just default to text after receiving nil.

    def column_for_attribute(attr)
     atr = hstore_metadata_for_properties[attr]
     if attr
       OpenStruct.new(type: atr)
     else
       super(attr)
     end
    end
jhirn commented 10 years ago

This seems useful. Can you create a PR with some tests?

peck commented 10 years ago

Will do, have to work through some things downstream in the process for updating attributes and making sure all is good there, but yes, Ill put it on my to-do list and see what I can come up with.

thegrubbsian commented 10 years ago

Shouldn't use OpenStruct here, just create a regular Struct somewhere to avoid method cache busting every time an hstore class is used in a form build.

jhirn commented 10 years ago

+1 for no OpenStruct. In fact, it should respect the original return type of column_for_attribute as closely as possible. While Struct/OpenStruct might meet the needs of simple_form, it could be surprising to someone expecting a different object.

crismali commented 9 years ago

@peck, are you still hacking on this?

crismali commented 9 years ago

This c1c16fb7713464ccce79818cd7d0d6a856fae26b is a start on this.