Module: Aenea::ActiveAdmin::ResourceDsl::UseParentActions

Included in:
Aenea::ActiveAdmin::ResourceDsl
Defined in:
lib/aenea/active_admin/resource_dsl/use_parent_actions.rb

Instance Method Summary collapse

Instance Method Details

#use_parent_action(action, options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/aenea/active_admin/resource_dsl/use_parent_actions.rb', line 25

def use_parent_action(action, options = {})
  raise 'invalid action' unless %i(create update destroy).include?(action)

  grandparent_type = options[:grandparent]
  verb = action == :destroy ? 'destroyed' : "#{action}d"

  controller do
    define_method action do
      super() do |success, failure|
        parent_name = parent_type.to_s rescue
          (options[:parent] or raise 'must specify parent for controller without belongs_to')

        success.html do
          flash[:notice] = "Succesfully #{verb} #{parent_name.titleize}"
        end

        failure.html do
          flash[:error] = "Could not #{action} #{parent_name.titleize}: #{resource.errors.full_messages.join(', ')}"
        end

        parent_id = params[:"#{parent_type}_id"]
        url = if grandparent_type
          parent = parent_type.to_s.camelize.constantize.find(parent_id)
          grandparent = parent.send grandparent_type
          route = :"admin_#{grandparent_type}_#{parent_type}_path"
          send route, grandparent, parent
        else
          parent_path parent_id
        end

        if options[:redirect_back]
          redirect_back fallback_location: url
        else
          redirect_to url
        end
      end
    end
  end
end

#use_parent_create(options = {}) ⇒ Object



65
66
67
# File 'lib/aenea/active_admin/resource_dsl/use_parent_actions.rb', line 65

def use_parent_create(options = {})
  use_parent_action :create, options
end

#use_parent_destroy(options = {}) ⇒ Object



73
74
75
# File 'lib/aenea/active_admin/resource_dsl/use_parent_actions.rb', line 73

def use_parent_destroy(options = {})
  use_parent_action :destroy, options
end

#use_parent_index(options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/aenea/active_admin/resource_dsl/use_parent_actions.rb', line 5

def use_parent_index(options = {})
  grandparent_type = options[:grandparent]

  controller do
    define_method :index do # don't use def so we can access 'options'
      parent_id = params[:"#{parent_type}_id"]
      url = if grandparent_type
        parent = parent_type.to_s.camelize.constantize.find(parent_id)
        grandparent = parent.send grandparent_type
        route = :"admin_#{grandparent_type}_#{parent_type}_path"
        send route, grandparent, parent
      else
        parent_path parent_id
      end

      redirect_to url
    end
  end
end

#use_parent_update(options = {}) ⇒ Object



69
70
71
# File 'lib/aenea/active_admin/resource_dsl/use_parent_actions.rb', line 69

def use_parent_update(options = {})
  use_parent_action :update, options
end