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

#annotated_object_is_persisted?

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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'app/models/role.rb', line 38

class Role < ApplicationRecord
  include Housekeeping::Users
  include Housekeeping::Timestamps
  include Shared::PolymorphicAnnotator # must be before Shared::IsData (for now)
  include Shared::IsData

  polymorphic_annotates(:role_object, presence_validate: false)
  acts_as_list scope: [:type, :role_object_type, :role_object_id]

  # !! Has to be after after_save to not interfer with initial calls
  # !! TODO: revist
  after_commit :set_cached

  belongs_to :organization, inverse_of: :roles
  belongs_to :person, inverse_of: :roles

  # Must come after belongs_to associations
  # !! This is only code isolation, not a shared library, probably should be removed
  include Roles::Person

  validates :person, presence: true, unless: Proc.new { organization.present? }
  validates :organization, presence: true, unless: Proc.new { person.present? }
  validates_presence_of :type
  validate :only_one_agent, :agent_is_legal #, :agent_present

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

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

  def agent
    return person if person_id
    organization
  end

  protected

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

  # TODO: redundant?
  def only_one_agent
    if person && organization
      errors.add(:person_id, 'organization is also selected')
      errors.add(:organization_id, 'person is also selected')
    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 is_last_role?
    role_object.roles.count == 0
  end

  #
  # Cache related methods
  #

  # Optionally defined in subclasses to limit
  # which cached (sub) methods should be called
  #   base_class_name: [:method, :method, :method]
  def cached_triggers
    {}
  end

  # @return boolean
  #   true in roles that should have no impact
  #   in any cached value setting.  If false then `set_cached`
  #   will be called unless cached_triggers.presence
  def do_not_set_cached
    false
  end

  def set_cached
    set_role_object_cached
  end

  def set_role_object_cached
    a = self.dup
    a.becomes(type.constantize).cached_trigger_methods(role_object).each do |m|
      role_object.send(m) unless role_object.destroyed?
    end
  end

  def cached_trigger_methods(object)
    k = object.class.base_class.name.to_sym
    if cached_triggers[k]
      cached_triggers[k]
    elsif
      object.respond_to?(:set_cached)
      if do_not_set_cached
        return []
      elsif role_object.respond_to?(:no_cached) && role_object.no_cached
        return []
      else
        return [:set_cached]
      end
    else
      []
    end
  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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'app/models/role.rb', line 38

class Role < ApplicationRecord
  include Housekeeping::Users
  include Housekeeping::Timestamps
  include Shared::PolymorphicAnnotator # must be before Shared::IsData (for now)
  include Shared::IsData

  polymorphic_annotates(:role_object, presence_validate: false)
  acts_as_list scope: [:type, :role_object_type, :role_object_id]

  # !! Has to be after after_save to not interfer with initial calls
  # !! TODO: revist
  after_commit :set_cached

  belongs_to :organization, inverse_of: :roles
  belongs_to :person, inverse_of: :roles

  # Must come after belongs_to associations
  # !! This is only code isolation, not a shared library, probably should be removed
  include Roles::Person

  validates :person, presence: true, unless: Proc.new { organization.present? }
  validates :organization, presence: true, unless: Proc.new { person.present? }
  validates_presence_of :type
  validate :only_one_agent, :agent_is_legal #, :agent_present

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

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

  def agent
    return person if person_id
    organization
  end

  protected

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

  # TODO: redundant?
  def only_one_agent
    if person && organization
      errors.add(:person_id, 'organization is also selected')
      errors.add(:organization_id, 'person is also selected')
    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 is_last_role?
    role_object.roles.count == 0
  end

  #
  # Cache related methods
  #

  # Optionally defined in subclasses to limit
  # which cached (sub) methods should be called
  #   base_class_name: [:method, :method, :method]
  def cached_triggers
    {}
  end

  # @return boolean
  #   true in roles that should have no impact
  #   in any cached value setting.  If false then `set_cached`
  #   will be called unless cached_triggers.presence
  def do_not_set_cached
    false
  end

  def set_cached
    set_role_object_cached
  end

  def set_role_object_cached
    a = self.dup
    a.becomes(type.constantize).cached_trigger_methods(role_object).each do |m|
      role_object.send(m) unless role_object.destroyed?
    end
  end

  def cached_trigger_methods(object)
    k = object.class.base_class.name.to_sym
    if cached_triggers[k]
      cached_triggers[k]
    elsif
      object.respond_to?(:set_cached)
      if do_not_set_cached
        return []
      elsif role_object.respond_to?(:no_cached) && role_object.no_cached
        return []
      else
        return [:set_cached]
      end
    else
      []
    end
  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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'app/models/role.rb', line 38

class Role < ApplicationRecord
  include Housekeeping::Users
  include Housekeeping::Timestamps
  include Shared::PolymorphicAnnotator # must be before Shared::IsData (for now)
  include Shared::IsData

  polymorphic_annotates(:role_object, presence_validate: false)
  acts_as_list scope: [:type, :role_object_type, :role_object_id]

  # !! Has to be after after_save to not interfer with initial calls
  # !! TODO: revist
  after_commit :set_cached

  belongs_to :organization, inverse_of: :roles
  belongs_to :person, inverse_of: :roles

  # Must come after belongs_to associations
  # !! This is only code isolation, not a shared library, probably should be removed
  include Roles::Person

  validates :person, presence: true, unless: Proc.new { organization.present? }
  validates :organization, presence: true, unless: Proc.new { person.present? }
  validates_presence_of :type
  validate :only_one_agent, :agent_is_legal #, :agent_present

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

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

  def agent
    return person if person_id
    organization
  end

  protected

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

  # TODO: redundant?
  def only_one_agent
    if person && organization
      errors.add(:person_id, 'organization is also selected')
      errors.add(:organization_id, 'person is also selected')
    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 is_last_role?
    role_object.roles.count == 0
  end

  #
  # Cache related methods
  #

  # Optionally defined in subclasses to limit
  # which cached (sub) methods should be called
  #   base_class_name: [:method, :method, :method]
  def cached_triggers
    {}
  end

  # @return boolean
  #   true in roles that should have no impact
  #   in any cached value setting.  If false then `set_cached`
  #   will be called unless cached_triggers.presence
  def do_not_set_cached
    false
  end

  def set_cached
    set_role_object_cached
  end

  def set_role_object_cached
    a = self.dup
    a.becomes(type.constantize).cached_trigger_methods(role_object).each do |m|
      role_object.send(m) unless role_object.destroyed?
    end
  end

  def cached_trigger_methods(object)
    k = object.class.base_class.name.to_sym
    if cached_triggers[k]
      cached_triggers[k]
    elsif
      object.respond_to?(:set_cached)
      if do_not_set_cached
        return []
      elsif role_object.respond_to?(:no_cached) && role_object.no_cached
        return []
      else
        return [:set_cached]
      end
    else
      []
    end
  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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'app/models/role.rb', line 38

class Role < ApplicationRecord
  include Housekeeping::Users
  include Housekeeping::Timestamps
  include Shared::PolymorphicAnnotator # must be before Shared::IsData (for now)
  include Shared::IsData

  polymorphic_annotates(:role_object, presence_validate: false)
  acts_as_list scope: [:type, :role_object_type, :role_object_id]

  # !! Has to be after after_save to not interfer with initial calls
  # !! TODO: revist
  after_commit :set_cached

  belongs_to :organization, inverse_of: :roles
  belongs_to :person, inverse_of: :roles

  # Must come after belongs_to associations
  # !! This is only code isolation, not a shared library, probably should be removed
  include Roles::Person

  validates :person, presence: true, unless: Proc.new { organization.present? }
  validates :organization, presence: true, unless: Proc.new { person.present? }
  validates_presence_of :type
  validate :only_one_agent, :agent_is_legal #, :agent_present

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

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

  def agent
    return person if person_id
    organization
  end

  protected

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

  # TODO: redundant?
  def only_one_agent
    if person && organization
      errors.add(:person_id, 'organization is also selected')
      errors.add(:organization_id, 'person is also selected')
    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 is_last_role?
    role_object.roles.count == 0
  end

  #
  # Cache related methods
  #

  # Optionally defined in subclasses to limit
  # which cached (sub) methods should be called
  #   base_class_name: [:method, :method, :method]
  def cached_triggers
    {}
  end

  # @return boolean
  #   true in roles that should have no impact
  #   in any cached value setting.  If false then `set_cached`
  #   will be called unless cached_triggers.presence
  def do_not_set_cached
    false
  end

  def set_cached
    set_role_object_cached
  end

  def set_role_object_cached
    a = self.dup
    a.becomes(type.constantize).cached_trigger_methods(role_object).each do |m|
      role_object.send(m) unless role_object.destroyed?
    end
  end

  def cached_trigger_methods(object)
    k = object.class.base_class.name.to_sym
    if cached_triggers[k]
      cached_triggers[k]
    elsif
      object.respond_to?(:set_cached)
      if do_not_set_cached
        return []
      elsif role_object.respond_to?(:no_cached) && role_object.no_cached
        return []
      else
        return [:set_cached]
      end
    else
      []
    end
  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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'app/models/role.rb', line 38

