Module: Housekeeping::Projects::ClassMethods

Defined in:
lib/housekeeping/projects.rb

Instance Method Summary collapse

Instance Method Details

#find_or_create_by(attributes, &block) ⇒ Object

These overrides scope bare class-level finder calls (e.g. Otu.find_or_create_by!(...)) to Current.project_id when neither project nor project_id is provided.

This primarily matters for the find step: without project scoping, a finder could match a record from another project. If the call ends up creating instead, project_id is also passed explicitly, though set_project_id would normally handle that as well.



39
40
41
# File 'lib/housekeeping/projects.rb', line 39

def find_or_create_by(attributes, &block)
  super(with_current_project(attributes), &block)
end

#find_or_create_by!(attributes, &block) ⇒ Object



43
44
45
# File 'lib/housekeeping/projects.rb', line 43

def find_or_create_by!(attributes, &block)
  super(with_current_project(attributes), &block)
end

#find_or_initialize_by(attributes, &block) ⇒ Object



47
48
49
# File 'lib/housekeeping/projects.rb', line 47

def find_or_initialize_by(attributes, &block)
  super(with_current_project(attributes), &block)
end

#in_project(project) ⇒ Scope

Scopes

Parameters:

Returns:

  • (Scope)


54
55
56
# File 'lib/housekeeping/projects.rb', line 54

def in_project(project)
  where(project:)
end

#with_current_project(attributes) ⇒ Object (private)



66
67
68
69
70
# File 'lib/housekeeping/projects.rb', line 66

def with_current_project(attributes)
  h = (attributes || {}).to_h.with_indifferent_access
  return h if h[:project_id].present? || h[:project].present?
  Current.project_id ? h.merge(project_id: Current.project_id) : h
end

#with_project_id(project_id) ⇒ Scope

Parameters:

  • project_id (Integer)

Returns:

  • (Scope)


60
61
62
# File 'lib/housekeeping/projects.rb', line 60

def with_project_id(project_id)
  where(project_id:)
end