Class: Polyblock::Block
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Polyblock::Block
- 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
-
#settings ⇒ Object
Returns the value of attribute settings.
Class Method Summary collapse
- .export ⇒ Object
- .fetch_or_create(name) ⇒ Object
- .fetch_or_initialize(name) ⇒ Object
- .import(pbs) ⇒ Object
- .next_id ⇒ Object
- .random_id ⇒ Object
- .strip_html(str) ⇒ Object
Instance Method Summary collapse
Instance Attribute Details
#settings ⇒ Object
Returns the value of attribute settings.
5 6 7 |
# File 'app/models/polyblock/block.rb', line 5 def settings @settings end |
Class Method Details
.export ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'app/models/polyblock/block.rb', line 31 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
68 69 70 71 72 |
# File 'app/models/polyblock/block.rb', line 68 def self.fetch_or_create(name) pb = fetch_or_initialize(name) pb.save if pb.new_record? pb end |
.fetch_or_initialize(name) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'app/models/polyblock/block.rb', line 56 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
22 23 24 25 26 27 28 29 |
# File 'app/models/polyblock/block.rb', line 22 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_id ⇒ Object
74 75 76 |
# File 'app/models/polyblock/block.rb', line 74 def self.next_id any? ? maximum(:id).next : 1 end |
.random_id ⇒ Object
77 78 79 80 |
# File 'app/models/polyblock/block.rb', line 77 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
82 83 84 85 86 87 |
# File 'app/models/polyblock/block.rb', line 82 def self.strip_html(str) output = str.gsub('<br><br>', '<br>').gsub(' ', ' ') output = CGI.unescapeHTML(output) output = ActionController::Base.helpers.(output) output.html_safe end |
Instance Method Details
#blank? ⇒ Boolean
48 49 50 |
# File 'app/models/polyblock/block.rb', line 48 def blank? nil? || content.blank? end |
#present? ⇒ Boolean
44 45 46 |
# File 'app/models/polyblock/block.rb', line 44 def present? !nil? && content.present? end |
#system? ⇒ Boolean
52 53 54 |
# File 'app/models/polyblock/block.rb', line 52 def system? self.contentable.nil? end |
#to_s ⇒ Object
40 41 42 |
# File 'app/models/polyblock/block.rb', line 40 def to_s content end |