rails_settlement 1.3.1 → 1.4.1
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 +4 -0
- data/lib/rails_settlement/version.rb +1 -1
- data/lib/rails_settlement.rb +24 -9
- 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: 8765d1ef60db470409b267f76185cbdf35d4c0c7d06019c335f4e31b9846fbe5
|
4
|
+
data.tar.gz: 152505edcd4f98175058775bd71aea9feb11fdd481666f37d3fcbcd2747e971d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 145fc2bad6d91b923477e12f4f4dc1d4a8cead461e87ff8ad24fd822dceb9e3b88b9f27b70ab2eb4946be2ec097509e849c204af6b3695e6e075cd6b4faa3da9
|
7
|
+
data.tar.gz: 54c441d03604c4e5a13f38b67f8bb155bff4fee312641fc046bf12aa4e1260c03514ac698f189bcdbed50f2b1f86f38d823a693a10afb9626c6d6372eed1bc4a
|
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,13 +14,16 @@ 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
|
23
|
-
|
24
|
+
query = _rs_associated_scope(scoped_relation: scoped_klass, associated_to: associated_to)
|
25
|
+
|
26
|
+
instance_variable_set("@#{variable}", query.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
|
26
29
|
|
@@ -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.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-
|
11
|
+
date: 2024-03-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|