implicit_resource 0.0.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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/implicit_resource.rb +98 -0
  3. metadata +43 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b8dc570c2c5edb6829f36082a1a55640a9eb28cf6327130f6bf71b8507b6eab4
4
+ data.tar.gz: 0aead15c4e3f4f49c5459c4c93ef090f4f9e179afefe4c04dd0ea5383679e89a
5
+ SHA512:
6
+ metadata.gz: 0f760d816d04c14e19a54972dad0802b79aa54be69d5170f0e9edf9060b96a5f8fac7208d7b8e81d3c061c225bc2873712a9d1a512932a2c7c16e45ec31356ba
7
+ data.tar.gz: 8e8a7c20ac40524bb3460adc5dc6bf8b3d90d66d05d9b2a424af2c14a7e5a3eccf8a1822eb9245f333b78419999edb701a288f0fbe7fd415e86cbbf78b30726e
@@ -0,0 +1,98 @@
1
+ module ImplicitResource
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ %i[index show destroy].each do |action|
6
+ define_method(action) do
7
+ response_object = (action == :index ? collection : act_on_resource)
8
+ respond_with(response_object)
9
+ end
10
+ end
11
+
12
+ %i[create update].each do |action|
13
+ define_method(action) do
14
+ if act_on_resource.errors.empty?
15
+ respond_with('', location: resource_location)
16
+ else
17
+ respond_with(act_on_resource)
18
+ end
19
+ end
20
+ end
21
+
22
+ helper_method :collection
23
+ helper_method :resource
24
+ end
25
+
26
+ protected
27
+
28
+ def resource_location
29
+ (namespace << act_on_resource)
30
+ end
31
+
32
+ def namespace
33
+ @namespace ||= controller_path.split('/')[0..-2].map(&:to_sym)
34
+ end
35
+
36
+ def model_klass
37
+ @model_klass ||= controller_name.classify.safe_constantize
38
+ end
39
+
40
+ def collection
41
+ @collection ||= policy_scope(apply_scopes(model_klass))
42
+ end
43
+
44
+ def resource
45
+ @resource ||= if params[:id].present?
46
+ collection.find(params[:id])
47
+ else
48
+ collection.new
49
+ end
50
+ end
51
+
52
+ # If the resource references user(s) go ahead and set with the current user
53
+ def assign_current_user
54
+ return unless current_user && resource.new_record?
55
+ resource.users << current_user if resource.respond_to?(:users=)
56
+ resource.user ||= current_user if resource.respond_to?(:user=)
57
+ end
58
+
59
+ # Assign some common associations if needed. This
60
+ # needs to be done before authorization
61
+ #
62
+ def bootstrap_resource
63
+ resource.tap do
64
+ assign_current_organization
65
+ assign_current_user
66
+ end
67
+ end
68
+
69
+ # Persist the passed in resource to the database
70
+ #
71
+ def persist_resource
72
+ return unless %w[create update].include?(action_name)
73
+
74
+ ActiveRecord::Base.transaction do
75
+ resource.attributes = permitted_attributes(resource)
76
+ resource.save
77
+ end
78
+ end
79
+
80
+ # Delete the given resource from the database
81
+ #
82
+ def destroy_resource
83
+ resource.destroy! if action_name == 'destroy'
84
+ end
85
+
86
+ # Build a resource based on contextual info
87
+ #
88
+ def act_on_resource
89
+ @act_on_resource ||= resource.tap do
90
+ authorize(resource)
91
+
92
+ unless destroy_resource
93
+ bootstrap_resource
94
+ persist_resource
95
+ end
96
+ end
97
+ end
98
+ end
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: implicit_resource
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Mike Kelley
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-03-29 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Rails Controller Boilerplate
14
+ email: mike@codezombie.org
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/implicit_resource.rb
20
+ homepage: https://rubygems.org/gems/implicit_resource
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubygems_version: 3.4.19
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: ImplicitResource
43
+ test_files: []