ref: master
plugins/container_block/lib/container_block_plugin/container_block.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
class ContainerBlockPlugin::ContainerBlock < Block after_create :create_box after_destroy :destroy_children after_destroy :destroy_box settings_items :container_box_id, :type => Integer, :default => nil settings_items :children_settings, :type => Hash, :default => {} validate :no_cyclical_reference, :if => 'container_box_id.present?' def no_cyclical_reference errors.add(:box_id, c_('cyclical reference is not allowed.')) if box_id == container_box_id end before_save do |b| raise "cyclical reference is not allowed" if b.box_id == b.container_box_id && !b.container_box_id.blank? end def self.description _('Container') end def help _('This block acts as a container for another blocks') end def cacheable? false end def layout_template nil end def destroy_children blocks.destroy_all end def create_box container_box = Box.new(:owner => owner) container_box.save! settings[:container_box_id] = container_box.id copy_blocks unless @blocks_to_copy.blank? container_box.update_attribute(:position, nil) save! end def destroy_box container_box.destroy end def container_box owner.boxes.find(container_box_id) end def block_classes=(classes) classes.each { |c| block = c.constantize.create!(:box_id => container_box.id) } if classes end def blocks container_box.blocks end def child_width(child_id) children_settings[child_id][:width] if children_settings[child_id] end def copy_from_with_container(block) copy_from_without_container(block) children_settings = block.children_settings @blocks_to_copy = block.blocks end alias_method_chain :copy_from, :container def copy_blocks new_children_settings = {} @blocks_to_copy.map do |child| new_block = child.class.new(:title => child[:title]) new_block.copy_from(child) container_box.blocks << new_block new_children_settings[new_block.id] = children_settings[child.id] if children_settings[child.id] end settings[:children_settings] = new_children_settings end end |