Commit 5f68bfb4b9d4e192f0ada9e437e78b374548828e
1 parent
26361944
Exists in
avoid_duplicated_tasks
Refactor
Showing
2 changed files
with
12 additions
and
17 deletions
Show diff stats
lib/proposals_discussion_plugin/proposal_task.rb
... | ... | @@ -9,11 +9,11 @@ class ProposalsDiscussionPlugin::ProposalTask < Task |
9 | 9 | |
10 | 10 | validates_presence_of :requestor_id, :target_id |
11 | 11 | validates_associated :article_object |
12 | - before_save(on: :create) do | |
12 | + before_validation do | |
13 | 13 | self.proposal_discussion_plugin_simplified_abstract = ProposalsDiscussionPlugin::ProposalTask.simplify(abstract) |
14 | 14 | end |
15 | + validates_uniqueness_of :proposal_discussion_plugin_simplified_abstract | |
15 | 16 | validate :require_category |
16 | - validate :unique_simplified_abstract? | |
17 | 17 | |
18 | 18 | settings_items :name, :type => String |
19 | 19 | settings_items :ip_address, :type => String |
... | ... | @@ -262,8 +262,8 @@ class ProposalsDiscussionPlugin::ProposalTask < Task |
262 | 262 | def self.simplify(s) |
263 | 263 | return nil if s.nil? |
264 | 264 | s=I18n.transliterate(s) |
265 | - s.gsub!(/(^\s)|([',.?!])|(\s$)/, "") | |
266 | 265 | s.gsub!(/\s{2,}/, " ") |
266 | + s.gsub!(/(^\s)|([',.?!])|(\s$)/, "") | |
267 | 267 | s.downcase |
268 | 268 | end |
269 | 269 | |
... | ... | @@ -275,14 +275,4 @@ class ProposalsDiscussionPlugin::ProposalTask < Task |
275 | 275 | end |
276 | 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 | 278 | end | ... | ... |
test/unit/proposal_task_test.rb
... | ... | @@ -86,15 +86,20 @@ class ProposalTaskTest < ActiveSupport::TestCase |
86 | 86 | |
87 | 87 | should 'not register duplicate task' do |
88 | 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 | 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 | 96 | task.categories = [fast_create(ProposalsDiscussionPlugin::TaskCategory)] |
93 | - assert !task.valid? | |
97 | + task.save! | |
98 | + assert task.valid?, "Allow proposal to be edited" | |
94 | 99 | end |
95 | 100 | |
96 | 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 | 103 | end |
99 | 104 | |
100 | 105 | end | ... | ... |