padrino-helpers 0.5.0 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +1 -1
- data/README.rdoc +2 -2
- data/Rakefile +3 -1
- data/VERSION +1 -1
- data/lib/padrino-helpers.rb +2 -0
- data/lib/padrino-helpers/asset_tag_helpers.rb +40 -39
- data/lib/padrino-helpers/form_builder/abstract_form_builder.rb +63 -53
- data/lib/padrino-helpers/form_helpers.rb +74 -43
- data/lib/padrino-helpers/format_helpers.rb +5 -4
- data/lib/padrino-helpers/locale/en.yml +1 -1
- data/lib/padrino-helpers/output_helpers.rb +34 -35
- data/padrino-helpers.gemspec +5 -5
- data/test/fixtures/markup_app/views/form_tag.erb +0 -1
- data/test/test_asset_tag_helpers.rb +4 -0
- data/test/test_form_builder.rb +15 -15
- data/test/test_form_helpers.rb +15 -12
- metadata +4 -4
data/LICENSE
CHANGED
data/README.rdoc
CHANGED
@@ -189,7 +189,7 @@ The list of defined helpers in the 'form helpers' category:
|
|
189
189
|
* Constructs a field_set to group fields with given options
|
190
190
|
* <tt>field_set_tag(:class => 'office-set') { }</tt>
|
191
191
|
* <tt>field_set_tag("Office", :class => 'office-set') { }</tt>
|
192
|
-
* <tt>error_messages_for(record, options={})</tt>
|
192
|
+
* <tt>error_messages_for(:record, options={})</tt>
|
193
193
|
* Constructs list html for the errors for a given object
|
194
194
|
* <tt>error_messages_for @user</tt>
|
195
195
|
* <tt>label_tag(name, options={}, &block)</tt>
|
@@ -485,4 +485,4 @@ See the wiki article for additional information: <...WIKI...>
|
|
485
485
|
|
486
486
|
== Copyright
|
487
487
|
|
488
|
-
Copyright (c)
|
488
|
+
Copyright (c) 2010 Padrino. See LICENSE for details.
|
data/Rakefile
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'rake'
|
3
3
|
|
4
|
+
GEM_VERSION = File.read(File.dirname(__FILE__) + '/VERSION')
|
5
|
+
|
4
6
|
begin
|
5
7
|
require 'jeweler'
|
6
8
|
Jeweler::Tasks.new do |gem|
|
@@ -11,7 +13,7 @@ begin
|
|
11
13
|
gem.homepage = "http://github.com/padrino/padrino-framework/tree/master/padrino-helpers"
|
12
14
|
gem.authors = ["Padrino Team", "Nathan Esquenazi", "Davide D'Agostino", "Arthur Chiu"]
|
13
15
|
gem.add_runtime_dependency "sinatra", ">= 0.9.2"
|
14
|
-
gem.add_runtime_dependency "padrino-core", "
|
16
|
+
gem.add_runtime_dependency "padrino-core", "= #{GEM_VERSION}"
|
15
17
|
gem.add_development_dependency "haml", ">= 2.2.1"
|
16
18
|
gem.add_development_dependency "shoulda", ">= 0"
|
17
19
|
gem.add_development_dependency "mocha", ">= 0.9.7"
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.6.1
|
data/lib/padrino-helpers.rb
CHANGED
@@ -17,15 +17,17 @@ module Padrino
|
|
17
17
|
# parameters: name, url='javascript:void(0)', options={}, &block
|
18
18
|
def link_to(*args, &block)
|
19
19
|
options = args.extract_options!
|
20
|
+
anchor = options[:anchor] ? "##{CGI.escape options.delete(:anchor).to_s}" : ""
|
21
|
+
options["data-remote"] = "true" if options.delete(:remote)
|
20
22
|
if block_given?
|
21
23
|
url = args[0] || 'javascript:void(0);'
|
22
|
-
options.reverse_merge!(:href => url)
|
24
|
+
options.reverse_merge!(:href => url + anchor)
|
23
25
|
link_content = capture_html(&block)
|
24
26
|
result_link = content_tag(:a, link_content, options)
|
25
27
|
block_is_template?(block) ? concat_content(result_link) : result_link
|
26
28
|
else
|
27
29
|
name, url = args[0], (args[1] || 'javascript:void(0);')
|
28
|
-
options.reverse_merge!(:href => url)
|
30
|
+
options.reverse_merge!(:href => url + anchor)
|
29
31
|
content_tag(:a, name, options)
|
30
32
|
end
|
31
33
|
end
|
@@ -75,47 +77,46 @@ module Padrino
|
|
75
77
|
end
|
76
78
|
|
77
79
|
protected
|
80
|
+
# stylesheet_tag('style', :media => 'screen')
|
81
|
+
def stylesheet_tag(source, options={})
|
82
|
+
options = options.dup.reverse_merge!(:href => stylesheet_path(source), :media => 'screen', :rel => 'stylesheet', :type => 'text/css')
|
83
|
+
tag(:link, options)
|
84
|
+
end
|
78
85
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
# javascript_tag 'application', :src => '/javascripts/base/application.js'
|
86
|
-
def javascript_tag(source, options={})
|
87
|
-
options = options.dup.reverse_merge!(:src => javascript_path(source), :type => 'text/javascript', :content => "")
|
88
|
-
tag(:script, options)
|
89
|
-
end
|
86
|
+
# javascript_tag 'application', :src => '/javascripts/base/application.js'
|
87
|
+
def javascript_tag(source, options={})
|
88
|
+
options = options.dup.reverse_merge!(:src => javascript_path(source), :type => 'text/javascript', :content => "")
|
89
|
+
tag(:script, options)
|
90
|
+
end
|
90
91
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
92
|
+
# Returns the javascript_path appending the default javascripts path if necessary
|
93
|
+
def javascript_path(source)
|
94
|
+
return source if source =~ /^http/
|
95
|
+
source = source.to_s.gsub(/\.js$/, '')
|
96
|
+
source_name = source; source_name << ".js" unless source =~ /\.js/
|
97
|
+
result_path = source_name if source =~ %r{^/} # absolute path
|
98
|
+
result_path ||= uri_root_path("javascripts", source_name)
|
99
|
+
stamp = File.exist?(result_path) ? File.mtime(result_path) : Time.now.to_i
|
100
|
+
"#{result_path}?#{stamp}"
|
101
|
+
end
|
101
102
|
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
103
|
+
# Returns the stylesheet_path appending the default stylesheets path if necessary
|
104
|
+
def stylesheet_path(source)
|
105
|
+
return source if source =~ /^http/
|
106
|
+
source = source.to_s.gsub(/\.css$/, '')
|
107
|
+
source_name = source; source_name << ".css" unless source =~ /\.css/
|
108
|
+
result_path = source_name if source =~ %r{^/} # absolute path
|
109
|
+
result_path ||= uri_root_path("stylesheets", source_name)
|
110
|
+
stamp = File.exist?(result_path) ? File.mtime(result_path) : Time.now.to_i
|
111
|
+
"#{result_path}?#{stamp}"
|
112
|
+
end
|
112
113
|
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
114
|
+
# Returns the uri root of the application, defaulting to '/'
|
115
|
+
# @example uri_root('javascripts')
|
116
|
+
def uri_root_path(*paths)
|
117
|
+
root_uri = self.class.uri_root if self.class.respond_to?(:uri_root)
|
118
|
+
File.join(root_uri || '/', *paths)
|
119
|
+
end
|
119
120
|
end
|
120
121
|
end
|
121
122
|
end
|
@@ -18,7 +18,7 @@ module Padrino
|
|
18
18
|
|
19
19
|
# f.label :username, :caption => "Nickname"
|
20
20
|
def label(field, options={})
|
21
|
-
options.reverse_merge!(:caption => "#{field
|
21
|
+
options.reverse_merge!(:caption => "#{field_human_name(field)}: ")
|
22
22
|
@template.label_tag(field_id(field), options)
|
23
23
|
end
|
24
24
|
|
@@ -30,26 +30,26 @@ module Padrino
|
|
30
30
|
|
31
31
|
# f.text_field :username, :value => "(blank)", :id => 'username'
|
32
32
|
def text_field(field, options={})
|
33
|
-
options.reverse_merge!(:value => field_value(field), :id => field_id(field))
|
33
|
+
options.reverse_merge!(:value => field_value(field), :id => field_id(field), :class => field_error(field, options))
|
34
34
|
@template.text_field_tag field_name(field), options
|
35
35
|
end
|
36
36
|
|
37
37
|
# f.text_area :summary, :value => "(enter summary)", :id => 'summary'
|
38
38
|
def text_area(field, options={})
|
39
|
-
options.reverse_merge!(:value => field_value(field), :id => field_id(field))
|
39
|
+
options.reverse_merge!(:value => field_value(field), :id => field_id(field), :class => field_error(field, options))
|
40
40
|
@template.text_area_tag field_name(field), options
|
41
41
|
end
|
42
42
|
|
43
43
|
# f.password_field :password, :id => 'password'
|
44
44
|
def password_field(field, options={})
|
45
|
-
options.reverse_merge!(:value => field_value(field), :id => field_id(field))
|
45
|
+
options.reverse_merge!(:value => field_value(field), :id => field_id(field), :class => field_error(field, options))
|
46
46
|
@template.password_field_tag field_name(field), options
|
47
47
|
end
|
48
48
|
|
49
49
|
# f.select :color, :options => ['red', 'green'], :include_blank => true
|
50
50
|
# f.select :color, :collection => @colors, :fields => [:name, :id]
|
51
51
|
def select(field, options={})
|
52
|
-
options.reverse_merge!(:id => field_id(field), :selected => field_value(field))
|
52
|
+
options.reverse_merge!(:id => field_id(field), :selected => field_value(field), :class => field_error(field, options))
|
53
53
|
@template.select_tag field_name(field), options
|
54
54
|
end
|
55
55
|
|
@@ -71,7 +71,7 @@ module Padrino
|
|
71
71
|
|
72
72
|
# f.file_field :photo, :class => 'avatar'
|
73
73
|
def file_field(field, options={})
|
74
|
-
options.reverse_merge!(:id => field_id(field))
|
74
|
+
options.reverse_merge!(:id => field_id(field), :class => field_error(field, options))
|
75
75
|
@template.file_field_tag field_name(field), options
|
76
76
|
end
|
77
77
|
|
@@ -86,53 +86,63 @@ module Padrino
|
|
86
86
|
end
|
87
87
|
|
88
88
|
protected
|
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
|
-
|
89
|
+
# Returns the known field types for a formbuilder
|
90
|
+
def self.field_types
|
91
|
+
[:hidden_field, :text_field, :text_area, :password_field, :file_field, :radio_button, :check_box, :select]
|
92
|
+
end
|
93
|
+
|
94
|
+
# Returns the object's models name
|
95
|
+
# => user_assignment
|
96
|
+
def object_name
|
97
|
+
object.is_a?(Symbol) ? object : object.class.to_s.underscore.gsub('/', '-')
|
98
|
+
end
|
99
|
+
|
100
|
+
# Returns true if the value matches the value in the field
|
101
|
+
# field_has_value?(:gender, 'male')
|
102
|
+
def values_matches_field?(field, value)
|
103
|
+
value.present? && (field_value(field).to_s == value.to_s || field_value(field).to_s == 'true')
|
104
|
+
end
|
105
|
+
|
106
|
+
# Returns the value for the object's field
|
107
|
+
# field_value(:username) => "Joey"
|
108
|
+
def field_value(field)
|
109
|
+
@object && @object.respond_to?(field) ? @object.send(field) : ""
|
110
|
+
end
|
111
|
+
|
112
|
+
def field_error(field, options)
|
113
|
+
if @object && @object.respond_to?(:errors) && @object.errors.respond_to?(:on) && @object.errors.on(field)
|
114
|
+
options[:class] = ["x-form-text", "x-form-invalid", options[:class]].compact.join(" ")
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
# Returns the name for the given field
|
119
|
+
# field_name(:username) => "user[username]"
|
120
|
+
def field_name(field)
|
121
|
+
"#{object_name}[#{field}]"
|
122
|
+
end
|
123
|
+
|
124
|
+
# Returns the human name of the field. Look that use builtin I18n.
|
125
|
+
def field_human_name(field)
|
126
|
+
I18n.translate("#{object_name}.#{field}", :count => 1, :default => field.to_s.humanize, :scope => [:model, :attributes])
|
127
|
+
end
|
128
|
+
|
129
|
+
# Returns the id for the given field
|
130
|
+
# field_id(:username) => "user_username"
|
131
|
+
# field_id(:gender, :male) => "user_gender_male"
|
132
|
+
def field_id(field, value=nil)
|
133
|
+
value.blank? ? "#{object_name}_#{field}" : "#{object_name}_#{field}_#{value}"
|
134
|
+
end
|
135
|
+
|
136
|
+
# explicit_object is either a symbol or a record
|
137
|
+
# Returns a new record of the type specified in the object
|
138
|
+
def build_object(object_or_symbol)
|
139
|
+
object_or_symbol.is_a?(Symbol) ? object_class(object_or_symbol).new : object_or_symbol
|
140
|
+
end
|
141
|
+
|
142
|
+
# Returns the class type for the given object
|
143
|
+
def object_class(explicit_object)
|
144
|
+
explicit_object.is_a?(Symbol) ? explicit_object.to_s.classify.constantize : explicit_object.class
|
145
|
+
end
|
136
146
|
end
|
137
147
|
end
|
138
148
|
end
|
@@ -25,6 +25,7 @@ module Padrino
|
|
25
25
|
def form_tag(url, options={}, &block)
|
26
26
|
options.reverse_merge!(:method => 'post', :action => url)
|
27
27
|
options[:enctype] = "multipart/form-data" if options.delete(:multipart)
|
28
|
+
options["data-remote"] = "true" if options.delete(:remote)
|
28
29
|
inner_form_html = hidden_form_method_field(options[:method]) + capture_html(&block)
|
29
30
|
concat_content content_tag('form', inner_form_html, options)
|
30
31
|
end
|
@@ -40,16 +41,47 @@ module Padrino
|
|
40
41
|
concat_content content_tag('fieldset', field_set_content, options)
|
41
42
|
end
|
42
43
|
|
43
|
-
# Constructs list html for the errors for a given
|
44
|
-
# error_messages_for
|
45
|
-
def error_messages_for(
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
44
|
+
# Constructs list html for the errors for a given symbol
|
45
|
+
# error_messages_for :user
|
46
|
+
def error_messages_for(*params)
|
47
|
+
options = params.extract_options!.symbolize_keys
|
48
|
+
objects = params.collect {|object_name| object_name.is_a?(Symbol) ? instance_variable_get("@#{object_name}") : object_name }.compact
|
49
|
+
count = objects.inject(0) {|sum, object| sum + object.errors.count }
|
50
|
+
|
51
|
+
unless count.zero?
|
52
|
+
html = {}
|
53
|
+
[:id, :class].each do |key|
|
54
|
+
if options.include?(key)
|
55
|
+
value = options[key]
|
56
|
+
html[key] = value unless value.blank?
|
57
|
+
else
|
58
|
+
html[key] = 'field-errors'
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
options[:object_name] ||= params.first.class
|
63
|
+
|
64
|
+
I18n.with_options :locale => options[:locale], :scope => [:models, :errors, :template] do |locale|
|
65
|
+
header_message = if options.include?(:header_message)
|
66
|
+
options[:header_message]
|
67
|
+
else
|
68
|
+
object_name = options[:object_name].to_s.gsub('_', ' ')
|
69
|
+
object_name = I18n.t(object_name, :default => object_name, :scope => :models, :count => 1)
|
70
|
+
locale.t :header, :count => count, :model => object_name
|
71
|
+
end
|
72
|
+
message = options.include?(:message) ? options[:message] : locale.t(:body)
|
73
|
+
error_messages = objects.map {|object| object.errors.full_messages.map {|msg| content_tag(:li, msg) } }.join
|
74
|
+
|
75
|
+
contents = ''
|
76
|
+
contents << content_tag(options[:header_tag] || :h2, header_message) unless header_message.blank?
|
77
|
+
contents << content_tag(:p, message) unless message.blank?
|
78
|
+
contents << content_tag(:ul, error_messages)
|
79
|
+
|
80
|
+
content_tag(:div, contents, html)
|
81
|
+
end
|
82
|
+
else
|
83
|
+
''
|
84
|
+
end
|
53
85
|
end
|
54
86
|
|
55
87
|
# Constructs a label tag from the given options
|
@@ -152,43 +184,42 @@ module Padrino
|
|
152
184
|
end
|
153
185
|
|
154
186
|
protected
|
187
|
+
# Returns an array of option items for a select field based on the given collection
|
188
|
+
# fields is an array containing the fields to display from each item in the collection
|
189
|
+
def options_from_collection(collection, fields)
|
190
|
+
return '' if collection.blank?
|
191
|
+
collection.collect { |item| [ item.send(fields.first), item.send(fields.last) ] }
|
192
|
+
end
|
155
193
|
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
# Returns the options tags for a select based on the given option items
|
164
|
-
def options_for_select(option_items, selected_value=nil)
|
165
|
-
return '' if option_items.blank?
|
166
|
-
option_items.collect do |caption, value|
|
167
|
-
value ||= caption
|
168
|
-
content_tag(:option, caption, :value => value, :selected => selected_value.to_s =~ /#{value}|#{caption}/)
|
194
|
+
# Returns the options tags for a select based on the given option items
|
195
|
+
def options_for_select(option_items, selected_value=nil)
|
196
|
+
return '' if option_items.blank?
|
197
|
+
option_items.collect do |caption, value|
|
198
|
+
value ||= caption
|
199
|
+
content_tag(:option, caption, :value => value, :selected => selected_value.to_s =~ /#{value}|#{caption}/)
|
200
|
+
end
|
169
201
|
end
|
170
|
-
end
|
171
202
|
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
203
|
+
# returns the hidden method field for 'put' and 'delete' forms
|
204
|
+
# Only 'get' and 'post' are allowed within browsers;
|
205
|
+
# 'put' and 'delete' are just specified using hidden fields with form action still 'put'.
|
206
|
+
# hidden_form_method_field('delete') => <input name="_method" value="delete" />
|
207
|
+
def hidden_form_method_field(desired_method)
|
208
|
+
return '' if (desired_method.to_s =~ /get|post/)
|
209
|
+
original_method = desired_method.to_s.dup
|
210
|
+
desired_method.to_s.replace('post')
|
211
|
+
hidden_field_tag(:_method, :value => original_method)
|
212
|
+
end
|
182
213
|
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
214
|
+
# Returns the FormBuilder class to use based on all available setting sources
|
215
|
+
# If explicitly defined, returns that, otherwise returns defaults
|
216
|
+
# configured_form_builder_class(nil) => StandardFormBuilder
|
217
|
+
def configured_form_builder_class(explicit_builder=nil)
|
218
|
+
default_builder = self.respond_to?(:options) && self.options.default_builder
|
219
|
+
configured_builder = explicit_builder || default_builder || 'StandardFormBuilder'
|
220
|
+
configured_builder = "Padrino::Helpers::FormBuilder::#{configured_builder}".constantize if configured_builder.is_a?(String)
|
221
|
+
configured_builder
|
222
|
+
end
|
192
223
|
end
|
193
224
|
end
|
194
225
|
end
|
@@ -169,16 +169,17 @@ module Padrino
|
|
169
169
|
|
170
170
|
# Used in xxxx.js.erb files to escape html so that it can be passed to javascript from Padrino
|
171
171
|
# js_escape_html("<h1>Hey</h1>")
|
172
|
-
def
|
172
|
+
def escape_javascript(html_content)
|
173
173
|
return '' unless html_content
|
174
174
|
javascript_mapping = { '\\' => '\\\\', '</' => '<\/', "\r\n" => '\n', "\n" => '\n' }
|
175
175
|
javascript_mapping.merge("\r" => '\n', '"' => '\\"', "'" => "\\'")
|
176
176
|
escaped_string = html_content.gsub(/(\\|<\/|\r\n|[\n\r"'])/) { javascript_mapping[$1] }
|
177
|
-
|
177
|
+
escaped_string
|
178
178
|
end
|
179
179
|
|
180
|
-
|
181
|
-
|
180
|
+
def js_escape_html(html_content)
|
181
|
+
"\"#{escape_javascript(html_content)}\""
|
182
|
+
end
|
182
183
|
|
183
184
|
end
|
184
185
|
end
|
@@ -53,46 +53,45 @@ module Padrino
|
|
53
53
|
end
|
54
54
|
|
55
55
|
protected
|
56
|
+
# Retrieves content_blocks stored by content_for or within yield_content
|
57
|
+
# content_blocks[:name] => ['...', '...']
|
58
|
+
def content_blocks
|
59
|
+
@content_blocks ||= Hash.new {|h,k| h[k] = [] }
|
60
|
+
end
|
56
61
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
# Used to capture the html from a block of erb code
|
64
|
-
# capture_erb(&block) => '...html...'
|
65
|
-
def capture_erb(*args, &block)
|
66
|
-
erb_with_output_buffer { block_given? && block.call(*args) }
|
67
|
-
end
|
62
|
+
# Used to capture the html from a block of erb code
|
63
|
+
# capture_erb(&block) => '...html...'
|
64
|
+
def capture_erb(*args, &block)
|
65
|
+
erb_with_output_buffer { block_given? && block.call(*args) }
|
66
|
+
end
|
68
67
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
68
|
+
# Concats directly to an erb template
|
69
|
+
# erb_concat("Direct to buffer")
|
70
|
+
def erb_concat(text)
|
71
|
+
@_out_buf << text if has_erb_buffer?
|
72
|
+
end
|
74
73
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
74
|
+
# Returns true if an erb buffer is detected
|
75
|
+
# has_erb_buffer? => true
|
76
|
+
def has_erb_buffer?
|
77
|
+
!@_out_buf.nil?
|
78
|
+
end
|
80
79
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
80
|
+
# Used to determine if a block is called from ERB.
|
81
|
+
# NOTE: This doesn't actually work yet because the variable __in_erb_template
|
82
|
+
# hasn't been defined in ERB. We need to find a way to fix this.
|
83
|
+
def block_is_erb?(block)
|
84
|
+
has_erb_buffer? || block && eval('defined? __in_erb_template', block)
|
85
|
+
end
|
87
86
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
87
|
+
# Used to direct the buffer for the erb capture
|
88
|
+
def erb_with_output_buffer(buf = '') #:nodoc:
|
89
|
+
@_out_buf, old_buffer = buf, @_out_buf
|
90
|
+
yield
|
91
|
+
@_out_buf
|
92
|
+
ensure
|
93
|
+
@_out_buf = old_buffer
|
94
|
+
end
|
96
95
|
end
|
97
96
|
end
|
98
97
|
end
|
data/padrino-helpers.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{padrino-helpers}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.6.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Padrino Team", "Nathan Esquenazi", "Davide D'Agostino", "Arthur Chiu"]
|
12
|
-
s.date = %q{2010-01-
|
12
|
+
s.date = %q{2010-01-14}
|
13
13
|
s.description = %q{Tag helpers, asset helpers, form helpers, form builders and many more helpers for padrino}
|
14
14
|
s.email = %q{nesquena@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -80,7 +80,7 @@ Gem::Specification.new do |s|
|
|
80
80
|
|
81
81
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
82
82
|
s.add_runtime_dependency(%q<sinatra>, [">= 0.9.2"])
|
83
|
-
s.add_runtime_dependency(%q<padrino-core>, ["
|
83
|
+
s.add_runtime_dependency(%q<padrino-core>, ["= 0.6.1"])
|
84
84
|
s.add_development_dependency(%q<haml>, [">= 2.2.1"])
|
85
85
|
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
86
86
|
s.add_development_dependency(%q<mocha>, [">= 0.9.7"])
|
@@ -88,7 +88,7 @@ Gem::Specification.new do |s|
|
|
88
88
|
s.add_development_dependency(%q<webrat>, [">= 0.5.1"])
|
89
89
|
else
|
90
90
|
s.add_dependency(%q<sinatra>, [">= 0.9.2"])
|
91
|
-
s.add_dependency(%q<padrino-core>, ["
|
91
|
+
s.add_dependency(%q<padrino-core>, ["= 0.6.1"])
|
92
92
|
s.add_dependency(%q<haml>, [">= 2.2.1"])
|
93
93
|
s.add_dependency(%q<shoulda>, [">= 0"])
|
94
94
|
s.add_dependency(%q<mocha>, [">= 0.9.7"])
|
@@ -97,7 +97,7 @@ Gem::Specification.new do |s|
|
|
97
97
|
end
|
98
98
|
else
|
99
99
|
s.add_dependency(%q<sinatra>, [">= 0.9.2"])
|
100
|
-
s.add_dependency(%q<padrino-core>, ["
|
100
|
+
s.add_dependency(%q<padrino-core>, ["= 0.6.1"])
|
101
101
|
s.add_dependency(%q<haml>, [">= 2.2.1"])
|
102
102
|
s.add_dependency(%q<shoulda>, [">= 0"])
|
103
103
|
s.add_dependency(%q<mocha>, [">= 0.9.7"])
|
@@ -30,6 +30,10 @@ class TestAssetTagHelpers < Test::Unit::TestCase
|
|
30
30
|
actual_html = link_to('Sign up', '/register', :class => 'first', :id => 'linky')
|
31
31
|
assert_has_tag('a#linky.first', :content => "Sign up", :href => '/register') { actual_html }
|
32
32
|
end
|
33
|
+
should "display link element with anchor attribute" do
|
34
|
+
actual_html = link_to("Anchor", "/anchor", :anchor => :foo)
|
35
|
+
assert_has_tag('a', :content => "Anchor", :href => '/anchor#foo') { actual_html }
|
36
|
+
end
|
33
37
|
should "display link element with void url and options" do
|
34
38
|
actual_link = link_to('Sign up', :class => "test")
|
35
39
|
assert_has_tag('a', :content => "Sign up", :href => 'javascript:void(0);', :class => 'test') { actual_link }
|
data/test/test_form_builder.rb
CHANGED
@@ -9,11 +9,11 @@ class TestFormBuilder < Test::Unit::TestCase
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def setup
|
12
|
-
error_stub = stub(:full_messages => ["1", "2"], :
|
12
|
+
error_stub = stub(:full_messages => ["1", "2"], :count => 2)
|
13
13
|
role_types = [stub(:name => 'Admin', :id => 1), stub(:name => 'Moderate', :id => 2), stub(:name => 'Limited', :id => 3)]
|
14
14
|
@user = stub(:errors => error_stub, :class => 'User', :first_name => "Joe", :session_id => 54)
|
15
15
|
@user.stubs(:role_types => role_types, :role => "1")
|
16
|
-
@user_none = stub(:errors => stub(:
|
16
|
+
@user_none = stub(:errors => stub(:count => 0), :class => 'User')
|
17
17
|
end
|
18
18
|
|
19
19
|
def standard_builder(object=@user)
|
@@ -130,25 +130,25 @@ class TestFormBuilder < Test::Unit::TestCase
|
|
130
130
|
|
131
131
|
should "display correct form html with valid record" do
|
132
132
|
actual_html = standard_builder.error_messages(:header_message => "Demo form cannot be saved")
|
133
|
-
assert_has_tag('div.field-errors
|
134
|
-
assert_has_tag('div.field-errors ul
|
135
|
-
assert_has_tag('div.field-errors ul
|
133
|
+
assert_has_tag('div.field-errors h2', :content => "Demo form cannot be saved") { actual_html }
|
134
|
+
assert_has_tag('div.field-errors ul li', :content => "1") { actual_html }
|
135
|
+
assert_has_tag('div.field-errors ul li', :content => "2") { actual_html }
|
136
136
|
end
|
137
137
|
|
138
138
|
should "display correct form in haml" do
|
139
139
|
visit '/haml/form_for'
|
140
|
-
assert_have_selector '#demo div.field-errors
|
141
|
-
assert_have_selector '#demo div.field-errors ul
|
142
|
-
assert_have_selector '#demo2 div.field-errors
|
143
|
-
assert_have_selector '#demo2 div.field-errors ul
|
140
|
+
assert_have_selector '#demo div.field-errors h2', :content => "custom MarkupUser cannot be saved!"
|
141
|
+
assert_have_selector '#demo div.field-errors ul li', :content => "This is a fake error"
|
142
|
+
assert_have_selector '#demo2 div.field-errors h2', :content => "custom MarkupUser cannot be saved!"
|
143
|
+
assert_have_selector '#demo2 div.field-errors ul li', :content => "This is a fake error"
|
144
144
|
end
|
145
145
|
|
146
146
|
should "display correct form in erb" do
|
147
147
|
visit '/erb/form_for'
|
148
|
-
assert_have_selector '#demo div.field-errors
|
149
|
-
assert_have_selector '#demo div.field-errors ul
|
150
|
-
assert_have_selector '#demo2 div.field-errors
|
151
|
-
assert_have_selector '#demo2 div.field-errors ul
|
148
|
+
assert_have_selector '#demo div.field-errors h2', :content => "custom MarkupUser cannot be saved!"
|
149
|
+
assert_have_selector '#demo div.field-errors ul li', :content => "This is a fake error"
|
150
|
+
assert_have_selector '#demo2 div.field-errors h2', :content => "custom MarkupUser cannot be saved!"
|
151
|
+
assert_have_selector '#demo2 div.field-errors ul li', :content => "This is a fake error"
|
152
152
|
end
|
153
153
|
end
|
154
154
|
|
@@ -551,13 +551,13 @@ class TestFormBuilder < Test::Unit::TestCase
|
|
551
551
|
|
552
552
|
should "display correct check box block in haml" do
|
553
553
|
visit '/haml/form_for'
|
554
|
-
assert_have_selector '#demo2 p label', :for => 'markup_user_remember_me', :content => "Remember
|
554
|
+
assert_have_selector '#demo2 p label', :for => 'markup_user_remember_me', :content => "Remember me: "
|
555
555
|
assert_have_selector '#demo2 p input.checker', :type => 'checkbox', :name => 'markup_user[remember_me]'
|
556
556
|
end
|
557
557
|
|
558
558
|
should "display correct check box block in erb" do
|
559
559
|
visit '/erb/form_for'
|
560
|
-
assert_have_selector '#demo2 p label', :for => 'markup_user_remember_me', :content => "Remember
|
560
|
+
assert_have_selector '#demo2 p label', :for => 'markup_user_remember_me', :content => "Remember me: "
|
561
561
|
assert_have_selector '#demo2 p input.checker', :type => 'checkbox', :name => 'markup_user[remember_me]'
|
562
562
|
end
|
563
563
|
end
|
data/test/test_form_helpers.rb
CHANGED
@@ -76,32 +76,35 @@ class TestFormHelpers < Test::Unit::TestCase
|
|
76
76
|
|
77
77
|
context 'for #error_messages_for method' do
|
78
78
|
should "display correct error messages list in ruby" do
|
79
|
-
user = stub(:class => "User", :errors => stub(:full_messages => ["1", "2"], :
|
79
|
+
user = stub(:class => "User", :errors => stub(:full_messages => ["1", "2"], :count => 2), :blank? => false)
|
80
80
|
actual_html = error_messages_for(user)
|
81
81
|
assert_has_tag('div.field-errors') { actual_html }
|
82
|
-
assert_has_tag('div.field-errors
|
83
|
-
assert_has_tag('div.field-errors
|
84
|
-
assert_has_tag('div.field-errors ul
|
82
|
+
assert_has_tag('div.field-errors h2', :content => "2 errors prohibited this User from being saved") { actual_html }
|
83
|
+
assert_has_tag('div.field-errors p', :content => "There were problems with the following fields:") { actual_html }
|
84
|
+
assert_has_tag('div.field-errors ul') { actual_html }
|
85
|
+
assert_has_tag('div.field-errors ul li', :count => 2) { actual_html }
|
85
86
|
end
|
86
87
|
|
87
88
|
should "display correct error messages list in erb" do
|
88
89
|
visit '/erb/form_tag'
|
89
90
|
assert_have_no_selector 'form.simple-form .field-errors'
|
90
91
|
assert_have_selector 'form.advanced-form .field-errors'
|
91
|
-
assert_have_selector 'form.advanced-form .field-errors
|
92
|
-
assert_have_selector 'form.advanced-form .field-errors
|
93
|
-
assert_have_selector 'form.advanced-form .field-errors ul
|
94
|
-
assert_have_selector 'form.advanced-form .field-errors ul
|
92
|
+
assert_have_selector 'form.advanced-form .field-errors h2', :content => "There are problems with saving user!"
|
93
|
+
assert_have_selector 'form.advanced-form .field-errors p', :content => "There were problems with the following fields:"
|
94
|
+
assert_have_selector 'form.advanced-form .field-errors ul'
|
95
|
+
assert_have_selector 'form.advanced-form .field-errors ul li', :count => 3
|
96
|
+
assert_have_selector 'form.advanced-form .field-errors ul li', :content => "This is a second fake error"
|
95
97
|
end
|
96
98
|
|
97
99
|
should "display correct error messages list in haml" do
|
98
100
|
visit '/haml/form_tag'
|
99
101
|
assert_have_no_selector 'form.simple-form .field-errors'
|
100
102
|
assert_have_selector 'form.advanced-form .field-errors'
|
101
|
-
assert_have_selector 'form.advanced-form .field-errors
|
102
|
-
assert_have_selector 'form.advanced-form .field-errors
|
103
|
-
assert_have_selector 'form.advanced-form .field-errors ul
|
104
|
-
assert_have_selector 'form.advanced-form .field-errors ul
|
103
|
+
assert_have_selector 'form.advanced-form .field-errors h2', :content => "There are problems with saving user!"
|
104
|
+
assert_have_selector 'form.advanced-form .field-errors p', :content => "There were problems with the following fields:"
|
105
|
+
assert_have_selector 'form.advanced-form .field-errors ul'
|
106
|
+
assert_have_selector 'form.advanced-form .field-errors ul li', :count => 3
|
107
|
+
assert_have_selector 'form.advanced-form .field-errors ul li', :content => "This is a second fake error"
|
105
108
|
end
|
106
109
|
end
|
107
110
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: padrino-helpers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Padrino Team
|
@@ -12,7 +12,7 @@ autorequire:
|
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
14
|
|
15
|
-
date: 2010-01-
|
15
|
+
date: 2010-01-14 00:00:00 +01:00
|
16
16
|
default_executable:
|
17
17
|
dependencies:
|
18
18
|
- !ruby/object:Gem::Dependency
|
@@ -31,9 +31,9 @@ dependencies:
|
|
31
31
|
version_requirement:
|
32
32
|
version_requirements: !ruby/object:Gem::Requirement
|
33
33
|
requirements:
|
34
|
-
- - "
|
34
|
+
- - "="
|
35
35
|
- !ruby/object:Gem::Version
|
36
|
-
version: 0.
|
36
|
+
version: 0.6.1
|
37
37
|
version:
|
38
38
|
- !ruby/object:Gem::Dependency
|
39
39
|
name: haml
|