Sha256: 6845936ad25bf0f156b7643a4ff653b5d3e2ad10426ac687174d0a8605f2790c

Contents?: true

Size: 1.71 KB

Versions: 32

Compression:

Stored size: 1.71 KB

Contents

module ActiveAdmin
  class AbstractViewFactory
    @@default_views = {}

    def self.register(view_hash)
      view_hash.each do |view_key, view_class|
        @@default_views[view_key] = view_class
      end
    end

    def initialize
      @views = {}
    end

    # Register a new view key with the view factory
    #
    # eg:
    #
    #   factory = AbstractViewFactory.new
    #   factory.register my_view: SomeViewClass
    #
    # You can setup many at the same time:
    #
    #   factory.register  my_view: SomeClass,
    #                     another_view: OtherViewClass
    #
    def register(view_hash)
      view_hash.each do |view_key, view_class|
        @views[view_key] = view_class
      end
    end

    def default_for(key)
      @@default_views[key.to_sym]
    end

    def has_key?(key)
      @views.has_key?(key.to_sym) || @@default_views.has_key?(key.to_sym)
    end

    def [](key)
      get_view_for_key(key)
    end

    def []=(key, value)
      set_view_for_key(key, value)
    end

    def respond_to_missing?(method, include_private)
      key = key_from_method_name(method)
      if has_key?(key)
        true
      else
        super
      end
    end

    private

    def method_missing(method, *args)
      key = key_from_method_name(method)
      if has_key?(key)
        if method.to_s.include?('=')
          set_view_for_key key, args.first
        else
          get_view_for_key key
        end
      else
        super
      end
    end

    def key_from_method_name(method)
      method.to_s.tr('=', '').to_sym
    end

    def get_view_for_key(key)
      @views[key.to_sym] || @@default_views[key.to_sym]
    end

    def set_view_for_key(key, view)
      @views[key.to_sym] = view
    end
  end
end

Version data entries

32 entries across 32 versions & 4 rubygems

Version Path
activeadmin-1.3.1 lib/active_admin/abstract_view_factory.rb
activeadmin-1.3.0 lib/active_admin/abstract_view_factory.rb
activeadmin-1.2.1 lib/active_admin/abstract_view_factory.rb
activeadmin_addons-1.1.2 vendor/bundle/ruby/2.3.0/bundler/gems/activeadmin-a5a53c3f2b8f/lib/active_admin/abstract_view_factory.rb
activeadmin-1.2.0 lib/active_admin/abstract_view_factory.rb
activeadmin_addons-1.1.1 vendor/bundle/ruby/2.2.0/bundler/gems/activeadmin-a5a53c3f2b8f/lib/active_admin/abstract_view_factory.rb
yousty-activeadmin-1.0.17.pre lib/active_admin/abstract_view_factory.rb
yousty-activeadmin-1.0.16.pre lib/active_admin/abstract_view_factory.rb
activeadmin_addons-1.1.0 vendor/bundle/ruby/2.2.0/bundler/gems/activeadmin-a5a53c3f2b8f/lib/active_admin/abstract_view_factory.rb
activeadmin_addons-1.0.1 vendor/bundle/ruby/2.2.0/bundler/gems/activeadmin-a5a53c3f2b8f/lib/active_admin/abstract_view_factory.rb
activeadmin_addons-1.0.0 vendor/bundle/ruby/2.2.0/bundler/gems/activeadmin-a5a53c3f2b8f/lib/active_admin/abstract_view_factory.rb
activeadmin-1.1.0 lib/active_admin/abstract_view_factory.rb
yousty-activeadmin-1.0.15.pre lib/active_admin/abstract_view_factory.rb
activeadmin-1.0.0 lib/active_admin/abstract_view_factory.rb
activeadmin-1.0.0.pre5 lib/active_admin/abstract_view_factory.rb
activeadmin-orac-1.0.0.pre4 lib/active_admin/abstract_view_factory.rb
activeadmin-orac-1.0.0 lib/active_admin/abstract_view_factory.rb
activeadmin-orac-1.0.0.pre.orac lib/active_admin/abstract_view_factory.rb
yousty-activeadmin-1.0.14.pre lib/active_admin/abstract_view_factory.rb
yousty-activeadmin-1.0.13.pre lib/active_admin/abstract_view_factory.rb