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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2c8e94ec51585b2d83c118eadff000068b305f42e4517a92178ad5c4f6eaa71b
4
- data.tar.gz: d927ccfdfa4cf67c4ea577ef7fc9683ffd99e7643e9e52d8b3c05138a8f596e9
3
+ metadata.gz: fea25e5290b99af4236253654e98e65428fbb8845cd3952ed57bddbfbd877aa5
4
+ data.tar.gz: bf7b83e4dcd6cde28b6fbca6bf96db247ec07d75e4ea76ffa8caec1f775dc7e3
5
5
  SHA512:
6
- metadata.gz: 72504a32b9a063436ed05d73fa9e4f372e4339e63a42edb46f43064dfa8526206be729d203e0b428b2fe656ffa8cdc3387c2ced7772b792506df5f76c193869c
7
- data.tar.gz: ed367d3d88d16264714db216cc995e27b83346f4d24d9ecadc744df67b3aa7e92773ee9f3af977f4c778ec449172dee34ca16c1b3850984c50aef54d05bb6418
6
+ metadata.gz: 61f3e0eb9b9909119252dfda0cced54624da78f22c531bf6ae7b03cdd30e3be30485182fda294c83d01475b0df03e56ebd4ded6fdbd1875605e053a677b4dddf
7
+ data.tar.gz: c19ca70fec032ce9b3ffb0868bbc92d4f4322a0138e1e5240d11a5581820b26824ae0ec2a13ef284c301c18b3b7abcb1d1c249e1224c9f0f366007a0d19ebbcc
data/.rubocop.yml CHANGED
@@ -13,7 +13,10 @@ Layout/LineLength:
13
13
  Max: 150
14
14
 
15
15
  Metrics/AbcSize:
16
- Max: 22
16
+ Max: 23
17
+
18
+ Metrics/MethodLength:
19
+ Max: 15
17
20
 
18
21
  Style/Documentation:
19
22
  Enabled: false
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailsSettlement
4
- VERSION = "1.3.1"
4
+ VERSION = "1.4.0"
5
5
  end
@@ -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 = _handle_match_data(matches)
18
- klass = _klass(variable, **options.extract!(:namespace))
19
- param_options = _param_options(klass: klass, options: options)
20
- scoped_klass = _handle_scopes(klass: klass, scopes: param_options[:scope_to])
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 _handle_match_data(match_data)
39
+ def _rs_handle_match_data(match_data)
37
40
  [match_data[:object], match_data[:raisable].present?]
38
41
  end
39
42
 
40
- def _klass(variable, namespace: nil)
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 _param_options(klass:, options:)
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 _handle_scopes(klass:, scopes:)
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.3.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-02-22 00:00:00.000000000 Z
11
+ date: 2024-03-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord