Class: Role

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

Overview

Had we started from scratch we might have implemented a polymorphic `role_agent`, though we reference people far more often than organziations, so it would have felt klunky to always de-reference to role_agent.

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 Shared::IsData

#errors_excepting, #full_error_messages_excepting, #identical, #is_community?, #is_destroyable?, #is_editable?, #is_in_use?, #is_in_users_projects?, #metamorphosize, #similar

Methods included from Housekeeping::Timestamps

#data_breakdown_for_chartkick_recent

Methods included from Housekeeping::Users

#set_created_by_id, #set_updated_by_id

Methods inherited from ApplicationRecord

transaction_with_retry

Instance Attribute Details

#organization_idInteger

Returns The ID of the Organization in the role.

Returns:

  • (Integer)

    The ID of the Organization in the role.



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
107
108
109
110
111
# File 'app/models/role.rb', line 38

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

  include Shared::PolymorphicAnnotator
  polymorphic_annotates(:role_object)

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

  belongs_to :organization, inverse_of: :roles, validate: true
  belongs_to :person, inverse_of: :roles, validate: true
  belongs_to :role_object, polymorphic: :true #, validate: true

  after_save :update_cached

  validates_presence_of :type
  validate :agent_present,
    :only_one_agent,
    :agent_is_legal

  # validates_uniqueness_of :person_id, scope: [:project_id, :role_object_type, :role_object_id]

  # role_object presence is a database constraint level
  # validates :role_object, presence: true

  # Must come after belongs_to associations
  include Roles::Person

  # Overrode in Roles::Organization
  def organization_allowed?
    false
  end

  def agent_type
    if person
      :person
    elsif organization
      :organization
    else
      nil
    end
  end

  protected

  def agent_present
    if !person.present? && !organization.present?
      errors.add(:base, 'missing an agent (person or organization)')
    end
  end

  def agent_is_legal
    if organization.present?
      errors.add(:organization_id, 'is not permitted for this role type') unless organization_allowed?
    end
  end

  def only_one_agent
    if person && organization
      errors.add(:person_id, 'organization is also selected')
      errors.add(:organization_id, 'organization is also selected')
    end
  end

  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 is_last_role?
    role_object.roles.count == 0
  end
end

#person_idInteger

Returns The ID of the Person in the role.

Returns:

  • (Integer)

    The ID of the Person in the role.



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
107
108
109
110
111
# File 'app/models/role.rb', line 38

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

  include Shared::PolymorphicAnnotator
  polymorphic_annotates(:role_object)

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

  belongs_to :organization, inverse_of: :roles, validate: true
  belongs_to :person, inverse_of: :roles, validate: true
  belongs_to :role_object, polymorphic: :true #, validate: true

  after_save :update_cached

  validates_presence_of :type
  validate :agent_present,
    :only_one_agent,
    :agent_is_legal

  # validates_uniqueness_of :person_id, scope: [:project_id, :role_object_type, :role_object_id]

  # role_object presence is a database constraint level
  # validates :role_object, presence: true

  # Must come after belongs_to associations
  include Roles::Person

  # Overrode in Roles::Organization
  def organization_allowed?
    false
  end

  def agent_type
    if person
      :person
    elsif organization
      :organization
    else
      nil
    end
  end

  protected

  def agent_present
    if !person.present? && !organization.present?
      errors.add(:base, 'missing an agent (person or organization)')
    end
  end

  def agent_is_legal
    if organization.present?
      errors.add(:organization_id, 'is not permitted for this role type') unless organization_allowed?
    end
  end

  def only_one_agent
    if person && organization
      errors.add(:person_id, 'organization is also selected')
      errors.add(:organization_id, 'organization is also selected')
    end
  end

  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 is_last_role?
    role_object.roles.count == 0
  end
end

#positionInteger

Returns Sort order.

Returns:

  • (Integer)

    Sort order.



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
107
108
109
110
111
# File 'app/models/role.rb', line 38

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

  include Shared::PolymorphicAnnotator
  polymorphic_annotates(:role_object)

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

  belongs_to :organization, inverse_of: :roles, validate: true
  belongs_to :person, inverse_of: :roles, validate: true
  belongs_to :role_object, polymorphic: :true #, validate: true

  after_save :update_cached

  validates_presence_of :type
  validate :agent_present,
    :only_one_agent,
    :agent_is_legal

  # validates_uniqueness_of :person_id, scope: [:project_id, :role_object_type, :role_object_id]

  # role_object presence is a database constraint level
  # validates :role_object, presence: true

  # Must come after belongs_to associations
  include Roles::Person

  # Overrode in Roles::Organization
  def organization_allowed?
    false
  end

  def agent_type
    if person
      :person
    elsif organization
      :organization
    else
      nil
    end
  end

  protected

  def agent_present
    if !person.present? && !organization.present?
      errors.add(:base, 'missing an agent (person or organization)')
    end
  end

  def agent_is_legal
    if organization.present?
      errors.add(:organization_id, 'is not permitted for this role type') unless organization_allowed?
    end
  end

  def only_one_agent
    if person && organization
      errors.add(:person_id, 'organization is also selected')
      errors.add(:organization_id, 'organization is also selected')
    end
  end

  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 is_last_role?
    role_object.roles.count == 0
  end
end

#project_idInteger

the project ID

Returns:

  • (Integer)


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
107
108
109
110
111
# File 'app/models/role.rb', line 38

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

  include Shared::PolymorphicAnnotator
  polymorphic_annotates(:role_object)

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

  belongs_to :organization, inverse_of: :roles, validate: true
  belongs_to :person, inverse_of: :roles, validate: true
  belongs_to :role_object, polymorphic: :true #, validate: true

  after_save :update_cached

  validates_presence_of :type
  validate :agent_present,
    :only_one_agent,
    :agent_is_legal

  # validates_uniqueness_of :person_id, scope: [:project_id, :role_object_type, :role_object_id]

  # role_object presence is a database constraint level
  # validates :role_object, presence: true

  # Must come after belongs_to associations
  include Roles::Person

  # Overrode in Roles::Organization
  def organization_allowed?
    false
  end

  def agent_type
    if person
      :person
    elsif organization
      :organization
    else
      nil
    end
  end

  protected

  def agent_present
    if !person.present? && !organization.present?
      errors.add(:base, 'missing an agent (person or organization)')
    end
  end

  def agent_is_legal
    if organization.present?
      errors.add(:organization_id, 'is not permitted for this role type') unless organization_allowed?
    end
  end

  def only_one_agent
    if person && organization
      errors.add(:person_id, 'organization is also selected')
      errors.add(:organization_id, 'organization is also selected')
    end
  end

  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 is_last_role?
    role_object.roles.count == 0
  end
end

#role_object_idInteger

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

Returns:

  • (Integer)

    The id of the object the role is bound to.



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
107
108
109
110
111
# File 'app/models/role.rb', line 38

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

  include Shared::PolymorphicAnnotator
  polymorphic_annotates(:role_object)

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

  belongs_to :organization, inverse_of: :roles, validate: true
  belongs_to :person, inverse_of: :roles, validate: true
  belongs_to :role_object, polymorphic: :true #, validate: true

  after_save :update_cached

  validates_presence_of :type
  validate :agent_present,
    :only_one_agent,
    :agent_is_legal

  # validates_uniqueness_of :person_id, scope: [:project_id, :role_object_type, :role_object_id]

  # role_object presence is a database constraint level
  # validates :role_object, presence: true

  # Must come after belongs_to associations
  include Roles::Person

  # Overrode in Roles::Organization
  def organization_allowed?
    false
  end

  def agent_type
    if person
      :person
    elsif organization
      :organization
    else
      nil
    end
  end

  protected

  def agent_present
    if !person.present? && !organization.present?
      errors.add(:base, 'missing an agent (person or organization)')
    end
  end

  def agent_is_legal
    if organization.present?
      errors.add(:organization_id, 'is not permitted for this role type') unless organization_allowed?
    end
  end

  def only_one_agent
    if person && organization
      errors.add(:person_id, 'organization is also selected')
      errors.add(:organization_id, 'organization is also selected')
    end
  end

  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 is_last_role?
    role_object.roles.count == 0
  end
end

#role_object_typeString

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

Returns:

  • (String)

    THe class of the object the role is bound to.



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
107
108
109
110
111
# File 'app/models/role.rb', line 38

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

  include Shared::PolymorphicAnnotator
  polymorphic_annotates(:role_object)

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

  belongs_to :organization, inverse_of: :roles, validate: true
  belongs_to :person, inverse_of: :roles, validate: true
  belongs_to :role_object, polymorphic: :true #, validate: true

  after_save :update_cached

  validates_presence_of :type
  validate :agent_present,
    :only_one_agent,
    :agent_is_legal

  # validates_uniqueness_of :person_id, scope: [:project_id, :role_object_type, :role_object_id]

  # role_object presence is a database constraint level
  # validates :role_object, presence: true

  # Must come after belongs_to associations
  include Roles::Person

  # Overrode in Roles::Organization
  def organization_allowed?
    false
  end

  def agent_type
    if person
      :person
    elsif organization
      :organization
    else
      nil
    end
  end

  protected

  def agent_present
    if !person.present? && !organization.present?
      errors.add(:base, 'missing an agent (person or organization)')
    end
  end

  def agent_is_legal
    if organization.present?
      errors.add(:organization_id, 'is not permitted for this role type') unless organization_allowed?
    end
  end

  def only_one_agent
    if person && organization
      errors.add(:person_id, 'organization is also selected')
      errors.add(:organization_id, 'organization is also selected')
    end
  end

  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 is_last_role?
    role_object.roles.count == 0
  end
end

#typeString

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

Returns:

  • (String)

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



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
107
108
109
110
111
# File 'app/models/role.rb', line 38

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

  include Shared::PolymorphicAnnotator
  polymorphic_annotates(:role_object)

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

  belongs_to :organization, inverse_of: :roles, validate: true
  belongs_to :person, inverse_of: :roles, validate: true
  belongs_to :role_object, polymorphic: :true #, validate: true

  after_save :update_cached

  validates_presence_of :type
  validate :agent_present,
    :only_one_agent,
    :agent_is_legal

  # validates_uniqueness_of :person_id, scope: [:project_id, :role_object_type, :role_object_id]

  # role_object presence is a database constraint level
  # validates :role_object, presence: true

  # Must come after belongs_to associations
  include Roles::Person

  # Overrode in Roles::Organization
  def organization_allowed?
    false
  end

  def agent_type
    if person
      :person
    elsif organization
      :organization
    else
      nil
    end
  end

  protected

  def agent_present
    if !person.present? && !organization.present?
      errors.add(:base, 'missing an agent (person or organization)')
    end
  end

  def agent_is_legal
    if organization.present?
      errors.add(:organization_id, 'is not permitted for this role type') unless organization_allowed?
    end
  end

  def only_one_agent
    if person && organization
      errors.add(:person_id, 'organization is also selected')
      errors.add(:organization_id, 'organization is also selected')
    end
  end

  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 is_last_role?
    role_object.roles.count == 0
  end
end

Instance Method Details



90
91
92
93
94
# File 'app/models/role.rb', line 90

def agent_is_legal
  if organization.present?
    errors.add(:organization_id, 'is not permitted for this role type') unless organization_allowed?
  end
end

#agent_presentObject (protected)



84
85
86
87
88
# File 'app/models/role.rb', line 84

def agent_present
  if !person.present? && !organization.present?
    errors.add(:base, 'missing an agent (person or organization)')
  end
end

#agent_typeObject



72
73
74
75
76
77
78
79
80
# File 'app/models/role.rb', line 72

def agent_type
  if person
    :person
  elsif organization
    :organization
  else
    nil
  end
end

#is_last_role?Boolean (protected)

Returns:

  • (Boolean)


108
109
110
# File 'app/models/role.rb', line 108

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

#only_one_agentObject (protected)



96
97
98
99
100
101
# File 'app/models/role.rb', line 96

def only_one_agent
  if person && organization
    errors.add(:person_id, 'organization is also selected')
    errors.add(:organization_id, 'organization is also selected')
  end
end

#organization_allowed?Boolean

Overrode in Roles::Organization

Returns:

  • (Boolean)


68
69
70
# File 'app/models/role.rb', line 68

def organization_allowed?
  false
end

#update_cachedObject (protected)



103
104
105
106
# File 'app/models/role.rb', line 103

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