Sha256: 9e0ebf1dfd5ce63ec938fb66ba84d08bb66a9a2b21d138ec24cdd571c292dc1b

Contents?: true

Size: 1.91 KB

Versions: 1

Compression:

Stored size: 1.91 KB

Contents

module Permalinker
  module Orm
    module Base
      module ClassMethods
        def setup_permalink(orm_options, from, options)
          options.reverse_merge!(orm_options)
          options.reverse_merge!({
            :to     => :permalink,
            :unique => false,
            :force  => false
          })

          self.permalink_options = {
            :from_column_name => from,
            :to_column_name   => options[:to],
            :to_param         => [options[:to_param]].flatten,
            :unique           => options[:unique],
            :force            => options[:force]
          }
          
          self.send(:attr_accessor, :overwrite_permalink)
          
          after_save :create_permalink
        end
      end

      module InstanceMethods
        def to_param
          to_param_option = self.class.permalink_options[:to_param]

          to_param_option.compact.collect do |name|
            if respond_to?(name)
              send(name).to_s
            else
              name.to_s
            end
          end.reject(&:blank?).join("-")
        end
        
        def permalink_value_without_id
          self.permalink.to_s.gsub(/\A[0-9]{1,}-/, "")
        end  
        
        private

        def from_permalink_name
          self.class.permalink_options[:from_column_name]
        end

        def to_permalink_name
          self.class.permalink_options[:to_column_name]
        end

        def from_permalink_value
          read_attribute(from_permalink_name)
        end

        def to_permalink_value
          read_attribute(to_permalink_name)
        end
        
       


        def create_permalink
          # Getting Permalinker data
          # Check overwrite
          value = self.overwrite_permalink.blank? ? self.to_param : self.overwrite_permalink
            self.update_column(to_permalink_name, "#{self.id}-#{value}".to_permalink)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
permalinker-1.0.3 lib/permalinker/orm/base.rb