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



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
124
125
126
127
128
129
130
131
132
133
134
# File 'app/models/effective/resources/actions.rb', line 75

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)
          obj = if part.to_s.ends_with?('_id') && target.respond_to?(part.to_s.chomp('_id'))
            target.try("#{part.to_s.chomp('_id')}")
          end

          h[part] = obj.try(:to_param) || 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)

  # Append default format from route (e.g., defaults: { format: :csv })
  if path.present? && (default_format = routes[action].defaults[:format]).present?
    path = "#{path}.#{default_format}"
  end

  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



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

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

#actionsObject



136
137
138
# File 'app/models/effective/resources/actions.rb', line 136

def actions
  @route_actions ||= routes.keys
end

#collection_actionsObject

GET actions



145
146
147
# File 'app/models/effective/resources/actions.rb', line 145

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

#collection_get_actionsObject



149
150
151
# File 'app/models/effective/resources/actions.rb', line 149

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



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

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



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

def crud_actions
  (actions & CRUD_ACTIONS)
end

#member_actionsObject

All actions



158
159
160
# File 'app/models/effective/resources/actions.rb', line 158

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

#member_delete_actionsObject



167
168
169
# File 'app/models/effective/resources/actions.rb', line 167

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



163
164
165
# File 'app/models/effective/resources/actions.rb', line 163

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



172
173
174
# File 'app/models/effective/resources/actions.rb', line 172

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

#route_enginesObject



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

def route_engines
  if tenant? && Tenant.current.present?
    [Rails.application, Tenant.Engine] + (Rails::Engine.subclasses.reverse - Tenant.route_engines)
  else
    [Rails.application] + Rails::Engine.subclasses.reverse
  end
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
55
56
# File 'app/models/effective/resources/actions.rb', line 23

def routes
  @routes ||= begin
    routes = nil

    engines = route_engines()

    # 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



58
59
60
# File 'app/models/effective/resources/actions.rb', line 58

def routes_app
  @routes_app if routes.present?
end

#url_helpersObject



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

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