Created
June 10, 2012 03:37
-
-
Save jpmckinney/2903748 to your computer and use it in GitHub Desktop.
Set default Formtastic input types for Mongoid field types.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# blog post: | |
require 'formtastic/version' | |
# Formtastic sets default input types for ActiveRecord column types. This does | |
# the same for Mongoid field types. | |
module Mongoid::Document | |
FORMTASTIC_2_2 = Formtastic::VERSION[/\A2\.2\.\d\z/] | |
# Map Mongoid field types to either ActiveRecord column types (which | |
# Formtastic understands) or directly to Formtastic input types. | |
# | |
# ActiveRecord column types: | |
# * :binary | |
# * :boolean | |
# * :date | |
# * :datetime | |
# * :decimal | |
# * :float | |
# * :integer | |
# * :string | |
# * :text | |
# * :time | |
# * :timestamp | |
# Formtastic input types: | |
# * :boolean | |
# * :check_boxes | |
# * :country | |
# * :date_select | |
# * :datetime_select | |
# * :file | |
# * :hidden | |
# * :number | |
# * :password | |
# * :phone | |
# * :radio | |
# * :range | |
# * :search | |
# * :select | |
# * :string | |
# * :text | |
# * :time_select | |
# * :time_zone | |
# * :url | |
# | |
# @see http://mongoid.org/en/mongoid/docs/documents.html#fields | |
COLUMN_TYPE_MAP = { | |
Array => :string, | |
BigDecimal => :decimal, | |
Boolean => :boolean, | |
Date => FORMTASTIC_2_2 ? :date_select : :date, | |
DateTime => FORMTASTIC_2_2 ? :datetime_select : :datetime, | |
Float => :float, | |
Hash => :string, | |
Integer => :integer, | |
Moped::BSON::ObjectId => :string, | |
Range => :range, | |
Regexp => :string, | |
String => :string, | |
Symbol => :string, | |
# The Formtastic :time input type displays only hours, minutes and seconds, | |
# but a Time object has both date and time parts. | |
Time => FORMTASTIC_2_2 ? :datetime_select : :datetime, | |
# Raises "uninitialized constant Mongoid::Document::TimeWithZone" | |
# TimeWithZone => :datetime, | |
# Fields with no type are objects. | |
Object => :string, | |
} | |
Column = Struct.new :name, :type | |
def column_for_attribute(attribute) | |
name = attribute.to_s | |
field = fields[name] | |
if field | |
type = Mongoid::Fields::ForeignKey === field ? 'select' : field.type | |
Column.new(name, COLUMN_TYPE_MAP[type] || type.to_s.downcase.to_sym) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For Mongoid < 3, see https://gist.github.com/2903748/a1a1aff5f14c47e3096c5d150811f31e934b834a