Sha256: 8925b1e1f371a8d4f9d38c4cfe06ce0227ef23b8cc4f7446ff7daca7f356d23e

Contents?: true

Size: 998 Bytes

Versions: 4

Compression:

Stored size: 998 Bytes

Contents

# frozen_string_literal: true

module Archangel
  ##
  # Collection model
  #
  class Collection < ApplicationRecord
    acts_as_paranoid

    before_validation :parameterize_slug

    after_destroy :column_reset

    validates :name, presence: true
    validates :slug, presence: true, uniqueness: true

    has_many :entries, inverse_of: :collection, dependent: :destroy
    has_many :fields, inverse_of: :collection, dependent: :destroy

    accepts_nested_attributes_for :fields, reject_if: :all_blank,
                                           allow_destroy: true

    belongs_to :site

    has_many :entries
    has_many :fields

    ##
    # Overwrite resource id to `slug`
    #
    # @return [String] the aliased resource param
    #
    def to_param
      slug
    end

    protected

    def parameterize_slug
      self.slug = slug.to_s.downcase.parameterize
    end

    def column_reset
      now = Time.current.to_i

      self.slug = "#{now}_#{slug}"

      save
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
archangel-0.0.5 app/models/archangel/collection.rb
archangel-0.0.4 app/models/archangel/collection.rb
archangel-0.0.3 app/models/archangel/collection.rb
archangel-0.0.2 app/models/archangel/collection.rb