class Role < ApplicationRecord
  include Housekeeping::Users
  include Housekeeping::Timestamps
  include Shared::PolymorphicAnnotator # must be before Shared::IsData (for now)
  include Shared::IsData

  polymorphic_annotates(:role_object, presence_validate: false)
  acts_as_list scope: [:type, :role_object_type, :role_object_id]

  # !! Has to be after after_save to not interfer with initial calls
  # !! TODO: revist
  after_commit :set_cached

  belongs_to :organization, inverse_of: :roles
  belongs_to :person, inverse_of: :roles

  # Must come after belongs_to associations
  # !! This is only code isolation, not a shared library, probably should be removed
  include Roles::Person

  validates :person, presence: true, unless: Proc.new { organization.present? }
  validates :organization, presence: true, unless: Proc.new { person.present? }
  validates_presence_of :type
  validate :only_one_agent, :agent_is_legal #, :agent_present

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

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

  def agent
    return person if person_id
    organization
  end

  protected

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

  # TODO: redundant?
  def only_one_agent
    if person && organization
      errors.add(:person_id, 'organization is also selected')
      errors.add(:organization_id, 'person is also selected')
    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 is_last_role?
    role_object.roles.count == 0
  end

  #
  # Cache related methods
  #

  # Optionally defined in subclasses to limit
  # which cached (sub) methods should be called
  #   base_class_name: [:method, :method, :method]
  def cached_triggers
    {}
  end

  # @return boolean
  #   true in roles that should have no impact
  #   in any cached value setting.  If false then `set_cached`
  #   will be called unless cached_triggers.presence
  def do_not_set_cached
    false
  end

  def set_cached
    set_role_object_cached
  end

  def set_role_object_cached
    a = self.dup
    a.becomes(type.constantize).cached_trigger_methods(role_object).each do |m|
      role_object.send(m) unless role_object.destroyed?
    end
  end

  def cached_trigger_methods(object)
    k = object.class.base_class.name.to_sym
    if cached_triggers[k]
      cached_triggers[k]
    elsif
      object.respond_to?(:set_cached)
      if do_not_set_cached
        return []
      elsif role_object.respond_to?(:no_cached) && role_object.no_cached
        return []
      else
        return [:set_cached]
      end
    else
      []
    end
  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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'app/models/role.rb', line 38

class Role < ApplicationRecord
  include Housekeeping::Users
  include Housekeeping::Timestamps
  include Shared::PolymorphicAnnotator # must be before Shared::IsData (for now)
  include Shared::IsData

  polymorphic_annotates(:role_object, presence_validate: false)
  acts_as_list scope: [:type, :role_object_type, :role_object_id]

  # !! Has to be after after_save to not interfer with initial calls
  # !! TODO: revist
  after_commit :set_cached

  belongs_to :organization, inverse_of: :roles
  belongs_to :person, inverse_of: :roles

  # Must come after belongs_to associations
  # !! This is only code isolation, not a shared library, probably should be removed
  include Roles::Person

  validates :person, presence: true, unless: Proc.new { organization.present? }
  validates :organization, presence: true, unless: Proc.new { person.present? }
  validates_presence_of :type
  validate :only_one_agent, :agent_is_legal #, :agent_present

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

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

  def agent
    return person if person_id
    organization
  end

  protected

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

  # TODO: redundant?
  def only_one_agent
    if person && organization
      errors.add(:person_id, 'organization is also selected')
      errors.add(:organization_id, 'person is also selected')
    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 is_last_role?
    role_object.roles.count == 0
  end

  #
  # Cache related methods
  #

  # Optionally defined in subclasses to limit
  # which cached (sub) methods should be called
  #   base_class_name: [:method, :method, :method]
  def cached_triggers
    {}
  end

  # @return boolean
  #   true in roles that should have no impact
  #   in any cached value setting.  If false then `set_cached`
  #   will be called unless cached_triggers.presence
  def do_not_set_cached
    false
  end

  def set_cached
    set_role_object_cached
  end

  def set_role_object_cached
    a = self.dup
    a.becomes(type.constantize).cached_trigger_methods(role_object).each do |m|
      role_object.send(m) unless role_object.destroyed?
    end
  end

  def cached_trigger_methods(object)
    k = object.class.base_class.name.to_sym
    if cached_triggers[k]
      cached_triggers[k]
    elsif
      object.respond_to?(:set_cached)
      if do_not_set_cached
        return []
      elsif role_object.respond_to?(:no_cached) && role_object.no_cached
        return []
      else
        return [:set_cached]
      end
    else
      []
    end
  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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'app/models/role.rb', line 38

