module Xass::ViewHelpers
Public Instance Methods
namespace(*names, reset: false, &block)
click to toggle source
# File lib/xass/view_helpers.rb, line 3 def namespace(*names, reset: false, &block) n = reset ? names : namespace_stack.last + names namespace_stack.push(n) res = capture(&block) namespace_stack.pop node = Nokogiri::HTML.fragment(res) process_namespace_classes(node, n) node.to_html.html_safe end
Also aliased as: dns
namespace!(*names, &block)
click to toggle source
# File lib/xass/view_helpers.rb, line 13 def namespace!(*names, &block) namespace(*names, reset: true, &block) end
Also aliased as: dns!
namespace_with_root(*names, tag: :div, attrs: {}, reset: false, &block)
click to toggle source
# File lib/xass/view_helpers.rb, line 17 def namespace_with_root(*names, tag: :div, attrs: {}, reset: false, &block) nss = reset ? [] : namespace_stack.last content_tag(tag, block ? namespace(*names, reset: reset, &block) : '', attrs_with_additional_class(attrs, ns_root!(*(nss + names)))) end
Also aliased as: dnsr
namespace_with_root!(*names, tag: :div, attrs: {}, &block)
click to toggle source
# File lib/xass/view_helpers.rb, line 22 def namespace_with_root!(*names, tag: :div, attrs: {}, &block) namespace_with_root(*names, tag: tag, attrs: attrs, reset: true, &(block || Proc.new {})) end
Also aliased as: dnsr!
ns(*names)
click to toggle source
# File lib/xass/view_helpers.rb, line 46 def ns(*names) ns!(*(namespace_stack.last + names)) end
ns!(*names)
click to toggle source
# File lib/xass/view_helpers.rb, line 50 def ns!(*names) "#{ns_root!(*names[0...-1])}___#{names[-1]}" end
ns_content(name = :content, _tag = nil, _attrs = nil, tag: :div, attrs: {}, &block)
click to toggle source
# File lib/xass/view_helpers.rb, line 32 def ns_content(name = :content, _tag = nil, _attrs = nil, tag: :div, attrs: {}, &block) _tag ||= tag _attrs ||= attrs ns_wrap(name, tag: _tag, attrs: _attrs, &block) end
Also aliased as: nsc
ns_link_to(name, _name = nil, options = nil, html_options = nil, &block)
click to toggle source
# File lib/xass/view_helpers.rb, line 54 def ns_link_to(name, _name = nil, options = nil, html_options = nil, &block) if block options ||= {} link_to(_name, attrs_with_additional_name(options, name), &block) else html_options ||= {} link_to(_name, options, attrs_with_additional_name(html_options, name), &block) end end
ns_root(*names)
click to toggle source
# File lib/xass/view_helpers.rb, line 38 def ns_root(*names) ns_root!(*(namespace_stack.last + names)) end
Also aliased as: nsr
ns_root!(*names)
click to toggle source
# File lib/xass/view_helpers.rb, line 42 def ns_root!(*names) names.map(&:to_s).join('__') end
Also aliased as: nsr!
ns_wrap(name = :wrap, _tag = nil, _attrs = nil, tag: :div, attrs: {}, &block)
click to toggle source
# File lib/xass/view_helpers.rb, line 26 def ns_wrap(name = :wrap, _tag = nil, _attrs = nil, tag: :div, attrs: {}, &block) _tag ||= tag _attrs ||= attrs content_tag(_tag, block ? capture(&block) : '', attrs_with_additional_name(_attrs, name)) end
Private Instance Methods
attrs_with_additional_class(attrs, klass)
click to toggle source
# File lib/xass/view_helpers.rb, line 78 def attrs_with_additional_class(attrs, klass) attrs.symbolize_keys! attrs[:class] = attrs[:class].blank? ? klass : "#{attrs[:class]} #{klass}" attrs end
attrs_with_additional_name(attrs, name)
click to toggle source
# File lib/xass/view_helpers.rb, line 84 def attrs_with_additional_name(attrs, name) attrs_with_additional_class(attrs, ns(name)) end
namespace_stack()
click to toggle source
# File lib/xass/view_helpers.rb, line 74 def namespace_stack @namespace_stack ||= [[]] end
process_namespace_classes(node, n)
click to toggle source
# File lib/xass/view_helpers.rb, line 88 def process_namespace_classes(node, n) unless node['class'].blank? node['class'] = node['class'].split(/ +/).map { |c| if c.start_with?('ns-') ns!(*n, *c[3..-1].split('--')) elsif c.start_with?('nsb-') ns!(*c[4..-1].split('--')) elsif c.start_with?('dns-') n = n + c[4..-1].split('--') '' elsif c.start_with?('dnsb-') n = c[5..-1].split('--') '' elsif c == 'nsr' nsr!(*n) elsif c.start_with?('nsr-') nsr!(*n, *c[4..-1].split('--')) elsif c.start_with?('nsrb-') nsr!(*c[5..-1].split('--')) elsif c.start_with?('dnsr-') n = n + c[5..-1].split('--') nsr!(*n) elsif c.start_with?('dnsrb-') n = c[6..-1].split('--') nsr!(*n) else c end }.join(' ') end node.children.each do |c| process_namespace_classes(c, n) end end