Commit 5399368c0bb7a313d4df4f4e0b5c26a8dd714123
1 parent
3d090c90
Exists in
master
Refactor validation of abstract max length
Showing
2 changed files
with
16 additions
and
10 deletions
Show diff stats
lib/ext/proposal.rb
1 | 1 | require_relative '../../../proposals_discussion/lib/proposals_discussion_plugin/proposal.rb' |
2 | 2 | |
3 | -class ProposalsDiscussionPlugin::Proposal < TinyMceArticle | |
4 | - validate :abstract_max_length | |
3 | +class ProposalsDiscussionPlugin::Proposal | |
4 | + | |
5 | + validates :abstract, length: {maximum: 200}, if: proc { |a| a.environment.plugin_enabled?(DialogaPlugin)} | |
5 | 6 | |
6 | - def abstract_max_length | |
7 | - if abstract.size > 200 and environment.plugin_enabled? "Dialoga" | |
8 | - errors.add(:proposal, "Proposal must be no longer than 200 characters") | |
9 | - end | |
10 | - end | |
11 | 7 | end | ... | ... |
test/unit/proposal_test.rb
... | ... | @@ -8,17 +8,27 @@ class ProposalTest < ActiveSupport::TestCase |
8 | 8 | @discussion = ProposalsDiscussionPlugin::Discussion.create!(:name => 'discussion', :profile => person, :allow_topics => false) |
9 | 9 | @proposal = ProposalsDiscussionPlugin::Proposal.new(:name => 'test', :abstract => 'abstract', :profile => @profile, :parent => @discussion) |
10 | 10 | @proposal.created_by = @person |
11 | + @environment = Environment.default | |
12 | + environment.enable_plugin(DialogaPlugin) | |
11 | 13 | end |
12 | 14 | |
13 | - attr_reader :profile, :proposal, :person, :discussion | |
15 | + attr_reader :profile, :proposal, :person, :discussion, :environment | |
14 | 16 | |
15 | - should 'should not save a proposal with an abstract > 200 chars' do | |
17 | + should 'should not save a proposal with an abstract greater than 200 chars' do | |
16 | 18 | proposal.abstract = 201.times.map{'B'}.join |
17 | 19 | refute proposal.valid? |
18 | 20 | refute proposal.save |
21 | + assert proposal.errors[:abstract].present? | |
19 | 22 | end |
20 | 23 | |
21 | - should 'should save a proposal with abstract <= 200 chars' do | |
24 | + should 'should save a proposal with an abstract greater than 200 chars and dialoga plugin is not enabled' do | |
25 | + environment.disable_plugin(DialogaPlugin) | |
26 | + proposal.abstract = 201.times.map{'B'}.join | |
27 | + assert proposal.valid? | |
28 | + assert proposal.save | |
29 | + end | |
30 | + | |
31 | + should 'should save a proposal with an abstract not greater than 200 chars' do | |
22 | 32 | proposal.abstract = 200.times.map{'B'}.join |
23 | 33 | assert proposal.valid? |
24 | 34 | assert proposal.save | ... | ... |