Class: Ticker

Inherits:
Content
  • Object
show all
Defined in:
app/models/ticker.rb

Constant Summary

DISPLAY_NAME =
'Text'

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from Content

#action_allowed?, active, #after_add_callback, all_subclasses, #can_display_in?, #cannot_be_own_parent, display_name, #end_time=, expired, filter_all_content, future, #has_children?, #is_active?, #is_approved?, #is_denied?, #is_expired?, #is_orphan?, #is_pending?, #perform_action, #pre_render, #render_details, #start_time=

Class Method Details

+ (Object) clean_html(html)

clear out the unapproved html tags



44
45
46
47
48
49
# File 'app/models/ticker.rb', line 44

def self.clean_html(html)
  # sanitize gem erased '<<<'' whereas ActionView's was more discerning
  ActionController::Base.helpers.sanitize(html, 
    :tags => %w(h1 h2 h3 h4 div b br i em li ol u ul p q small strong), 
    :attributes => %w(style class)) unless html.nil?
end

+ (Object) convert_markdown(s)



38
39
40
41
# File 'app/models/ticker.rb', line 38

def self.convert_markdown(s)
  md = Redcarpet::Markdown.new(Redcarpet::Render::HTML)
  md.render(s)
end

+ (Object) form_attributes

Ticker Text also accepts the kind because the user can change it to HtmlText



57
58
59
60
# File 'app/models/ticker.rb', line 57

def self.form_attributes
  attributes = super()
  attributes.concat([:kind_id])
end

+ (Object) preview(data)

return the cleaned input data



52
53
54
# File 'app/models/ticker.rb', line 52

def self.preview(data)
  clean_html(convert_markdown(data.to_s))
end

Instance Method Details

- (Object) alter_type

if the user has specified that the kind should be text then change the type so this is just like an HtmlText content item instead of a Ticker content item



32
33
34
35
36
# File 'app/models/ticker.rb', line 32

def alter_type
  if self.kind == Kind.where(:name => 'Text').first
    self.type = 'HtmlText'
  end
end

- (Object) process_markdown

if markdown text is present in ticker, it will be converted to html and cleaned before it is saved



25
26
27
28
# File 'app/models/ticker.rb', line 25

def process_markdown
  self.data = self.class.convert_markdown(self.data)
  sanitize_html
end

- (Object) sanitize_html

make sure the data only contains authorized html tags



19
20
21
# File 'app/models/ticker.rb', line 19

def sanitize_html
  self.data = self.class.clean_html(self.data) unless self.data.nil?
end

- (Object) set_kind

Automatically set the kind for the content if it is new.



13
14
15
16
# File 'app/models/ticker.rb', line 13

def set_kind
  return unless new_record?
  self.kind = Kind.where(:name => 'Ticker').first
end