Module: ActiveRecord::HashidsUri::ClassMethods
- Defined in:
- lib/hashids_uri.rb
Instance Method Summary collapse
Instance Method Details
#find(*args) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/hashids_uri.rb', line 31 def find(*args) id = args.first return super if args.count != 1 || (Integer(id) rescue false) find_by_hash(id).tap { |result| return result unless result.nil? } raise ActiveRecord::RecordNotFound, "cannot find record with hash id: #{id.inspect}" end |
#find_by_hash(hash) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/hashids_uri.rb', line 22 def find_by_hash(hash) decoded_id = ::Hashids.new( self.salt, self.min_length ).decode(hash).first find(decoded_id) end |
#has_hashids_uri(options = {}) ⇒ Object
12 13 14 15 16 17 18 19 20 |
# File 'lib/hashids_uri.rb', line 12 def has_hashids_uri( = {}) class_attribute :salt, :min_length self.salt = .fetch(:salt, '') self.min_length = .fetch(:min_length, 6) define_method(:hashid) { read_attribute(:hashid) || generate_hashid } after_create { update_attribute(:hashid, generate_hashid) unless hashid? } end |