sluggit 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/slugable_ppj.rb +56 -0
 - metadata +45 -0
 
    
        data/lib/slugable_ppj.rb
    ADDED
    
    | 
         @@ -0,0 +1,56 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            module Sluggable
         
     | 
| 
      
 2 
     | 
    
         
            +
              extend ActiveSupport::Concern
         
     | 
| 
      
 3 
     | 
    
         
            +
             
     | 
| 
      
 4 
     | 
    
         
            +
              included do
         
     | 
| 
      
 5 
     | 
    
         
            +
                class_attribute :slug_column
         
     | 
| 
      
 6 
     | 
    
         
            +
                before_save :generate_slug!
         
     | 
| 
      
 7 
     | 
    
         
            +
              end
         
     | 
| 
      
 8 
     | 
    
         
            +
             
     | 
| 
      
 9 
     | 
    
         
            +
              module ClassMethods
         
     | 
| 
      
 10 
     | 
    
         
            +
                def set_slug_column_to(column)
         
     | 
| 
      
 11 
     | 
    
         
            +
                  self.slug_column = column
         
     | 
| 
      
 12 
     | 
    
         
            +
                end
         
     | 
| 
      
 13 
     | 
    
         
            +
              end
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
              def to_param
         
     | 
| 
      
 16 
     | 
    
         
            +
                self.slug
         
     | 
| 
      
 17 
     | 
    
         
            +
              end
         
     | 
| 
      
 18 
     | 
    
         
            +
             
     | 
| 
      
 19 
     | 
    
         
            +
              private
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
              def generate_slug!
         
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
      
 23 
     | 
    
         
            +
                the_slug = to_slug(self.send(self.class.slug_column))
         
     | 
| 
      
 24 
     | 
    
         
            +
                model = self.class.to_s
         
     | 
| 
      
 25 
     | 
    
         
            +
             
     | 
| 
      
 26 
     | 
    
         
            +
                count = 1
         
     | 
| 
      
 27 
     | 
    
         
            +
                record = self.class.find_by slug: the_slug
         
     | 
| 
      
 28 
     | 
    
         
            +
                while record and record != self
         
     | 
| 
      
 29 
     | 
    
         
            +
                  the_slug = make_unique(the_slug, count)
         
     | 
| 
      
 30 
     | 
    
         
            +
                  record = self.class.find_by slug: the_slug
         
     | 
| 
      
 31 
     | 
    
         
            +
                  count += 1
         
     | 
| 
      
 32 
     | 
    
         
            +
                end
         
     | 
| 
      
 33 
     | 
    
         
            +
             
     | 
| 
      
 34 
     | 
    
         
            +
                self.slug = the_slug
         
     | 
| 
      
 35 
     | 
    
         
            +
              end
         
     | 
| 
      
 36 
     | 
    
         
            +
             
     | 
| 
      
 37 
     | 
    
         
            +
              def to_slug(str)                # str=" @#$@ My First @#2@%#@ Post!!
         
     | 
| 
      
 38 
     | 
    
         
            +
                str = str.strip               #  --> @#$@ My First @#2@%#@ Post!!
         
     | 
| 
      
 39 
     | 
    
         
            +
                str.gsub!(/[^A-Za-z0-9]/,'-') #  --> -----My-First---2-----Post--
         
     | 
| 
      
 40 
     | 
    
         
            +
                str.gsub!(/-+/,'-')           #  --> -My-First-2-Post-
         
     | 
| 
      
 41 
     | 
    
         
            +
                str.gsub!(/^-+/,'')           #  --> My-First-2-Post-
         
     | 
| 
      
 42 
     | 
    
         
            +
                str.gsub!(/-+$/,'')           #  --> My-First-2-Post
         
     | 
| 
      
 43 
     | 
    
         
            +
                str.downcase                  #  --> my-first-2-post
         
     | 
| 
      
 44 
     | 
    
         
            +
              end
         
     | 
| 
      
 45 
     | 
    
         
            +
             
     | 
| 
      
 46 
     | 
    
         
            +
              def make_unique(the_slug, count)
         
     | 
| 
      
 47 
     | 
    
         
            +
                arr = the_slug.split('-')
         
     | 
| 
      
 48 
     | 
    
         
            +
                if arr.last.to_i == 0
         
     | 
| 
      
 49 
     | 
    
         
            +
                  the_slug = the_slug + '-' + count.to_s
         
     | 
| 
      
 50 
     | 
    
         
            +
                else
         
     | 
