Module: Weeler::Seoable

Extended by:
ActiveSupport::Concern
Includes:
ActionView::Helpers::TextHelper
Defined in:
app/models/concerns/weeler/seoable.rb

Instance Method Summary collapse

Instance Method Details

#generate_seoObject

Generate seo data in each avaivable locale



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/models/concerns/weeler/seoable.rb', line 19

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

#prepare_seoabled_text(text, length: 80) ⇒ Object

Strip, sanitize and truncate text in Rails way



56
57
58
# File 'app/models/concerns/weeler/seoable.rb', line 56

def prepare_seoabled_text text, length: 80
  truncate(::ActionView::Base.full_sanitizer.sanitize(text).strip, omission: '', length: length)
end

#seo_attribute(attribute, length: 80) ⇒ Object

Prepeare attribute



48
49
50
51
52
# File 'app/models/concerns/weeler/seoable.rb', line 48

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