Class: Caboose::Block
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Caboose::Block
- Defined in:
- app/models/caboose/block.rb
Class Method Summary collapse
-
.parse_checkbox_multiple_value(b, arr) ⇒ Object
Parses the value given for a checkbox multiple block.
- .unique_file_upload_name(str) ⇒ Object
- .unique_image_upload_name(str) ⇒ Object
Instance Method Summary collapse
- #block_message(block, ex) ⇒ Object
- #caste_value ⇒ Object
- #child(name) ⇒ Object
- #child_value(name) ⇒ Object
- #constrain_all ⇒ Object
- #create_children(block_type_override = nil) ⇒ Object
-
#duplicate_page_block(site_id, page_id, new_block_type_id = nil, new_parent_id = nil) ⇒ Object
Assumes that we start the duplicate process at the top level block.
- #full_name ⇒ Object
-
#get_global_value(site_id) ⇒ Object
Returns the master block for this global block.
- #js_hash ⇒ Object
- #log_helper(prefix = '') ⇒ Object
- #migrate_media ⇒ Object
-
#move_down ⇒ Object
Move a block down.
-
#move_up ⇒ Object
Move a block up.
- #partial(name, options) ⇒ Object
- #partial_message(name, ex) ⇒ Object
- #render(block, options) ⇒ Object
- #render_from_function(options) ⇒ Object
- #render_helper(view, options, block, full_name, arr, i) ⇒ Object
- #rendered_child_value(name, options) ⇒ Object
-
#title ⇒ Object
def child_block_link return “<div class=‘new_block’ id=‘new_block_#selfself.id’>New Block</div>” end def new_block_before_link return “<div class=‘new_block_before’ id=‘new_block_before_#selfself.id’>New Block</div>” end def new_block_after_link return “<div class=‘new_block_after’ id=‘new_block_after_#selfself.id’>New Block</div>” end.
- #unique_file_upload_name(str) ⇒ Object
- #unique_image_upload_name(str) ⇒ Object
-
#update_global_value(value, site_id) ⇒ Object
Updates all the global blocks for the given site.
Class Method Details
.parse_checkbox_multiple_value(b, arr) ⇒ Object
Parses the value given for a checkbox multiple block
418 419 420 421 422 423 424 425 426 427 428 |
# File 'app/models/caboose/block.rb', line 418 def self.parse_checkbox_multiple_value(b, arr) current_value = b.value ? b.value.split('|') : [] v = arr[0] checked = arr[1].to_i == 1 if checked && !current_value.include?(v) current_value << v elsif !checked && current_value.include?(v) current_value.delete(v) end return current_value.join('|') end |
.unique_file_upload_name(str) ⇒ Object
499 500 501 502 503 504 505 506 507 508 |
# File 'app/models/caboose/block.rb', line 499 def self.unique_file_upload_name(str) base = str.downcase.gsub(' ', '-').gsub(/[^\w-]/, '') str = "#{base}" i = 1 while Caboose::Block.where("file_upload_name = ?", str).exists? && i < 100 do str = "#{base}-#{i}" i = i + 1 end return str end |
.unique_image_upload_name(str) ⇒ Object
510 511 512 513 514 515 516 517 518 519 |
# File 'app/models/caboose/block.rb', line 510 def self.unique_image_upload_name(str) base = str.downcase.gsub(' ', '-').gsub(/[^\w-]/, '') str = "#{base}" i = 1 while Caboose::Block.where("image_upload_name = ?", str).exists? && i < 100 do str = "#{base}-#{i}" i = i + 1 end return str end |
Instance Method Details
#block_message(block, ex) ⇒ Object
261 262 263 264 265 266 267 268 269 |
# File 'app/models/caboose/block.rb', line 261 def (block, ex) msg = block ? (block.block_type ? "Error with #{block.block_type.name} block (block_type_id #{block.block_type.id}, block_id #{block.id})\n" : "Error with block (block_id #{block.id})\n") : '' if ex.is_a?(String) Caboose.log("#{msg}#{ex}") else Caboose.log("#{msg}#{ex.}\n#{ex.backtrace.join("\n")}") end return msg end |
#caste_value ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'app/models/caboose/block.rb', line 41 def caste_value if self.block_type_id.nil? bt = Caboose::BlockType.where(:field_type => 'text').first if bt.nil? bt = Caboose::BlockType.create(:name => 'text', :description => 'Text', :field_type => 'text', :default => '', :width => 800, :height => 400, :fixed_placeholder => false) end self.block_type_id = bt.id end #if self.block_type_id.field_type.nil? # self.block_type.field_type = 'text' #end #if self.block_type.field_type == 'checkbox' # v = self.value # self.value = v ? (v == 1 || v == '1' || v == true ? 1 : 0) : 0 #end end |
#child(name) ⇒ Object
95 96 97 |
# File 'app/models/caboose/block.rb', line 95 def child(name) Caboose::Block.where("parent_id = ? and name = ?", self.id, name).first end |
#child_value(name) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'app/models/caboose/block.rb', line 63 def child_value(name) b = self.child(name) return nil if b.nil? if b.block_type.field_type == 'image' return b.media.image if b.media return b.image end if b.block_type.field_type == 'file' return b.media.file if b.media return b.file end #return b.image if b.block_type.field_type == 'image' #return b.file if b.block_type.field_type == 'file' return b.value end |
#constrain_all ⇒ Object
470 471 472 473 474 475 |
# File 'app/models/caboose/block.rb', line 470 def constrain_all self.children.each do |b| return false if b.full_width == true end return true end |
#create_children(block_type_override = nil) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'app/models/caboose/block.rb', line 99 def create_children(block_type_override = nil) bt = block_type_override ? block_type_override : block_type bt.children.each do |bt2| bt_id = bt2.id #if bt2.parent_id # new_bt_id = Caboose::BlockType.where(:name => bt2.field_type).first.id #end if self.child(bt2.name).nil? b = Caboose::Block.create( :post_id => self.post_id, :page_id => self.page_id, :parent_id => self.id, :block_type_id => bt_id, :name => bt2.name, :value => bt2.default ) b.create_children(bt2) end end end |
#duplicate_page_block(site_id, page_id, new_block_type_id = nil, new_parent_id = nil) ⇒ Object
Assumes that we start the duplicate process at the top level block
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 |
# File 'app/models/caboose/block.rb', line 554 def duplicate_page_block(site_id, page_id, new_block_type_id = nil, new_parent_id = nil) m = self.media_id ? self.media.duplicate(site_id) : nil b = Caboose::Block.create( :page_id => page_id, :post_id => nil, :parent_id => new_parent_id, :media_id => self.media_id ? m.id : nil, :block_type_id => new_block_type_id, :sort_order => self.sort_order, :constrain => self.constrain, :full_width => self.full_width, :name => self.name, :value => self.value ) self.children.each do |b2| if b2.name # The block is part of the block type, so we have to find the corresponding child block in the new block type bt = Caboose::BlockType.where(:parent_id => new_block_type_id, :name => b2.name).first if bt b2.duplicate_page_block(site_id, page_id, bt.id, b.id) else # Don't duplicate it because the corresponding child block doesn't exist in the new block type end else # The block is a child block that isn't part of the block type definition b2.duplicate_page_block(site_id, page_id, b2.block_type_id, b.id) end end end |
#full_name ⇒ Object
58 59 60 61 |
# File 'app/models/caboose/block.rb', line 58 def full_name return self.name if parent_id.nil? return "#{parent.full_name}_#{self.name}" end |
#get_global_value(site_id) ⇒ Object
Returns the master block for this global block
385 386 387 388 389 390 391 392 393 394 395 |
# File 'app/models/caboose/block.rb', line 385 def get_global_value(site_id) return if !self.block_type.is_global return if !Caboose::Block.includes(:page).where("blocks.id <> ? and block_type_id = ? and pages.site_id = ?", self.id, self.block_type_id, site_id).exists? b = Caboose::Block.includes(:page).where("blocks.id <> ? and block_type_id = ? and pages.site_id = ?", self.id, self.block_type_id, site_id).reorder("blocks.id").first self.value = b.value self.save self.children.each do |b2| b2.get_global_value(site_id) if b2.block_type.is_global end end |
#js_hash ⇒ Object
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 |
# File 'app/models/caboose/block.rb', line 334 def js_hash kids = self.children.collect { |b| b.js_hash } bt = self.block_type return { 'id' => self.id, 'post_id' => self.post_id, 'page_id' => self.page_id, 'parent_id' => self.parent_id, 'parent_title' => self.parent ? self.parent.title : '', 'block_type_id' => self.block_type_id, 'sort_order' => self.sort_order, 'name' => self.name, 'value' => self.value, 'children' => kids, 'block_type' => { 'id' => bt.id, 'parent_id' => bt.parent_id, 'name' => bt.name, 'description' => bt.description, 'render_function' => bt.render_function, 'use_render_function' => bt.use_render_function, 'use_render_function_for_layout' => bt.use_render_function_for_layout, 'allow_child_blocks' => bt.allow_child_blocks, 'field_type' => bt.field_type, 'default' => bt.default, 'width' => bt.width, 'height' => bt.height, 'fixed_placeholder' => bt.fixed_placeholder, 'options' => bt., 'options_function' => bt., 'options_url' => bt. }, 'file' => { 'url' => self.file.url }, 'image' => { 'tiny_url' => self.image.url(:tiny), 'thumb_url' => self.image.url(:thumb), 'large_url' => self.image.url(:large), } } end |
#log_helper(prefix = '') ⇒ Object
377 378 379 380 381 382 |
# File 'app/models/caboose/block.rb', line 377 def log_helper(prefix = '') puts "#{prefix}#{self.id}" self.children.each do |b| b.log_helper("#{prefix}-") end end |
#migrate_media ⇒ Object
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 |
# File 'app/models/caboose/block.rb', line 521 def migrate_media if self.block_type.field_type == 'image' && !self.image_file_name.nil? && self.media_id.nil? site_id = self.page_id ? self.page.site_id : self.post.site_id cat = Caboose::MediaCategory.top_category(site_id) m = self.media m = Caboose::Media.create(:media_category_id => cat.id, :original_name => self.image_file_name, :name => Caboose::Media.upload_name(self.image_file_name)) if m.nil? m.image = URI.parse(self.image.url(:original)) m.processed = true m.save self.media_id = m.id self.save elsif self.block_type.field_type == 'file' && !self.file_file_name.nil? && self.media_id.nil? site_id = self.page_id ? self.page.site_id : self.post.site_id cat = Caboose::MediaCategory.top_category(site_id) m = self.media m = Caboose::Media.create(:media_category_id => cat.id, :original_name => self.file_file_name, :name => Caboose::Media.upload_name(self.file_file_name)) if m.nil? m.file = URI.parse(self.file.url) m.processed = true m.save self.media_id = m.id self.save end end |
#move_down ⇒ Object
Move a block down
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 |
# File 'app/models/caboose/block.rb', line 451 def move_down siblings = Caboose::Block.where(:parent_id => self.parent_id).reorder(:sort_order).all siblings.each_with_index do |b2, i| b2.sort_order = i b2.save end changed = false siblings.each_with_index do |b2, i| if i < (siblings.count-1) && b2.id == self.id siblings[i+1].sort_order = i siblings[i+1].save b2.sort_order = i + 1 b2.save changed = true end end return changed end |
#move_up ⇒ Object
Move a block up
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 |
# File 'app/models/caboose/block.rb', line 431 def move_up siblings = Caboose::Block.where(:parent_id => self.parent_id).reorder(:sort_order).all siblings.each_with_index do |b2, i| b2.sort_order = i b2.save end changed = false siblings.each_with_index do |b2, i| if i > 0 && b2.id == self.id siblings[i-1].sort_order = i siblings[i-1].save b2.sort_order = i - 1 b2.save changed = true end end return changed end |
#partial(name, options) ⇒ Object
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'app/models/caboose/block.rb', line 279 def partial(name, ) defaults = { :modal => false, :empty_text => '', :editing => false, :css => nil, :js => nil } = nil if .is_a?(Hash) = defaults.merge() else = { :modal => .modal, :empty_text => .empty_text, :editing => .editing, :css => .css, :js => .js } end [:block] = self view = ActionView::Base.new(ActionController::Base.view_paths) site = [:site] begin str = view.render(:partial => "../../sites/#{site.name}/blocks/#{name}", :locals => ) rescue ActionView::MissingTemplate => ex begin str = view.render(:partial => "caboose/blocks/#{name}", :locals => ) rescue Exception => ex Caboose.log("Partial caboose/blocks/#{name} doesn't exist.") str = "<p class='note error'>#{self.(name, ex)}</p>" end end return str end |
#partial_message(name, ex) ⇒ Object
306 307 308 309 310 311 312 313 314 |
# File 'app/models/caboose/block.rb', line 306 def (name, ex) msg = "Error with partial #{name}:\n" if ex.is_a?(String) Caboose.log("#{msg}#{ex}") else Caboose.log("#{msg}#{ex.}\n#{ex.backtrace.join("\n")}") end return msg end |
#render(block, options) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'app/models/caboose/block.rb', line 120 def render(block, ) #Caboose.log("block.full_name = #{block.full_name}") #Caboose.log("block.render\nself.id = #{self.id}\nblock = #{block}\nblock.full_name = #{block.full_name}\noptions.class = #{options.class}\noptions = #{options}") if block && block.is_a?(String) #Caboose.log("Block #{block} is a string, finding block object... self.id = #{self.id}") b = self.child(block) if b.nil? self.create_children b = self.child(block) if b.nil? Caboose.log("No block exists with name \"#{block}\".") return false end end block = b end str = "" defaults = { :view => nil, :controller_view_content => nil, :modal => false, :empty_text => '', :editing => false, :css => nil, :js => nil, :csrf_meta_tags => nil, :csrf_meta_tags2 => nil, :logged_in_user => nil } #defaults = { :modal => false, :empty_text => '', :editing => false, :css => nil, :js => nil, :block => block } = nil if .is_a?(Hash) = defaults.merge() else #options2 = { :modal => options.modal, :empty_text => options.empty_text, :editing => options.editing, :css => options.css, :js => options.js } = { :view => .view ? .view : nil, :controller_view_content => .controller_view_content ? .controller_view_content : nil, :modal => .modal ? .modal : nil, :empty_text => .empty_text ? .empty_text : nil, :editing => .editing ? .editing : nil, :css => .css ? .css : nil, :js => .js ? .js : nil, :csrf_meta_tags => . ? . : nil, :csrf_meta_tags2 => . ? . : nil, :logged_in_user => .logged_in_user ? .logged_in_user : nil } end [:block] = block view = [:view] view = ActionView::Base.new(ActionController::Base.view_paths) if view.nil? if block.block_type.use_render_function && block.block_type.render_function begin str = view.render(:partial => "caboose/blocks/render_function", :locals => ) rescue Exception => ex msg = block ? (block.block_type ? "Error with #{block.block_type.name} block (block_type_id #{block.block_type.id}, block_id #{block.id})\n" : "Error with block (block_id #{block.id})\n") : '' Caboose.log("#{msg}#{ex.}\n#{ex.backtrace.join("\n")}") str = "<p class='note error'>#{msg}</p>" end else full_name = block.block_type.full_name full_name = "lksdjflskfjslkfjlskdfjlkjsdf" if full_name.nil? || full_name.length == 0 # Check the local site first site = [:site] if site.nil? self.(block, "Error: site variable is nil.") end #begin str = view.render(:partial => "../../sites/#{site.name}/blocks/#{full_name}", :locals => options2) #rescue ActionView::MissingTemplate => ex # begin str = view.render(:partial => "../../sites/#{site.name}/blocks/#{block.block_type.name}", :locals => options2) # rescue ActionView::MissingTemplate => ex # begin str = view.render(:partial => "../../sites/#{site.name}/blocks/#{block.block_type.field_type}", :locals => options2) # rescue ActionView::MissingTemplate => ex # begin str = view.render(:partial => "../../app/views/caboose/blocks/#{full_name}", :locals => options2) # rescue ActionView::MissingTemplate => ex # begin str = view.render(:partial => "../../app/views/caboose/blocks/#{block.block_type.name}", :locals => options2) # rescue ActionView::MissingTemplate => ex # begin str = view.render(:partial => "../../app/views/caboose/blocks/#{block.block_type.field_type}", :locals => options2) # rescue ActionView::MissingTemplate => ex # begin str = view.render(:partial => "caboose/blocks/#{full_name}", :locals => options2) # rescue ActionView::MissingTemplate => ex # begin str = view.render(:partial => "caboose/blocks/#{block.block_type.name}", :locals => options2) # rescue ActionView::MissingTemplate => ex # begin str = view.render(:partial => "caboose/blocks/#{block.block_type.field_type}", :locals => options2) # rescue Exception => ex # str = "<p class='note error'>#{self.block_message(block, ex)}</p>" # end # rescue Exception => ex { str = "<p class='note error'>#{self.block_message(block, ex)}</p>" } # end # rescue Exception => ex { str = "<p class='note error'>#{self.block_message(block, ex)}</p>" } # end # rescue Exception => ex { str = "<p class='note error'>#{self.block_message(block, ex)}</p>" } # end # rescue Exception => ex { str = "<p class='note error'>#{self.block_message(block, ex)}</p>" } # end # rescue Exception => ex { str = "<p class='note error'>#{self.block_message(block, ex)}</p>" } # end # rescue Exception => ex { str = "<p class='note error'>#{self.block_message(block, ex)}</p>" } # end #rescue Exception => ex { str = "<p class='note error'>#{self.block_message(block, ex)}</p>" } #end arr = [ "../../sites/#{site.name}/blocks/#{full_name}", "../../sites/#{site.name}/blocks/#{block.block_type.name}", "../../sites/#{site.name}/blocks/#{block.block_type.field_type}", "../../app/views/caboose/blocks/#{full_name}", "../../app/views/caboose/blocks/#{block.block_type.name}", "../../app/views/caboose/blocks/#{block.block_type.field_type}", "caboose/blocks/#{full_name}", "caboose/blocks/#{block.block_type.name}", "caboose/blocks/#{block.block_type.field_type}" ] #Caboose.log(arr) str = self.render_helper(view, , block, full_name, arr, 0) end return str end |
#render_from_function(options) ⇒ Object
271 272 273 274 275 276 277 |
# File 'app/models/caboose/block.rb', line 271 def render_from_function() self.create_children #locals = OpenStruct.new(:block => self, :empty_text => empty_text, :editing => editing) locals = OpenStruct.new() erb = ERB.new(block_type.render_function) return erb.result(locals.instance_eval { binding }) end |
#render_helper(view, options, block, full_name, arr, i) ⇒ Object
246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'app/models/caboose/block.rb', line 246 def render_helper(view, , block, full_name, arr, i) return "<p class='note error'>Could not find block view anywhere.</p>" if i > arr.count begin str = view.render(:partial => arr[i], :locals => ) #Caboose.log("Level #{i+1} for #{full_name}: Found partial #{arr[i]}") rescue ActionView::MissingTemplate => ex #Caboose.log("Level #{i+1} for #{full_name}: #{ex.message}") str = render_helper(view, , block, full_name, arr, i+1) rescue Exception => ex #Caboose.log("Level #{i+1} for #{full_name}: #{ex.message}") str = "<p class='note error'>#{self.(block, ex)}</p>" end return str end |
#rendered_child_value(name, options) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'app/models/caboose/block.rb', line 79 def rendered_child_value(name, ) b = self.child(name) return nil if b.nil? if b.block_type.field_type == 'image' return b.media.image if b.media return b.image end if b.block_type.field_type == 'file' return b.media.file if b.media return b.file end return "" if b.value.nil? || b.value.strip.length == 0 view = && [:view] ? [:view] : ActionView::Base.new(ActionController::Base.view_paths) return view.render(:inline => b.value, :locals => ) end |
#title ⇒ Object
def child_block_link
return "<div class='new_block' id='new_block_#{self.id}'>New Block</div>"
end
def new_block_before_link
return "<div class='new_block_before' id='new_block_before_#{self.id}'>New Block</div>"
end
def new_block_after_link
return "<div class='new_block_after' id='new_block_after_#{self.id}'>New Block</div>"
end
326 327 328 329 330 331 332 |
# File 'app/models/caboose/block.rb', line 326 def title str = "#{self.block_type.name}" if self.name && self.name.strip.length > 0 str << " (#{self.name})" end return str end |
#unique_file_upload_name(str) ⇒ Object
477 478 479 480 481 482 483 484 485 486 |
# File 'app/models/caboose/block.rb', line 477 def unique_file_upload_name(str) base = str.downcase.gsub(' ', '-').gsub(/[^\w-]/, '') str = "#{base}" i = 1 while Caboose::Block.where("id <> ? and file_upload_name = ?", self.id, str).exists? && i < 100 do str = "#{base}-#{i}" i = i + 1 end return str end |
#unique_image_upload_name(str) ⇒ Object
488 489 490 491 492 493 494 495 496 497 |
# File 'app/models/caboose/block.rb', line 488 def unique_image_upload_name(str) base = str.downcase.gsub(' ', '-').gsub(/[^\w-]/, '') str = "#{base}" i = 1 while Caboose::Block.where("id <> ? and image_upload_name = ?", self.id, str).exists? && i < 100 do str = "#{base}-#{i}" i = i + 1 end return str end |
#update_global_value(value, site_id) ⇒ Object
Updates all the global blocks for the given site
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 |
# File 'app/models/caboose/block.rb', line 398 def update_global_value(value, site_id) return if !self.block_type.is_global return if !Caboose::Block.includes(:page).where("blocks.id <> ? and block_type_id = ? and pages.site_id = ?", self.id, self.block_type_id, site_id).exists? sql = nil if self.page_id sql = ["update blocks set value = ? where id in ( select B.id from blocks B left join pages P on B.page_id = P.id where B.id <> ? and B.block_type_id = ? and P.site_id = ? )", value, self.id, self.block_type_id, site_id] elsif self.post_id sql = ["update blocks set value = ? where id in ( select B.id from blocks B left join posts P on B.post_id = P.id where B.id <> ? and B.block_type_id = ? and P.site_id = ? )", value, self.id, self.block_type_id, site_id] end ActiveRecord::Base.connection.execute(ActiveRecord::Base.send(:sanitize_sql_array, sql)) end |