1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
require 'base_helper'
require 'action_view'
require 'action_view/template'
require 'action_view/models'
require 'client_side_validations/action_view'
module ActionController
class Base
include ActionDispatch::Routing::RouteSet.new.url_helpers
end
end
module ActionViewTestSetup
include ::ClientSideValidations::ActionView::Helpers::FormHelper
include ::ClientSideValidations::ActionView::Helpers::FormTagHelper
def form_for(*)
@output_buffer = super
end
Routes = ActionDispatch::Routing::RouteSet.new
Routes.draw do
resources :posts do
resources :comments
end
root :to => 'main#index'
end
def url_for(object)
@url_for_options = object
if object.is_a?(Hash) && object[:use_route].blank? && object[:controller].blank?
object.merge!(:controller => "main", :action => "index")
end
super
end
def _routes
Routes
end
# Rails 3.2.0 dropped size from the form elements
def legacy_size
if Rails.version < '3.2.0'
'size="30" '
end
end
def hidden_input_for_select(name)
if Rails.version >= '3.2.0'
%{<input name="#{name}" type="hidden" value="" />}
end
end
include Routes.url_helpers
def setup
super
# Create 'label' locale for testing I18n label helpers
I18n.backend.store_translations 'label', {
:activemodel => {
:attributes => {
:post => {
:cost => 'Total cost'
}
}
},
:helpers => {
:label => {
:post => {
:body => 'Write entire text here'
}
}
}
}
# Create "submit" locale for testing I18n submit helpers
I18n.backend.store_translations 'submit', {
:helpers => {
:submit => {
:create => 'Create %{model}',
:update => 'Confirm %{model} changes',
:submit => 'Save changes',
:another_post => {
:update => 'Update your %{model}'
}
}
}
}
@post = Post.new
@comment = Comment.new
if defined?(ActionView::OutputFlow)
@view_flow = ActionView::OutputFlow.new
else
@_content_for = Hash.new { |h,k| h[k] = ActiveSupport::SafeBuffer.new }
end
end
def snowman(method = nil)
txt = %{<div style="margin:0;padding:0;display:inline">}
txt << %{<input name="utf8" type="hidden" value="✓" />}
txt << %{<input name="_method" type="hidden" value="#{method}" />} if method
txt << %{</div>}
end
def form_text(action = "http://www.example.com", id = nil, html_class = nil, remote = nil, validators = nil, file = nil)
txt = %{<form accept-charset="UTF-8" action="#{action}"}
txt << %{ data-remote="true"} if remote
txt << %{ class="#{html_class}"} if html_class
txt << %{ data-validate="true"} if validators
txt << %{ enctype="multipart/form-data"} if file
txt << %{ id="#{id}"} if id
txt << %{ method="post"}
txt << %{ novalidate="novalidate"} if validators
txt << %{>}
end
def whole_form(action = "http://www.example.com", id = nil, html_class = nil, options = nil)
contents = block_given? ? yield : ""
if options.is_a?(Hash)
method, remote, validators, file = options.values_at(:method, :remote, :validators, :file)
else
method = options
end
html = form_text(action, id, html_class, remote, validators, file) + snowman(method) + (contents || "") + "</form>"
if options.is_a?(Hash) && options[:validators]
build_script_tag(html, id, options[:validators])
else
html
end
end
def build_script_tag(html, id, validators)
number_format = {:separator => '.', :delimiter => ','}
patterns = {:numericality=>"/^(-|\\+)?(?:\\d+|\\d{1,3}(?:\\#{number_format[:delimiter]}\\d{3})+)(?:\\#{number_format[:separator]}\\d*)?$/"}
(html || '') + %Q{<script>//<![CDATA[\nif(window.ClientSideValidations===undefined)window.ClientSideValidations={};window.ClientSideValidations.disabled_validators=#{ClientSideValidations::Config.disabled_validators.to_json};window.ClientSideValidations.number_format=#{number_format.to_json};if(window.ClientSideValidations.patterns===undefined)window.ClientSideValidations.patterns = {};window.ClientSideValidations.patterns.numericality=#{patterns[:numericality]};if(window.ClientSideValidations.forms===undefined)window.ClientSideValidations.forms={};window.ClientSideValidations.forms['#{id}'] = #{client_side_form_settings_helper.merge(:validators => validators).to_json};\n//]]></script>}
end
protected
def comments_path(post)
"/posts/#{post.id}/comments"
end
alias_method :post_comments_path, :comments_path
def comment_path(post, comment)
"/posts/#{post.id}/comments/#{comment.id}"
end
alias_method :post_comment_path, :comment_path
def admin_comments_path(post)
"/admin/posts/#{post.id}/comments"
end
alias_method :admin_post_comments_path, :admin_comments_path
def admin_comment_path(post, comment)
"/admin/posts/#{post.id}/comments/#{comment.id}"
end
alias_method :admin_post_comment_path, :admin_comment_path
def posts_path(options={})
"/posts"
end
def post_path(post, options = {})
if options[:format]
"/posts/#{post.id}.#{options[:format]}"
else
"/posts/#{post.id}"
end
end
def protect_against_forgery?
false
end
end
|