Sha256: 9f104d8f64d396b6979a8e7d7a2ba0f13ecdf69368a37004260e7acd366d2d82

Contents?: true

Size: 1.09 KB

Versions: 4

Compression:

Stored size: 1.09 KB

Contents

class BlockInstance < ActiveRecord::Base
  belongs_to :page
  belongs_to :user
  belongs_to :block
  
  validates :instance_id, :presence=>true, :length=>{:minimum=>1, :maximum=>200}

  def display_version
    return "Current" if self.version==0
    return "Draft" if self.version==-1
    return "Autosave" if self.version==-2
    return self.version
  end

  def prompt
    body = self.block.body

    if body =~ /\[\[#{self.field_name}\:[^\:]+\:([^\:]+)\]\]/
      return $1
    else
      field_name.titleize
    end
  end

  def render
    return "[No block definition #{self.block_id}]" unless self.block

    self.block.body.gsub(/\[\[([^\:]+)\:([^\:]+)\:[^\]]+\]\]/) do
      bi = self.page.get_block_instances(self.instance_id, self.version, $1, self.block_id)
      return "[No block instances]" unless bi
      block_instance = bi.first
      if block_instance && block_instance.field_value
        if $2 == "friendly"
          block_instance.field_value.friendly_format 
        else
          block_instance.field_value
        end
      else
        "[Missing value '#{$1}']"
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
kit_cms-2.3.9 app/models/block_instance.rb
kit_cms-2.3.8 app/models/block_instance.rb
kit_cms-2.3.7 app/models/block_instance.rb
kit_cms-2.3.6 app/models/block_instance.rb