Module: Effective::Resources::Actions

Included in:
Effective::Resource
Defined in:
app/models/effective/resources/actions.rb

Constant Summary collapse

EMPTY_HASH =
{}
POST_VERBS =
['POST', 'PUT', 'PATCH']
CRUD_ACTIONS =
%i(index new create show edit update destroy)

Instance Method Summary collapse

Instance Method Details

#action_path(action, resource = nil, opts = nil) ⇒ Object

Effective::Resource.new(‘admin/posts’).action_path(:edit, Post.last) => ‘/admin/posts/3/edit’ Will work for any action. Returns the real path



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/models/effective/resources/actions.rb', line 55

def action_path(action, resource = nil, opts = nil)
  opts ||= EMPTY_HASH

  if klass.nil? && resource.present? && initialized_name.kind_of?(ActiveRecord::Reflection::BelongsToReflection)
    return Effective::Resource.new(resource, namespace: namespace).action_path(action, resource, opts)
  end

  return unless routes[action]

  if resource.kind_of?(Hash)
    opts = resource; resource = nil
  end

  # edge case: Effective::Resource.new('admin/comments').action_path(:new, @post)
  if resource && klass && !resource.kind_of?(klass)
    if (bt = belongs_to(resource)).present? && instance.respond_to?("#{bt.name}=")
      return routes[action].format(klass.new(bt.name => resource)).presence
    end
  end

  target = (resource || instance)

  formattable = if routes[action].parts.include?(:id)
    if target.respond_to?(:to_param) && target.respond_to?(:id) && (target.to_param != target.id.to_s)
      routes[action].parts.each_with_object({}) do |part, h|
        if part == :id
          h[part] = target.to_param
        elsif part == :format
          # Nothing
        elsif target.respond_to?(part)
          h[part] = target.public_send(part)
        end
      end
    else
      target
    end
  end

  # Generate the path
  path = routes[action].format(formattable || EMPTY_HASH)

  if path.present? && opts.present?
    uri = URI.parse(path)
    uri.query = URI.encode_www_form(opts)
    path = uri.to_s
  end

  path
end

#action_path_helper(action) ⇒ Object

Effective::Resource.new(‘admin/posts’).action_path_helper(:edit) => ‘edit_admin_posts_path’ This will return empty for create, update and destroy



48
49
50
51
# File 'app/models/effective/resources/actions.rb', line 48

def action_path_helper(action)
  return unless routes[action]
  return (routes[action].name + '_path') if routes[action].name.present?
end

#actionsObject



105
106
107
# File 'app/models/effective/resources/actions.rb', line 105

def actions
  @route_actions ||= routes.keys
end

#collection_actionsObject

GET actions



114
115
116
117
118
# File 'app/models/effective/resources/actions.rb', line 114

def collection_actions
  @collection_actions ||= (
    routes.map { |_, route| route.defaults[:action].to_sym if is_collection_route?(route) }.tap(&:compact!)
  )
end

#collection_get_actionsObject



120
121
122
123
124
# File 'app/models/effective/resources/actions.rb', line 120

def collection_get_actions
  @collection_get_actions ||= (
    routes.map { |_, route| route.defaults[:action].to_sym if is_collection_route?(route) && is_get_route?(route) }.tap(&:compact!)
  )
end

#collection_post_actionsObject



126
127
128
129
130
# File 'app/models/effective/resources/actions.rb', line 126

def collection_post_actions
  @collection_post_actions ||= (
    routes.map { |_, route| route.defaults[:action].to_sym if is_collection_route?(route) && is_post_route?(route) }.tap(&:compact!)
  )
end

#controller_pathObject

Same as controller_path in the view



160
161
162
# File 'app/models/effective/resources/actions.rb', line 160

def controller_path
  [namespace, plural_name].compact * '/'
end

#crud_actionsObject



109
110
111
# File 'app/models/effective/resources/actions.rb', line 109

def crud_actions
  @crud_actions ||= (actions & CRUD_ACTIONS)
end

#member_actionsObject

All actions



133
134
135
136
137
# File 'app/models/effective/resources/actions.rb', line 133

def member_actions
  @member_actions ||= (
    routes.map { |_, route| route.defaults[:action].to_sym if is_member_route?(route) }.tap(&:compact!)
  )
end

#member_delete_actionsObject



146
147
148
149
150
# File 'app/models/effective/resources/actions.rb', line 146

def member_delete_actions
  @member_delete_actions ||= (
    routes.map { |_, route| route.defaults[:action].to_sym if is_member_route?(route) && is_delete_route?(route) }.tap(&:compact!)
  )
end

#member_get_actionsObject

GET actions



140
141
142
143
144
# File 'app/models/effective/resources/actions.rb', line 140

def member_get_actions
  @member_get_actions ||= (
    routes.map { |_, route| route.defaults[:action].to_sym if is_member_route?(route) && is_get_route?(route) }.tap(&:compact!)
  )
end

#member_post_actionsObject

POST/PUT/PATCH actions



153
154
155
156
157
# File 'app/models/effective/resources/actions.rb', line 153

def member_post_actions
  @member_post_actions ||= (
    routes.map { |_, route| route.defaults[:action].to_sym if is_member_route?(route) && is_post_route?(route) }.tap(&:compact!)
  )
end

#routesObject

This was written for the Edit actions fallback templates and Datatables Effective::Resource.new(‘admin/posts’).routes



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/models/effective/resources/actions.rb', line 12

def routes
  @routes ||= (
    matches = [
      [namespace, plural_name].compact.join('/'),
      [namespace, name].compact.join('/')
    ]

    # Check main Rails app
    routes = Rails.application.routes.routes.select do |route|
      (matches & [route.defaults[:controller]]).present? && !route.name.to_s.end_with?('root')
    end

    # Check engine routes
    if routes.blank?
      matches = [
        [namespace, plural_name].compact.join('/'),
        [namespace, name].compact.join('/'),
        ['effective', namespace, plural_name].compact.join('/'),
        ['effective', namespace, name].compact.join('/')
      ]

      (Rails::Engine.subclasses.reverse + [Rails.application]).each do |engine|
        routes = engine.routes.routes.select do |route|
          (matches & [route.defaults[:controller]]).present? && !route.name.to_s.end_with?('root')
        end

        break if routes.present?
      end
    end

    Array(routes).inject({}) { |h, route| h[route.defaults[:action].to_sym] = route; h }
  )
end