Skip to content

Commit

Permalink
Cleanup nested attributes.
Browse files Browse the repository at this point in the history
  • Loading branch information
DAddYE committed Mar 12, 2012
1 parent 8173344 commit a7af2e0
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 50 deletions.
6 changes: 1 addition & 5 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,7 @@ task :test do
# Omit the padrino metagem since no tests there
GEM_PATHS[0..-2].each do |g|
# Hardcode the 'cd' into the command and do not use Dir.chdir because this causes random tests to fail
begin
sh "cd #{File.join(ROOT, g)} && #{Gem.ruby} -S rake test"
rescue RuntimeError
#Don't stop tests if one fails
end
sh "cd #{File.join(ROOT, g)} && #{Gem.ruby} -S rake test"
end
end

Expand Down
4 changes: 2 additions & 2 deletions padrino-core/lib/padrino-core/reloader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ def figure_path(file)
# Removes the specified class and constant.
#
def remove_constant(const)
return if exclude_constants.compact.uniq.any? { |c| const.to_s.index(c) == 0 } &&
!include_constants.compact.uniq.any? { |c| const.to_s.index(c) == 0 }
return if exclude_constants.compact.uniq.any? { |c| const.name.index(c) == 0 } &&
!include_constants.compact.uniq.any? { |c| const.name.index(c) == 0 }
begin
parts = const.to_s.sub(/^::(Object)?/, 'Object::').split('::')
object = parts.pop
Expand Down
31 changes: 18 additions & 13 deletions padrino-helpers/lib/padrino-helpers/asset_tag_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@ module AssetTagHelpers
# @example
# flash_tag(:notice, :id => 'flash-notice')
# # Generates: <div class="notice">flash-notice</div>
# flash_tag(:error, :success)
# # Generates: <div class="error">flash-error</div>
# # <div class="success">flash-success</div>
#
# @api public
def flash_tag(kind, options={})
flash_text = flash[kind]
return '' if flash_text.blank?
options.reverse_merge!(:class => kind)
content_tag(:div, flash_text, options)
def flash_tag(*args)
options = args.extract_options!
args.map do |kind|
flash_text = flash[kind]
next if flash_text.blank?
content_tag(:div, flash_text, options.reverse_merge(:class => kind))
end.compact * "\n"
end

##
Expand Down Expand Up @@ -69,14 +74,14 @@ def link_to(*args, &block)
anchor = "##{CGI.escape options.delete(:anchor).to_s}" if options[:anchor]

if block_given?
url = args[0] ? args[0] + anchor.to_s : anchor || 'javascript:void(0);'
url = args[0] ? args[0] + anchor.to_s : anchor || '#'
options.reverse_merge!(:href => url)
link_content = capture_html(&block)
return '' unless parse_conditions(url, options)
result_link = content_tag(:a, link_content, options)
block_is_template?(block) ? concat_content(result_link) : result_link
else
name, url = args[0], (args[1] ? args[1] + anchor.to_s : anchor || 'javascript:void(0);')
name, url = args[0], (args[1] ? args[1] + anchor.to_s : anchor || '#')
return name unless parse_conditions(url, options)
options.reverse_merge!(:href => url)
content_tag(:a, name, options)
Expand Down Expand Up @@ -119,8 +124,8 @@ def button_to(*args, &block)
desired_method = options[:method]
options.delete(:method) if options[:method].to_s !~ /get|post/i
options.reverse_merge!(:method => 'post', :action => url)
options[:enctype] = "multipart/form-data" if options.delete(:multipart)
options["data-remote"] = "true" if options.delete(:remote)
options[:enctype] = 'multipart/form-data' if options.delete(:multipart)
options['data-remote'] = 'true' if options.delete(:remote)
inner_form_html = hidden_form_method_field(desired_method)
inner_form_html += block_given? ? capture_html(&block) : submit_tag(name)
content_tag('form', inner_form_html, options)
Expand Down Expand Up @@ -419,13 +424,13 @@ def parse_conditions(url, options)
#
def parse_js_attributes(options)
options = options.dup
options["data-remote"] = "true" if options.delete(:remote)
options['data-remote'] = 'true' if options.delete(:remote)
if link_confirm = options.delete(:confirm)
options["data-confirm"] = link_confirm
options['data-confirm'] = link_confirm
end
if link_method = options.delete(:method)
options["data-method"] = link_method
options["rel"] = "nofollow"
options['data-method'] = link_method
options['rel'] = 'nofollow'
end
options
end
Expand Down
61 changes: 35 additions & 26 deletions padrino-helpers/lib/padrino-helpers/tag_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,35 +186,44 @@ def tag(name, options = nil, open = false)
end

