module JT::Rails::Meta

Public Instance Methods

add_meta_extra(extra_params, previous_key = nil) click to toggle source

Add meta other than title, description, keywords Params:

extra_params

hash containing the meta(s) wanted

# File lib/jt-rails-meta.rb, line 124
def add_meta_extra(extra_params, previous_key = nil)
        for key, value in extra_params
                current_key = previous_key ? "#{previous_key}:#{key}" : key

                if value.is_a?(String) || value.is_a?(Symbol)
                        @meta[:extra] << { name: current_key, content: value.to_s }
                elsif value.is_a?(Hash)
                        add_meta_extra(value, current_key)
                elsif value.is_a?(Array)
                        for v in value
                                @meta[:extra] << { name: current_key, content: v.to_s }
                        end
                end
        end
end
add_meta_keywords(keywords) click to toggle source

Add custom keywords to the meta keywords, must be call before `set_meta_keywords` Params:

keywords

array of keywords added to the default keywords

# File lib/jt-rails-meta.rb, line 111
def add_meta_keywords(keywords)
        if keywords.is_a?(String)
                @meta[:keywords] = keywords
        elsif keywords.is_a?(Array)
                @meta[:keywords] = keywords.join(',')
        end

        set_meta_keywords
end
meta_description() click to toggle source

Content of meta description

# File lib/jt-rails-meta.rb, line 47
def meta_description
        @meta[:description] ||= set_meta_description
end
meta_keywords() click to toggle source

Content of meta keywords

# File lib/jt-rails-meta.rb, line 52
def meta_keywords
        @meta[:keywords] ||= set_meta_keywords
end
meta_tags() click to toggle source

Generate HTML tags title, description, keywords and others meta

# File lib/jt-rails-meta.rb, line 18
def meta_tags
        output = ''

        output << content_tag('title', meta_title)
        output << "\n"
        output << tag('meta', name: 'description', content: meta_description)
        output << "\n"
        output << tag('meta', name: 'keywords', content: meta_keywords)
        output << "\n"

        for link in @meta[:links]
                output << tag('link', link[:options])
                output << "\n"
        end

        for extra_params in @meta[:extra]
                output << tag('meta', name: extra_params[:name], content: extra_params[:content])
                output << "\n"
        end

        output.html_safe
end
meta_title() click to toggle source

Content of meta title

# File lib/jt-rails-meta.rb, line 42
def meta_title
        @meta[:title] ||= set_meta_title
end
meta_title_raw() click to toggle source

Content of meta title without suffix or prefix

# File lib/jt-rails-meta.rb, line 57
def meta_title_raw
        @meta[:title_raw]
end
set_meta_description(options = {}) click to toggle source

Generate meta description Use meta.default.description if no meta found for the current controller/action Params:

options

options passed to I18n

# File lib/jt-rails-meta.rb, line 84
def set_meta_description(options = {})
        @meta[:description] = I18n.translate("#{meta_key}.description", options)
        @meta[:description] = I18n.translate('meta.default.description') if !have_translation?(@meta[:description])
        @meta[:description]
end
set_meta_keywords(options = {}) click to toggle source

Generate meta keywords Use meta.default.keywords if no meta found for the current controller/action Params:

options

options passed to I18n

# File lib/jt-rails-meta.rb, line 94
def set_meta_keywords(options = {})
        keywords = I18n.translate("#{meta_key}.keywords", options)
        keywords = I18n.translate('meta.default.keywords') if !have_translation?(keywords)
        keywords = '' if !have_translation?(keywords)

        if @meta[:keywords].blank?
                @meta[:keywords] = keywords
        else
                @meta[:keywords] << ',' << keywords
        end

        @meta[:keywords]
end
set_meta_title(options = {}) click to toggle source

Generate meta title Use meta.default.title if no meta found for the current controller/action Params:

options

options passed to I18n

# File lib/jt-rails-meta.rb, line 65
def set_meta_title(options = {})
        @meta[:title_raw] = I18n.translate("#{meta_key}.title", options)

        if have_translation?(@meta[:title_raw])
                @meta[:title] = "#{@meta[:prefix]}#{@meta[:title_raw]}#{@meta[:suffix]}"
        else
                @meta[:title] = I18n.translate("#{meta_key}.full_title", options)
                @meta[:title] = I18n.translate('meta.default.title') if !have_translation?(@meta[:title])

                @meta[:title_raw] = @meta[:title]
        end
        
        @meta[:title]
end

Private Instance Methods

create_meta_hash() click to toggle source
# File lib/jt-rails-meta.rb, line 166
def create_meta_hash
        @meta = HashWithIndifferentAccess.new
        
        @meta[:extra] = []
        @meta[:links] = []

        @meta[:prefix] = I18n.translate('meta.prefix')
        @meta[:prefix] = "" if !have_translation?(@meta[:prefix])

        @meta[:suffix] = I18n.translate('meta.suffix')
        @meta[:suffix] = "" if !have_translation?(@meta[:suffix])
end
have_translation?(text) click to toggle source
# File lib/jt-rails-meta.rb, line 184
def have_translation?(text)
        !text.start_with?('translation missing')
end
meta_key() click to toggle source

Key used by I18n for the current controller/action

# File lib/jt-rails-meta.rb, line 180
def meta_key
        "meta.#{request[:controller].sub('/', '.')}.#{request[:action]}"
end