Class: Role

Inherits:
ApplicationRecord show all
Includes:
Housekeeping::Timestamps, Housekeeping::Users, Roles::Person, Shared::IsData
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 Housekeeping::Timestamps

#data_breakdown_for_chartkick_recent

Methods included from Housekeeping::Users

#set_created_by_id, #set_updated_by_id

Instance Attribute Details

- (Integer) organization_id

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

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

  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

  # role_object presences 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

- (Integer) person_id

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

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

  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

  # role_object presences 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

- (Integer) position

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

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

  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

  # role_object presences 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

- (Integer) project_id

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

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

  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

  # role_object presences 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

- (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.



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 38

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

  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

  # role_object presences 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

- (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.



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 38

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

  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

  # role_object presences 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

- (String) type

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

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

  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

  # role_object presences 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



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

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

- (Object) agent_present (protected)



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

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

- (Object) agent_type



67
68
69
70
71
72
73
74
75
# File 'app/models/role.rb', line 67

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

- (Boolean) is_last_role? (protected)

Returns:

  • (Boolean)


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

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

- (Object) only_one_agent (protected)



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

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

- (Boolean) organization_allowed?

Overrode in Roles::Organization

Returns:

  • (Boolean)


63
64
65
# File 'app/models/role.rb', line 63

def organization_allowed?
  false
end

- (Object) update_cached (protected)



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

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