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.



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

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

#find_or_create_by!(attributes, &block) ⇒ Object



50
51
52
# File 'lib/housekeeping/projects.rb', line 50

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

#find_or_initialize_by(attributes, &block) ⇒ Object



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

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

#in_project(project) ⇒ Scope

Scopes

Parameters:

Returns:

  • (Scope)


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

def in_project(project)
  where(project:)
end

#with_current_project(attributes) ⇒ Object (private)



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

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)


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

def with_project_id(project_id)
  where(project_id:)
end