lib/formtastic.rb in nofxx-formtastic-0.1.5 vs lib/formtastic.rb in nofxx-formtastic-0.1.6

- old
+ new

@@ -340,23 +340,25 @@ object = @object.send(association).build associated_name = extract_option_or_class_name(opts, :name, object) variable = "formtastic_next_#{associated_name}_id" opts.symbolize_keys! - partial = opts.delete(:partial) || associated_name container = opts.delete(:expression) || "'#{opts.delete(:container) || '#'+associated_name.pluralize}'" - function = "if (typeof #{variable} == 'undefined') #{variable} = 0; - $(#{container}).append($.template(" + - [self.render_associated_form(object, :fields_for => { :javascript => true }, - :partial => partial)].flatten.first.to_json+"), { number: --#{variable}});" + form = self.render_associated_form(object, :partial => partial) + form.gsub!(/attributes_(\d+)/, '__idx__') + form.gsub!(/\[(\d+)\]/, '__idxx__') + function = "if (typeof #{variable} == 'undefined') #{variable} = #{$1}; + $(#{container}).append($.template(#{form.to_json}.replace(/__idx__/g, \"attributes_\" + + #{variable}).replace(/__idxx__/g, \"[\" + #{variable}++ + \"]\")))" + template.link_to_function(name, function, opts) end - # Render associated form + # Render associated form def render_associated_form(associated, opts = {}) associated = associated.is_a?(Array) ? associated : [associated] # preserve association proxy if this is one opts.symbolize_keys! (opts[:new] - associated.select(&:new_record?).length).times { associated.build } if opts[:new] @@ -367,11 +369,11 @@ partial = opts[:partial] || name local_assign_name = partial.split('/').last.split('.').first output = associated.map do |element| fields_for(association_name(name), element, (opts[:fields_for] || {}).merge(:name => name)) do |f| - template.render({:partial => "#{partial}", :locals => {local_assign_name.to_sym => f.object, :f => f}.merge(opts[:locals] || {})}.merge(opts[:render] || {})) + template.render({:partial => "#{partial}", :locals => {local_assign_name.to_sym => element, :f => f}.merge(opts[:locals] || {})}.merge(opts[:render] || {})) end end output.join end end @@ -381,10 +383,10 @@ def association_name(class_name) @object.respond_to?("#{class_name}_attributes=") ? class_name : class_name.pluralize end def extract_option_or_class_name(hash, option, object) - (hash.delete(option) || object.class.name.split('::').last.underscore).to_s + (hash.delete(option) || object.class.name.split('::').last.underscore) end # End attribute_fu magic # protected