Sha256: 5b28aa509ef72f835e63ac4bcc33a2b859682211e3bddf21f7aceaaf566cd6e3

Contents?: true

Size: 1.71 KB

Versions: 15

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

15 entries across 15 versions & 1 rubygems

Version Path
alchemy_cms-2.8.3 app/models/alchemy/page/cells.rb
alchemy_cms-2.7.5 app/models/alchemy/page/cells.rb
alchemy_cms-2.6.3 app/models/alchemy/page/cells.rb
alchemy_cms-2.7.4 app/models/alchemy/page/cells.rb
alchemy_cms-2.8.2 app/models/alchemy/page/cells.rb
alchemy_cms-2.8.1 app/models/alchemy/page/cells.rb
alchemy_cms-2.7.3 app/models/alchemy/page/cells.rb
alchemy_cms-2.7.2 app/models/alchemy/page/cells.rb
alchemy_cms-2.7.1 app/models/alchemy/page/cells.rb
alchemy_cms-2.7.0 app/models/alchemy/page/cells.rb
alchemy_cms-2.6.2.1 app/models/alchemy/page/cells.rb
alchemy_cms-2.6.2 app/models/alchemy/page/cells.rb
alchemy_cms-2.6.1 app/models/alchemy/page/cells.rb
alchemy_cms-2.6.0 app/models/alchemy/page/cells.rb
alchemy_cms-2.6.0.rc5 app/models/alchemy/page/cells.rb