Class: Polyblock::Block

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/polyblock/block.rb

Constant Summary collapse

DEFAULTS =
{
  :association => {
      :class_name => "Polyblock::Block",
      :as => :contentable,
      :dependent => :destroy
  },
  :nested_attributes => {},
  :validates => {},
  :html => true,
  :default_text => "[Editable content space for Polyblock <code>#{name}</code>]"
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#settings_cacheObject

Returns the value of attribute settings_cache.



5
6
7
# File 'app/models/polyblock/block.rb', line 5

def settings_cache
  @settings_cache
end

Class Method Details

.exportObject



30
31
32
33
34
35
36
37
# File 'app/models/polyblock/block.rb', line 30

def self.export
  output = all.as_json(:except => [:id, :created_at, :updated_at])
  puts ""
  puts "Run the following in Rails Console on the remote server:"
  puts ""
  puts "Polyblock::Block.import(#{output})"
  puts ""
end

.fetch_or_create(name) ⇒ Object



72
73
74
75
76
# File 'app/models/polyblock/block.rb', line 72

def self.fetch_or_create(name)
  pb = fetch_or_initialize(name)
  pb.save if pb.new_record?
  pb
end

.fetch_or_initialize(name) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/models/polyblock/block.rb', line 60

def self.fetch_or_initialize(name)
  if name.is_a? Block
    name
  elsif name.is_a? String
    matches = where :name => name
    if matches.any?
      matches.first
    else
      new({:name => name})
    end
  end
end

.import(pbs) ⇒ Object



21
22
23
24
25
26
27
28
# File 'app/models/polyblock/block.rb', line 21

def self.import(pbs)
  outputs = pbs.map do |pb|
    block = fetch_or_initialize pb['name']
    block.content = pb['content'] if pb.key?('content') && pb['content'].present? && pb['content'] != block.content
    block.save
  end
  outputs.include?(false)
end

.next_idObject



78
79
80
# File 'app/models/polyblock/block.rb', line 78

def self.next_id
  any? ? maximum(:id).next : 1
end

.random_idObject



81
82
83
84
# File 'app/models/polyblock/block.rb', line 81

def self.random_id
  o = [('a'..'z'), ('A'..'Z')].map { |i| i.to_a }.flatten
  (0...50).map { o[rand(o.length)] }.join
end

.strip_html(str) ⇒ Object



86
87
88
89
90
91
# File 'app/models/polyblock/block.rb', line 86

def self.strip_html(str)
  output = str.gsub('<br><br>', '<br>').gsub('&nbsp;', ' ')
  output = CGI.unescapeHTML(output)
  output = ActionController::Base.helpers.strip_tags(output)
  output.html_safe
end

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


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

def blank?
  nil? || content.blank?
end

#present?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'app/models/polyblock/block.rb', line 48

def present?
  !nil? && content.present?
end

#settingsObject



39
40
41
42
# File 'app/models/polyblock/block.rb', line 39

def settings
  return self.settings_cache unless self.settings_cache.nil?
  self.settings_cache = self.contentable.present? ? self.contentable.send("#{self.name}_settings") : DEFAULTS
end

#system?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'app/models/polyblock/block.rb', line 56

def system?
  self.contentable.nil?
end

#to_sObject



44
45
46
# File 'app/models/polyblock/block.rb', line 44

def to_s
  content
end