Sha256: e0e871ac53b2d787b568e35fa8c7c05d02d336363a77237a75f19261b68cd0b8

Contents?: true

Size: 1.83 KB

Versions: 4

Compression:

Stored size: 1.83 KB

Contents

module Weeler
  module Seoable
    extend ActiveSupport::Concern
    include ::ActionView::Helpers::TextHelper

    included do
      # Set relations
      has_one :seo, as: :seoable, dependent: :destroy, class_name: "Weeler::Seo"
      accepts_nested_attributes_for :seo, reject_if: :all_blank, allow_destroy: true

      # Callbacks
      after_save :generate_seo

      @@force_seoable = true
    end

    # Generate seo data in each avaivable locale
    #
    def generate_seo
      self.seo = Weeler::Seo.create if self.seo.blank?

      I18n.available_locales.each do |locale|
        Globalize.with_locale(locale) do

          if self.respond_to? :seo_title
            self.seo.title = prepare_seoabled_text(seo_title) if (self.seo.title.blank? || @@force_seoable)
          else
            self.seo.title = seo_attribute :title
          end

          if self.respond_to? :seo_description
            self.seo.description = prepare_seoabled_text(seo_description, length: 159) if (self.seo.description.blank? || @@force_seoable)
          else
            self.seo.description = seo_attribute :content, length: 159
          end

          if self.respond_to? :seo_keywords
            self.seo.keywords = prepare_seoabled_text(seo_keywords, length: 200) if (self.seo.keywords.blank? || @@force_seoable)
          end

          self.seo.save
        end
      end
    end

    # Prepeare attribute
    #
    def seo_attribute attribute, length: 80
      if self.has_attribute?(attribute) && self[attribute].present? && (self.seo.title.blank? || @@force_seoable)
        prepare_seoabled_text self[attribute], length: length
      end
    end

    # Strip, sanitize and truncate text in Rails way
    #
    def prepare_seoabled_text text, length: 80
      truncate(ActionView::Base.full_sanitizer.sanitize(text).strip, omission: '', length: length)
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
weeler-1.0.3 app/models/concerns/weeler/seoable.rb
weeler-1.0.2 app/models/concerns/weeler/seoable.rb
weeler-1.0.1 app/models/concerns/weeler/seoable.rb
weeler-1.0.0 app/models/concerns/weeler/seoable.rb