How can I coerce Postgres "citext" fields into text fields instead of text areas?

211 views
Skip to first unread message

HuckyDucky

unread,
May 7, 2013, 12:36:41 AM5/7/13
to plataformate...@googlegroups.com
The case-sensitive nature of Postgres was causing a problem for my application, so we converted all varchar columns to be of type "citext" instead. citext is a Postgres extension that performs some downcases behind the scenes in order to do case-insensitive matching.  However, now every form in my app is jacked up because "citext" is being rendered as a 20 unit high text "area" instead of a text "field".

I'm wading through the code, but perhaps someone knows of a way I can short-circuit this process.

Thanks for any help you can offer!

HuckyDucky

unread,
May 7, 2013, 12:55:27 AM5/7/13
to plataformate...@googlegroups.com
I'm aware I can pass "as: :string" to every f.input message but I don't want to do that if I can avoid it.

I know Rails abstracts some of these database datatypes away, but I can still get the correct column type like this:

    User.last.column_for_attribute('email').sql_type

That returns:

  "citext"

But this:

User.last.column_for_attribute('email').type

returns:

    :text

Carlos Antonio da Silva

unread,
May 7, 2013, 8:24:28 AM5/7/13
to plataformate...@googlegroups.com
Well, since, Rails knows about "text", Simple Form also knows about it, and will generate a text area input for those columns: https://github.com/plataformatec/simple_form/blob/master/lib/simple_form/form_builder.rb#L488

What you could try to do is to create your own TextInput, inheriting from https://github.com/plataformatec/simple_form/blob/master/lib/simple_form/inputs/text_input.rb, to check the sql_type and see if it is a "citext", if it is, you just delegate the input to generate a text_field, otherwise call super and let it generate the default text_area.

Hope that helps!


--
You received this message because you are subscribed to the Google Groups "SimpleForm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to plataformatec-simp...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
At.
Carlos Antonio

HuckyDucky

unread,
May 7, 2013, 9:27:45 AM5/7/13
to plataformate...@googlegroups.com
Thanks Carlos. I've never had to crack open or subclass something like this before. I created a class to inherit TextInput, but not I'm not sure how to use it since SimpleForm doesn't know about it. This is what I have:

# ci_text_input.rb
class CiTextInput << SimpleForm::Inputs::TextInput
  def input
    if column.sql_type == "citext"
      @builder.text_field(attribute_name, input_html_options)
    else
      super
    end
  end
end

How do I hook up this custom class to simple_form?

Thanks again!
To unsubscribe from this group and stop receiving emails from it, send an email to plataformatec-simpleform+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
At.
Carlos Antonio

HuckyDucky

unread,
May 7, 2013, 9:40:35 AM5/7/13
to plataformate...@googlegroups.com
YES! It helps to read instructions. Your docs, like your gems, are awesome! Thanks so much!

class TextInput < SimpleForm::Inputs::TextInput
  def input
    if column.sql_type == "citext"
      @builder.text_field(attribute_name, input_html_options)
    else
      super
    end
  end
end


On Tuesday, May 7, 2013 8:24:28 AM UTC-4, Carlos Antonio da Silva wrote:
To unsubscribe from this group and stop receiving emails from it, send an email to plataformatec-simpleform+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
At.
Carlos Antonio
Reply all
Reply to author
Forward
0 new messages