Sha256: d435e60d631c7cc778cfeb6b9c862f4b8e0dcb6e6db3c5f7d761985f59d4e63b

Contents?: true

Size: 843 Bytes

Versions: 1

Compression:

Stored size: 843 Bytes

Contents

require 'cgi'
require 'active_record'
require 'has_meta/version'

module HasMeta
  module Extensions
    def self.included(base)
      base.extend(ClassMethods)
    end

    module ClassMethods
      def has_meta(options = {})
        options.each_pair do |meth, fields|
          define_method("meta_#{meth}") {|*args|
            length = args.first if args.is_a? Array
            length ||= 255
            field = [*fields].detect{|f| send(f).present?}
            return nil if field.nil?
            str = send(field).to_s.strip
            str.gsub!(' ', ' ')
            str.gsub!(/<.*?>/, '')
            str = ::CGI::unescapeHTML(str)
            str = (str[0,length - 3] + '...') if str.size > length
            str
          }
        end
      end
    end 

  end
end

ActiveRecord::Base.send :include, HasMeta::Extensions

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
has_meta-0.0.2 lib/has_meta.rb