private
##
# Returns a compiled list of HTML attributes
##
def tag_options(options)
return if options.blank?
attributes = []
options.each do |attribute, value|
next if value.nil? || value == false
if attribute == :data && value.is_a?(Hash)
while sub = value.detect{|k, v| v.is_a?(Hash)} do #Recurse through sub hashes
sub[1].each { |k, v| value["#{sub[0].to_s.dasherize}-#{k.to_s.dasherize}"] = v }
value.delete(sub[0])
end
value.each { |k, v| attributes << %[data-#{k.to_s.dasherize}="#{escape_value(v)}"] }
elsif BOOLEAN_ATTRIBUTES.include?(attribute)
attributes << attribute.to_s
else
attributes << %[#{attribute}="#{escape_value(value)}"]
end
##
# Returns a compiled list of HTML attributes
##
def tag_options(options)
return if options.blank?
attributes = []
options.each do |attribute, value|
next if value.nil? || value == false
if value.is_a?(Hash)
attributes << nested_values(attribute, value)
elsif BOOLEAN_ATTRIBUTES.include?(attribute)
attributes << attribute.to_s
else
attributes << %(#{attribute}="#{escape_value(value)}")
end
" #{attributes.join(' ')}"
end
" #{attributes.join(' ')}"
end

##
# Escape tag values to their HTML/XML entities.
##
def escape_value(string)
string.to_s.gsub(Regexp.union(*ESCAPE_VALUES.keys)){|c| ESCAPE_VALUES[c] }
end
##
# Escape tag values to their HTML/XML entities.
##
def escape_value(string)
string.to_s.gsub(Regexp.union(*ESCAPE_VALUES.keys)){|c| ESCAPE_VALUES[c] }
end

##
# Iterate through nested values
#
def nested_values(attribute, hash)
hash.map do |k, v|
if v.is_a?(Hash)
nested_values("#{attribute}-#{k.to_s.dasherize}", v)
else
%(#{attribute}-#{k.to_s.dasherize}="#{escape_value(v)}")
end
end * ' '
end
end # TagHelpers
end # Helpers
end # Padrino
12 changes: 10 additions & 2 deletions padrino-helpers/test/test_asset_tag_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def app
end

def flash
{ :notice => "Demo notice" }
@_flash ||= { :notice => "Demo notice" }
end

context 'for #flash_tag method' do
Expand All @@ -20,6 +20,14 @@ def flash
actual_html = flash_tag(:notice, :class => 'notice', :id => 'notice-area')
assert_has_tag('div.notice#notice-area', :content => "Demo notice") { actual_html }
end
should "display multiple flash tags with given attributes" do
flash[:error] = 'wrong'
flash[:success] = 'okey'
actual_html = flash_tag(:success, :error, :id => 'area')
assert_has_tag('div.success#area', :content => flash[:success]) { actual_html }
assert_has_tag('div.error#area', :content => flash[:error]) { actual_html }
assert_has_no_tag('div.notice') { actual_html }
end
end

context 'for #link_to method' do
Expand All @@ -39,7 +47,7 @@ def flash

should "display link element with void url and options" do
actual_link = link_to('Sign up', :class => "test")
assert_has_tag('a', :content => "Sign up", :href => 'javascript:void(0);', :class => 'test') { actual_link }
assert_has_tag('a', :content => "Sign up", :href => '#', :class => 'test') { actual_link }
end

should "display link element with remote option" do
Expand Down
4 changes: 2 additions & 2 deletions padrino-helpers/test/test_tag_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ def app
assert_has_tag(:a, 'data-remote' => 'true', 'data-method' => 'post') { actual_html }
end

should "support nested data attributes" do
should "support nested attributes" do
actual_html = tag(:div, :data => {:dojo => {:type => 'dijit.form.TextBox', :props => 'readOnly: true'}})
assert_has_tag(:div, 'data-dojo-type' => 'dijit.form.TextBox', 'data-dojo-props' => 'readOnly: true') { actual_html }
end

should "support open tags" do
actual_html = tag(:p, { :class => 'demo' }, true)
assert_equal "<p class=\"demo\">", actual_html
Expand Down

0 comments on commit a7af2e0

Please sign in to comment.