Commit efef1fc99875b93c1e1b090014b75119f61c1f18

Authored by Evandro Junior
1 parent a1650079

Avoid duplicated tasks

db/migrate/20150903210329_add_proposal_discussion_plugin_simplified_abstract_to_tasks.rb 0 → 100644
... ... @@ -0,0 +1,9 @@
  1 +class AddProposalDiscussionPluginSimplifiedAbstractToTasks < ActiveRecord::Migration
  2 + def change
  3 + add_column :tasks, :proposal_discussion_plugin_simplified_abstract, :text
  4 + add_index :tasks, :proposal_discussion_plugin_simplified_abstract
  5 + ProposalsDiscussionPlugin::ProposalTask.find_each do |t|
  6 + t.update_attribute :proposal_discussion_plugin_simplified_abstract, ProposalsDiscussionPlugin::ProposalTask.simplify(t.abstract)
  7 + end
  8 + end
  9 +end
... ...
lib/proposals_discussion_plugin/proposal_task.rb
... ... @@ -10,8 +10,9 @@ class ProposalsDiscussionPlugin::ProposalTask &lt; Task
10 10  
11 11 validates_presence_of :requestor_id, :target_id
12 12 validates_associated :article_object
13   -
  13 + before_validation :simplify_abstract
14 14 validate :require_category
  15 + validates_uniqueness_of :proposal_discussion_plugin_simplified_abstract, :scope => ['target_id'], :message => _("Cannot create duplicate proposal")
15 16  
16 17 settings_items :name, :type => String
17 18 settings_items :ip_address, :type => String
... ... @@ -258,8 +259,20 @@ class ProposalsDiscussionPlugin::ProposalTask &lt; Task
258 259 parent.name if parent
259 260 end
260 261  
  262 + def self.simplify(s)
  263 + return nil if s.nil?
  264 + s=I18n.transliterate(s)
  265 + s.gsub!(/(^\s)|([',.?!])|(\s$)/, "")
  266 + s.gsub!(/\s{2,}/, " ")
  267 + s.downcase
  268 + end
  269 +
261 270 protected
262 271  
  272 + def simplify_abstract
  273 + proposal_discussion_plugin_simplified_abstract = ProposalsDiscussionPlugin::ProposalTask.simplify(self[:data][:article]["abstract"])
  274 + end
  275 +
263 276 def require_category
264 277 if categories.count == 0 && flagged?
265 278 errors.add :categories, _('Select at least one category')
... ...
test/unit/proposal_task_test.rb
  1 +# encoding: UTF-8
1 2 require_relative '../test_helper'
2 3  
3 4 class ProposalTaskTest < ActiveSupport::TestCase
... ... @@ -83,4 +84,13 @@ class ProposalTaskTest &lt; ActiveSupport::TestCase
83 84 assert task.information.present?
84 85 end
85 86  
  87 + should 'not register duplicate task' do
  88 + task = ProposalsDiscussionPlugin::ProposalTask.create!(:requestor => person, :target => profile, :article => {:name => 'proposal 1', :abstract => ' Alô ! ruby é legal a bêçá. seu moÇo, não Acha? '})
  89 + task.categories = [fast_create(ProposalsDiscussionPlugin::TaskCategory)]
  90 + task.save!
  91 + task = ProposalsDiscussionPlugin::ProposalTask.new(:requestor => person, :target => profile, :article => {:name => 'proposal 1', :abstract => 'alo ruby e legal a beca seu moco nao acha'})
  92 + task.categories = [fast_create(ProposalsDiscussionPlugin::TaskCategory)]
  93 + assert !task.valid?
  94 + end
  95 +
86 96 end
... ...