diff --git a/app/models/view.rb b/app/models/view.rb index d7e29c2f23f..efe6423e548 100644 --- a/app/models/view.rb +++ b/app/models/view.rb @@ -3,7 +3,7 @@ require 'ipaddr' class View < ActiveRecord::Base belongs_to :parent, polymorphic: true belongs_to :user - validates_presence_of :parent_type, :parent_id, :ip, :viewed_at + validates_presence_of :parent_type, :parent_id, :ip_address, :viewed_at # TODO: This could happen asyncronously def self.create_for(parent, ip, user=nil) @@ -20,7 +20,7 @@ class View < ActiveRecord::Base $redis.expire(redis_key, 1.day.to_i) View.transaction do - View.create(parent: parent, ip: IPAddr.new(ip).to_i, viewed_at: Date.today, user: user) + View.create(parent: parent, ip_address: ip, viewed_at: Date.today, user: user) # Update the views count in the parent, if it exists. if parent.respond_to?(:views) @@ -37,9 +37,9 @@ end # # parent_id :integer not null # parent_type :string(50) not null -# ip :integer not null # viewed_at :date not null # user_id :integer +# ip_address :string not null # # Indexes # diff --git a/db/migrate/20130625022454_change_ip_to_inet_in_views.rb b/db/migrate/20130625022454_change_ip_to_inet_in_views.rb new file mode 100644 index 00000000000..30a0e8390bc --- /dev/null +++ b/db/migrate/20130625022454_change_ip_to_inet_in_views.rb @@ -0,0 +1,22 @@ +require 'ipaddr' + +class ChangeIpToInetInViews < ActiveRecord::Migration + def up + table = :views + add_column table, :ip_address, :inet + + execute "UPDATE views SET ip_address = inet( + (ip >> 24 & 255) || '.' || + (ip >> 16 & 255) || '.' || + (ip >> 8 & 255) || '.' || + (ip >> 0 & 255) + );" + + change_column table, :ip_address, :inet, { :null => false } + remove_column table, :ip + end + + def down + raise ActiveRecord::IrreversibleMigration + end +end diff --git a/spec/models/view_spec.rb b/spec/models/view_spec.rb index 6a2ac8d9f15..b4e0052db01 100644 --- a/spec/models/view_spec.rb +++ b/spec/models/view_spec.rb @@ -6,7 +6,7 @@ describe View do it { should belong_to :user } it { should validate_presence_of :parent_type } it { should validate_presence_of :parent_id } - it { should validate_presence_of :ip } + it { should validate_presence_of :ip_address } it { should validate_presence_of :viewed_at }