add secondary validations to aliases+implications+requests

This commit is contained in:
r888888888
2016-02-10 14:52:26 -08:00
parent 01eac1e587
commit d75546a4e4
18 changed files with 239 additions and 89 deletions

View File

@@ -1,10 +1,11 @@
class AliasAndImplicationImporter
attr_accessor :text, :commands, :forum_id, :rename_aliased_pages
attr_accessor :text, :commands, :forum_id, :rename_aliased_pages, :skip_secondary_validations
def initialize(text, forum_id, rename_aliased_pages = "0")
def initialize(text, forum_id, rename_aliased_pages = "0", skip_secondary_validations = true)
@forum_id = forum_id
@text = text
@rename_aliased_pages = rename_aliased_pages
@skip_secondary_validations = skip_secondary_validations
end
def process!
@@ -54,13 +55,13 @@ class AliasAndImplicationImporter
tokens.map do |token|
case token[0]
when :create_alias
tag_alias = TagAlias.new(:forum_topic_id => forum_id, :status => "pending", :antecedent_name => token[1], :consequent_name => token[2])
tag_alias = TagAlias.new(:forum_topic_id => forum_id, :status => "pending", :antecedent_name => token[1], :consequent_name => token[2], :skip_secondary_validations => skip_secondary_validations)
unless tag_alias.valid?
raise "Error: #{tag_alias.errors.full_messages.join("; ")} (create alias #{tag_alias.antecedent_name} -> #{tag_alias.consequent_name})"
end
when :create_implication
tag_implication = TagImplication.new(:forum_topic_id => forum_id, :status => "pending", :antecedent_name => token[1], :consequent_name => token[2])
tag_implication = TagImplication.new(:forum_topic_id => forum_id, :status => "pending", :antecedent_name => token[1], :consequent_name => token[2], :skip_secondary_validations => skip_secondary_validations)
unless tag_implication.valid?
raise "Error: #{tag_implication.errors.full_messages.join("; ")} (create implication #{tag_implication.antecedent_name} -> #{tag_implication.consequent_name})"
end
@@ -81,7 +82,7 @@ private
tokens.map do |token|
case token[0]
when :create_alias
tag_alias = TagAlias.create(:forum_topic_id => forum_id, :status => "pending", :antecedent_name => token[1], :consequent_name => token[2])
tag_alias = TagAlias.create(:forum_topic_id => forum_id, :status => "pending", :antecedent_name => token[1], :consequent_name => token[2], :skip_secondary_validations => skip_secondary_validations)
unless tag_alias.valid?
raise "Error: #{tag_alias.errors.full_messages.join("; ")} (create alias #{tag_alias.antecedent_name} -> #{tag_alias.consequent_name})"
end
@@ -89,7 +90,7 @@ private
tag_alias.delay(:queue => "default").process!(false)
when :create_implication
tag_implication = TagImplication.create(:forum_topic_id => forum_id, :status => "pending", :antecedent_name => token[1], :consequent_name => token[2])
tag_implication = TagImplication.create(:forum_topic_id => forum_id, :status => "pending", :antecedent_name => token[1], :consequent_name => token[2], :skip_secondary_validations => skip_secondary_validations)
unless tag_implication.valid?
raise "Error: #{tag_implication.errors.full_messages.join("; ")} (create implication #{tag_implication.antecedent_name} -> #{tag_implication.consequent_name})"
end

View File

