Class: Jets::Router::MethodCreator::Index

Inherits:
Code
  • Object
show all
Defined in:
lib/jets/router/method_creator/index.rb

Instance Method Summary collapse

Methods inherited from Code

#action, #full_as, #full_meth_name, #full_path, #initialize, #meth_args, #meth_result, #method_name_leaf, #param_name, #path_method, #url_method

Methods included from Util

#get_controller_action, #handle_on!, #join, #underscore

Constructor Details

This class inherits a constructor from Jets::Router::MethodCreator::Code

Instance Method Details

#meth_nameObject



3
4
5
6
7
8
9
10
11
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
45
46
# File 'lib/jets/router/method_creator/index.rb', line 3

def meth_name
  # TODO: figure out how to improve this and make easier to follow.
  #
  # Example 1:
  #
  #     resources :users, only: [] do
  #       resources :posts, only: :index
  #     end
  #
  # Results in:
  #
  #     full_as: user_posts
  #     method_name_leaf: nil
  #
  # Example 2:
  #
  #     resources :users, only: [] do
  #       get "posts", to: "posts#index"
  #     end
  #
  # Results in:
  #
  #     full_as: users
  #     method_name_leaf: posts
  #
  # This is because using resources contains all the info we need in parent scopes.
  # The scope.full_as already has the desired meth_name.
  #
  # However, when using the simple create_route methods like get, the parent scope does not contain
  # all the info we need. In this tricky case, the method_name_leaf is set.
  # We then have to reconstruct the meth_name.
  #

  if method_name_leaf
    path_items = @path.to_s.split('/')
    if path_items.size == 1
      join(singularize(full_as), method_name_leaf) # reconstruct
    else
      nil # fallback: do not define url method
    end
  else  # comes from resources
    join(full_as) # construct entirely from scope info
  end
end