Skip to content

Instantly share code, notes, and snippets.

@ildarsafin
Last active August 29, 2015 14:02
Show Gist options
  • Save ildarsafin/8621874a4509b6a5db56 to your computer and use it in GitHub Desktop.
Save ildarsafin/8621874a4509b6a5db56 to your computer and use it in GitHub Desktop.
All scopes in task model
scope :complete, ->(value) { where(summary_status: 'complete', state: 'active').limit(value) }
scope :accessible_by_user, ->(user) do
includes(:assignee_tasks)
.where('tasks.user_id = :user_id or assignee_tasks.user_id = :user_id', user_id: user.id)
end
scope :accessible_by_user_with_state, ->(user, state) do
includes(:assignee_tasks)
.where('tasks.user_id = :user_id AND tasks.state = :state OR assignee_tasks.user_id = :user_id AND assignee_tasks.state = :state',
user_id: user.id, state: state)
end
scope :accessible_to_update, ->(user) do
includes(:assignee_tasks)
.where('tasks.user_id = :user_id OR assignee_tasks.user_id = :user_id AND assignee_tasks.status != :status',
user_id: user.id, status: 'visible')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment