Commit 5f68bfb4b9d4e192f0ada9e437e78b374548828e

Authored by Evandro Junior
1 parent 26361944

Refactor

lib/proposals_discussion_plugin/proposal_task.rb
@@ -9,11 +9,11 @@ class ProposalsDiscussionPlugin::ProposalTask < Task @@ -9,11 +9,11 @@ class ProposalsDiscussionPlugin::ProposalTask < Task
9 9
10 validates_presence_of :requestor_id, :target_id 10 validates_presence_of :requestor_id, :target_id
11 validates_associated :article_object 11 validates_associated :article_object
12 - before_save(on: :create) do 12 + before_validation do
13 self.proposal_discussion_plugin_simplified_abstract = ProposalsDiscussionPlugin::ProposalTask.simplify(abstract) 13 self.proposal_discussion_plugin_simplified_abstract = ProposalsDiscussionPlugin::ProposalTask.simplify(abstract)
14 end 14 end
  15 + validates_uniqueness_of :proposal_discussion_plugin_simplified_abstract
15 validate :require_category 16 validate :require_category
16 - validate :unique_simplified_abstract?  
17 17
18 settings_items :name, :type => String 18 settings_items :name, :type => String
19 settings_items :ip_address, :type => String 19 settings_items :ip_address, :type => String
@@ -262,8 +262,8 @@ class ProposalsDiscussionPlugin::ProposalTask < Task @@ -262,8 +262,8 @@ class ProposalsDiscussionPlugin::ProposalTask < Task
262 def self.simplify(s) 262 def self.simplify(s)
263 return nil if s.nil? 263 return nil if s.nil?
264 s=I18n.transliterate(s) 264 s=I18n.transliterate(s)
265 - s.gsub!(/(^\s)|([',.?!])|(\s$)/, "")  
266 s.gsub!(/\s{2,}/, " ") 265 s.gsub!(/\s{2,}/, " ")
  266 + s.gsub!(/(^\s)|([',.?!])|(\s$)/, "")
267 s.downcase 267 s.downcase
268 end 268 end
269 269
@@ -275,14 +275,4 @@ class ProposalsDiscussionPlugin::ProposalTask < Task @@ -275,14 +275,4 @@ class ProposalsDiscussionPlugin::ProposalTask < Task
275 end 275 end
276 end 276 end
277 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  
288 end 278 end
test/unit/proposal_task_test.rb
@@ -86,15 +86,20 @@ class ProposalTaskTest &lt; ActiveSupport::TestCase @@ -86,15 +86,20 @@ class ProposalTaskTest &lt; ActiveSupport::TestCase
86 86
87 should 'not register duplicate task' do 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Í?!. '}) 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! 89 + assert task.valid?
91 task = ProposalsDiscussionPlugin::ProposalTask.new(:requestor => person, :target => profile, :article => {:name => 'proposal 1', :abstract => 'e a e ef kdsk we32uiii '}) 90 task = ProposalsDiscussionPlugin::ProposalTask.new(:requestor => person, :target => profile, :article => {:name => 'proposal 1', :abstract => 'e a e ef kdsk we32uiii '})
  91 + assert !task.valid?, 'duplicated proposal is not valid'
  92 + end
  93 +
  94 + should 'allow edition' do
  95 + task = ProposalsDiscussionPlugin::ProposalTask.create!(:requestor => person, :target => profile, :article => {:name => 'proposal 1', :abstract => ' é Á e ef KDsk we32UiIÍ?!. '})
92 task.categories = [fast_create(ProposalsDiscussionPlugin::TaskCategory)] 96 task.categories = [fast_create(ProposalsDiscussionPlugin::TaskCategory)]
93 - assert !task.valid? 97 + task.save!
  98 + assert task.valid?, "Allow proposal to be edited"
94 end 99 end
95 100
96 should 'simplify abstract' do 101 should 'simplify abstract' do
97 - assert_equal ProposalsDiscussionPlugin::ProposalTask.simplify(' é Á e ef KDsk we32UiIÍ?!. '), "e a e ef kdsk we32uiii " 102 + assert_equal ProposalsDiscussionPlugin::ProposalTask.simplify(' é Á e ef KDsk we32UiIÍ?!. '), "e a e ef kdsk we32uiii"
98 end 103 end
99 104
100 end 105 end