Class: Role

Inherits:
ApplicationRecord show all
Includes:
Housekeeping::Timestamps, Housekeeping::Users, Shared::IsData
Defined in:
app/models/role.rb

Overview

A Role relates a Person (a Person is data in TaxonWorks) to other data.

Direct Known Subclasses

AttributionRole, ProjectRole, SourceRole

Defined Under Namespace

Classes: AttributionRole, ProjectRole, SourceRole

Instance Attribute Summary (collapse)

Attributes included from Housekeeping::Users

#by

Instance Method Summary (collapse)

Methods included from Housekeeping::Timestamps

#data_breakdown_for_chartkick_recent

Methods included from Housekeeping::Users

#set_created_by_id, #set_updated_by_id

Instance Attribute Details

- (Integer) person_id

Returns The ID of the person in the role.

Returns:

  • (Integer)

    The ID of the person in the role.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/models/role.rb', line 27

class Role < ApplicationRecord
  include Housekeeping::Users
  include Housekeeping::Timestamps
  include Shared::IsData

  acts_as_list scope: [:type, :role_object_type, :role_object_id]

  after_save :vet_person
  after_save :update_cached
  after_save :update_person_year_metadata
  after_destroy :check_for_last

  belongs_to :role_object, polymorphic: :true #, validate: true
  belongs_to :person, inverse_of: :roles, validate: true
  accepts_nested_attributes_for :person, reject_if: :all_blank, allow_destroy: true

  # role_object is required but at the database constraint level at present
  # validates :role_object, presence: true
  validates_presence_of :type
  validates :person, presence: true
  validates_uniqueness_of :person_id, scope: [:role_object_id, :role_object_type, :type]

  protected

  def update_cached
    # TODO: optimize, perhaps on set_author_year
    role_object.send(:set_cached) if role_object.respond_to?(:set_cached, true)
  end

  def check_for_last
    if is_last_role? && role_object_type == 'Source'
      add_touch = false
      if type == 'SourceAuthor'
        role_object.update_columns(author: nil, cached_author_string: nil)
        add_touch = true
      end
      if type == 'SourceEditor'
        add_touch = true
        role_object.update_columns(editor: nil)
      end
      role_object.touch
    else
      role_object.send(:set_cached) if role_object.respond_to?(:set_cached, true)
    end
  end

  def is_last_role?
    role_object.roles.count == 0
  end

  # See /app/models/person.rb for a definition of vetted
  def vet_person
    # Check whether there are one or more *other* roles besides this one, 
    # i.e. there are at least *2* for person_id
    if Role.where(person_id: person_id).where.not(id: id).any?
      person.update_column(:type, 'Person::Vetted') 
    end
  end

  def 
    if role_object.respond_to?(:year)
      begin
        y = role_object.try(:year)
        y ||= role_object.try(:year_of_publication)

        yas = [y, person.year_active_start].compact.map(&:to_i).min
        yae = [y, person.year_active_end].compact.map(&:to_i).max

        person.update(
          year_active_end: yae,
          year_active_start: yas
        )

      rescue ActiveRecord::RecordInvalid
        # probably a year conflict, allow quietly
      end
    end
  end

end

- (Integer) position

Returns Sort order.

Returns:

  • (Integer)

    Sort order.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/models/role.rb', line 27

class Role < ApplicationRecord
  include Housekeeping::Users
  include Housekeeping::Timestamps
  include Shared::IsData

  acts_as_list scope: [:type, :role_object_type, :role_object_id]

  after_save :vet_person
  after_save :update_cached
  after_save :update_person_year_metadata
  after_destroy :check_for_last

  belongs_to :role_object, polymorphic: :true #, validate: true
  belongs_to :person, inverse_of: :roles, validate: true
  accepts_nested_attributes_for :person, reject_if: :all_blank, allow_destroy: true

  # role_object is required but at the database constraint level at present
  # validates :role_object, presence: true
  validates_presence_of :type
  validates :person, presence: true
  validates_uniqueness_of :person_id, scope: [:role_object_id, :role_object_type, :type]

  protected

  def update_cached
    # TODO: optimize, perhaps on set_author_year
    role_object.send(:set_cached) if role_object.respond_to?(:set_cached, true)
  end

  def check_for_last
    if is_last_role? && role_object_type == 'Source'
      add_touch = false
      if type == 'SourceAuthor'
        role_object.update_columns(author: nil, cached_author_string: nil)
        add_touch = true
      end
      if type == 'SourceEditor'
        add_touch = true
        role_object.update_columns(editor: nil)
      end
      role_object.touch
    else
      role_object.send(:set_cached) if role_object.respond_to?(:set_cached, true)
    end
  end

  def is_last_role?
    role_object.roles.count == 0
  end

  # See /app/models/person.rb for a definition of vetted
  def vet_person
    # Check whether there are one or more *other* roles besides this one, 
    # i.e. there are at least *2* for person_id
    if Role.where(person_id: person_id).where.not(id: id).any?
      person.update_column(:type, 'Person::Vetted') 
    end
  end

  def 
    if role_object.respond_to?(:year)
      begin
        y = role_object.try(:year)
        y ||= role_object.try(:year_of_publication)

        yas = [y, person.year_active_start].compact.map(&:to_i).min
        yae = [y, person.year_active_end].compact.map(&:to_i).max

        person.update(
          year_active_end: yae,
          year_active_start: yas
        )

      rescue ActiveRecord::RecordInvalid
        # probably a year conflict, allow quietly
      end
    end
  end

end

- (Integer) project_id

the project ID

Returns:

  • (Integer)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/models/role.rb', line 27

class Role < ApplicationRecord
  include Housekeeping::Users
  include Housekeeping::Timestamps
  include Shared::IsData

  acts_as_list scope: [:type, :role_object_type, :role_object_id]

  after_save :vet_person
  after_save :update_cached
  after_save :update_person_year_metadata
  after_destroy :check_for_last

  belongs_to :role_object, polymorphic: :true #, validate: true
  belongs_to :person, inverse_of: :roles, validate: true
  accepts_nested_attributes_for :person, reject_if: :all_blank, allow_destroy: true

  # role_object is required but at the database constraint level at present
  # validates :role_object, presence: true
  validates_presence_of :type
  validates :person, presence: true
  validates_uniqueness_of :person_id, scope: [:role_object_id, :role_object_type, :type]

  protected

  def update_cached
    # TODO: optimize, perhaps on set_author_year
    role_object.send(:set_cached) if role_object.respond_to?(:set_cached, true)
  end

  def check_for_last
    if is_last_role? && role_object_type == 'Source'
      add_touch = false
      if type == 'SourceAuthor'
        role_object.update_columns(author: nil, cached_author_string: nil)
        add_touch = true
      end
      if type == 'SourceEditor'
        add_touch = true
        role_object.update_columns(editor: nil)
      end
      role_object.touch
    else
      role_object.send(:set_cached) if role_object.respond_to?(:set_cached, true)
    end
  end

  def is_last_role?
    role_object.roles.count == 0
  end

  # See /app/models/person.rb for a definition of vetted
  def vet_person
    # Check whether there are one or more *other* roles besides this one, 
    # i.e. there are at least *2* for person_id
    if Role.where(person_id: person_id).where.not(id: id).any?
      person.update_column(:type, 'Person::Vetted') 
    end
  end

  def 
    if role_object.respond_to?(:year)
      begin
        y = role_object.try(:year)
        y ||= role_object.try(:year_of_publication)

        yas = [y, person.year_active_start].compact.map(&:to_i).min
        yae = [y, person.year_active_end].compact.map(&:to_i).max

        person.update(
          year_active_end: yae,
          year_active_start: yas
        )

      rescue ActiveRecord::RecordInvalid
        # probably a year conflict, allow quietly
      end
    end
  end

end

- (Integer) role_object_id

Returns The id of the object the role is bound to.

Returns:

  • (Integer)

    The id of the object the role is bound to.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/models/role.rb', line 27

