Sha256: 55329c80788444e01c31cd4ec8477cffb0823fa56f75f70be6019842aadd2640

Contents?: true

Size: 1006 Bytes

Versions: 7

Compression:

Stored size: 1006 Bytes

Contents

#TODO may use mumukit slug
module WithSlug
  extend ActiveSupport::Concern

  included do
    validates_presence_of :slug
    validates_uniqueness_of :slug
  end

  def slug_parts
    org, repo = slug.split('/')
    {organization: org, repository: repo}
  end

  ## Copy and Rebase

  def rebase!(organization)
    self.slug = self.slug.to_mumukit_slug.rebase(organization).to_s
  end

  def rebased_dup(organization)
    dup.tap { |it| it.rebase! organization }
  end

  ## Filtering

  class_methods do

    def allowed(permissions)
      all.select { |it| permissions&.writer?(it.slug) }
    end

    def visible(permissions)
      # FIXME no truly generic
      all.select { |it| !it.private? || permissions&.writer?(it.slug) }
    end

    def by_slug_parts!(args)
      find_by!(slug: "#{args[:organization]}/#{args[:repository]}")
    end

    ## Resource Protocol

    def sync_key_id_field
      :slug
    end

    def locate_resource(key)
      find_or_initialize_by(slug: key)
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
mumuki-domain-6.4.2 app/models/concerns/with_slug.rb
mumuki-domain-6.4.1 app/models/concerns/with_slug.rb
mumuki-domain-6.4.0 app/models/concerns/with_slug.rb
mumuki-domain-6.2.2 app/models/concerns/with_slug.rb
mumuki-domain-6.2.1 app/models/concerns/with_slug.rb
mumuki-domain-6.3.0 app/models/concerns/with_slug.rb
mumuki-domain-6.2.0 app/models/concerns/with_slug.rb