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



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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'app/models/effective/resources/actions.rb', line 73

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
    elsif target.respond_to?(:to_param) || target.respond_to?(:id)
      target
    else
      {id: target}
    end
  end

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

  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



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

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

#actionsObject



125
126
127
# File 'app/models/effective/resources/actions.rb', line 125

def actions
  @route_actions ||= routes.keys
end

#collection_actionsObject

GET actions



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

def collection_actions
  routes.map { |_, route| route.defaults[:action].to_sym if is_collection_route?(route) } - [nil]
end

#collection_get_actionsObject



138
139
140
# File 'app/models/effective/resources/actions.rb', line 138

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

#collection_post_actionsObject



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

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

#controller_pathObject

This will have been set by init from crud_controller, or from a class and namespace



11
12
13
# File 'app/models/effective/resources/actions.rb', line 11

def controller_path
  @controller_path ||= route_name #[namespace, plural_name].compact * '/')
end

#crud_actionsObject



129
130
131
# File 'app/models/effective/resources/actions.rb', line 129

def crud_actions
  (actions & CRUD_ACTIONS)
end

#enginesObject



15
16
17
18
19
20
21
# File 'app/models/effective/resources/actions.rb', line 15

def engines
  return ([Rails.application] + Rails::Engine.subclasses.reverse) unless tenant?

  [Rails.application, Tenant.Engine] + Rails::Engine.subclasses.reverse.reject do |klass|
    tenant_engines_blacklist.any? { |name| klass.name.start_with?(name) }
  end
end

#member_actionsObject

All actions



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

def member_actions
  routes.map { |_, route| route.defaults[:action].to_sym if is_member_route?(route) } - [nil]
end

#member_delete_actionsObject



156
157
158
# File 'app/models/effective/resources/actions.rb', line 156

def member_delete_actions
  routes.map { |_, route| route.defaults[:action].to_sym if is_member_route?(route) && is_delete_route?(route) } - [nil]
end

#member_get_actionsObject

GET actions



152
153
154
# File 'app/models/effective/resources/actions.rb', line 152

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

#member_post_actionsObject

POST/PUT/PATCH actions



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

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

#routesObject



23
24
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
# File 'app/models/effective/resources/actions.rb', line 23

def routes
  @routes ||= begin
    routes = nil

    # Check from controller_path. This is generally correct.
    engines.each do |engine|
      routes = engine.routes.routes.select do |route|
        controller_path == route.defaults[:controller] && !(route.name || '').end_with?('root')
      end

      if routes.present?
        @routes_app = engine; break
      end
    end

    if routes.blank?
      matches = route_name_fallbacks()

      engines.each do |engine|
        routes = engine.routes.routes.select do |route|
          (matches & [route.defaults[:controller]]).present? && !(route.name || '').end_with?('root')
        end

        if routes.present?
          @routes_app = engine; break
        end
      end
    end

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

#routes_appObject



56
57
58
# File 'app/models/effective/resources/actions.rb', line 56

def routes_app
  @routes_app if routes.present?
end

#url_helpersObject



60
61
62
# File 'app/models/effective/resources/actions.rb', line 60

def url_helpers
  (routes_app || Rails.application).routes.url_helpers
end