Class: Coupdoeil::Popover

Inherits:
AbstractController::Base
  • Object
show all
Includes:
AbstractController::Caching, AbstractController::Callbacks, AbstractController::Helpers, AbstractController::Logger, AbstractController::Rendering, AbstractController::Translation, ActionController::Cookies, ActionController::Helpers, ActionView::Layouts, ActionView::Rendering
Defined in:
app/models/coupdoeil/popover.rb,
app/models/coupdoeil/popover/setup.rb,
app/models/coupdoeil/popover/option.rb,
app/models/coupdoeil/popover/registry.rb,
app/models/coupdoeil/popover/options_set.rb,
app/models/coupdoeil/popover/option/cache.rb,
app/models/coupdoeil/popover/option/offset.rb,
app/models/coupdoeil/popover/option/loading.rb,
app/models/coupdoeil/popover/option/trigger.rb,
app/models/coupdoeil/popover/option/animation.rb,
app/models/coupdoeil/popover/option/placement.rb,
app/models/coupdoeil/popover/option/opening_delay.rb

Defined Under Namespace

Classes: Option, OptionsSet, Registry, Setup

Constant Summary collapse

DoubleRenderError =
Class.new(::AbstractController::DoubleRenderError)

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params, context_controller) ⇒ Popover

Returns a new instance of Popover.

Parameters:

  • params (HashWithIndifferentAccess)

    the deserialized popover params that were given to ‘.with`

  • context_controller (ActionController::Base)

    an instance of Coupdoeil::PopoversController or the current controller if the popover is rendered inline because of ‘loading: :preload` option.



115
116
117
118
119
# File 'app/models/coupdoeil/popover.rb', line 115

def initialize(params, context_controller)
  super()
  @params = params
  @context_controller = context_controller
end

Class Attribute Details

.registryObject (readonly)

Returns the value of attribute registry.



59
60
61
# File 'app/models/coupdoeil/popover.rb', line 59

def registry
  @registry
end

Instance Attribute Details

#context_controllerObject (readonly)

Returns the value of attribute context_controller.



107
108
109
# File 'app/models/coupdoeil/popover.rb', line 107

def context_controller
  @context_controller
end

#paramsObject (readonly)

Returns the value of attribute params.



107
108
109
# File 'app/models/coupdoeil/popover.rb', line 107

def params
  @params
end

Class Method Details

.default_optionsObject



70
# File 'app/models/coupdoeil/popover.rb', line 70

def default_options(...) = default_options_for(DEFAULT_OPTIONS_KEY, ...)

.default_options_for(*action_names, **option_values) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'app/models/coupdoeil/popover.rb', line 72

def default_options_for(*action_names, **option_values)
  if option_values.blank?
    @default_options_by_method[action_names.first] || default_options
  else
    action_names.each do |action_name|
      options = @default_options_by_method[action_name] || default_options
      @default_options_by_method[action_name] = options.merge(OptionsSet.new(option_values))
    end
  end
end

.inherited(subclass) ⇒ Object



64
65
66
67
68
# File 'app/models/coupdoeil/popover.rb', line 64

def inherited(subclass)
  super
  Coupdoeil::Popover.registry.register(subclass.popover_resource_name, subclass)
  subclass.instance_variable_set(:@default_options_by_method, @default_options_by_method.dup)
end

.method_missing(method_name, *args) ⇒ Object



83
84
85
86
87
88
89
90
# File 'app/models/coupdoeil/popover.rb', line 83

def method_missing(method_name, *args, &)
  return super unless action_methods.include?(method_name.name)

  action_methods.each do |action_name|
    define_singleton_method(action_name) { setup_class.new(self).with_type(action_name) }
  end
  public_send(method_name)
end

.popover_resource_nameObject



61
# File 'app/models/coupdoeil/popover.rb', line 61

def popover_resource_name = @popover_resource_name ||= name.delete_suffix("Popover").underscore

.respond_to_missing?(method, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
# File 'app/models/coupdoeil/popover.rb', line 92

def respond_to_missing?(method, include_all = false)
  action_methods.include?(method.name) || super
end

.setup_classObject



96
97
98
99
100
101
102
103
104
# File 'app/models/coupdoeil/popover.rb', line 96

def setup_class
  @setup_class ||= begin
    setup_klass = Class.new(Setup)
    action_methods.each do |action_name|
      setup_klass.define_method(action_name) { with_type(action_name) }
    end
    setup_klass
  end
end

.withObject



62
# File 'app/models/coupdoeil/popover.rb', line 62

def with(...) = setup_class.new(self).with_params(...)

Instance Method Details

#controllerObject



130
# File 'app/models/coupdoeil/popover.rb', line 130

def controller = context_controller

#process(method_name) ⇒ Object



144
145
146
147
148
149
150
151
# File 'app/models/coupdoeil/popover.rb', line 144

def process(method_name, *)
  benchmark("processed popover #{self.class.popover_resource_name}/#{method_name}", silence: true) do
    ActiveSupport::Notifications.instrument("render_popover.coupdoeil") do
      super
      response_body || render(action_name)
    end
  end
end

#renderObject

Raises:



137
138
139
140
141
142
# File 'app/models/coupdoeil/popover.rb', line 137

def render(...)
  return super unless response_body

  raise DoubleRenderError, "Render was called multiple times in this action. \
Also note that render does not terminate execution of the action."
end