rails_settlement 1.2.0 → 1.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -1
- data/README.md +6 -2
- data/lib/rails_settlement/version.rb +1 -1
- data/lib/rails_settlement.rb +14 -12
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2c8e94ec51585b2d83c118eadff000068b305f42e4517a92178ad5c4f6eaa71b
|
4
|
+
data.tar.gz: d927ccfdfa4cf67c4ea577ef7fc9683ffd99e7643e9e52d8b3c05138a8f596e9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 72504a32b9a063436ed05d73fa9e4f372e4339e63a42edb46f43064dfa8526206be729d203e0b428b2fe656ffa8cdc3387c2ced7772b792506df5f76c193869c
|
7
|
+
data.tar.gz: ed367d3d88d16264714db216cc995e27b83346f4d24d9ecadc744df67b3aa7e92773ee9f3af977f4c778ec449172dee34ca16c1b3850984c50aef54d05bb6418
|
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
@@ -25,6 +25,12 @@ class UsersController < ApplicationController
|
|
25
25
|
# set_user scope_to: :is_active, only: %i[show] #=> User.is_active.find_by
|
26
26
|
# set_user! scope_to: %i[is_active is_admin] #=> User.is_active.is_admin.find_by
|
27
27
|
|
28
|
+
# You can also set a namespace for the model with the `namespace` option. It can be a string or a symbol.
|
29
|
+
# Suppose you have a model 'Admin::User', you can define:
|
30
|
+
# set_user namespace: :admin, ...
|
31
|
+
# If you have a model that's nested more than once, like 'Admin::Account::User', you can define as:
|
32
|
+
# set_user namespace: 'admin/account', ...
|
33
|
+
|
28
34
|
def show
|
29
35
|
do_something with: user # You also have an attribute reader with the same name available if the object is found. If not, it defaults to nil (unless a bang method is used, in which case an error is raised!)
|
30
36
|
end
|
@@ -47,6 +53,4 @@ end
|
|
47
53
|
|
48
54
|
This approach allows you to use any model instead of a `User`. Just follow the naming convention set_[model_name_in_snake_case]!.
|
49
55
|
|
50
|
-
Note: This implementation does not support STI or any Namespaced models. If you have a solution to make them work, please submit a pull request!
|
51
|
-
|
52
56
|
That's about it!
|
data/lib/rails_settlement.rb
CHANGED
@@ -10,17 +10,17 @@ module RailsSettlement
|
|
10
10
|
|
11
11
|
SETTABLE_REGEX = /\Aset_(?<object>[a-z_]+)(?<raisable>!?)\z/.freeze
|
12
12
|
|
13
|
-
class_methods do
|
13
|
+
class_methods do # rubocop:disable Metrics/BlockLength
|
14
14
|
def method_missing(method, **options)
|
15
15
|
super unless (matches = method.to_s.match(SETTABLE_REGEX))
|
16
16
|
|
17
17
|
variable, raisable = _handle_match_data(matches)
|
18
|
-
klass = _klass(variable)
|
18
|
+
klass = _klass(variable, **options.extract!(:namespace))
|
19
19
|
param_options = _param_options(klass: klass, options: options)
|
20
|
+
scoped_klass = _handle_scopes(klass: klass, scopes: param_options[:scope_to])
|
20
21
|
|
21
22
|
before_action(**options) do
|
22
|
-
|
23
|
-
instance_variable_set("@#{variable}", klass.find_by(param_options[:model_key] => params[param_options[:params_key]]))
|
23
|
+
instance_variable_set("@#{variable}", scoped_klass.find_by(param_options[:model_key] => params[param_options[:params_key]]))
|
24
24
|
raise ActiveRecord::RecordNotFound if raisable && instance_variable_get("@#{variable}").nil?
|
25
25
|
end
|
26
26
|
|
@@ -37,22 +37,24 @@ module RailsSettlement
|
|
37
37
|
[match_data[:object], match_data[:raisable].present?]
|
38
38
|
end
|
39
39
|
|
40
|
-
def _klass(variable)
|
41
|
-
variable.classify
|
40
|
+
def _klass(variable, namespace: nil)
|
41
|
+
variable = variable.classify
|
42
|
+
variable = "#{namespace.to_s.classify}::#{variable}" if namespace.present?
|
43
|
+
variable.constantize
|
42
44
|
end
|
43
45
|
|
44
46
|
def _param_options(klass:, options:)
|
45
47
|
(klass.try(:settable_params) || {}).merge(options.extract!(:model_key, :params_key, :scope_to))
|
46
48
|
end
|
47
|
-
end
|
48
49
|
|
49
|
-
|
50
|
-
|
50
|
+
def _handle_scopes(klass:, scopes:)
|
51
|
+
return klass if scopes.blank?
|
51
52
|
|
52
|
-
|
53
|
-
|
53
|
+
scopes = [scopes] unless scopes.is_a?(Array)
|
54
|
+
scopes.each { |scope| klass = klass.public_send(scope) }
|
54
55
|
|
55
|
-
|
56
|
+
klass
|
57
|
+
end
|
56
58
|
end
|
57
59
|
end
|
58
60
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_settlement
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tejas
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-02-
|
11
|
+
date: 2024-02-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|