rails_settlement 1.3.1 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +4 -1
- data/README.md +4 -0
- 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
@@ -31,6 +31,10 @@ class UsersController < ApplicationController
|
|
31
31
|
# If you have a model that's nested more than once, like 'Admin::Account::User', you can define as:
|
32
32
|
# set_user namespace: 'admin/account', ...
|
33
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
|
+
|
34
38
|
def show
|
35
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!)
|
36
40
|
end
|
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
|