[Haml] Convert Haml::Helpers::ActionViewExtensions docs to YARD.

This commit is contained in:
Nathan Weizenbaum 2009-05-02 02:35:30 -07:00
parent 83b0fc8209
commit f60d0f691e
3 changed files with 173 additions and 179 deletions

View File

@ -1,5 +1,7 @@
require 'haml/helpers/action_view_mods'
require 'haml/helpers/action_view_extensions'
if defined?(ActionView)
require 'haml/helpers/action_view_mods'
require 'haml/helpers/action_view_extensions'
end
module Haml
# This module contains various helpful methods to make it easier to do various tasks.

View File

@ -1,45 +1,40 @@
require 'haml/helpers/action_view_mods'
if defined?(ActionView)
module Haml
module Helpers
# This module contains various useful helper methods
# that either tie into ActionView or the rest of the ActionPack stack,
# or are only useful in that context.
# Thus, the methods defined here are only available
# if ActionView is installed.
module ActionViewExtensions
# Returns a value for the "class" attribute
# unique to this controller/action pair.
# This can be used to target styles specifically at this action or controller.
# For example, if the current action were EntryController#show,
#
# %div{:class => page_class} My Div
#
# would become
#
# <div class="entry show">My Div</div>
#
# Then, in a stylesheet
# (shown here as Sass),
# you could refer to this specific action:
#
# .entry.show
# :font-weight bold
#
# or to all actions in the entry controller:
#
# .entry
# :color #00f
#
def page_class
controller.controller_name + " " + controller.action_name
end
# :stopdoc:
alias_method :generate_content_class_names, :page_class
# :startdoc:
module Haml
module Helpers
# This module contains various useful helper methods
# that either tie into ActionView or the rest of the ActionPack stack,
# or are only useful in that context.
# Thus, the methods defined here are only available
# if ActionView is installed.
module ActionViewExtensions
# Returns a value for the "class" attribute
# unique to this controller/action pair.
# This can be used to target styles specifically at this action or controller.
# For example, if the current action were `EntryController#show`,
#
# %div{:class => page_class} My Div
#
# would become
#
# <div class="entry show">My Div</div>
#
# Then, in a stylesheet (shown here as {Sass}),
# you could refer to this specific action:
#
# .entry.show
# :font-weight bold
#
# or to all actions in the entry controller:
#
# .entry
# :color #00f
#
# @return [String] The class name for the current page
def page_class
controller.controller_name + " " + controller.action_name
end
alias_method :generate_content_class_names, :page_class
end
end
end

View File

