rails_settlement 1.3.0 → 1.4.0
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.
- checksums.yaml +4 -4
- data/.rubocop.yml +4 -1
- data/README.md +10 -2
- data/lib/rails_settlement/version.rb +1 -1
- data/lib/rails_settlement.rb +23 -8
- 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: fea25e5290b99af4236253654e98e65428fbb8845cd3952ed57bddbfbd877aa5
|
4
|
+
data.tar.gz: bf7b83e4dcd6cde28b6fbca6bf96db247ec07d75e4ea76ffa8caec1f775dc7e3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 61f3e0eb9b9909119252dfda0cced54624da78f22c531bf6ae7b03cdd30e3be30485182fda294c83d01475b0df03e56ebd4ded6fdbd1875605e053a677b4dddf
|
7
|
+
data.tar.gz: c19ca70fec032ce9b3ffb0868bbc92d4f4322a0138e1e5240d11a5581820b26824ae0ec2a13ef284c301c18b3b7abcb1d1c249e1224c9f0f366007a0d19ebbcc
|
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
@@ -25,6 +25,16 @@ 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
|
+
|
34
|
+
# If you want to search an object's relationship, you can use the `associated_to` option.
|
35
|
+
# set_articles! associated_to: :user, ... # => Article.where(user => user)
|
36
|
+
# set_articles! associated_to: :user, scope_to: :published # => Article.published.where(user => user).find_by
|
37
|
+
|
28
38
|
def show
|
29
39
|
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
40
|
end
|
@@ -47,6 +57,4 @@ end
|
|
47
57
|
|
48
58
|
This approach allows you to use any model instead of a `User`. Just follow the naming convention set_[model_name_in_snake_case]!.
|
49
59
|
|
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
60
|
That's about it!
|
data/lib/rails_settlement.rb
CHANGED
@@ -14,12 +14,15 @@ module RailsSettlement
|
|
14
14
|
def method_missing(method, **options)
|
15
15
|
super unless (matches = method.to_s.match(SETTABLE_REGEX))
|
16
16
|
|
17
|
-
variable, raisable =
|
18
|
-
klass =
|
19
|
-
param_options =
|
20
|
-
scoped_klass =
|
17
|
+
variable, raisable = _rs_handle_match_data(matches)
|
18
|
+
klass = _rs_klass(variable, **options.extract!(:namespace))
|
19
|
+
param_options = _rs_param_options(klass: klass, options: options)
|
20
|
+
scoped_klass = _rs_handle_scopes(klass: klass, scopes: param_options[:scope_to])
|
21
|
+
associated_to = _rs_handle_associated_to(options: options)
|
21
22
|
|
22
23
|
before_action(**options) do
|
24
|
+
scoped_klass = _rs_associated_scope(scoped_relation: scoped_klass, associated_to: associated_to)
|
25
|
+
|
23
26
|
instance_variable_set("@#{variable}", scoped_klass.find_by(param_options[:model_key] => params[param_options[:params_key]]))
|
24
27
|
raise ActiveRecord::RecordNotFound if raisable && instance_variable_get("@#{variable}").nil?
|
25
28
|
end
|
@@ -33,21 +36,21 @@ module RailsSettlement
|
|
33
36
|
|
34
37
|
private
|
35
38
|
|
36
|
-
def
|
39
|
+
def _rs_handle_match_data(match_data)
|
37
40
|
[match_data[:object], match_data[:raisable].present?]
|
38
41
|
end
|
39
42
|
|
40
|
-
def
|
43
|
+
def _rs_klass(variable, namespace: nil)
|
41
44
|
variable = variable.classify
|
42
45
|
variable = "#{namespace.to_s.classify}::#{variable}" if namespace.present?
|
43
46
|
variable.constantize
|
44
47
|
end
|
45
48
|
|
46
|
-
def
|
49
|
+
def _rs_param_options(klass:, options:)
|
47
50
|
(klass.try(:settable_params) || {}).merge(options.extract!(:model_key, :params_key, :scope_to))
|
48
51
|
end
|
49
52
|
|
50
|
-
def
|
53
|
+
def _rs_handle_scopes(klass:, scopes:)
|
51
54
|
return klass if scopes.blank?
|
52
55
|
|
53
56
|
scopes = [scopes] unless scopes.is_a?(Array)
|
@@ -55,6 +58,18 @@ module RailsSettlement
|
|
55
58
|
|
56
59
|
klass
|
57
60
|
end
|
61
|
+
|
62
|
+
def _rs_handle_associated_to(options:)
|
63
|
+
return nil unless options.key?(:associated_to)
|
64
|
+
|
65
|
+
options.delete(:associated_to)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def _rs_associated_scope(scoped_relation:, associated_to: nil)
|
70
|
+
return scoped_relation if associated_to.blank?
|
71
|
+
|
72
|
+
scoped_relation.where(associated_to => public_send(associated_to))
|
58
73
|
end
|
59
74
|
end
|
60
75
|
|
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.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tejas
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-03-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|