Skip to content

Instantly share code, notes, and snippets.

@camallen
camallen / gist:3803552
Created September 29, 2012 09:24
Custom simple form Hash field input
#Simple form Input class for a hash
class HashInput < SimpleForm::Inputs::Base
def input
key = input_html_options.delete(:key)
#override the params to be in the format that rails will auto correlate back to an
# attribute hash with key: value
input_html_options.merge!({:name => "#{self.object_name}[#{attribute_name}][#{key}]"})
@builder.text_field(attribute_name, input_html_options)
end
end
@camallen
camallen / gist:3795872
Created September 27, 2012 19:17
Simple Form: Array field custom input
#Simple form Input class for arrays
class ArrayInput < SimpleForm::Inputs::Base
def input
#override the attribute name for arrays to allow rails to handle array forms
input_html_options.merge!({:name => "#{self.object_name}[#{attribute_name}][]"})
@builder.text_field(attribute_name, input_html_options)
end
end
@camallen
camallen / dynamic_document.rb
Created November 22, 2011 12:25
Extend the fields of a Mongoid document from a .yml file
module ExtendFields
require "yaml"
private
def extend_fields
dynamic_fields.each { |name, type| self.class.field name, type: type }
end
def dynamic_fields
YAML.load_file( File.join( Rails.root, 'config', 'class_fields', "#{self.class.name.underscore}.yml" ) ) rescue {}