Skip to content

Instantly share code, notes, and snippets.

@stengland
Last active December 28, 2015 05:09
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 stengland/7447589 to your computer and use it in GitHub Desktop.
Save stengland/7447589 to your computer and use it in GitHub Desktop.
# Adds ng-model attrs to all formatastic inputs
# based on ActiveModel naming conventions
class AngularFormBuilder < Formtastic::FormBuilder
# Almost exact dupilicate of the formtastic input method
# but thhe magick is in the extends :) (Line 13)
def input(method, options = {})
method = method.to_sym if method.is_a?(String)
options = options.dup
options[:as] ||= default_input_type(method, options)
klass = input_class(options[:as])
klass.new(self, template, @object, @object_name, method, options).extend(NgAttrs).to_html
end
private
module NgAttrs
def input_html_options
{
'ng-model' => ng_model_name
}.merge(super)
end
def ng_model_name
[
builder.custom_namespace,
object_name.to_s.split(/\[|\]/),
dom_index,
association_primary_key || sanitized_method_name
].flatten.reject { |x| x.blank? }.join('.')
end
end
end
module ApplicationHelper
def angular_form_for(record_or_name_or_array, *args, &proc)
options = args.extract_options!
options[:builder] ||= AngularFormBuilder
semantic_form_for(record_or_name_or_array, *(args << options), &proc)
end
# snip...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment