Sha256: 1eb3a1f13c5b957fa739d9e29b4a7c18d88dfd76942aa3df177048bd64ab5c9d

Contents?: true

Size: 907 Bytes

Versions: 7

Compression:

Stored size: 907 Bytes

Contents

module Guts
  # Navigation item model
  class NavigationItem < ActiveRecord::Base
    include MultisiteScopeConcern

    before_create :set_position

    validates :title, presence: true

    belongs_to :site
    belongs_to :navigation
    belongs_to :navigatable, polymorphic: true, required: false
    has_many :media, as: :filable, dependent: :destroy
    has_many :metafields, as: :fieldable, dependent: :destroy

    default_scope { order(position: :asc) }

    # Determines if the navigation item has a custom link
    # @return [Boolean]
    def custom?
      self[:navigatable_type].nil? || self[:navigatable_type].empty?
    end

    private

    # Sets the position (ordering) for a navigation item before creation
    def set_position
      max_position = Navigation.find(self[:navigation_id]).navigation_items.maximum(:position) || -1

      self[:position] = max_position + 1
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
guts-2.1.0 app/models/guts/navigation_item.rb
guts-2.0.2 app/models/guts/navigation_item.rb
guts-2.0.1 app/models/guts/navigation_item.rb
guts-2.0.0 app/models/guts/navigation_item.rb
guts-1.4.0 app/models/guts/navigation_item.rb
guts-1.3.6 app/models/guts/navigation_item.rb
guts-1.3.5 app/models/guts/navigation_item.rb