Sha256: fe151bb5cbf851aa5cf9313d7908757bf57afc4454321fb21a2a88a8df9ecb61

Contents?: true

Size: 1.02 KB

Versions: 3

Compression:

Stored size: 1.02 KB

Contents

module AngularSprinkles
  module Helpers
    module FormForHelper

      ##
      # ng_form_for
      #
      # Wraps the form_for helper so that form submission can be bound
      # an Angular service function. Accepts an additional argument which
      # is the name of a service. Two arguments are passed to the service:
      # the object and the Angular representation of the form.
      def ng_form_for(record, submit_callback, options = {}, &block)
        form_name = ObjectKeyWrapper.new('form', JavaScript::NoOp)

        record = [record] if !record.is_a?(Array) # ensures that record will be handled in the same way whether or not it's an array

        html_options = {
          'name' => form_name,
          'ng-submit' => ng_service(submit_callback, *record.map(&:bind), form_name)
        }.merge(options[:html] || {})

        augmented_options = options.merge(url: "", method: "", html: html_options)

        form_for(record.first, augmented_options, &block).gsub('action="" ', '').html_safe
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
angular_sprinkles-0.3.3 lib/angular_sprinkles/helpers/form_for_helper.rb
angular_sprinkles-0.3.2 lib/angular_sprinkles/helpers/form_for_helper.rb
angular_sprinkles-0.3.1 lib/angular_sprinkles/helpers/form_for_helper.rb