| 
      
 51 
     | 
    
         
            +
                  the_slug = arr[0...-1].join('-') + '-' + count.to_s
         
     | 
| 
      
 52 
     | 
    
         
            +
                end
         
     | 
| 
      
 53 
     | 
    
         
            +
                the_slug
         
     | 
| 
      
 54 
     | 
    
         
            +
              end
         
     | 
| 
      
 55 
     | 
    
         
            +
             
     | 
| 
      
 56 
     | 
    
         
            +
            end
         
     | 
    
        metadata
    ADDED
    
    | 
         @@ -0,0 +1,45 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            --- !ruby/object:Gem::Specification
         
     | 
| 
      
 2 
     | 
    
         
            +
            name: sluggit
         
     | 
| 
      
 3 
     | 
    
         
            +
            version: !ruby/object:Gem::Version
         
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.0.0
         
     | 
| 
      
 5 
     | 
    
         
            +
              prerelease: 
         
     | 
| 
      
 6 
     | 
    
         
            +
            platform: ruby
         
     | 
| 
      
 7 
     | 
    
         
            +
            authors:
         
     | 
| 
      
 8 
     | 
    
         
            +
            - Prasanna Joshi
         
     | 
| 
      
 9 
     | 
    
         
            +
            autorequire: 
         
     | 
| 
      
 10 
     | 
    
         
            +
            bindir: bin
         
     | 
| 
      
 11 
     | 
    
         
            +
            cert_chain: []
         
     | 
| 
      
 12 
     | 
    
         
            +
            date: 2014-10-20 00:00:00.000000000 Z
         
     | 
| 
      
 13 
     | 
    
         
            +
            dependencies: []
         
     | 
| 
      
 14 
     | 
    
         
            +
            description: A cool gem for slugifying models.
         
     | 
| 
      
 15 
     | 
    
         
            +
            email: prasanna3003@hotmail.com
         
     | 
| 
      
 16 
     | 
    
         
            +
            executables: []
         
     | 
| 
      
 17 
     | 
    
         
            +
            extensions: []
         
     | 
| 
      
 18 
     | 
    
         
            +
            extra_rdoc_files: []
         
     | 
| 
      
 19 
     | 
    
         
            +
            files:
         
     | 
| 
      
 20 
     | 
    
         
            +
            - lib/slugable_ppj.rb
         
     | 
| 
      
 21 
     | 
    
         
            +
            homepage: http://www.github.com/ppj
         
     | 
| 
      
 22 
     | 
    
         
            +
            licenses: []
         
     | 
| 
      
 23 
     | 
    
         
            +
            post_install_message: 
         
     | 
| 
      
 24 
     | 
    
         
            +
            rdoc_options: []
         
     | 
| 
      
 25 
     | 
    
         
            +
            require_paths:
         
     | 
| 
      
 26 
     | 
    
         
            +
            - lib
         
     | 
| 
      
 27 
     | 
    
         
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         
     | 
| 
      
 28 
     | 
    
         
            +
              none: false
         
     | 
| 
      
 29 
     | 
    
         
            +
              requirements:
         
     | 
| 
      
 30 
     | 
    
         
            +
              - - ! '>='
         
     | 
| 
      
 31 
     | 
    
         
            +
                - !ruby/object:Gem::Version
         
     | 
| 
      
 32 
     | 
    
         
            +
                  version: '0'
         
     | 
| 
      
 33 
     | 
    
         
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         
     | 
| 
      
 34 
     | 
    
         
            +
              none: false
         
     | 
| 
      
 35 
     | 
    
         
            +
              requirements:
         
     | 
| 
      
 36 
     | 
    
         
            +
              - - ! '>='
         
     | 
| 
      
 37 
     | 
    
         
            +
                - !ruby/object:Gem::Version
         
     | 
| 
      
 38 
     | 
    
         
            +
                  version: '0'
         
     | 
| 
      
 39 
     | 
    
         
            +
            requirements: []
         
     | 
| 
      
 40 
     | 
    
         
            +
            rubyforge_project: 
         
     | 
| 
      
 41 
     | 
    
         
            +
            rubygems_version: 1.8.28
         
     | 
| 
      
 42 
     | 
    
         
            +
            signing_key: 
         
     | 
| 
      
 43 
     | 
    
         
            +
            specification_version: 3
         
     | 
| 
      
 44 
     | 
    
         
            +
            summary: A slug generator gem
         
     | 
| 
      
 45 
     | 
    
         
            +
            test_files: []
         
     |