Sha256: 96fca00c9ddd4ac0c60a5848d82cde61e387a18f416398b45fadc0740f1ba417

Contents?: true

Size: 955 Bytes

Versions: 1

Compression:

Stored size: 955 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

            if fields.is_a? Proc
              str = fields.call(self)
            else
              field = [*fields].detect{|f| send(f).present?}
              return nil if field.nil?
              str = send(field).to_s.strip
            end

            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.3 lib/has_meta.rb