Skip to content

Commit

Permalink
Add form_with support
Browse files Browse the repository at this point in the history
  • Loading branch information
tagliala committed Apr 22, 2019
1 parent 6127f98 commit 45046da
Show file tree
Hide file tree
Showing 5 changed files with 843 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Metrics/ModuleLength:

Metrics/ParameterLists:
Max: 8
Exclude:
- 'test/**/*'

Metrics/PerceivedComplexity:
Exclude:
Expand Down
5 changes: 5 additions & 0 deletions lib/client_side_validations/action_view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ module Helpers

require 'client_side_validations/core_ext'
require 'client_side_validations/action_view/form_helper'

if ActionView::Helpers::FormHelper.method_defined?(:form_with)
require 'client_side_validations/action_view/form_with_helper'
end

require 'client_side_validations/action_view/form_builder'

ActionView::Base.send(:include, ClientSideValidations::ActionView::Helpers::FormHelper)
Expand Down
47 changes: 47 additions & 0 deletions lib/client_side_validations/action_view/form_with_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
module ClientSideValidations
module ActionView
module Helpers
module FormHelper
def form_with(model: nil, scope: nil, url: nil, format: nil, **options)
return super unless options[:validate]

options[:allow_method_names_outside_object] = true
options[:skip_default_ids] = !try(:form_with_generates_ids)

if model
url ||= polymorphic_path(model, format: format)

model = model.last if model.is_a?(Array)
scope ||= model_name_from_record_or_class(model).param_key
end

if block_given?
@validators = {}

builder = instantiate_builder(scope, model, options)
output = capture(builder, &Proc.new)
options[:multipart] ||= builder.multipart?

build_bound_validators! options

html_options = html_options_for_form_with(url, model, options)

if model
html_options[:novalidate] ||= 'novalidate'
apply_csv_html_options! html_options, options, builder
end

form_tag_with_body(html_options, output)
else
html_options = html_options_for_form_with(url, model, options)
form_tag_html(html_options)
end
end
end
end
end
end
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
37 changes: 35 additions & 2 deletions test/action_view/cases/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ def form_for(*)
@output_buffer = super
end

def form_with(*)
@output_buffer = super
end

Routes = ActionDispatch::Routing::RouteSet.new
include Routes.url_helpers
def _routes
Expand Down Expand Up @@ -108,15 +112,15 @@ def snowman(method = nil)
txt
end

def form_field(tag, id: nil, name: nil, type: nil, value: nil, multiple: false, tag_content: nil, custom_name: nil)
def form_field(tag, id: nil, name: nil, type: nil, value: nil, multiple: false, tag_content: nil, custom_name: nil, automatic_id: true)
txt = %(<#{tag}).dup

txt << %( name="#{custom_name}") if custom_name
txt << %( type="#{type}") if type
txt << %( value="#{value}") if value
txt << %( multiple="multiple") if multiple
txt << %( name="#{name}") if name
txt << %( id="#{id}") if id
txt << %( id="#{id}") if id && automatic_id
txt <<
if %w[select textarea].include?(tag)
%(\>#{tag_content}</#{tag}>)
Expand Down Expand Up @@ -156,6 +160,35 @@ def whole_form(action = 'http://www.example.com', id = nil, html_class = nil, op
form_text(action, id, html_class, remote, (validators || no_validate), file, custom_id) + snowman(method) + (contents || '') + '</form>'
end

def form_with_text(action = 'http://www.example.com', id = nil, html_class = nil, local = nil, validators = nil, file = nil)
txt = %(<form action="#{action}" accept-charset="UTF-8" method="post").dup

if validators
txt << %( data-client-side-validations="#{CGI.escapeHTML(csv_data_attribute(validators))}")
txt << %( novalidate="novalidate") if validators
end

txt << %( data-remote="true") unless local
txt << %( id="#{id}") if id
txt << %( class="#{html_class}") if html_class
txt << %( enctype="multipart/form-data") if file
txt << %(\>)

txt
end

def whole_form_with(action = 'http://www.example.com', options = nil)
contents = block_given? ? yield : ''

if options.is_a?(Hash)
method, local, validators, file, id, html_class, no_validate = options.values_at(:method, :local, :validators, :file, :id, :class, :no_validate)
else
method = options
end

form_with_text(action, id, html_class, local, (validators || no_validate), file) + snowman(method) + (contents || '') + '</form>'
end

def csv_data_attribute(validators)
{
html_settings: client_side_form_settings_helper,
Expand Down
Loading

0 comments on commit 45046da

Please sign in to comment.