Skip to content

Instantly share code, notes, and snippets.

@ccurtisj
Forked from jpmckinney/formtastic-mongoid.rb
Last active December 30, 2015 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ccurtisj/7857816 to your computer and use it in GitHub Desktop.
Save ccurtisj/7857816 to your computer and use it in GitHub Desktop.
# 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
# * :email
# * :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,
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