Skip to content

Instantly share code, notes, and snippets.

@pda
Created July 4, 2011 05:39
Show Gist options
  • Save pda/1062944 to your computer and use it in GitHub Desktop.
Save pda/1062944 to your computer and use it in GitHub Desktop.
StoresIpAddress for ActiveRecord
module StoresIpAddress
extend ActiveSupport::Concern
included do
def self.stores_ip_address(attr)
define_method(attr) do
require "ipaddr"
IPAddr.new(read_attribute(attr), Socket::AF_INET).to_s
end
define_method(:"#{attr}=") do |ip|
require "ipaddr"
write_attribute :remote_ip, IPAddr.new(ip).to_i
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment