Class: Role
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Role
- 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
Defined Under Namespace
Classes: AttributionRole, ProjectRole, SourceRole
Instance Attribute Summary collapse
-
#organization_id ⇒ Integer
The ID of the Organization in the role.
-
#person_id ⇒ Integer
The ID of the Person in the role.
-
#position ⇒ Integer
Sort order.
-
#project_id ⇒ Integer
the project ID.
-
#role_object_id ⇒ Integer
The id of the object the role is bound to.
-
#role_object_type ⇒ String
THe class of the object the role is bound to.
-
#type ⇒ String
The type (subclass) of the role, e.g.
Attributes included from Housekeeping::Users
Instance Method Summary collapse
- #agent_is_legal ⇒ Object protected
- #agent_present ⇒ Object protected
- #agent_type ⇒ Object
- #is_last_role? ⇒ Boolean protected
- #only_one_agent ⇒ Object protected
-
#organization_allowed? ⇒ Boolean
Overrode in Roles::Organization.
- #update_cached ⇒ Object protected
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
Instance Attribute Details
#organization_id ⇒ Integer
Returns 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_id ⇒ Integer
Returns 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 |
#position ⇒ Integer
Returns 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_id ⇒ Integer
the project ID
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_id ⇒ Integer
Returns 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_type ⇒ String
Returns 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 |
#type ⇒ String
Returns 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
#agent_is_legal ⇒ Object (protected)
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_present ⇒ Object (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_type ⇒ Object
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)
108 109 110 |
# File 'app/models/role.rb', line 108 def is_last_role? role_object.roles.count == 0 end |
#only_one_agent ⇒ Object (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
68 69 70 |
# File 'app/models/role.rb', line 68 def organization_allowed? false end |
#update_cached ⇒ Object (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 |