lib/hashid/rails.rb in hashid-rails-0.5.0 vs lib/hashid/rails.rb in hashid-rails-0.6.0
- old
+ new
@@ -1,12 +1,12 @@
-require 'hashid/rails/version'
-require 'hashids'
-require 'active_record'
+require "hashid/rails/version"
+require "hashid/rails/configuration"
+require "hashids"
+require "active_record"
module Hashid
module Rails
-
# Get configuration or load defaults
def self.configuration
@configuration ||= Configuration.new
end
@@ -29,14 +29,13 @@
end
def to_param
encoded_id
end
- alias_method :hashid, :to_param
+ alias hashid to_param
module ClassMethods
-
def hashids
secret = Hashid::Rails.configuration.secret
length = Hashid::Rails.configuration.length
alphabet = Hashid::Rails.configuration.alphabet
@@ -54,48 +53,38 @@
end
end
def decode_id(ids)
if ids.is_a?(Array)
- ids.map { |id| hashid_decode(id) }
+ decoded_ids = ids.map { |id| hashid_decode(id) }
+ decoded_ids.any? ? decoded_ids : nil
else
hashid_decode(ids)
end
end
def find(hashid)
- model_reload? ? super(hashid) : super( decode_id(hashid) || hashid )
+ model_reload? ? super(hashid) : super(decode_id(hashid) || hashid)
end
def find_by_hashid(hashid)
find_by!(id: hashid_decode(hashid))
end
private
def model_reload?
- caller.any? {|s| s =~ /active_record\/persistence.*reload/}
+ caller.any? { |s| s =~ %r{ active_record/persistence.*reload } }
end
def hashid_decode(id)
hashids.decode(id.to_s).first
end
def hashid_encode(id)
hashids.encode(id)
end
end
-
- class Configuration
- attr_accessor :secret, :length, :alphabet
-
- def initialize
- @secret = ''
- @length = 6
- @alphabet = nil
- end
- end
-
end
end
ActiveRecord::Base.send :include, Hashid::Rails