lib/zafu/node_context.rb in zafu-0.7.9 vs lib/zafu/node_context.rb in zafu-0.8.0
- old
+ new
@@ -11,10 +11,13 @@
# The current DOM prefix to use when building DOM ids. This is set by the parser when
# it has a name or dom id defined ('main', 'related', 'list', etc).
attr_writer :dom_prefix
+ # This is used to force a given dom_id (in saved templates for example).
+ attr_accessor :saved_dom_id
+
# Any kind of information that the compiler might need to use (QueryBuilder query used
# to fetch the node for example).
attr_reader :opts
def initialize(name, klass, up = nil, opts = {})
@@ -46,11 +49,13 @@
# The convention is to use the class of the current object to build this name.
# You can also use an 'after_class' parameter to move up in the current object's class hierarchy to get
# ivar name (see #master_class).
def as_main(after_class = nil)
klass = after_class ? master_class(after_class) : single_class
- self.class.new("@#{klass.to_s.underscore}", single_class)
+ res = self.class.new("@#{klass.to_s.underscore}", single_class, nil, :new_record => @opts[:new_record])
+ res.dom_prefix = self.dom_prefix
+ res
end
# Find the class just afer 'after_class' in the class hierarchy.
# For example if we have Dog < Mamal < Animal < Creature,
# master_class(Creature) would return Animal
@@ -62,28 +67,39 @@
end while klass = up
return self.klass
end
# Generate a unique DOM id for this element based on dom_scopes defined in parent contexts.
+ # :code option returns ruby
+ # :erb option returns either string content or "<%= ... %>"
+ # default returns something to insert in interpolated string such as '#{xxx}'
def dom_id(opts = {})
dom_prefix = opts[:dom_prefix] || self.dom_prefix
options = {:list => true, :erb => true}.merge(opts)
- if options[:erb]
- dom = dom_id(options.merge(:erb => false))
+ if options[:erb] || options[:code]
+ dom = dom_id(options.merge(:erb => false, :code => false))
+
if dom =~ /^#\{([^\{]+)\}$/
- "<%= #{$1} %>"
+ code = $1
elsif dom =~ /#\{/
- "<%= %Q{#{dom}} %>"
+ code = "%Q{#{dom}}"
else
- dom
+ str = dom
+ code = dom.inspect
end
- else
- if @up
- [dom_prefix] + @up.dom_scopes + (options[:list] ? [make_scope_id] : [])
+
+ if options[:code]
+ code
else
- [dom_prefix] + (options[:list] ? [make_scope_id] : [])
- end.compact.uniq.join('_')
+ str || "<%= #{code} %>"
+ end
+ else
+ @saved_dom_id || (
+ [dom_prefix] +
+ dom_scopes +
+ (options[:list] ? [make_scope_id] : [])
+ ).compact.uniq.join('_')
end
end
# This holds the current context's unique name if it has it's own or one from the hierarchy. If
# none is found, it builds one.
\ No newline at end of file