Class: ActiveAdmin::AbstractViewFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/active_admin/abstract_view_factory.rb

Direct Known Subclasses

ViewFactory

Constant Summary

@@default_views =
{}

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (AbstractViewFactory) initialize

Returns a new instance of AbstractViewFactory



11
12
13
# File 'lib/active_admin/abstract_view_factory.rb', line 11

def initialize
  @views = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(method, *args) (private)



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/active_admin/abstract_view_factory.rb', line 61

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

Class Method Details

+ (Object) register(view_hash)



5
6
7
8
9
# File 'lib/active_admin/abstract_view_factory.rb', line 5

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

Instance Method Details

- (Object) [](key)



41
42
43
# File 'lib/active_admin/abstract_view_factory.rb', line 41

def [](key)
  get_view_for_key(key)
end

- (Object) []=(key, value)



45
46
47
# File 'lib/active_admin/abstract_view_factory.rb', line 45

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

- (Object) default_for(key)



33
34
35
# File 'lib/active_admin/abstract_view_factory.rb', line 33

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

- (Boolean) has_key?(key)

Returns:

  • (Boolean)


37
38
39
# File 'lib/active_admin/abstract_view_factory.rb', line 37

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

- (Object) register(view_hash)

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


27
28
29
30
31
# File 'lib/active_admin/abstract_view_factory.rb', line 27

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

- (Boolean) respond_to?(method)

Override respond to to include keys

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
# File 'lib/active_admin/abstract_view_factory.rb', line 50

def respond_to?(method)
  key = key_from_method_name(method)
  if has_key?(key)
    true
  else
    super
  end
end