Sha256: 4827ffab5d48592299c6d808e3eaa1119670b467814f03f125881499a9e82054

Contents?: true

Size: 1.43 KB

Versions: 13

Compression:

Stored size: 1.43 KB

Contents

module AbAdmin
  module Config
    # Shareable module to give a #display_on?(action) method
    # which returns true or false depending on an options hash.
    #
    # The options hash accepts:
    #
    # only: :index
    # only: [:index, :show]
    # except: :index
    # except: [:index, :show]
    #
    # call #normalize_display_options! after @options has been set
    # to ensure that the display options are setup correctly

    module OptionalDisplay
      def for_context?(render_context = nil)
        if @options[:if]
          symbol_or_proc = @options[:if]
          return case symbol_or_proc
                   when Symbol, String
                     render_context ? render_context.send(symbol_or_proc) : self.send(symbol_or_proc)
                   when Proc
                     render_context ? render_context.instance_exec(&symbol_or_proc) : instance_exec(&symbol_or_proc)
                   else
                     symbol_or_proc
                 end
        end
        true
      end

      def for_action?(action)
        return false if @options[:only] && !@options[:only].include?(action.to_sym)
        return false if @options[:except] && @options[:except].include?(action.to_sym)
        true
      end

      private

      def normalize_display_options!
        @options[:only] = Array(@options[:only]) if @options[:only]
        @options[:except] = Array(@options[:except]) if @options[:except]
      end
    end

  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
ab_admin-0.8.0 lib/ab_admin/config/optional_display.rb
ab_admin-0.7.0 lib/ab_admin/config/optional_display.rb
ab_admin-0.6.1 lib/ab_admin/config/optional_display.rb
ab_admin-0.6.0 lib/ab_admin/config/optional_display.rb
ab_admin-0.5.0 lib/ab_admin/config/optional_display.rb
ab_admin-0.4.0 lib/ab_admin/config/optional_display.rb
ab_admin-0.3.6 lib/ab_admin/config/optional_display.rb
ab_admin-0.3.5 lib/ab_admin/config/optional_display.rb
ab_admin-0.3.4 lib/ab_admin/config/optional_display.rb
ab_admin-0.3.3 lib/ab_admin/config/optional_display.rb
ab_admin-0.3.2 lib/ab_admin/config/optional_display.rb
ab_admin-0.3.1 lib/ab_admin/config/optional_display.rb
ab_admin-0.3.0 lib/ab_admin/config/optional_display.rb