Class: Betterdocs::Dsl::Controller::Action
Defined Under Namespace
Classes: Param, Response
Instance Method Summary
collapse
inherited, #initialize
#set_context
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
in the class Betterdocs::Dsl::Common
Instance Method Details
#add_to_collector(collector) ⇒ Object
124
125
126
|
# File 'lib/betterdocs/dsl/controller/action.rb', line 124
def add_to_collector(collector)
collector.element = self
end
|
86
87
88
89
|
# File 'lib/betterdocs/dsl/controller/action.rb', line 86
def configure_for_action(action_name)
action action_name
action_method controller.instance_method(action_name)
end
|
#include_params(callee) ⇒ Object
91
92
93
|
# File 'lib/betterdocs/dsl/controller/action.rb', line 91
def include_params(callee)
instance_eval(&callee)
end
|
#inspect ⇒ Object
120
121
122
|
# File 'lib/betterdocs/dsl/controller/action.rb', line 120
def inspect
"#{controller}##{action}(#{params.keys * ', '})"
end
|
#json_params ⇒ Object
45
46
47
|
# File 'lib/betterdocs/dsl/controller/action.rb', line 45
def json_params
json_params_representer.full?(:params)
end
|
#json_params_example_json ⇒ Object
49
50
51
52
53
54
55
56
57
|
# File 'lib/betterdocs/dsl/controller/action.rb', line 49
def json_params_example_json
if params = json_params_representer.full?(:params)
data = {}
params.each_with_object(data) do |(name, param), d|
d[name] = param.value
end
JSON.pretty_generate(JSON.load(JSON.dump(data)), quirks_mode: true) end
end
|
#json_params_like(klass) ⇒ Object
40
41
42
43
|
# File 'lib/betterdocs/dsl/controller/action.rb', line 40
def json_params_like(klass)
json_params_representer klass.docs
self
end
|
#param(name, &block) ⇒ Object
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/betterdocs/dsl/controller/action.rb', line 61
def param(name, &block)
name = name.to_sym
if block
param = Betterdocs::Dsl::Controller::Action::Param.new(name, &block)
param.value or param.value params.size + 1
params[name] = param
else
params[name]
end
end
|
#request ⇒ Object
106
107
108
|
# File 'lib/betterdocs/dsl/controller/action.rb', line 106
def request
"#{http_method.to_s.upcase} #{url}"
end
|
#response(name = :default, &block) ⇒ Object
74
75
76
77
78
79
80
81
82
|
# File 'lib/betterdocs/dsl/controller/action.rb', line 74
def response(name = :default, &block)
if block
responses[name] = Betterdocs::Dsl::Controller::Action::Response.new(
name, &block
).set_context(self)
else
responses[name]
end
end
|
#to_s ⇒ Object
110
111
112
113
114
115
116
117
118
|
# File 'lib/betterdocs/dsl/controller/action.rb', line 110
def to_s
(
[ request, '' ] +
[ inspect, '' ] +
params.map { |name, param|
"#{name}(=#{param.value}): #{param.description}"
} + [ '', description, '', action_method.source_location * ':', '' ]
) * "\n"
end
|
#url ⇒ Object
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/betterdocs/dsl/controller/action.rb', line 95
def url
url_params = params.select { |_, param| param.use_in_url? }
url_params = {
controller: controller.name.underscore.sub(/_controller\z/, ''),
action: action,
} | url_params | Betterdocs::Global.config.api_url_options
Betterdocs.rails.application.routes.url_for(url_params)
rescue ActionController::UrlGenerationError => e
raise e, "Could not generate URL for parameters #{url_params}. Check your param definitions!"
end
|