class Role < ApplicationRecord
  include Housekeeping::Users
  include Housekeeping::Timestamps
  include Shared::PolymorphicAnnotator # must be before Shared::IsData (for now)
  include Shared::IsData

  polymorphic_annotates(:role_object, presence_validate: false)
  acts_as_list scope: [:type, :role_object_type, :role_object_id]

  # !! Has to be after after_save to not interfer with initial calls
  # !! TODO: revist
  after_commit :set_cached

  belongs_to :organization, inverse_of: :roles
  belongs_to :person, inverse_of: :roles

  # Must come after belongs_to associations
  # !! This is only code isolation, not a shared library, probably should be removed
  include Roles::Person

  validates :person, presence: true, unless: Proc.new { organization.present? }
  validates :organization, presence: true, unless: Proc.new { person.present? }
  validates_presence_of :type
  validate :only_one_agent, :agent_is_legal #, :agent_present

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

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

  def agent
    return person if person_id
    organization
  end

  protected

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

  # TODO: redundant?
  def only_one_agent
    if person && organization
      errors.add(:person_id, 'organization is also selected')
      errors.add(:organization_id, 'person is also selected')
    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 is_last_role?
    role_object.roles.count == 0
  end

  #
  # Cache related methods
  #

  # Optionally defined in subclasses to limit
  # which cached (sub) methods should be called
  #   base_class_name: [:method, :method, :method]
  def cached_triggers
    {}
  end

  # @return boolean
  #   true in roles that should have no impact
  #   in any cached value setting.  If false then `set_cached`
  #   will be called unless cached_triggers.presence
  def do_not_set_cached
    false
  end

  def set_cached
    set_role_object_cached
  end

  def set_role_object_cached
    a = self.dup
    a.becomes(type.constantize).cached_trigger_methods(role_object).each do |m|
      role_object.send(m) unless role_object.destroyed?
    end
  end

  def cached_trigger_methods(object)
    k = object.class.base_class.name.to_sym
    if cached_triggers[k]
      cached_triggers[k]
    elsif
      object.respond_to?(:set_cached)
      if do_not_set_cached
        return []
      elsif role_object.respond_to?(:no_cached) && role_object.no_cached
        return []
      else
        return [:set_cached]
      end
    else
      []
    end
  end

end

Instance Method Details

#agentObject



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

def agent
  return person if person_id
  organization
end


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

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)



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

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

#agent_typeObject



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

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

#cached_trigger_methods(object) ⇒ Object (protected)



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'app/models/role.rb', line 139

def cached_trigger_methods(object)
  k = object.class.base_class.name.to_sym
  if cached_triggers[k]
    cached_triggers[k]
  elsif
    object.respond_to?(:set_cached)
    if do_not_set_cached
      return []
    elsif role_object.respond_to?(:no_cached) && role_object.no_cached
      return []
    else
      return [:set_cached]
    end
  else
    []
  end
end

#cached_triggersObject (protected)

Optionally defined in subclasses to limit which cached (sub) methods should be called

base_class_name: [:method, :method, :method]


116
117
118
# File 'app/models/role.rb', line 116

def cached_triggers
  {}
end

#do_not_set_cachedObject (protected)

Returns boolean true in roles that should have no impact in any cached value setting. If false then ‘set_cached` will be called unless cached_triggers.presence.

Returns:

  • boolean true in roles that should have no impact in any cached value setting. If false then ‘set_cached` will be called unless cached_triggers.presence



124
125
126
# File 'app/models/role.rb', line 124

def do_not_set_cached
  false
end

#is_last_role?Boolean (protected)

Returns:

  • (Boolean)


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

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

#only_one_agentObject (protected)

TODO: redundant?



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

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

#organization_allowed?Boolean

Overrode in Roles::Organization

Returns:

  • (Boolean)


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

def organization_allowed?
  false
end

#set_cachedObject (protected)



128
129
130
# File 'app/models/role.rb', line 128

def set_cached
  set_role_object_cached
end

#set_role_object_cachedObject (protected)



132
133
134
135
136
137
# File 'app/models/role.rb', line 132

def set_role_object_cached
  a = self.dup
  a.becomes(type.constantize).cached_trigger_methods(role_object).each do |m|
    role_object.send(m) unless role_object.destroyed?
  end
end