Module: ModelConcerns::Protectable::ClassMethods

Defined in:
lib/model_concerns/protectable.rb

Instance Method Summary collapse

Instance Method Details

#find(*args) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/model_concerns/protectable.rb', line 22

def find(*args)
  scope = args.slice!(0)
  options = args.slice!(0) || {}
  if protectable? && !options[:real_id]
    if scope.is_a?(Array)
      scope.map! {|a| find_id_by_fake_id(a).to_i}
    else
      scope = find_id_by_fake_id(scope)
    end
  end

  super(scope)
end

#find_by_fake_id(fake_id) ⇒ Object



44
45
46
# File 'lib/model_concerns/protectable.rb', line 44

def find_by_fake_id(fake_id)
  find_by_id(find_id_by_fake_id(fake_id.to_i))
end

#find_id_by_fake_id(fake_id) ⇒ Object



48
49
50
# File 'lib/model_concerns/protectable.rb', line 48

def find_id_by_fake_id(fake_id)
  fake_id.to_i ^ protect_seed
end

#protect_seedObject



36
37
38
39
40
41
42
# File 'lib/model_concerns/protectable.rb', line 36

def protect_seed
  alphabet = Array("a".."z") 
  number = name.split("").collect do |char|
    alphabet.index(char)
  end
  @protect_seed ||= number.shift(12).join.to_i
end

#protectable?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/model_concerns/protectable.rb', line 52

def protectable?
  true
end