@ -1,163 +1,139 @@
if defined?(ActionView) and not defined?(Merb::Plugins)
module ActionView
class Base
def render_with_haml(*args, &block)
options = args.first
module ActionView
class Base
def render_with_haml(*args, &block)
options = args.first
# If render :layout is used with a block,
# it concats rather than returning a string
# so we need it to keep thinking it's Haml
# until it hits the sub-render
if is_haml? && !(options.is_a?(Hash) && options[:layout] && block_given?)
return non_haml { render_without_haml(*args, &block) }
end
render_without_haml(*args, &block)
# If render :layout is used with a block,
# it concats rather than returning a string
# so we need it to keep thinking it's Haml
# until it hits the sub-render
if is_haml? && !(options.is_a?(Hash) && options[:layout] && block_given?)
return non_haml { render_without_haml(*args, &block) }
end
alias_method :render_without_haml, :render
alias_method :render, :render_with_haml
render_without_haml(*args, &block)
end
alias_method :render_without_haml, :render
alias_method :render, :render_with_haml
# Rails >2.1
if Haml::Util.has?(:instance_method, self, :output_buffer)
def output_buffer_with_haml
return haml_buffer.buffer if is_haml?
output_buffer_without_haml
# Rails >2.1
if Haml::Util.has?(:instance_method, self, :output_buffer)
def output_buffer_with_haml
return haml_buffer.buffer if is_haml?
output_buffer_without_haml
end
alias_method :output_buffer_without_haml, :output_buffer
alias_method :output_buffer, :output_buffer_with_haml
def set_output_buffer_with_haml(new)
if is_haml?
haml_buffer.buffer = new
else
set_output_buffer_without_haml new
end
alias_method :output_buffer_without_haml, :output_buffer
alias_method :output_buffer, :output_buffer_with_haml
end
alias_method :set_output_buffer_without_haml, :output_buffer=
alias_method :output_buffer=, :set_output_buffer_with_haml
end
end
def set_output_buffer_with_haml(new)
if is_haml?
haml_buffer.buffer = new
module Helpers
# In Rails <=2.1, we've got to override considerable capturing infrastructure.
# In Rails >2.1, we can make do with only overriding #capture
# (which no longer behaves differently in helper contexts).
unless Haml::Util.has?(:instance_method, ActionView::Base, :output_buffer)
module CaptureHelper
def capture_with_haml(*args, &block)
# Rails' #capture helper will just return the value of the block
# if it's not actually in the template context,
# as detected by the existance of an _erbout variable.
# We've got to do the same thing for compatibility.
if is_haml? && block_is_haml?(block)
capture_haml(*args, &block)
else
set_output_buffer_without_haml new
capture_without_haml(*args, &block)
end
end
alias_method :set_output_buffer_without_haml, :output_buffer=
alias_method :output_buffer=, :set_output_buffer_with_haml
alias_method :capture_without_haml, :capture
alias_method :capture, :capture_with_haml
def capture_erb_with_buffer_with_haml(buffer, *args, &block)
if is_haml?
capture_haml(*args, &block)
else
capture_erb_with_buffer_without_haml(buffer, *args, &block)
end
end
alias_method :capture_erb_with_buffer_without_haml, :capture_erb_with_buffer
alias_method :capture_erb_with_buffer, :capture_erb_with_buffer_with_haml
end
module TextHelper
def concat_with_haml(string, binding = nil)
if is_haml?
haml_buffer.buffer.concat(string)
else
concat_without_haml(string, binding)
end
end
alias_method :concat_without_haml, :concat
alias_method :concat, :concat_with_haml
end
else
module CaptureHelper
def capture_with_haml(*args, &block)
if Haml::Helpers.block_is_haml?(block)
capture_haml(*args, &block)
else
capture_without_haml(*args, &block)
end
end
alias_method :capture_without_haml, :capture
alias_method :capture, :capture_with_haml
end
end
module Helpers
# In Rails <=2.1, we've got to override considerable capturing infrastructure.
# In Rails >2.1, we can make do with only overriding #capture
# (which no longer behaves differently in helper contexts).
unless Haml::Util.has?(:instance_method, ActionView::Base, :output_buffer)
module CaptureHelper
def capture_with_haml(*args, &block)
# Rails' #capture helper will just return the value of the block
# if it's not actually in the template context,
# as detected by the existance of an _erbout variable.
# We've got to do the same thing for compatibility.
module TagHelper
def content_tag_with_haml(name, *args, &block)
return content_tag_without_haml(name, *args, &block) unless is_haml?
if is_haml? && block_is_haml?(block)
capture_haml(*args, &block)
else
capture_without_haml(*args, &block)
end
end
alias_method :capture_without_haml, :capture
alias_method :capture, :capture_with_haml
preserve = haml_buffer.options[:preserve].include?(name.to_s)
def capture_erb_with_buffer_with_haml(buffer, *args, &block)
if is_haml?
capture_haml(*args, &block)
else
capture_erb_with_buffer_without_haml(buffer, *args, &block)
end
end
alias_method :capture_erb_with_buffer_without_haml, :capture_erb_with_buffer
alias_method :capture_erb_with_buffer, :capture_erb_with_buffer_with_haml
if block_given? && block_is_haml?(block) && preserve
return content_tag_without_haml(name, *args) {preserve(&block)}
end
module TextHelper
def concat_with_haml(string, binding = nil)
if is_haml?
haml_buffer.buffer.concat(string)
else
concat_without_haml(string, binding)
end
end
alias_method :concat_without_haml, :concat
alias_method :concat, :concat_with_haml
end
else
module CaptureHelper
def capture_with_haml(*args, &block)
if Haml::Helpers.block_is_haml?(block)
capture_haml(*args, &block)
else
capture_without_haml(*args, &block)
end
end
alias_method :capture_without_haml, :capture
alias_method :capture, :capture_with_haml
returning content_tag_without_haml(name, *args, &block) do |content|
return Haml::Helpers.preserve(content) if preserve && content
end
end
module TagHelper
def content_tag_with_haml(name, *args, &block)
return content_tag_without_haml(name, *args, &block) unless is_haml?
alias_method :content_tag_without_haml, :content_tag
alias_method :content_tag, :content_tag_with_haml
end
preserve = haml_buffer.options[:preserve].include?(name.to_s)
class InstanceTag
# Includes TagHelper
if block_given? && block_is_haml?(block) && preserve
return content_tag_without_haml(name, *args) {preserve(&block)}
end
returning content_tag_without_haml(name, *args, &block) do |content|
return Haml::Helpers.preserve(content) if preserve && content
end
end
alias_method :content_tag_without_haml, :content_tag
alias_method :content_tag, :content_tag_with_haml
def haml_buffer
@template_object.send :haml_buffer
end
class InstanceTag
# Includes TagHelper
def haml_buffer
@template_object.send :haml_buffer
end
def is_haml?
@template_object.send :is_haml?
end
alias_method :content_tag_without_haml, :content_tag
alias_method :content_tag, :content_tag_with_haml
def is_haml?
@template_object.send :is_haml?
end
module FormTagHelper
def form_tag_with_haml(url_for_options = {}, options = {}, *parameters_for_url, &proc)
if is_haml?
if block_given?
oldproc = proc
proc = haml_bind_proc do |*args|
concat "\n"
tab_up
oldproc.call(*args)
tab_down
concat haml_indent
end
concat haml_indent
end
res = form_tag_without_haml(url_for_options, options, *parameters_for_url, &proc) + "\n"
concat "\n" if block_given?
res
else
form_tag_without_haml(url_for_options, options, *parameters_for_url, &proc)
end
end
alias_method :form_tag_without_haml, :form_tag
alias_method :form_tag, :form_tag_with_haml
end
alias_method :content_tag_without_haml, :content_tag
alias_method :content_tag, :content_tag_with_haml
end
module FormHelper
def form_for_with_haml(object_name, *args, &proc)
if block_given? && is_haml?
module FormTagHelper
def form_tag_with_haml(url_for_options = {}, options = {}, *parameters_for_url, &proc)
if is_haml?
if block_given?
oldproc = proc
proc = haml_bind_proc do |*args|
concat "\n"
tab_up
oldproc.call(*args)
tab_down
@ -165,13 +141,34 @@ if defined?(ActionView) and not defined?(Merb::Plugins)
end
concat haml_indent
end
form_for_without_haml(object_name, *args, &proc)
concat "\n" if block_given? && is_haml?
res = form_tag_without_haml(url_for_options, options, *parameters_for_url, &proc) + "\n"
concat "\n" if block_given?
res
else
form_tag_without_haml(url_for_options, options, *parameters_for_url, &proc)
end
alias_method :form_for_without_haml, :form_for
alias_method :form_for, :form_for_with_haml
end
alias_method :form_tag_without_haml, :form_tag
alias_method :form_tag, :form_tag_with_haml
end
module FormHelper
def form_for_with_haml(object_name, *args, &proc)
if block_given? && is_haml?
oldproc = proc
proc = haml_bind_proc do |*args|
tab_up
oldproc.call(*args)
tab_down
concat haml_indent
end
concat haml_indent
end
form_for_without_haml(object_name, *args, &proc)
concat "\n" if block_given? && is_haml?
end
alias_method :form_for_without_haml, :form_for
alias_method :form_for, :form_for_with_haml
end
end
end