Sha256: da3f24aa5542a2f0d38bb7ab420e02eff4c7af786229eabda1c7b416808b1ca6

Contents?: true

Size: 1.15 KB

Versions: 4

Compression:

Stored size: 1.15 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: { scope: :site_id }

    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.3.0 app/models/archangel/widget.rb
archangel-0.0.8 app/models/archangel/widget.rb
archangel-0.0.7 app/models/archangel/widget.rb
archangel-0.0.6 app/models/archangel/widget.rb