Created
July 4, 2011 05:39
-
-
Save pda/1062944 to your computer and use it in GitHub Desktop.
StoresIpAddress for ActiveRecord
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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