@@ -1,40 +1,73 @@
class TagAliasRequest
class ValidationError < Exception ; end
include ActiveModel::Validations
attr_reader :antecedent_name, :consequent_name, :reason, :tag_alias, :forum_topic
attr_reader :antecedent_name, :consequent_name, :reason, :skip_secondary_validations, :tag_alias, :forum_topic
def initialize(antecedent_name, consequent_name, reason)
@antecedent_name = antecedent_name.strip.tr(" ", "_")
@consequent_name = consequent_name.strip.tr(" ", "_")
@reason = reason
validate :validate_tag_alias
validate :validate_forum_topic
def initialize(attributes)
@antecedent_name = attributes[:antecedent_name].strip.tr(" ", "_")
@consequent_name = attributes[:consequent_name].strip.tr(" ", "_")
@reason = attributes[:reason]
self.skip_secondary_validations = attributes[:skip_secondary_validations]
end
def create
return false if invalid?
TagAlias.transaction do
create_alias
create_forum_topic
@tag_alias = build_tag_alias
@tag_alias.save
@forum_topic = build_forum_topic(@tag_alias.id)
@forum_topic.save
@tag_alias.update_attribute(:forum_topic_id, @forum_topic.id)
end
end
def create_alias
@tag_alias = TagAlias.create(:antecedent_name => antecedent_name, :consequent_name => consequent_name, :status => "pending")
if @tag_alias.errors.any?
raise ValidationError.new(@tag_alias.errors.full_messages.join("; "))
end
def build_tag_alias
TagAlias.new(
:antecedent_name => antecedent_name,
:consequent_name => consequent_name,
:status => "pending",
:skip_secondary_validations => skip_secondary_validations
)
end
def create_forum_topic
@forum_topic = ForumTopic.create(
def build_forum_topic(tag_alias_id)
ForumTopic.new(
:title => "Tag alias: #{antecedent_name} -> #{consequent_name}",
:original_post_attributes => {
:body => "create alias [[#{antecedent_name}]] -> [[#{consequent_name}]]\n\n\"Link to alias\":/tag_aliases?search[id]=#{tag_alias.id}\n\n#{reason}"
:body => "create alias [[#{antecedent_name}]] -> [[#{consequent_name}]]\n\n\"Link to alias\":/tag_aliases?search[id]=#{tag_alias_id}\n\n#{reason}"
},
:category_id => 1
)
if @forum_topic.errors.any?
raise ValidationError.new(@forum_topic.errors.full_messages.join("; "))
end
end
tag_alias.update_attribute(:forum_topic_id, @forum_topic.id)
def validate_tag_alias
ta = @tag_alias || build_tag_alias
if ta.invalid?
self.errors.add(:base, ta.errors.full_messages.join("; "))
return false
end
end
def validate_forum_topic
ft = @forum_topic || build_forum_topic(nil)
if ft.invalid?
self.errors.add(:base, ft.errors.full_messages.join("; "))
return false
end
end
def skip_secondary_validations=(v)
if v == "1" or v == true
@skip_secondary_validations = true
else
@skip_secondary_validations = false
end
end
end

View File

@@ -1,40 +1,73 @@
class TagImplicationRequest
class ValidationError < Exception ; end
include ActiveModel::Validations
attr_reader :antecedent_name, :consequent_name, :reason, :tag_implication, :forum_topic
attr_reader :antecedent_name, :consequent_name, :reason, :tag_implication, :forum_topic, :skip_secondary_validations
def initialize(antecedent_name, consequent_name, reason)
@antecedent_name = antecedent_name.strip.tr(" ", "_")
@consequent_name = consequent_name.strip.tr(" ", "_")
@reason = reason
validate :validate_tag_implication
validate :validate_forum_topic
def initialize(attributes)
@antecedent_name = attributes[:antecedent_name].strip.tr(" ", "_")
@consequent_name = attributes[:consequent_name].strip.tr(" ", "_")
@reason = attributes[:reason]
self.skip_secondary_validations = attributes[:skip_secondary_validations]
end
def create
return false if invalid?
TagImplication.transaction do
create_implication
create_forum_topic
@tag_implication = build_tag_implication
@tag_implication.save
@forum_topic = build_forum_topic(@tag_implication.id)
@forum_topic.save
@tag_implication.update_attribute(:forum_topic_id, @forum_topic.id)
end
end
def create_implication
@tag_implication = TagImplication.create(:antecedent_name => antecedent_name, :consequent_name => consequent_name, :status => "pending")
if @tag_implication.errors.any?
raise ValidationError.new(@tag_implication.errors.full_messages.join("; "))
end
def build_tag_implication
TagImplication.new(
:antecedent_name => antecedent_name,
:consequent_name => consequent_name,
:status => "pending",
:skip_secondary_validations => skip_secondary_validations
)
end
def create_forum_topic
@forum_topic = ForumTopic.create(
def build_forum_topic(tag_implication_id)
ForumTopic.new(
:title => "Tag implication: #{antecedent_name} -> #{consequent_name}",
:original_post_attributes => {
:body => "create implication [[#{antecedent_name}]] -> [[#{consequent_name}]]\n\n\"Link to implication\":/tag_implications?search[id]=#{tag_implication.id}\n\n#{reason}"
:body => "create implication [[#{antecedent_name}]] -> [[#{consequent_name}]]\n\n\"Link to implication\":/tag_implications?search[id]=#{tag_implication_id}\n\n#{reason}"
},
:category_id => 1
)
if @forum_topic.errors.any?
raise ValidationError.new(@forum_topic.errors.full_messages.join("; "))
end
end
tag_implication.update_attribute(:forum_topic_id, @forum_topic.id)
def validate_tag_implication
ti = @tag_implication || build_tag_implication
if ti.invalid?
self.errors.add(:base, ti.errors.full_messages.join("; "))
return false
end
end
def validate_forum_topic
ft = @forum_topic || build_forum_topic(nil)
if ft.invalid?
self.errors.add(:base, ft.errors.full_messages.join("; "))
return false
end
end
def skip_secondary_validations=(v)
if v == "1" or v == true
@skip_secondary_validations = true
else
@skip_secondary_validations = false
end
end
end