class Role < ApplicationRecord
  include Housekeeping::Users
  include Housekeeping::Timestamps
  include Shared::IsData

  acts_as_list scope: [:type, :role_object_type, :role_object_id]

  after_save :vet_person
  after_save :update_cached
  after_save :update_person_year_metadata
  after_destroy :check_for_last

  belongs_to :role_object, polymorphic: :true #, validate: true
  belongs_to :person, inverse_of: :roles, validate: true
  accepts_nested_attributes_for :person, reject_if: :all_blank, allow_destroy: true

  # role_object is required but at the database constraint level at present
  # validates :role_object, presence: true
  validates_presence_of :type
  validates :person, presence: true
  validates_uniqueness_of :person_id, scope: [:role_object_id, :role_object_type, :type]

  protected

  def update_cached
    # TODO: optimize, perhaps on set_author_year
    role_object.send(:set_cached) if role_object.respond_to?(:set_cached, true)
  end

  def check_for_last
    if is_last_role? && role_object_type == 'Source'
      add_touch = false
      if type == 'SourceAuthor'
        role_object.update_columns(author: nil, cached_author_string: nil)
        add_touch = true
      end
      if type == 'SourceEditor'
        add_touch = true
        role_object.update_columns(editor: nil)
      end
      role_object.touch
    else
      role_object.send(:set_cached) if role_object.respond_to?(:set_cached, true)
    end
  end

  def is_last_role?
    role_object.roles.count == 0
  end

  # See /app/models/person.rb for a definition of vetted
  def vet_person
    # Check whether there are one or more *other* roles besides this one, 
    # i.e. there are at least *2* for person_id
    if Role.where(person_id: person_id).where.not(id: id).any?
      person.update_column(:type, 'Person::Vetted') 
    end
  end

  def 
    if role_object.respond_to?(:year)
      begin
        y = role_object.try(:year)
        y ||= role_object.try(:year_of_publication)

        yas = [y, person.year_active_start].compact.map(&:to_i).min
        yae = [y, person.year_active_end].compact.map(&:to_i).max

        person.update(
          year_active_end: yae,
          year_active_start: yas
        )

      rescue ActiveRecord::RecordInvalid
        # probably a year conflict, allow quietly
      end
    end
  end

end

- (String) role_object_type

Returns THe class of the object the role is bound to.

Returns:

  • (String)

    THe class of the object the role is bound to.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/models/role.rb', line 27

class Role < ApplicationRecord
  include Housekeeping::Users
  include Housekeeping::Timestamps
  include Shared::IsData

  acts_as_list scope: [:type, :role_object_type, :role_object_id]

  after_save :vet_person
  after_save :update_cached
  after_save :update_person_year_metadata
  after_destroy :check_for_last

  belongs_to :role_object, polymorphic: :true #, validate: true
  belongs_to :person, inverse_of: :roles, validate: true
  accepts_nested_attributes_for :person, reject_if: :all_blank, allow_destroy: true

  # role_object is required but at the database constraint level at present
  # validates :role_object, presence: true
  validates_presence_of :type
  validates :person, presence: true
  validates_uniqueness_of :person_id, scope: [:role_object_id, :role_object_type, :type]

  protected

  def update_cached
    # TODO: optimize, perhaps on set_author_year
    role_object.send(:set_cached) if role_object.respond_to?(:set_cached, true)
  end

  def check_for_last
    if is_last_role? && role_object_type == 'Source'
      add_touch = false
      if type == 'SourceAuthor'
        role_object.update_columns(author: nil, cached_author_string: nil)
        add_touch = true
      end
      if type == 'SourceEditor'
        add_touch = true
        role_object.update_columns(editor: nil)
      end
      role_object.touch
    else
      role_object.send(:set_cached) if role_object.respond_to?(:set_cached, true)
    end
  end

  def is_last_role?
    role_object.roles.count == 0
  end

  # See /app/models/person.rb for a definition of vetted
  def vet_person
    # Check whether there are one or more *other* roles besides this one, 
    # i.e. there are at least *2* for person_id
    if Role.where(person_id: person_id).where.not(id: id).any?
      person.update_column(:type, 'Person::Vetted') 
    end
  end

  def 
    if role_object.respond_to?(:year)
      begin
        y = role_object.try(:year)
        y ||= role_object.try(:year_of_publication)

        yas = [y, person.year_active_start].compact.map(&:to_i).min
        yae = [y, person.year_active_end].compact.map(&:to_i).max

        person.update(
          year_active_end: yae,
          year_active_start: yas
        )

      rescue ActiveRecord::RecordInvalid
        # probably a year conflict, allow quietly
      end
    end
  end

end

- (String) type

Returns The type (subclass) of the role, e.g. TaxonDeterminer.

