Sha256: 03a2f94bb836258aa8f0128d5555cd67f42b5d19c1e9b070b62345e0bad96826

Contents?: true

Size: 962 Bytes

Versions: 10

Compression:

Stored size: 962 Bytes

Contents

module Godmin
  module Resource
    module Scopes
      extend ActiveSupport::Concern

      included do
        helper_method :scope_map
      end

      def scope_map
        self.class.scope_map
      end

      def apply_scope(resources)
        if params[:scope].blank?
          params[:scope] = default_scope
        end

        if params[:scope] && scope_map.key?(params[:scope].to_sym)
          send("scope_#{params[:scope]}", resources)
        else
          resources
        end
      end

      protected

      def default_scope
        scope = scope_map.find -> { scope_map.first } do |_key, value|
          value[:default] == true
        end

        scope ? scope[0].to_s : nil
      end

      module ClassMethods
        def scope_map
          @scope_map ||= {}
        end

        def scope(attr, options = {})
          scope_map[attr] = {
            default: false
          }.merge(options)
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
godmin-0.9.9 lib/godmin/resource/scopes.rb
godmin-0.9.8 lib/godmin/resource/scopes.rb
godmin-0.9.7 lib/godmin/resource/scopes.rb
godmin-0.9.6 lib/godmin/resource/scopes.rb
godmin-0.9.5 lib/godmin/resource/scopes.rb
godmin-0.9.4 lib/godmin/resource/scopes.rb
godmin-0.9.3 lib/godmin/resource/scopes.rb
godmin-0.9.2 lib/godmin/resource/scopes.rb
godmin-0.9.1 lib/godmin/resource/scopes.rb
godmin-0.9.0 lib/godmin/resource/scopes.rb