Sha256: 619c9dd071ea2660e0a7e435465354cce30a5b869b24095523c0a6b6b5afb5bb

Contents?: true

Size: 1.64 KB

Versions: 4

Compression:

Stored size: 1.64 KB

Contents

module NoCms::Blocks
  class Block < ActiveRecord::Base

    include  NoCms::Blocks::Concerns::TranslationScopes

    acts_as_nested_set

    scope :drafts, ->() { where_with_locale(draft: true) }
    scope :no_drafts, ->() { where_with_locale(draft: false) }
    scope :roots, ->() { where parent_id: nil }

    translates :draft
    include  NoCms::Blocks::Concerns::SerializingFields

    ##
    # In the block we get all the fields so it can accept all of them
    def fields_configuration
      layout_config.fields
    end

    validates :layout, presence: true

    ##
    # A block dups all it's children and the translations
    def duplicate_self new_self

      new_self.translations = translations.map(&:dup)
      new_self.translations.each { |t| t.globalized_model = new_self }

      children.each do |child|
        new_self.children << child.dup
      end
    end

    class Translation

      ##
      # In the translation we get only the translated fields
      def fields_configuration
        layout_config.translated_fields
      end

      ##
      # Sometimes the translation still won't have the globalized_model linked
      # (e.g. before it's saved for the first time) and we must have a mechanism
      # to be able to store a temporary layout
      attr_accessor :layout

      ##
      # If we don't have a globalized model yet we return our temporary layout
      def layout
        globalized_model.nil? ? @layout : globalized_model.layout
      end

      include  NoCms::Blocks::Concerns::SerializingFields
    end

    accepts_nested_attributes_for :children, allow_destroy: true
    accepts_nested_attributes_for :translations

  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
nocms-blocks-1.1.3 app/models/no_cms/blocks/block.rb
nocms-blocks-1.1.2 app/models/no_cms/blocks/block.rb
nocms-blocks-1.1.1 app/models/no_cms/blocks/block.rb
nocms-blocks-1.1.0 app/models/no_cms/blocks/block.rb