Module: UrlEncrypt::InstanceMethods::ClassMethods

Defined in:
lib/url_encrypt.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *args) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/url_encrypt.rb', line 68

def method_missing(method_id, *args)
  if match = /^find_(all_by|by)_encrypted_([_a-zA-Z]\w*)$/.match(method_id.to_s)          
    finder = determine_finder(match)
    attribute_names = extract_attribute_names_from_match(match)
    super unless all_attributes_exists?(attribute_names)

    self.class_eval %{
      def self.#{method_id}(*args)
        encrypted_str = args[0]
        raise(ActiveRecord::RecordNotFound) if encrypted_str.nil?
        decrypted_str = decrypt(encrypted_str)              
        
        args[0] = decrypted_str

        options = args.extract_options!
        attributes = construct_attributes_from_arguments([:#{attribute_names.join(',:')}], args)
        finder_options = { :conditions => attributes }
        validate_find_options(options)
        set_readonly_option!(options)

        if options[:conditions]
          with_scope(:find => finder_options) do
            ActiveSupport::Deprecation.silence { self.send(:#{finder}, options) }
          end
        else
          ActiveSupport::Deprecation.silence { self.send(:#{finder}, options.merge(finder_options)) }
        end
      end
    }, __FILE__, __LINE__
    self.send(method_id, *args)
  else
    super
  end
end