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
62
63
64
65
66
67
68
69
70
# File 'app/models/effective/resources/actions.rb', line 38

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

  # This generates the correct route when an object is overriding to_param
  if (resource || instance).respond_to?(:attributes)
    formattable = (resource || instance).attributes.symbolize_keys.merge(id: (resource || instance).to_param)
  end

  path = routes[action].format(formattable || {}).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'.freeze) if routes[action].name.present?
end

#actionsObject



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

def actions
  routes.keys
end

#collection_actionsObject

GET actions



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

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

#collection_get_actionsObject



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

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

#collection_post_actionsObject



89
90
91
# File 'app/models/effective/resources/actions.rb', line 89

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

#controller_pathObject

Same as controller_path in the view



113
114
115
# File 'app/models/effective/resources/actions.rb', line 113

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

#crud_actionsObject



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

def crud_actions
  actions & %i(index new create show edit update destroy)
end

#member_actionsObject

All actions



94
95
96
# File 'app/models/effective/resources/actions.rb', line 94

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

#member_delete_actionsObject



103
104
105
# File 'app/models/effective/resources/actions.rb', line 103

def member_delete_actions
  routes.values.map { |route| route.defaults[:action].to_sym if is_member_route?(route) && is_delete_route?(route) }.compact
end

#member_get_actionsObject

GET actions



99
100
101
# File 'app/models/effective/resources/actions.rb', line 99

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

#member_post_actionsObject

POST/PUT/PATCH actions



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

def member_post_actions
  routes.values.map { |route| route.defaults[:action].to_sym if is_member_route?(route) && is_post_route?(route) }.compact
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('/'.freeze), [namespace, name].compact.join('/'.freeze)]

    routes_engine.routes.routes.select do |route|
      matches.any? { |match| match == route.defaults[:controller] } && !route.name.to_s.end_with?('root')
    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'.freeze
    EffectiveOrders::Engine
  else
    Rails.application
  end
end