Commit 26361944f6f737a29388ab5a989a2174fe8475eb

Authored by Evandro Junior
1 parent a1650079

avoid duplicated proposal, can be improved

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/api.rb
... ... @@ -23,7 +23,7 @@ class ProposalsDiscussionPlugin::API &lt; Grape::API
23 23 proposal_task.requestor = current_person
24 24  
25 25 unless proposal_task.save
26   - render_api_errors!(proposal_task.article_object.errors.full_messages)
  26 + render_api_errors!(proposal_task.errors)
27 27 end
28 28 {:success => true}
29 29 #present proposal_task, :with => Entities::Task, :fields => params[:fields]
... ...
lib/proposals_discussion_plugin/proposal_task.rb
1 1 class ProposalsDiscussionPlugin::ProposalTask < Task
2   -
3 2 has_and_belongs_to_many :categories,
4 3 class_name: "ProposalsDiscussionPlugin::TaskCategory",
5 4 join_table: :proposals_discussion_plugin_task_categories,
... ... @@ -10,8 +9,11 @@ class ProposalsDiscussionPlugin::ProposalTask &lt; Task
10 9  
11 10 validates_presence_of :requestor_id, :target_id
12 11 validates_associated :article_object
13   -
  12 + before_save(on: :create) do
  13 + self.proposal_discussion_plugin_simplified_abstract = ProposalsDiscussionPlugin::ProposalTask.simplify(abstract)
  14 + end
14 15 validate :require_category
  16 + validate :unique_simplified_abstract?
15 17  
16 18 settings_items :name, :type => String
17 19 settings_items :ip_address, :type => String
... ... @@ -20,7 +22,6 @@ class ProposalsDiscussionPlugin::ProposalTask &lt; Task
20 22 settings_items :article, :type => Hash, :default => {}
21 23 settings_items :closing_statment, :article_parent_id
22 24  
23   -
24 25 scope :pending_evaluated, lambda { |profile, filter_type, filter_text|
25 26 self
26 27 .to(profile)
... ... @@ -258,6 +259,14 @@ 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  
263 272 def require_category
... ... @@ -266,4 +275,14 @@ class ProposalsDiscussionPlugin::ProposalTask &lt; Task
266 275 end
267 276 end
268 277  
  278 + def unique_simplified_abstract?
  279 + ActiveRecord::Base.logger = Logger.new(STDOUT)
  280 + sa = ProposalsDiscussionPlugin::ProposalTask.simplify(abstract)
  281 + if id.present?
  282 + r = ProposalsDiscussionPlugin::ProposalTask.find_by_sql ["SELECT 1 AS one FROM tasks WHERE tasks.id <> ? and tasks.type IN ('ProposalsDiscussionPlugin::ProposalTask') AND (tasks.proposal_discussion_plugin_simplified_abstract = ? AND tasks.target_id = ?) LIMIT 1", id, sa, target_id]
  283 + else
  284 + r = ProposalsDiscussionPlugin::ProposalTask.find_by_sql ["SELECT 1 AS one FROM tasks WHERE tasks.type IN ('ProposalsDiscussionPlugin::ProposalTask') AND (tasks.proposal_discussion_plugin_simplified_abstract = ? AND tasks.target_id = ?) LIMIT 1", sa, target_id]
  285 + end
  286 + errors.add :base, _('This proposal has been previously registered') unless r.empty?
  287 + end
269 288 end
... ...
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,17 @@ 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 => ' é Á e ef KDsk we32UiIÍ?!. '})
  89 + task.categories = [fast_create(ProposalsDiscussionPlugin::TaskCategory)]
  90 + task.save!
  91 + task = ProposalsDiscussionPlugin::ProposalTask.new(:requestor => person, :target => profile, :article => {:name => 'proposal 1', :abstract => 'e a e ef kdsk we32uiii '})
  92 + task.categories = [fast_create(ProposalsDiscussionPlugin::TaskCategory)]
  93 + assert !task.valid?
  94 + end
  95 +
  96 + should 'simplify abstract' do
  97 + assert_equal ProposalsDiscussionPlugin::ProposalTask.simplify(' é Á e ef KDsk we32UiIÍ?!. '), "e a e ef kdsk we32uiii "
  98 + end
  99 +
86 100 end
... ...