Last active
December 28, 2015 05:09
-
-
Save stengland/7447589 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# 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 |
This file contains hidden or 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
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