Sha256: d861894dcb0e8657f036a3c4b0021264f9a3e83e02c4e7469152e69986f17bc9

Contents?: true

Size: 644 Bytes

Versions: 5

Compression:

Stored size: 644 Bytes

Contents

module Spina
  class Resource < ApplicationRecord
    has_many :pages, dependent: :restrict_with_exception

    belongs_to :parent_page, class_name: "Spina::Page", optional: true

    after_save :scope_pages_to_parent_page

    def pages
      case order_by
      when "title"
        super.joins(:translations).where(spina_page_translations: {locale: I18n.locale}).order("spina_page_translations.title")
      else
        super.order(created_at: :desc)
      end
    end

    private

      def scope_pages_to_parent_page
        pages.roots.each do |root_page|
          root_page.update(parent: parent_page)
        end
      end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
spina-1.1.4 app/models/spina/resource.rb
spina-1.1.3 app/models/spina/resource.rb
spina-1.1.2 app/models/spina/resource.rb
spina-1.1.1 app/models/spina/resource.rb
spina-1.1.0 app/models/spina/resource.rb