Returns:

  • (String)

    The type (subclass) of the role, e.g. TaxonDeterminer.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/models/role.rb', line 27

class Role < ApplicationRecord
  include Housekeeping::Users
  include Housekeeping::Timestamps
  include Shared::IsData

  acts_as_list scope: [:type, :role_object_type, :role_object_id]

  after_save :vet_person
  after_save :update_cached
  after_save :update_person_year_metadata
  after_destroy :check_for_last

  belongs_to :role_object, polymorphic: :true #, validate: true
  belongs_to :person, inverse_of: :roles, validate: true
  accepts_nested_attributes_for :person, reject_if: :all_blank, allow_destroy: true

  # role_object is required but at the database constraint level at present
  # validates :role_object, presence: true
  validates_presence_of :type
  validates :person, presence: true
  validates_uniqueness_of :person_id, scope: [:role_object_id, :role_object_type, :type]

  protected

  def update_cached
    # TODO: optimize, perhaps on set_author_year
    role_object.send(:set_cached) if role_object.respond_to?(:set_cached, true)
  end

  def check_for_last
    if is_last_role? && role_object_type == 'Source'
      add_touch = false
      if type == 'SourceAuthor'
        role_object.update_columns(author: nil, cached_author_string: nil)
        add_touch = true
      end
      if type == 'SourceEditor'
        add_touch = true
        role_object.update_columns(editor: nil)
      end
      role_object.touch
    else
      role_object.send(:set_cached) if role_object.respond_to?(:set_cached, true)
    end
  end

  def is_last_role?
    role_object.roles.count == 0
  end

  # See /app/models/person.rb for a definition of vetted
  def vet_person
    # Check whether there are one or more *other* roles besides this one, 
    # i.e. there are at least *2* for person_id
    if Role.where(person_id: person_id).where.not(id: id).any?
      person.update_column(:type, 'Person::Vetted') 
    end
  end

  def 
    if role_object.respond_to?(:year)
      begin
        y = role_object.try(:year)
        y ||= role_object.try(:year_of_publication)

        yas = [y, person.year_active_start].compact.map(&:to_i).min
        yae = [y, person.year_active_end].compact.map(&:to_i).max

        person.update(
          year_active_end: yae,
          year_active_start: yas
        )

      rescue ActiveRecord::RecordInvalid
        # probably a year conflict, allow quietly
      end
    end
  end

end

Instance Method Details

- (Object) check_for_last (protected)



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/models/role.rb', line 56

def check_for_last
  if is_last_role? && role_object_type == 'Source'
    add_touch = false
    if type == 'SourceAuthor'
      role_object.update_columns(author: nil, cached_author_string: nil)
      add_touch = true
    end
    if type == 'SourceEditor'
      add_touch = true
      role_object.update_columns(editor: nil)
    end
    role_object.touch
  else
    role_object.send(:set_cached) if role_object.respond_to?(:set_cached, true)
  end
end

- (Boolean) is_last_role? (protected)

Returns:

  • (Boolean)


73
74
75
# File 'app/models/role.rb', line 73

def is_last_role?
  role_object.roles.count == 0
end

- (Object) update_cached (protected)



51
52
53
54
# File 'app/models/role.rb', line 51

def update_cached
  # TODO: optimize, perhaps on set_author_year
  role_object.send(:set_cached) if role_object.respond_to?(:set_cached, true)
end

- (Object) update_person_year_metadata (protected)



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/models/role.rb', line 86

def 
  if role_object.respond_to?(:year)
    begin
      y = role_object.try(:year)
      y ||= role_object.try(:year_of_publication)

      yas = [y, person.year_active_start].compact.map(&:to_i).min
      yae = [y, person.year_active_end].compact.map(&:to_i).max

      person.update(
        year_active_end: yae,
        year_active_start: yas
      )

    rescue ActiveRecord::RecordInvalid
      # probably a year conflict, allow quietly
    end
  end
end

- (Object) vet_person (protected)

See /app/models/person.rb for a definition of vetted



78
79
80
81
82
83
84
# File 'app/models/role.rb', line 78

def vet_person
  # Check whether there are one or more *other* roles besides this one, 
  # i.e. there are at least *2* for person_id
  if Role.where(person_id: person_id).where.not(id: id).any?
    person.update_column(:type, 'Person::Vetted') 
  end
end