rails_settlement 1.0.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 59429b5aef8ee3fb19702c66a17ddba2240017bfa215ef0799483ee076f2b64c
4
- data.tar.gz: 3d153d543d3010e5c067eb9d79bf8c3feb188c6b1c82ec7ca41dee1e6ebba0e1
3
+ metadata.gz: 5f6997e50cad74b6bfb65514e1efc59a454db83eca5d903fed3cee4ae35326aa
4
+ data.tar.gz: 0f372b19be2e0f3614c9c55b01394d38b6aebb915bf9905857b6cc5c746266a5
5
5
  SHA512:
6
- metadata.gz: 812f2ab5b2974f245af355d5cfdb68b71b1069437c0e5a2e70911f01a44c890acec96bd25aa1d9b031e756090aba9a1961eba113a6b7d17db3aac742fb62d43f
7
- data.tar.gz: f8bb4d5f49e407dda8ee652651a03d3bbcb78fcd1f0173d17196635638c55203bacde0ee84f4dcf392c06faa188b525a27469a8be24c850b9344ddc0148ba31a
6
+ metadata.gz: 8bf231039431dbf2c302aab711be354ef8373cbf9570cf053d32100e3f3660b2399a9067f8be965c2dd553b303f4333a5fe06fe2ebe031ed12e54523bce86b78
7
+ data.tar.gz: 881deb50b91a6628596d9767a55ad4e57a15cf6055a8452d1a2275f0bd051b84eba7aa8bf429629e306fdd4b79073112a939abb73460f350c8b1a55262b0df1c
data/README.md CHANGED
@@ -9,31 +9,44 @@ Add to your Gemfile:
9
9
  gem 'rails_settlement'
10
10
  ```
11
11
 
12
- ## How to User
12
+ ## How to Use
13
13
 
14
- Let's suppose you want to fetch a `User` before performing a controller action:
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] # Internally, it's just a before_action callback, so you can pass options to the callback directly.
19
- set_user! only: %i[edit] # Using this method with a bang (!) would trigger an ActiveRecord::RecordNotFound Exception.
18
+ set_user only: %i[show] # It's essentially a before_action callback, allowing you to pass options directly.
20
19
 
21
- ...
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
27
+
28
+ def show
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!)
30
+ end
22
31
  end
23
32
  ```
24
33
 
25
34
  ```ruby
26
35
  class User < ApplicationRecord
27
- # This method would be used internally by set_user to fetch correct param (using params_key), and to send a key to #find_by (using model_key)
28
- # This can be also overridden on the fly by passing any of these two keys (or both!) in the controller directly
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.
29
40
  # Ex: set_user params_key: :user_username, only: %i[index], ...
41
+ # You can also pass a `scope_to` option in the hash below.
30
42
  def self.settable_params
31
43
  { params_key: :username, model_key: :username }
32
44
  end
33
45
  end
34
46
  ```
35
47
 
36
- This way you can use any model instead of a `User`. Just follow the naming convention `set_[model_name_in_snake_case]!`.
37
- P.S: This should not work with STI or any Namespaced models. If you have a way to make them work, please raise a pull request!
48
+ This approach allows you to use any model instead of a `User`. Just follow the naming convention set_[model_name_in_snake_case]!.
49
+
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!
38
51
 
39
52
  That's about it!
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailsSettlement
4
- VERSION = "1.0.0"
4
+ VERSION = "1.2.0"
5
5
  end
@@ -12,16 +12,14 @@ module RailsSettlement
12
12
 
13
13
  class_methods do
14
14
  def method_missing(method, **options)
15
- super unless (match_data = method.to_s.match(SETTABLE_REGEX))
15
+ super unless (matches = method.to_s.match(SETTABLE_REGEX))
16
16
 
17
- variable = match_data[:object]
18
- raisable = match_data[:raisable].present?
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,9 +30,32 @@ 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
 
38
- ActiveSupport.on_load(:action_controller_base) do
59
+ ActiveSupport.on_load(:action_controller) do
39
60
  include RailsSettlement
40
61
  end
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.0.0
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-10 00:00:00.000000000 Z
11
+ date: 2024-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord