Sha256: a8a88db9d7217efa98fdc3c5ab5673b597b2ac4004ccb445437069c33eac7221

Contents?: true

Size: 1.96 KB

Versions: 71

Compression:

Stored size: 1.96 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

    # Override respond to to include keys
    def respond_to?(method)
      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?('=')
          self.class_eval <<-EOS
            def #{key}=(value)
              set_view_for_key(:#{key}, value)
            end
          EOS
        else
          self.class_eval <<-EOS
            def #{key}
              get_view_for_key(:#{key})
            end
          EOS
        end
        self.send(method, *args)
      else
        super
      end
    end

    def key_from_method_name(method)
      method.to_s.gsub('=', '').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

71 entries across 71 versions & 5 rubygems

Version Path
lalala-4.0.0.dev.141 vendor/deps/active_admin/lib/active_admin/abstract_view_factory.rb
lalala-4.0.0.dev.136 vendor/deps/active_admin/lib/active_admin/abstract_view_factory.rb
lalala-4.0.0.dev.135 vendor/deps/active_admin/lib/active_admin/abstract_view_factory.rb
lalala-4.0.0.dev.134 vendor/deps/active_admin/lib/active_admin/abstract_view_factory.rb
lalala-4.0.0.dev.133 vendor/deps/active_admin/lib/active_admin/abstract_view_factory.rb
lalala-4.0.0.dev.132 vendor/deps/active_admin/lib/active_admin/abstract_view_factory.rb
lalala-4.0.0.dev.131 vendor/deps/active_admin/lib/active_admin/abstract_view_factory.rb
lalala-4.0.0.dev.129 vendor/deps/active_admin/lib/active_admin/abstract_view_factory.rb
lalala-4.0.0.dev.128 vendor/deps/active_admin/lib/active_admin/abstract_view_factory.rb
lalala-4.0.0.dev.126 vendor/deps/active_admin/lib/active_admin/abstract_view_factory.rb
lalala-4.0.0.dev.125 vendor/deps/active_admin/lib/active_admin/abstract_view_factory.rb
lalala-4.0.0.dev.124 vendor/deps/active_admin/lib/active_admin/abstract_view_factory.rb
lalala-4.0.0.dev.123 vendor/deps/active_admin/lib/active_admin/abstract_view_factory.rb
lalala-4.0.0.dev.118 vendor/deps/active_admin/lib/active_admin/abstract_view_factory.rb
lalala-4.0.0.dev.116 vendor/deps/active_admin/lib/active_admin/abstract_view_factory.rb
lalala-4.0.0.dev.114 vendor/deps/active_admin/lib/active_admin/abstract_view_factory.rb
lalala-4.0.0.dev.113 vendor/deps/active_admin/lib/active_admin/abstract_view_factory.rb
lalala-4.0.0.dev.111 vendor/deps/active_admin/lib/active_admin/abstract_view_factory.rb
lalala-4.0.0.dev.109 vendor/deps/active_admin/lib/active_admin/abstract_view_factory.rb
lalala-4.0.0.dev.107 vendor/deps/active_admin/lib/active_admin/abstract_view_factory.rb