rails_settlement 1.1.0 → 1.2.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/README.md +19 -9
- data/lib/rails_settlement/version.rb +1 -1
- data/lib/rails_settlement.rb +28 -7
- 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: 5f6997e50cad74b6bfb65514e1efc59a454db83eca5d903fed3cee4ae35326aa
|
4
|
+
data.tar.gz: 0f372b19be2e0f3614c9c55b01394d38b6aebb915bf9905857b6cc5c746266a5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8bf231039431dbf2c302aab711be354ef8373cbf9570cf053d32100e3f3660b2399a9067f8be965c2dd553b303f4333a5fe06fe2ebe031ed12e54523bce86b78
|
7
|
+
data.tar.gz: 881deb50b91a6628596d9767a55ad4e57a15cf6055a8452d1a2275f0bd051b84eba7aa8bf429629e306fdd4b79073112a939abb73460f350c8b1a55262b0df1c
|
data/README.md
CHANGED
@@ -9,34 +9,44 @@ Add to your Gemfile:
|
|
9
9
|
gem 'rails_settlement'
|
10
10
|
```
|
11
11
|
|
12
|
-
## How to
|
12
|
+
## How to Use
|
13
13
|
|
14
|
-
|
14
|
+
Suppose you need to fetch a User before executing a controller action:
|
15
15
|
|
16
16
|
```ruby
|
17
17
|
class UsersController < ApplicationController
|
18
|
-
set_user only: %i[show] #
|
19
|
-
|
18
|
+
set_user only: %i[show] # It's essentially a before_action callback, allowing you to pass options directly.
|
19
|
+
|
20
|
+
# Using the bang (!) with this method triggers an ActiveRecord::RecordNotFound Exception.
|
21
|
+
set_user! only: %i[edit]
|
22
|
+
|
23
|
+
# You can also define a scope with the `scope_to` option. It can be either a string/symbol or an Array of string/symbols.
|
24
|
+
# This fetches the user after chaining all the scopes together.
|
25
|
+
# set_user scope_to: :is_active, only: %i[show] #=> User.is_active.find_by
|
26
|
+
# set_user! scope_to: %i[is_active is_admin] #=> User.is_active.is_admin.find_by
|
20
27
|
|
21
28
|
def show
|
22
|
-
do_something with: user # You
|
29
|
+
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!)
|
23
30
|
end
|
24
31
|
end
|
25
32
|
```
|
26
33
|
|
27
34
|
```ruby
|
28
35
|
class User < ApplicationRecord
|
29
|
-
|
30
|
-
|
36
|
+
scope :is_active, -> { where(is_active: true) }
|
37
|
+
|
38
|
+
# This method is used internally by `set_user` to fetch the correct param (using `params_key`) and to send a key to `find_by` (using `model_key`).
|
39
|
+
# You can override this on the fly by passing any of these two keys (or both!) in the controller directly.
|
31
40
|
# Ex: set_user params_key: :user_username, only: %i[index], ...
|
41
|
+
# You can also pass a `scope_to` option in the hash below.
|
32
42
|
def self.settable_params
|
33
43
|
{ params_key: :username, model_key: :username }
|
34
44
|
end
|
35
45
|
end
|
36
46
|
```
|
37
47
|
|
38
|
-
This
|
48
|
+
This approach allows you to use any model instead of a `User`. Just follow the naming convention set_[model_name_in_snake_case]!.
|
39
49
|
|
40
|
-
|
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!
|
41
51
|
|
42
52
|
That's about it!
|
data/lib/rails_settlement.rb
CHANGED
@@ -12,16 +12,14 @@ module RailsSettlement
|
|
12
12
|
|
13
13
|
class_methods do
|
14
14
|
def method_missing(method, **options)
|
15
|
-
super unless (
|
15
|
+
super unless (matches = method.to_s.match(SETTABLE_REGEX))
|
16
16
|
|
17
|
-
variable =
|
18
|
-
|
19
|
-
|
20
|
-
klass = variable.classify.constantize
|
21
|
-
|
22
|
-
param_options = (klass.try(:settable_params) || {}).merge(options.extract!(:model_key, :params_key))
|
17
|
+
variable, raisable = _handle_match_data(matches)
|
18
|
+
klass = _klass(variable)
|
19
|
+
param_options = _param_options(klass: klass, options: options)
|
23
20
|
|
24
21
|
before_action(**options) do
|
22
|
+
klass = _handle_scopes(klass, param_options[:scope_to])
|
25
23
|
instance_variable_set("@#{variable}", klass.find_by(param_options[:model_key] => params[param_options[:params_key]]))
|
26
24
|
raise ActiveRecord::RecordNotFound if raisable && instance_variable_get("@#{variable}").nil?
|
27
25
|
end
|
@@ -32,6 +30,29 @@ module RailsSettlement
|
|
32
30
|
def respond_to_missing?(method, include_private = false)
|
33
31
|
super || method.to_s.match?(SETTABLE_REGEX)
|
34
32
|
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def _handle_match_data(match_data)
|
37
|
+
[match_data[:object], match_data[:raisable].present?]
|
38
|
+
end
|
39
|
+
|
40
|
+
def _klass(variable)
|
41
|
+
variable.classify.constantize
|
42
|
+
end
|
43
|
+
|
44
|
+
def _param_options(klass:, options:)
|
45
|
+
(klass.try(:settable_params) || {}).merge(options.extract!(:model_key, :params_key, :scope_to))
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def _handle_scopes(klass, scopes)
|
50
|
+
return klass if scopes.blank?
|
51
|
+
|
52
|
+
scopes = [scopes] unless scopes.is_a?(Array)
|
53
|
+
scopes.each { |scope| klass = klass.public_send(scope) }
|
54
|
+
|
55
|
+
klass
|
35
56
|
end
|
36
57
|
end
|
37
58
|
|
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.2.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-02-
|
11
|
+
date: 2024-02-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|