Sha256: 51877241a48fdb4509d24c5c604e4e587045a431e1a04da0a99ceef5ebdf4079

Contents?: true

Size: 1.37 KB

Versions: 4

Compression:

Stored size: 1.37 KB

Contents

module ActiveAdmin
  
  # 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 display_on?(action, render_context = nil)
      return false if @options[:only] && !@options[:only].include?(action.to_sym)
      return false if @options[:except] && @options[:except].include?(action.to_sym)
      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

    private

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
activeadmin-0.5.1 lib/active_admin/helpers/optional_display.rb
activeadmin-0.5.0 lib/active_admin/helpers/optional_display.rb
activeadmin-0.5.0.pre1 lib/active_admin/helpers/optional_display.rb
activeadmin-0.5.0.pre lib/active_admin/helpers/optional_display.rb