lib/voltron/encryptable.rb in voltron-encrypt-0.1.6 vs lib/voltron/encryptable.rb in voltron-encrypt-0.1.7

- old
+ new

@@ -12,10 +12,32 @@ has_one :encryptable, as: :resource, class_name: "Voltron::Id" before_create do self.build_encryptable id: find_id end + + default_scope { joins(:encryptable).includes(:encryptable) } + + after_initialize do + self.class.reflect_on_all_associations(:belongs_to).each do |belongs| + + # Override the attribute setter method, intercept any value and try and find it by + # it's encrypted id. We're assuming the ids passed in the params are encrypted since + # our model is using an encrypted id + self.class.send(:define_method, "#{belongs.name}_id=") do |val| + begin + klass = (belongs.options[:class_name] || belongs.name).to_s.classify.constantize + return super(val) unless klass.has_encrypted_id? + record = klass.find(val) + super(record.id) + rescue NameError, ActiveRecord::RecordNotFound + super(val) + end + end + end + end + end module ClassMethods def has_encrypted_id? true @@ -36,10 +58,10 @@ if conditions.is_a?(String) # If conditions is a string, assume it's an encoded id super(decoded_ids(conditions)) else # Otherwise do what exists? normally does - super(conditions) + super end end def destroy(id) super(decoded_ids(id))