Sha256: 3341f04e0581cc1a3699167047799bad51a5752ec17ec7429f64e4eae4cd62d3

Contents?: true

Size: 1.14 KB

Versions: 4

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

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

    before_validation :parameterize_slug

    after_destroy :column_reset

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

    validate :valid_liquid_content

    belongs_to :site
    belongs_to :template, -> { where(partial: true) },
               class_name: "Archangel::Template",
               optional: true

    ##
    # 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

    def valid_liquid_content
      return if valid_liquid_content?

      errors.add(:content, Archangel.t(:liquid_invalid))
    end

    def valid_liquid_content?
      ::Liquid::Template.parse(content)

      true
    rescue ::Liquid::SyntaxError => _e
      false
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

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