Skip to content

Instantly share code, notes, and snippets.

@chadoh
Created September 27, 2011 20:06
Show Gist options
  • Save chadoh/1246081 to your computer and use it in GitHub Desktop.
Save chadoh/1246081 to your computer and use it in GitHub Desktop.
def widget(title, options = {}, &block)
options = (options.is_a? Hash) ? options : { options => true }
collapsed = (
(options.has_key?(:<) || options.has_key?(:collapsed)) && \
cookies["widget_#{title.parameterize.downcase}"] != 'expanded'
) \
|| cookies["widget_#{title.parameterize.downcase}"] == 'collapsed' \
|| options.has_key?(:ajax)
widget_class = options.fetch(:class, "")
widget_class << ' collapsed' if collapsed
widget_class << ' fixed' if options.fetch(:fixed, false)
link_attributes = { tabindex: 0, title: "click to show/hide" }
if options.has_key?(:ajax)
link_attributes[:href] = eval(options[:ajax])
link_attributes["data-remote"] = 'true'
widget_content_attributes = { class: 'loading' }
end
capture_haml do
haml_tag "section.widget", :<, class: widget_class do
haml_tag ".widget", :< do
haml_tag "header", :< do
unless options.fetch(:fixed, false)
haml_tag :a, :<, link_attributes do
haml_tag :h3, :< do
haml_concat title
end
end
else
haml_tag :h3, :< do
haml_concat title
end
end
end
haml_tag ".widget-content", :<, widget_content_attributes do
yield if block_given?
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment