Class: Jets::Router::MethodCreator::Code

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/jets/router/method_creator/code.rb

Direct Known Subclasses

Edit, Generic, Index, New, Root, Show

Instance Method Summary collapse

Methods included from Util

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

Constructor Details

#initialize(options, scope, controller, action = nil) ⇒ Code

Returns a new instance of Code.



5
6
7
8
# File 'lib/jets/router/method_creator/code.rb', line 5

def initialize(options, scope, controller, action=nil)
  @options, @scope, @controller, @action = options, scope, controller, action
  @path, @as = options[:path], options[:as]
end

Instance Method Details

#actionObject



35
36
37
# File 'lib/jets/router/method_creator/code.rb', line 35

def action
  @action || self.class.name.split('::').last.downcase # MethodCreator::Edit, MethodCreator::New, etc
end

#full_asObject



39
40
41
# File 'lib/jets/router/method_creator/code.rb', line 39

def full_as
  @scope&.full_as
end

#full_meth_name(suffix = nil) ⇒ Object



51
52
53
54
55
# File 'lib/jets/router/method_creator/code.rb', line 51

def full_meth_name(suffix=nil)
  as =  @as || meth_name
  name = [as, suffix].compact.join('_')
  underscore(name)
end

#full_pathObject



30
31
32
33
# File 'lib/jets/router/method_creator/code.rb', line 30

def full_path
  route = Jets::Router::Route.new(@options, @scope)
  route.compute_path
end

#meth_argsObject



10
11
12
13
14
15
# File 'lib/jets/router/method_creator/code.rb', line 10

def meth_args
  params = full_path.split('/').select { |x| x.include?(':') }
  items = params.map { |x| x.sub(':','') }

  items.empty? ? nil : "("+items.join(', ')+")"
end

#meth_resultObject



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/jets/router/method_creator/code.rb', line 17

def meth_result
  results = full_path.split('/').map do |x|
    if x.include?(':')
      variable = x.sub(':','')
      "\#{#{variable}.to_param}"
    else
      x
    end
  end

  '/' + results.join('/') unless results.empty?
end

#method_name_leafObject

The method_name_leaf is used to generate method names. Can be nil because sometimes the name is fully acquired from the scope.



45
46
47
48
49
# File 'lib/jets/router/method_creator/code.rb', line 45

def method_name_leaf
  unless %w[resource resources].include?(@scope.from.to_s) && @options[:from_scope]
    @controller
  end
end

#param_name(name) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/jets/router/method_creator/code.rb', line 78

def param_name(name)
  # split('/').last for case:
  #
  #   resources :posts, prefix: "articles", only: :index do
  #     resources :comments, only: :new
  #   end
  #
  # Since the prefix at the scope level is added to the posts item, which results in:
  #
  #   param_name("articles/posts")
  #
  # We drop the articles prefix portion. The resources items can only be words with no /.
  #
  name.to_s.split('/').last.singularize + "_id"
end

#path_methodObject



57
58
59
60
61
62
63
64
# File 'lib/jets/router/method_creator/code.rb', line 57

def path_method
  return if @as == :disabled
  <<~EOL
    def #{full_meth_name(:path)}#{meth_args}
      "#{meth_result}"
    end
  EOL
end

#url_methodObject



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/jets/router/method_creator/code.rb', line 66

def url_method
  return if @as == :disabled
  path_method_call = "#{full_meth_name(:path)}#{meth_args}"
  # Note: It is important lazily get the value of ENV['JETS_HOST'] within the method.
  # Since it is not set until the request goes through the main middleware.
  <<~EOL
    def #{full_meth_name(:url)}#{meth_args}
      "\#{ENV['JETS_HOST']}\#{#{path_method_call}}"
    end
  EOL
end