Skip to content

Instantly share code, notes, and snippets.

@mcasimir
Created May 7, 2012 17:19
Show Gist options
  • Save mcasimir/2629071 to your computer and use it in GitHub Desktop.
Save mcasimir/2629071 to your computer and use it in GitHub Desktop.
FriendlyIdSchema is a micro gem for Ruby on Rails with which you can add permalinks to models with ActiveRecordSchema and FriendlyId

FriendlyIdSchema

FriendlyIdSchema is a micro gem that combines FriendlyId with ActiveRecordSchema

Usage

Call #permalink inside a model like this:

class Post
  permalink

  field :title
  
end

and then run rails g migration with --from option to let ActiveRecordSchema generate the required migration

ex.

rails g migration add_permalink_to_posts --from Post

Specify a field to derive permalink

class User
  field :name
  permalink :name
  
end

Use history

Run these commands first:

rails generate friendly_id
rake db:migrate
class Post
  permalink :history => true

  field :title
  
end
Gem::Specification.new do |s|
s.name = 'friendly_id_schema'
s.summary = 'A Ruby Micro Gem for use FriendlyId with ActiveRecordSchema.'
s.version = '0.1.1'
s.platform = Gem::Platform::RUBY
s.files = %w(friendly_id_schema.rb)
s.require_path = '.'
s.author = 'Maurizio Casimirri'
s.email = 'maurizio.cas@gmail.com'
s.add_dependency "friendly_id", "~> 4.0.1"
s.add_dependency "active_record_schema"
end
# Author:: Maurizio Casimirri (mailto:maurizio.cas@gmail.com)
# Copyright:: Copyright (c) 2012 Maurizio Casimirri
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
require 'friendly_id'
module FriendlyIdSchema
extend ActiveSupport::Concern
module ClassMethods
def permalink(method, options = {})
use = options[:history] ? :history : :slugged
field :slug, :index => { :unique => true }
validates :slug, :presence => true, :uniqueness => true
if ActiveRecord::Base.connection.table_exists?(self.table_name)
self.send :extend, FriendlyId
friendly_id method, :use => use
end
end
end
end
ActiveRecord::Base.send :include, FriendlyIdSchema
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment