Module: Effective::Resources::Actions

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

Instance Method Summary collapse

Instance Method Details

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

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



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/models/effective/resources/actions.rb', line 38

def action_path(action, resource = nil, opts = {})
  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.present? && !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

  path = routes[action].format(resource || instance).presence

  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



31
32
33
34
# File 'app/models/effective/resources/actions.rb', line 31

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

#actionsObject



63
64
65
# File 'app/models/effective/resources/actions.rb', line 63

def actions
  routes.keys
end

#collection_actionsObject

GET actions



68
69
70
# File 'app/models/effective/resources/actions.rb', line 68

def collection_actions
  routes.values.map { |route| route.defaults[:action].to_sym if is_collection_route?(route) }.compact - crud_actions
end

#collection_get_actionsObject



72
73
74
# File 'app/models/effective/resources/actions.rb', line 72

def collection_get_actions
  routes.values.map { |route| route.defaults[:action].to_sym if is_collection_route?(route) && is_get_route?(route) }.compact - crud_actions
end

#collection_post_actionsObject



76
77
78
# File 'app/models/effective/resources/actions.rb', line 76

def collection_post_actions
  routes.values.map { |route| route.defaults[:action].to_sym if is_collection_route?(route) && is_post_route?(route) }.compact - crud_actions
end

#controller_pathObject

Same as controller_path in the view



96
97
98
# File 'app/models/effective/resources/actions.rb', line 96

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

#member_actionsObject

All actions



81
82
83
# File 'app/models/effective/resources/actions.rb', line 81

def member_actions
  routes.values.map { |route| route.defaults[:action].to_sym if is_member_route?(route) }.compact - crud_actions
end

#member_get_actionsObject

GET actions



86
87
88
# File 'app/models/effective/resources/actions.rb', line 86

def member_get_actions
  routes.values.map { |route| route.defaults[:action].to_sym if is_member_route?(route) && is_get_route?(route) }.compact - crud_actions
end

#member_post_actionsObject

POST/PUT/PATCH actions



91
92
93
# File 'app/models/effective/resources/actions.rb', line 91

def member_post_actions
  routes.values.map { |route| route.defaults[:action].to_sym if is_member_route?(route) && is_post_route?(route) }.compact - crud_actions
end

#routesObject

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



7
8
9
10
11
12
13
14
15
16
17
# File 'app/models/effective/resources/actions.rb', line 7

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

    routes_engine.routes.routes.select do |route|
      matches.any? { |match| match == route.defaults[:controller] }
    end.inject({}) do |h, route|
      h[route.defaults[:action].to_sym] = route; h
    end
  )
end

#routes_engineObject

Effective::Resource.new(‘effective/order’, namespace: :admin)



20
21
22
23
24
25
26
27
# File 'app/models/effective/resources/actions.rb', line 20

def routes_engine
  case class_name
  when 'Effective::Order'
    EffectiveOrders::Engine
  else
    Rails.application
  end
end