Module: Housekeeping::Projects

Extended by:
ActiveSupport::Concern
Included in:
CachedMap, CachedMapItem, CachedMapRegister, News::Project
Defined in:
lib/housekeeping/projects.rb

Overview

Concern the provides housekeeping and related methods for models that belong_to a Project

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#annotates_community_object?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/housekeeping/projects.rb', line 88

def annotates_community_object?
  self.respond_to?(:is_community_annotation?) && self.is_community_annotation?
end

#associated_project_id_for_validation(reflection, foreign_key) ⇒ Object (private)



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/housekeeping/projects.rb', line 119

def associated_project_id_for_validation(reflection, foreign_key)
  if reflection.polymorphic?
    associated_class_name = public_send(reflection.foreign_type)
    return nil if associated_class_name.blank?

    klass = associated_class_name.safe_constantize
  else
    klass = reflection.klass
  end

  return nil unless klass&.column_names&.include?('project_id')

  klass.where(id: public_send(foreign_key)).pick(:project_id)
end

#is_community?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/housekeeping/projects.rb', line 93

def is_community?
  (self.class <= Shared::SharedAcrossProjects) ? true : false
end

#project_foreign_keys_are_in_same_projectObject (private)



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/housekeeping/projects.rb', line 99

def project_foreign_keys_are_in_same_project
  return if Utilities::ThreadStore[:tw_project_unification]
  return unless project_id.present?

  self.class.reflect_on_all_associations(:belongs_to).each do |reflection|
    next if reflection.name == :project

    foreign_key = reflection.foreign_key.to_sym
    next if public_send(foreign_key).blank?

    # Note this is currently n+1 (though very fast fk lookups). We may
    # eventually want to combine these into a single query.
    associated_project_id = associated_project_id_for_validation(reflection, foreign_key)
    next unless associated_project_id.present?
    next if associated_project_id == project_id

    errors.add(foreign_key, "must belong to the same project (record id: #{id} is in project_id: #{project_id}, #{foreign_key}: #{public_send(foreign_key)} is in project_id: #{associated_project_id})")
  end
end

#set_project_idObject



73
74
75
76
77
# File 'lib/housekeeping/projects.rb', line 73

def set_project_id
  if self.new_record?
    self.project_id ||= Current.project_id
  end
end