Commit 3ec38214b97d1c65da67881636a517136aaea621
1 parent
ea021e7f
Exists in
master
and in
27 other branches
curriculum_lattes_plugin: avoid affecting core
When plugin is not enabled on environment, the plugin extensions can't affect other features
Showing
2 changed files
with
41 additions
and
3 deletions
Show diff stats
plugins/lattes_curriculum/lib/ext/person.rb
| @@ -4,16 +4,28 @@ class Person | @@ -4,16 +4,28 @@ class Person | ||
| 4 | 4 | ||
| 5 | attr_accessible :lattes_url, :academic_info_attributes | 5 | attr_accessible :lattes_url, :academic_info_attributes |
| 6 | 6 | ||
| 7 | - has_one :academic_info, :dependent=>:delete | 7 | + has_one :academic_info |
| 8 | + | ||
| 9 | + after_destroy do |person| | ||
| 10 | + if !person.environment.nil? && | ||
| 11 | +person.environment.plugin_enabled?(LattesCurriculumPlugin) && | ||
| 12 | +!person.academic_info.nil? | ||
| 13 | + person.academic_info.destroy | ||
| 14 | + end | ||
| 15 | + end | ||
| 8 | 16 | ||
| 9 | accepts_nested_attributes_for :academic_info | 17 | accepts_nested_attributes_for :academic_info |
| 10 | 18 | ||
| 11 | def lattes_url | 19 | def lattes_url |
| 12 | - self.academic_info.nil? ? nil : self.academic_info.lattes_url | 20 | + if self.environment && self.environment.plugin_enabled?(LattesCurriculumPlugin) |
| 21 | + self.academic_info.nil? ? nil : self.academic_info.lattes_url | ||
| 22 | + end | ||
| 13 | end | 23 | end |
| 14 | 24 | ||
| 15 | def lattes_url= value | 25 | def lattes_url= value |
| 16 | - self.academic_info.lattes_url = value unless self.academic_info.nil? | 26 | + if self.environment && self.environment.plugin_enabled?(LattesCurriculumPlugin) |
| 27 | + self.academic_info.lattes_url = value unless self.academic_info.nil? | ||
| 28 | + end | ||
| 17 | end | 29 | end |
| 18 | 30 | ||
| 19 | FIELDS << "lattes_url" | 31 | FIELDS << "lattes_url" |
| @@ -0,0 +1,26 @@ | @@ -0,0 +1,26 @@ | ||
| 1 | +require "test_helper" | ||
| 2 | + | ||
| 3 | +class PersonTest < ActiveSupport::TestCase | ||
| 4 | + | ||
| 5 | + def setup | ||
| 6 | + @environment = Environment.default | ||
| 7 | + @environment.enable_plugin(LattesCurriculumPlugin) | ||
| 8 | + end | ||
| 9 | + | ||
| 10 | + attr_reader :environment | ||
| 11 | + | ||
| 12 | + should 'destroy academic info if person is removed' do | ||
| 13 | + person = fast_create(Person) | ||
| 14 | + academic_info = fast_create(AcademicInfo, :person_id => person.id, | ||
| 15 | +:lattes_url => 'http://lattes.cnpq.br/2193972715230') | ||
| 16 | + | ||
| 17 | + assert_difference 'AcademicInfo.count', -1 do | ||
| 18 | + person.destroy | ||
| 19 | + end | ||
| 20 | + end | ||
| 21 | + | ||
| 22 | + should 'add lattes_url field to Person' do | ||
| 23 | + assert_includes Person.fields, 'lattes_url' | ||
| 24 | + end | ||
| 25 | + | ||
| 26 | +end |