Class: Mongoid::Scribe::Builders::Field
- Inherits:
-
Object
- Object
- Mongoid::Scribe::Builders::Field
- Includes:
- ActionView::Context, ActionView::Helpers::FormHelper
- Defined in:
- lib/mongoid/scribe/builders/field.rb
Instance Method Summary collapse
- #field ⇒ Object
-
#initialize(form, klass, document, name, config) ⇒ Field
constructor
A new instance of Field.
Constructor Details
#initialize(form, klass, document, name, config) ⇒ Field
Returns a new instance of Field.
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/mongoid/scribe/builders/field.rb', line 8 def initialize(form, klass, document, name, config) @form = form @klass = klass @document = document @name = name.to_s @config = config @label_method = @config.fetch(:label, :id) @value_method = @config.fetch(:value, :id) @type = @config.fetch(:type, nil) @polymorphic = false end |
Instance Method Details
#field ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/mongoid/scribe/builders/field.rb', line 20 def field return nil if private_field? if @klass.fields[@name].type == Boolean form_field = @form.check_box(@name) elsif @klass.fields[@name].type == Integer form_field = @form.number_field(@name) elsif @type == :textarea form_field = @form.text_area(@name) elsif (@name =~ /email/) || (@type == :email) form_field = @form.email_field(@name) elsif @klass.fields[@name].type == Array && values form_field = checkbox_array elsif values && values.all?{ |value| value.is_a?(Mongoid::Document) || value.is_a?(Mongoid::Criteria) } if @polymorphic = values.map do |value| [ value.send(@label_method), value.send(@value_method), {data: {type: value.class.name}} ] end form_field = @form.select(@name, , {}, {class: "polymorphic"}) form_field += @form.hidden_field(@name.gsub("_id", "_type")) else form_field = @form.collection_select(@name, values, @value_method, @label_method) end elsif values form_field = @form.select(@name, values) else form_field = @form.text_field(@name) end return content_tag(:div, (label + form_field).html_safe, class: 'control-group') end |