Class: Polyblock::Block

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exportObject



17
18
19
20
21
22
23
24
# File 'app/models/polyblock/block.rb', line 17

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



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

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

.fetch_or_initialize(name) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/polyblock/block.rb', line 38

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

.import(pbs) ⇒ Object



8
9
10
11
12
13
14
15
# File 'app/models/polyblock/block.rb', line 8

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



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

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

.random_idObject



59
60
61
62
# File 'app/models/polyblock/block.rb', line 59

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

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'app/models/polyblock/block.rb', line 34

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

#present?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'app/models/polyblock/block.rb', line 30

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

#to_sObject



26
27
28
# File 'app/models/polyblock/block.rb', line 26

def to_s
  content
end