<commit_before>class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= User.new

    # Circles
    can :read, Circle
    can :create, Circle unless user.circle.present?
    if user.circle
      can [:update, :destroy], Circle, admin: user

      # Work Groups
      can :read, WorkingGroup, circle_id: user.circle_id
      can [:create, :update, :destroy], WorkingGroup, circle: { admin: user }

      # Tasks
      can :read, Task, working_group: { circle_id: user.circle_id }
      can [:create, :update, :destroy], Task, working_group: { circle: { admin: user } }
      can :volunteer, Task, working_group: { circle: user.circle }
      cannot :volunteer, Task, volunteer_assignments: { user: user }
      can :complete, Task, volunteer_assignments: { user: user }, completed_at: nil
    end
  end
end<commit_msg>overhauling abilities to be aligned with circle/working group/task roles<commit_after>class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= User.new

    # Circles
    can :create, Circle
    can :read,   Circle do |circle|
      circle.users.include? user
    end

    can :manage, Circle do |circle|
      circle.admins.include? user
    end

    can :create_task, Circle do |circle|
      can?(:manage, circle) or
      user.working_group_roles.admin.for_circle(circle).exists?
    end

    can :create_group, Circle do |circle|
      can?(:manage, circle)
    end

    can :create_item, Circle do |circle|
      can?(:create_task, circle) or
      can?(:create_group, circle)
    end


    # Work Groups
    can :read, WorkingGroup do |wg|
      can? :read, wg.circle
    end

    can :manage, WorkingGroup do |wg|
      can?(:manage, wg.circle) ||
      wg.admins.include?(user)
    end

    can :create_task, WorkingGroup do |wg|
      can?(:manage, wg)
    end


    # Tasks
    can :read, Task do |task|
      can? :read, task.circle
    end

    can :manage, Task do |task|
      task.organizers.include?(user) or
      can?(:manage, task.working_group) or
      can?(:manage, task.circle)
    end

    can :volunteer, Task do |task|
      can?(:read, task)
    end
    cannot :volunteer, Task do |task|
      task.complete? or task.volunteers.include?(user)
    end


    can :complete, Task do |task|
      task.due_date < Time.now and (
        task.volunteers.include?(user) or
        task.organizers.include?(user)
      ) or
      can?(:manage, task.working_group) or
      can?(:manage, task.circle)
    end
    cannot :complete, Task do |task|
      task.complete?
    end

  end
end
{'fraction_non_alphanumeric': 0.11206559937524405, 'fraction_numerical': 0.0, 'mean_word_length': 2.6495726495726495, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '75308', 'n_tokens_mistral': 856, 'n_tokens_neox': 820, 'n_words': 241}