Class: TextAsset

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Radiant::Taggable
Defined in:
app/models/text_asset.rb

Direct Known Subclasses

Javascript, Stylesheet

Defined Under Namespace

Classes: TagError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_from_file(file) ⇒ Object

Takes an uploaded file (in memory) and creates a new text asset from it



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/models/text_asset.rb', line 47

def self.create_from_file(file)
  @text_asset = self.new
  if file.blank?
    @text_asset.errors.add(:uploaded_file, 'no file submitted for upload')

  elsif !file.kind_of?(ActionController::UploadedFile)
    @text_asset.errors.add(:uploaded_file, 'unusable format')

  elsif file.size > 262144 # 256k (that's a HUGE script or stylesheet)
    @text_asset.errors.add(:uploaded_file, 'file size larger than 256kB')

  else
    @text_asset.name = file.original_filename.gsub(/\s/, '-')
    @text_asset.content = file.read
    # everthing else passed so run through the std validations (save if valid)
    @text_asset.save
  end
  @text_asset
end

Instance Method Details

#parse(text) ⇒ Object

Parses the content using a TextAssetContext



37
38
39
40
41
42
43
# File 'app/models/text_asset.rb', line 37

def parse(text)
  unless @parser and @context
    @context = TextAssetContext.new(self)
    @parser = Radius::Parser.new(@context, :tag_prefix => 'r')
  end
  @parser.parse(text)
end

#renderObject

Parses, and filters the current content for output



31
32
33
# File 'app/models/text_asset.rb', line 31

def render
  self.filter.filter(parse(self.content))
end

#urlObject

URL relative to the web root (accounting for Sns::Config settings)



24
25
26
27
# File 'app/models/text_asset.rb', line 24

def url
  "/" + Sns::Config["#{self.class.to_s.underscore}_directory"] +
      "/" + self.name
end