Sha256: d7d53598c65b67ee98943d2b23419f74630ce8ec5d3fe0b6f7568e0bc72f740e

Contents?: true

Size: 1.78 KB

Versions: 3

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

module PaidUp
  # PaidUp Ability model
  module Ability
    include CanCan::Ability

    def initialize_paid_up(user)
      PaidUp::Feature.all.each do |feature|
        method = "enable_#{feature.setting_type}".to_sym
        send(method, user, feature)
      end
    end

    private

    def enable_read(model)
      can :index, model
      can :show, model, &:enabled?
    end

    def enable_rows(model, allowed, remaining)
      if allowed.positive?
        can :own, model
        cannot :create, model
        can(%i(create new), model) if remaining.positive?
      else
        cannot %i(delete update own create), model
      end
    end

    def enable_table_rows(user, feature)
      model = feature.feature_model
      enable_read(model)
      return if user.new_record?
      slug = feature.slug
      table_setting = user.table_setting(slug)
      allowed = table_setting.rows_allowed
      remaining = table_setting.rows_remaining
      can :manage, model, user: user if allowed.positive?
      enable_rows(model, allowed, remaining)
    end

    def enable_rolify_rows(user, feature)
      model = feature.feature_model
      enable_read(model)
      return if user.new_record?
      slug = feature.slug
      rolify_setting = user.rolify_setting(slug)
      allowed = rolify_setting.rows_allowed
      remaining = rolify_setting.rows_remaining
      if allowed.positive?
        # This was a nice idea, but broke in practice
        # can :manage, model, roles: { name: 'owner', users: { id: user.id } }
        can :manage, model, id: model.with_role(:owner, user).ids
      end
      enable_rows(model, allowed, remaining)
    end

    def enable_boolean(user, feature)
      slug = feature.slug
      can :use, slug.to_sym if user.plan.feature_setting slug
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
paid_up-0.13.5 app/models/paid_up/ability.rb
paid_up-0.13.4 app/models/paid_up/ability.rb
paid_up-0.13.3 app/models/paid_up/ability.rb