Sha256: 719afc0aa538d3b528d2382bb962487f3955a455de4f683aeb58cb5251d93e80

Contents?: true

Size: 1.73 KB

Versions: 1

Compression:

Stored size: 1.73 KB

Contents

class Rtml::Dom::FrontendElement < Rtml::Dom::Element
  def screen
    return @screen if @screen
    p = self
    while (p = p.parent).kind_of?(Rtml::Dom::Element)
      return @screen = p if p.name == 'screen'
    end
    raise "Could not find screen for #{self.inspect}"
  end

  def inner_content=(text)
    self.property 'inner_content', text
  end

  def content_options=(hash)
    hash.each do |key, value|
      self.property key, value
    end
  end

  def to_tml
    if self.property('inner_content').blank?
      to_tml_with_view
    else
      self.property('inner_content').to_s
    end
  end

  private
  def options
    options = HashWithIndifferentAccess.new
    properties.each { |p| options[p.name] = p.value unless p.blank? }
    preprocess_options(options)
  end

  def preprocess_options(options)
    if !has_filename_or_content?(options)
      options[:file] = screen.path
    elsif document && document.name
      options.each do |key, value|
        case key
          when 'partial', 'file', 'template'
            options[key] = "#{document.name}/#{value}" unless document.name.blank? or value =~ /\//
        end
      end
    end
    options
  end

  def has_filename_or_content?(options)
    (options.keys - %w(partial file template text)) != options.keys
  end

  def to_tml_with_view
    # Options is passed directly into the document's view.
    if document
      copy_ivars_to_view(options[:assigns])
      document.render(options, options[:locals])
    else
      raise Rtml::Errors::MissingDocumentError, "No document through which to render options: #{options.inspect}"
    end
  end

  def copy_ivars_to_view(hash)
    hash.each do |name, value|
      document.view.instance_variable_set("@#{name}", value)
    end if hash
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rtml-2.0.4 builtin/models/rtml/dom/frontend_element.rb