lib/zafu/node_context.rb in zafu-0.6.0 vs lib/zafu/node_context.rb in zafu-0.6.1

- old
+ new

@@ -34,14 +34,15 @@ end # Return a new node context that corresponds to the current object when rendered alone (in an ajax response or # from a direct 'show' in a controller). The returned node context has no parent (up is nil). # 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 (see #master_class). + # 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) : self.klass - NodeContext.new("@#{klass.to_s.underscore}", klass) + klass = after_class ? master_class(after_class) : Array(self.klass).first + NodeContext.new("@#{klass.to_s.underscore}", Array(self.klass).first) 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 @@ -54,16 +55,27 @@ end while klass = up return self.klass end # Generate a unique DOM id for this element based on dom_scopes defined in parent contexts. - def dom_id - @dom_id ||= begin + def dom_id(opts = {}) + options = {:list => true, :erb => true}.merge(opts) + + if options[:erb] + dom = dom_id(options.merge(:erb => false)) + if dom =~ /^#\{([^\{]+)\}$/ + "<%= #{$1} %>" + elsif dom =~ /#\{/ + "<%= %Q{#{dom}} %>" + else + dom + end + else if @up - [dom_prefix] + @up.dom_scopes + [make_scope_id] + [dom_prefix] + @up.dom_scopes + (options[:list] ? [make_scope_id] : []) else - [dom_prefix] + [make_scope_id] + [dom_prefix] + (options[:list] ? [make_scope_id] : []) end.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 @@ -72,24 +84,24 @@ @dom_prefix || (@up ? @up.dom_prefix : nil) end # Mark the current context as being a looping element (each) whose DOM id needs to be propagated to sub-nodes # in order to ensure uniqueness of the dom_id (loops in loops problem). - def dom_scope! + def propagate_dom_scope! @dom_scope = true end def get(klass) if list_context? - if klass <= self.klass.first + if self.klass.first <= klass NodeContext.new("#{self.name}.first", self.klass.first) elsif @up @up.get(klass) else nil end - elsif self.klass.ancestors.include?(klass) + elsif self.klass <= klass return self elsif @up @up.get(klass) else nil @@ -132,9 +144,9 @@ (@up ? @up.dom_scopes : []) + (@dom_scope ? [make_scope_id] : []) end private def make_scope_id - "<%= #{@name}.zip %>" + "\#{#{@name}.zip}" end end end \ No newline at end of file