lib/zafu/node_context.rb in zafu-0.7.4 vs lib/zafu/node_context.rb in zafu-0.7.5
- old
+ new
@@ -20,11 +20,11 @@
def initialize(name, klass, up = nil, opts = {})
@name, @klass, @up, @opts = name, klass, up, opts
end
def move_to(name, klass, opts={})
- NodeContext.new(name, klass, self, opts)
+ self.class.new(name, klass, self, opts)
end
# Since the idiom to write the node context name is the main purpose of this class, it
# deserves this shortcut.
def to_s
@@ -36,21 +36,21 @@
end
# Return true if the NodeContext represents an element of the given type. We use 'will_be' because
# it is equivalent to 'is_a', but for future objects (during rendering).
def will_be?(type)
- single_class.ancestors.include?(type)
+ single_class <= type
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 to get
# ivar name (see #master_class).
def as_main(after_class = nil)
klass = after_class ? master_class(after_class) : single_class
- NodeContext.new("@#{klass.to_s.underscore}", single_class)
+ self.class.new("@#{klass.to_s.underscore}", single_class)
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
@@ -63,10 +63,11 @@
return self.klass
end
# Generate a unique DOM id for this element based on dom_scopes defined in parent contexts.
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 dom =~ /^#\{([^\{]+)\}$/
@@ -127,17 +128,19 @@
# Return the name of the current class with underscores like 'sub_page'.
def underscore
class_name.to_s.underscore
end
- # Return the class name or the superclass name if the current class is an anonymous class.
- # FIXME: just use klass.to_s (so that we can do clever things with 'to_s')
+ # Return the 'real' class name or the superclass name if the current class is an anonymous class.
def class_name
+ klass = single_class
+ while klass.name == ''
+ klass = klass.superclass
+ end
if list_context?
- klass = single_class
- "[#{(klass.name.blank? ? klass.superclass : klass).name}]"
+ "[#{klass}]"
else
- (@klass.name.blank? ? @klass.superclass : @klass).name
+ klass.name
end
end
# Return the name to use for input fields
def form_name
\ No newline at end of file