Module: Flame::Controller::Actions

Includes:
Memery
Included in:
Flame::Controller
Defined in:
lib/flame/controller/actions.rb

Overview

Module for work with actions

Instance Method Summary collapse

Instance Method Details

#actionsArray<Symbol>

Shortcut for not-inherited public methods: actions

Returns:

  • array of actions (public instance methods)



15
16
17
# File 'lib/flame/controller/actions.rb', line 15

def actions
  public_instance_methods(false)
end

#inherit_actions(actions = nil, exclude: [], from: superclass) ⇒ Object

Re-define public instance methods (actions) from parent

Examples:

Inherit all parent actions

class MyController < BaseController
  inherit_actions
end

Inherit certain parent actions

class MyController < BaseController
  inherit_actions i[index show]
end

Inherit all parent actions exclude certain

class MyController < BaseController
  inherit_actions exclude: i[edit update]
end

Inherit certain actions from specific module

class MyController < BaseController
  inherit_actions i[index show], from: ModuleWithActions
end

Parameters:

  • (defaults to: nil)

    Actions for inheritance

  • (defaults to: [])

    Actions for excluding from inheritance

  • (defaults to: superclass)

    Module (or Class) from which actions will be inherited



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/flame/controller/actions.rb', line 40

def inherit_actions(actions = nil, exclude: [], from: superclass)
  actions = from.actions if actions.nil?
  actions -= exclude

  actions.each do |action|
    define_method action, from.public_instance_method(action)
  end

  return unless from.respond_to?(:refined_http_methods)

  refined_http_methods.merge!(
    from.refined_http_methods.slice(*actions)
  )
end

#with_actions(mod, exclude: [], only: nil) ⇒ Object

Re-define public instance method from module

Examples:

Define actions from module in controller

class MyController < BaseController
  include with_actions Module1
  include with_actions Module2
  ....
end

Define actions from module exclude some actions in controller

class MyController < BaseController
  include with_actions Module1, exclude: i[action1 action2 ...]
  include with_actions Module2, exclude: i[action1 action2 ...]
  ....
end

Define actions from module according list in controller

class MyController < BaseController
  include with_actions Module1, only: i[action1 action2 ...]
  include with_actions Module2, only: i[action1 action2 ...]
  ....
end

Parameters:

  • Module for including to controller

  • (defaults to: [])

    Actions for excluding from module public instance methods

  • (defaults to: nil)

    Actions for re-defining from module public instance methods



79
80
81
82
83
84
85
86
# File 'lib/flame/controller/actions.rb', line 79

def with_actions(mod, exclude: [], only: nil)
  Module.new do
    @mod = mod
    @actions = only || (@mod.public_instance_methods(false) - exclude)

    extend ModuleWithActions
  end
end