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

#settingsObject

Returns the value of attribute settings.



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

def settings
  @settings
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



67
68
69
70
71
# File 'app/models/polyblock/block.rb', line 67

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

.fetch_or_initialize(name) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/models/polyblock/block.rb', line 55

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



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

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

.random_idObject



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

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



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

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)


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

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

#present?Boolean

Returns:

  • (Boolean)


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

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

#system?Boolean

Returns:

  • (Boolean)


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

def system?
  self.contentable.nil?
end

#to_sObject



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

def to_s
  content
end