Class: ActiveAdmin::AbstractViewFactory

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

Direct Known Subclasses

ViewFactory

Constant Summary collapse

@@default_views =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAbstractViewFactory

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

#method_missing(method, *args) ⇒ Object (private)



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 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?('=')
      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

Class Method Details

.register(view_hash) ⇒ Object



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

#[](key) ⇒ Object



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

def [](key)
  get_view_for_key(key)
end

#[]=(key, value) ⇒ Object



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

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

#default_for(key) ⇒ Object



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

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

#has_key?(key) ⇒ Boolean

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

#register(view_hash) ⇒ Object

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

#respond_to?(method) ⇒ Boolean

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