Sha256: 348b933b406cd7d9f406e64e6cd58f75a627f27ce47741222d8edef1738171ba

Contents?: true

Size: 1.72 KB

Versions: 18

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true

module Cmor::Seo
  class Item < ApplicationRecord
    include ActsAsPublished::ActiveRecord

    # class OrValidator < ActiveModel::EachValidator
    #   def validate_each(record, attribute, value)
    #     values = options[:others].map{ |o| record.send(o) } + [value]
    #     attribute_names = [attribute] + options[:others]

    #     if options[:exclusive]
    #       if values.count { |x| x.present? } != 1
    #         record.errors.add(attribute, options[:message] || :or)
    #         # record.errors[attribute] << (options[:message] || "exactly one of #{attribute_names.to_sentence} may be present.")
    #       end
    #     else
    #       if values.all? { |x| x.blank? }
    #         record.errors.add(attribute, options[:message] || :or)
    #         # record.errors[attribute] << (options[:message] || "at least one of #{attribute_names.to_sentence} must be present.")
    #       end
    #     end
    #   end
    # end

    belongs_to :resource, polymorphic: true, optional: true
    has_many :meta_tags, -> { order(position: :asc) }, inverse_of: :item
    accepts_nested_attributes_for :meta_tags, reject_if: :all_blank, allow_destroy: true

    acts_as_published

    validates :resource, or: { others: [ :path ], exclusive: true }

    def human
      if resource.present?
        "#{resource.class.name}: #{resource_label}"
      else
        path
      end
    end

    def meta_tags_count
      meta_tags.count
    end

    def meta_tags_to_html
      meta_tags.map(&:to_html).join("\n")
    end

    private
      def resource_label
        resource.try(:human).presence || resource.try(:email).presence || resource.try(:name) || resource.try(:identifier).presence || resource.to_s
      end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
cmor_seo-0.0.60.pre app/models/cmor/seo/item.rb
cmor_seo-0.0.59.pre app/models/cmor/seo/item.rb
cmor_seo-0.0.58.pre app/models/cmor/seo/item.rb
cmor_seo-0.0.57.pre app/models/cmor/seo/item.rb
cmor_seo-0.0.56.pre app/models/cmor/seo/item.rb
cmor_seo-0.0.55.pre app/models/cmor/seo/item.rb
cmor_seo-0.0.54.pre app/models/cmor/seo/item.rb
cmor_seo-0.0.53.pre app/models/cmor/seo/item.rb
cmor_seo-0.0.52.pre app/models/cmor/seo/item.rb
cmor_seo-0.0.51.pre app/models/cmor/seo/item.rb
cmor_seo-0.0.50.pre app/models/cmor/seo/item.rb
cmor_seo-0.0.49.pre app/models/cmor/seo/item.rb
cmor_seo-0.0.48.pre app/models/cmor/seo/item.rb
cmor_seo-0.0.45.pre app/models/cmor/seo/item.rb
cmor_seo-0.0.44.pre app/models/cmor/seo/item.rb
cmor_seo-0.0.43.pre app/models/cmor/seo/item.rb
cmor_seo-0.0.42.pre app/models/cmor/seo/item.rb
cmor_seo-0.0.41.pre app/models/cmor/seo/item.rb