Sha256: 05c05e08aa2e437820449983a383d9638b74369d1770d6e608f0cc1e94bf9b57

Contents?: true

Size: 1.71 KB

Versions: 1

Compression:

Stored size: 1.71 KB

Contents

module Alchemy
  module Page::Cells

    extend ActiveSupport::Concern

    included do
      has_many :cells, :dependent => :destroy
      after_create :create_cells, :if => :can_have_cells?, :unless => :systempage?
    end

    module ClassMethods

      # Copy page cells
      #
      # @param source [Alchemy::Page]
      # @param target [Alchemy::Page]
      # @return [Array]
      #
      def copy_cells(source, target)
        new_cells = []
        source.cells.each do |cell|
          new_cells << Cell.create(:name => cell.name, :page_id => target.id)
        end
        new_cells
      end

    end

    # Returns true, if the page's page_layout defines cells.
    def can_have_cells?
      definition['cells'].present?
    end

    # Returns true, if the page has cells.
    def has_cells?
      cells.any?
    end

    # Returns the cell definitions from page's page_layout defintion.
    def cell_definitions
      cell_names = self.layout_description['cells']
      return [] if cell_names.blank?
      Cell.all_definitions_for(cell_names)
    end

    # Returns elements grouped by cell.
    def elements_grouped_by_cells
      elements.not_trashed.in_cell.group_by(&:cell)
    end

    # Returns element names from cell definition.
    def element_names_from_cells
      cell_definitions.collect { |c| c['elements'] }.flatten.uniq
    end

    # Returns element names that are not defined in a cell.
    def element_names_not_in_cell
      layout_description['elements'].uniq - element_names_from_cells
    end

    private

    # Creates cells that are defined in page's page_layout definition.
    def create_cells
      definition['cells'].each do |cellname|
        cells.create!(:name => cellname)
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
alchemy_cms-3.0.0.rc5 app/models/alchemy/page/cells.rb