Sha256: f43be9a059fdcfd4ac14d0f26fdf5edb37a1cffb7df85e5098d020f1dcc7d7d5

Contents?: true

Size: 730 Bytes

Versions: 2

Compression:

Stored size: 730 Bytes

Contents

module PloymorphicModule

  def self.included(base)
    base.extend ClassMethods
  end  

  module ClassMethods

    def acts_as_wuid options={}
      default_options = { :as => :wuidable, :dependent => :destroy }
      default_options.merge!(options) unless options.empty?
      has_one :wuid, default_options 
      include PloymorphicModule::InstanceMethods
    end

  end

  module InstanceMethods

    def wid
      self.wuid.rand_token rescue nil
    end

    def after_create
      super
      create_wuid
    end

    def create_wuid
      begin 
        wuid.create!
      rescue ActiveRecord::StatementInvalid => e 
        logger.error " create wuid faild with #{e.message}"
        retry
      end
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ploymorphic_wuid-0.5.2 lib/wuid/ploymorphic_module.rb
ploymorphic_wuid-0.5.1 lib/wuid/ploymorphic_module.rb