* Refactored routes some
* Fixed some major bugs with pools and pool versioning
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
class Pool < ActiveRecord::Base
|
||||
attr_accessor :updater_id, :updater_ip_addr
|
||||
validates_uniqueness_of :name
|
||||
validates_presence_of :name, :updater_id, :updater_ip_addr
|
||||
validates_format_of :name, :with => /\A[^\s;,]+\Z/, :on => :create, :message => "cannot have whitespace, commas, or semicolons"
|
||||
belongs_to :creator, :class_name => "User"
|
||||
belongs_to :updater, :class_name => "User"
|
||||
has_many :versions, :class_name => "PoolVersion", :dependent => :destroy
|
||||
before_save :normalize_name
|
||||
before_validation :normalize_name
|
||||
before_validation :initialize_creator, :on => :create
|
||||
after_save :create_version
|
||||
attr_accessible :name, :description, :post_ids, :is_active
|
||||
|
||||
@@ -18,14 +17,16 @@ class Pool < ActiveRecord::Base
|
||||
Pool.new do |pool|
|
||||
pool.name = "TEMP:#{Time.now.to_f}.#{rand(1_000_000)}"
|
||||
pool.creator = creator
|
||||
pool.updater_id = creator.id
|
||||
pool.updater_ip_addr = creator_ip_addr
|
||||
pool.save
|
||||
pool.name = "anonymous:#{pool.id}"
|
||||
pool.save
|
||||
end
|
||||
end
|
||||
|
||||
def initialize_creator
|
||||
self.creator_id = CurrentUser.id
|
||||
end
|
||||
|
||||
def normalize_name
|
||||
self.name = name.downcase
|
||||
end
|
||||
@@ -39,13 +40,13 @@ class Pool < ActiveRecord::Base
|
||||
return if post_ids =~ /(?:\A| )#{post.id}(?:\Z| )/
|
||||
|
||||
self.post_ids += " #{post.id}"
|
||||
self.post_ids.strip!
|
||||
self.post_ids = post_ids.strip
|
||||
save
|
||||
end
|
||||
|
||||
def remove_post!(post)
|
||||
post_ids.gsub!(/(?:\A| )#{post.id}(?:\Z| )/, " ")
|
||||
post_ids.strip!
|
||||
self.post_ids = post_ids.gsub(/(?:\A| )#{post.id}(?:\Z| )/, " ")
|
||||
self.post_ids = post_ids.strip
|
||||
save
|
||||
end
|
||||
|
||||
@@ -81,14 +82,10 @@ class Pool < ActiveRecord::Base
|
||||
def create_version
|
||||
last_version = versions.last
|
||||
|
||||
if last_version && updater_ip_addr == last_version.updater_ip_addr && updater_id == last_version.updater_id
|
||||
if last_version && CurrentUser.ip_addr == last_version.updater_ip_addr && CurrentUser.id == last_version.updater_id
|
||||
last_version.update_attribute(:post_ids, post_ids)
|
||||
else
|
||||
versions.create(
|
||||
:post_ids => post_ids,
|
||||
:updater_id => updater_id,
|
||||
:updater_ip_addr => updater_ip_addr
|
||||
)
|
||||
versions.create(:post_ids => post_ids)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -3,4 +3,10 @@ class PoolVersion < ActiveRecord::Base
|
||||
|
||||
validates_presence_of :updater_id, :updater_ip_addr
|
||||
belongs_to :pool
|
||||
before_validation :initialize_updater
|
||||
|
||||
def initialize_updater
|
||||
self.updater_id = CurrentUser.id
|
||||
self.updater_ip_addr = CurrentUser.ip_addr
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user