Commit efef1fc99875b93c1e1b090014b75119f61c1f18
1 parent
a1650079
Exists in
master
and in
7 other branches
Avoid duplicated tasks
Showing
3 changed files
with
33 additions
and
1 deletions
Show diff stats
db/migrate/20150903210329_add_proposal_discussion_plugin_simplified_abstract_to_tasks.rb
0 → 100644
| @@ -0,0 +1,9 @@ | @@ -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 < Task | @@ -10,8 +10,9 @@ class ProposalsDiscussionPlugin::ProposalTask < Task | ||
| 10 | 10 | ||
| 11 | validates_presence_of :requestor_id, :target_id | 11 | validates_presence_of :requestor_id, :target_id |
| 12 | validates_associated :article_object | 12 | validates_associated :article_object |
| 13 | - | 13 | + before_validation :simplify_abstract |
| 14 | validate :require_category | 14 | validate :require_category |
| 15 | + validates_uniqueness_of :proposal_discussion_plugin_simplified_abstract, :scope => ['target_id'], :message => _("Cannot create duplicate proposal") | ||
| 15 | 16 | ||
| 16 | settings_items :name, :type => String | 17 | settings_items :name, :type => String |
| 17 | settings_items :ip_address, :type => String | 18 | settings_items :ip_address, :type => String |
| @@ -258,8 +259,20 @@ class ProposalsDiscussionPlugin::ProposalTask < Task | @@ -258,8 +259,20 @@ class ProposalsDiscussionPlugin::ProposalTask < Task | ||
| 258 | parent.name if parent | 259 | parent.name if parent |
| 259 | end | 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 | protected | 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 | def require_category | 276 | def require_category |
| 264 | if categories.count == 0 && flagged? | 277 | if categories.count == 0 && flagged? |
| 265 | errors.add :categories, _('Select at least one category') | 278 | errors.add :categories, _('Select at least one category') |
test/unit/proposal_task_test.rb
| 1 | +# encoding: UTF-8 | ||
| 1 | require_relative '../test_helper' | 2 | require_relative '../test_helper' |
| 2 | 3 | ||
| 3 | class ProposalTaskTest < ActiveSupport::TestCase | 4 | class ProposalTaskTest < ActiveSupport::TestCase |
| @@ -83,4 +84,13 @@ class ProposalTaskTest < ActiveSupport::TestCase | @@ -83,4 +84,13 @@ class ProposalTaskTest < ActiveSupport::TestCase | ||
| 83 | assert task.information.present? | 84 | assert task.information.present? |
| 84 | end | 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 | end | 96 | end |