ensure_param_exists 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/ensure_param_exists.rb +23 -13
- metadata +1 -1
data/lib/ensure_param_exists.rb
CHANGED
@@ -1,26 +1,36 @@
|
|
1
1
|
require 'rails'
|
2
|
+
require 'action_controller'
|
2
3
|
|
3
4
|
module EnsureParamExists
|
4
5
|
extend ActiveSupport::Concern
|
5
6
|
|
6
7
|
included do
|
7
|
-
def self.
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
8
|
+
def self.ensure_param(expected_param, opts = {})
|
9
|
+
_ensure_param_exists_(:and, [expected_param, opts])
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.ensure_any_params(*expected_params)
|
13
|
+
_ensure_param_exists_(:or, expected_params)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.ensure_all_params(*expected_params)
|
17
|
+
_ensure_param_exists_(:and, expected_params)
|
15
18
|
end
|
16
19
|
|
17
|
-
def self.
|
18
|
-
|
20
|
+
def self._ensure_param_exists_(operator, expected_params)
|
21
|
+
opts = expected_params.last.kind_of?(Hash) ? expected_params.pop : {}
|
22
|
+
|
23
|
+
method_name = "ensure_#{expected_params.join("_#{operator}_")}_exists"
|
19
24
|
define_method(method_name) do
|
20
|
-
return
|
21
|
-
render json: { success: false, message: "missing #{expected_params.join("
|
25
|
+
return if expected_params.send(_operator_map_[operator]) { |expected_param| params[expected_param.to_sym].present? }
|
26
|
+
render json: { success: false, message: "missing #{expected_params.join(" #{operator} ")} parameter" }, status: 422
|
22
27
|
end
|
28
|
+
|
29
|
+
before_filter method_name.to_sym, opts
|
23
30
|
end
|
24
|
-
end
|
25
31
|
|
32
|
+
def _operator_map_
|
33
|
+
{ and: 'all?', or: 'any?' }
|
34
|
+
end
|
35
|
+
end
|
26
36
|
end
|