rails-rest-kit 0.1.0 → 0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9791aca5b378e9e0306e7f746c34842852d50c7be0c471237e6898e14767b097
4
- data.tar.gz: a37b7cd5667dff1eb33b1ba20d5a5f17ce017c1098f9df811fead1f6c9adb3f6
3
+ metadata.gz: bdce6fff4de34b5f24f476fc966128a63391f33182434e97523c64dba8a147b6
4
+ data.tar.gz: ed150eeb5e7d9de29569cf15dc9e9d25b5d30dd1905e53714f61a3e38ad2b8b5
5
5
  SHA512:
6
- metadata.gz: b1259eb65ad5841f432ab688bf18f6b7c884fd74b3590ae5ede627c94565fa50965d42c4317d38e14d48ca3f21c0352ef441df0ff0518ae02e11d0433048f257
7
- data.tar.gz: f217291537dde6c234aa3fbf03549ef8774de0fd34f68ea53fd0aabfcc75d34541adc7825e0880a136aefe4e5781bac2660889b777993a8e8e3b284ac95c316c
6
+ metadata.gz: fefb836097ddc75a27cff0a7cc4d0d803ca3b85264c5d1ba50b28c0aac07c5c9884f7650d23c6436f7bb4ae1604c939d276aaa6520703e50048ae95740e71352
7
+ data.tar.gz: 3903916b339d925e8169cf0cc08dd74acd34eb1bff0e16ca2d2f25c7d780c014959ab7cc2190c215753c88c81a9ac37501ad0338baa0d665f63dd1fdd6b90f5d
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright Brandon Woodruff
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,219 @@
1
+ # Rails Rest Kit
2
+ Rails Rest Kit is a comprehensive Rails gem that automates RESTful controller actions with built-in lifecycle callbacks and intelligent parameter permitting. It eliminates boilerplate code while providing a flexible, convention-over-configuration approach to building REST APIs.
3
+
4
+ ## Installation
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem "rails-rest-kit"
9
+ ```
10
+
11
+ And then execute:
12
+ ```bash
13
+ $ bundle install
14
+ ```
15
+
16
+ Or install it yourself as:
17
+ ```bash
18
+ $ gem install rails-rest-kit
19
+ ```
20
+
21
+ ## Usage
22
+ Include the `RailsRestKit::RestfulControllerActions` module in your controller. This will automatically provide callback hooks for your default REST actions (`index, show, new, create, edit, update, destroy`):
23
+
24
+ ```ruby
25
+ class UsersController < ApplicationController
26
+ include RailsRestKit::RestfulControllerActions
27
+ end
28
+ ```
29
+
30
+ You can hook into a specific callback by defining callback blocks for each hook:
31
+
32
+ ```ruby
33
+ class UsersController < ApplicationController
34
+ include RailsRestKit::RestfulControllerActions
35
+
36
+ before_index do
37
+ Rails.logger.info("Before index")
38
+ end
39
+
40
+ around_show do
41
+ Rails.logger.info("Around show")
42
+ end
43
+
44
+ after_create_valid do
45
+ Rails.logger.info("Succesfully created resource")
46
+ end
47
+ end
48
+ ```
49
+ You can reference all callback hooks here.
50
+
51
+ ### Permitters
52
+
53
+ You can use `RailsRestKit::Permitter` in your controller by including the `RailsRestKit::Helpers::PermitterHelper` or `RailsRestKit::RestfulControllerActions` in your controller:
54
+
55
+ ```ruby
56
+ class UsersController < ApplicationController
57
+ include RailsRestKit::Helpers::PermitterHelper
58
+ end
59
+ ```
60
+
61
+ You can define permitted params for a controller using the `RailsRestKit::Permitter` DSL:
62
+
63
+ ```ruby
64
+ class UsersController < ApplicationController
65
+ include RailsRestKit::Helpers::PermitterHelper
66
+
67
+ permit_resource :user do
68
+ attributes :name, :email
69
+ nested :address do
70
+ attributes :street, :city, :state
71
+ end
72
+ collection :posts do
73
+ attributes :content
74
+ end
75
+ end
76
+ end
77
+ ```
78
+
79
+ Then you can access permissions with the `permit_params` method:
80
+
81
+ ```ruby
82
+ class UsersController < ApplicationController
83
+ include RailsRestKit::Helpers::PermitterHelper
84
+
85
+ permit_resource :user do
86
+ attributes :name, :email
87
+ nested :address do
88
+ attributes :street, :city, :state
89
+ end
90
+ collection :posts do
91
+ attributes :content
92
+ end
93
+ end
94
+
95
+ # Incoming params:
96
+ # user: {
97
+ # name: "John Doe",
98
+ # email: "john@example.com",
99
+ # age: 27
100
+ # address: { street: "123 Main St", city: "Anytown", zip: 12345 },
101
+ # posts: [{ title: "First Post", content: "Hello" }]
102
+ # }
103
+ def test_action
104
+ permitted_user_params = permit_params(:user)
105
+ # Permitted params:
106
+ # {
107
+ # name: "John Doe",
108
+ # email: "john@example.com"
109
+ # address: { street: "123 Main St", city: "Anytown" },
110
+ # posts: [{ content: "Hello" }]
111
+ # }
112
+ end
113
+ end
114
+ ```
115
+
116
+ You can use the `required: true` flag to require resource params:
117
+
118
+ ```ruby
119
+ class UsersController < ApplicationController
120
+ include RailsRestKit::Helpers::PermitterHelper
121
+
122
+ permit_resource :user, required: true do
123
+ attributes :name, :email
124
+ nested :address do
125
+ attributes :street, :city, :state
126
+ end
127
+ collection :posts do
128
+ attributes :content
129
+ end
130
+ end
131
+
132
+ # Incoming params:
133
+ # user: {}
134
+ def test_action
135
+ permitted_user_params = permit_params(:user) # Raises a ActionController::ParameterMissing error
136
+ end
137
+ end
138
+ ```
139
+
140
+ ### Helpers
141
+
142
+ Rails Rest Kit provides a number of controller helper methods to use by including the `RailsRestKit::Helpers::ResourceHelper` or `RailsRestKit::RestfulControllerActions` in your controller:
143
+
144
+ ```ruby
145
+ class UsersController < ApplicationController
146
+ include RailsRestKit::Helpers::ResourceHelper
147
+ end
148
+ ```
149
+
150
+ Then you can call the setter helper methods to set resource instance variables:
151
+
152
+ ```js
153
+ // Users table
154
+ [
155
+ {
156
+ id: 1,
157
+ name: "John Doe",
158
+ email: "john@example.com",
159
+ },
160
+ {
161
+ id: 2,
162
+ name: "Jane Doe",
163
+ email: "jane@example.com",
164
+ }
165
+ ]
166
+ ```
167
+
168
+ ```ruby
169
+ class UsersController < ApplicationController
170
+ include RailsRestKit::Helpers::ResourceHelper
171
+
172
+ def test_index_action
173
+ set_users
174
+ Rails.logger.info(@users)
175
+ # [
176
+ # {
177
+ # id: 1,
178
+ # name: "John Doe",
179
+ # email: "john@example.com",
180
+ # },
181
+ # {
182
+ # id: 2,
183
+ # name: "Jane Doe",
184
+ # email: "jane@example.com",
185
+ # }
186
+ # ]
187
+ end
188
+
189
+ # Incoming params:
190
+ # { id: 1 }
191
+ def test_show_action
192
+ set_user
193
+ Rails.logger.info(@user)
194
+ # {
195
+ # id: 1,
196
+ # name: "John Doe",
197
+ # email: "john@example.com",
198
+ # }
199
+ end
200
+
201
+ def test_new_action
202
+ set_new_user
203
+ Rails.logger.info(@user)
204
+ # {
205
+ # id: nil,
206
+ # name: nil,
207
+ # email: nil,
208
+ # }
209
+ end
210
+ end
211
+ ```
212
+
213
+ You can see a detailed list of all helper methods here.
214
+
215
+ ## Contributing
216
+ We welcome contributions to Rails Rest Kit!
217
+
218
+ ## License
219
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -1,3 +1,5 @@
1
+ require "rails_rest_kit"
2
+
1
3
  module RailsRestKit
2
4
  class Engine < ::Rails::Engine
3
5
  end
@@ -9,8 +9,8 @@ module RailsRestKit
9
9
  end
10
10
 
11
11
  class_methods do
12
- def permit_resource(resource_name, &block)
13
- permitter.configure(resource_name, &block)
12
+ def permit_resource(resource_name, required: false, &block)
13
+ permitter.configure(resource_name, required: required, &block)
14
14
  end
15
15
  end
16
16
 
@@ -21,21 +21,4 @@ module RailsRestKit
21
21
  end
22
22
  end
23
23
  end
24
- end
25
-
26
- # permitter.configure :blog_post do
27
- # require :blog_post
28
- # attributes :title, :body
29
- # nested :author do
30
- # attributes :name, :email
31
- # nested :profile do
32
- # attributes :bio
33
- # end
34
- # end
35
- # collection :comments do
36
- # attributes :content
37
- # nested :user do
38
- # attributes :username
39
- # end
40
- # end
41
- # end
24
+ end
@@ -1,5 +1,5 @@
1
1
  module RailsRestKit
2
- module Helper
2
+ module Helpers
3
3
  module ResourceHelper
4
4
  extend ActiveSupport::Concern
5
5
 
@@ -19,6 +19,9 @@ module RailsRestKit
19
19
  instance_variable_set("@#{model_slug.pluralize}", model_class.all) if !instance_variable_get("@#{model_slug.pluralize}")
20
20
  instance_variable_get("@#{model_slug.pluralize}")
21
21
  end
22
+
23
+ # Instance method aliases for class methods
24
+ delegate :model_name, :resource_name, :model_slug, :resource_slug, :model_class, :resource_class, to: :class
22
25
  end
23
26
 
24
27
  class_methods do
@@ -7,8 +7,8 @@ module RailsRestKit
7
7
  end
8
8
 
9
9
  # Configure permitted attributes for a resource
10
- def configure(resource_name, &block)
11
- configurations[resource_name.to_s] = Configuration.new(&block)
10
+ def configure(resource_name, required: false, &block)
11
+ configurations[resource_name.to_s] = Configuration.new(required: required, &block)
12
12
  end
13
13
 
14
14
  # Get configuration for a specific resource
@@ -18,33 +18,28 @@ module RailsRestKit
18
18
 
19
19
  # Permit parameters for a specific resource
20
20
  def permit(resource_name, params)
21
- config = for(resource_name)
21
+ config = self.for(resource_name)
22
22
  raise ArgumentError, "No configuration found for resource: #{resource_name}" unless config
23
-
24
- config.permit(params)
23
+ if config.required
24
+ resource_params = params.require(resource_name)
25
+ else
26
+ resource_params = params[resource_name] || params
27
+ end
28
+ config.permit(resource_params)
25
29
  end
26
30
 
27
31
  # Configuration class for each resource
28
32
  class Configuration
29
- attr_reader :attributes, :nested_attributes, :collections
33
+ attr_reader :attributes, :nested_attributes, :collections, :required
30
34
 
31
- def initialize(&block)
35
+ def initialize(required: false, &block)
32
36
  @attributes = []
33
37
  @nested_attributes = {}
34
38
  @collections = {}
39
+ @required = required
35
40
  instance_eval(&block) if block_given?
36
41
  end
37
42
 
38
- # Specify required top-level keys
39
- def require(*keys)
40
- @required_keys.concat(keys.map(&:to_s))
41
- end
42
-
43
- # Specify expected top-level keys
44
- def expect(*keys)
45
- @expected_keys.concat(keys.map(&:to_s))
46
- end
47
-
48
43
  # Define basic attributes
49
44
  def attributes(*attrs)
50
45
  @attributes.concat(attrs.map(&:to_s))
@@ -62,25 +57,19 @@ module RailsRestKit
62
57
 
63
58
  # Permit parameters based on configuration
64
59
  def permit(params)
65
- filtered = params
66
-
67
- # Apply require and expect to params
68
- @required_keys.each { |key| filtered = filtered.require(key) }
69
- @expected_keys.each { |key| filtered = filtered.expect(key) }
70
-
71
- permitted = filtered.permit(@attributes)
60
+ permitted = params.permit(@attributes)
72
61
 
73
62
  # Handle nested attributes
74
63
  @nested_attributes.each do |nested_name, nested_config|
75
- if filtered[nested_name].present?
76
- permitted[nested_name] = nested_config.permit(filtered[nested_name])
64
+ if params[nested_name].present?
65
+ permitted[nested_name] = nested_config.permit(params[nested_name])
77
66
  end
78
67
  end
79
68
 
80
69
  # Handle collection attributes
81
70
  @collections.each do |collection_name, collection_config|
82
- if filtered[collection_name].present?
83
- permitted[collection_name] = filtered[collection_name].map do |item|
71
+ if params[collection_name].present?
72
+ permitted[collection_name] = params[collection_name].map do |item|
84
73
  collection_config.permit(item)
85
74
  end
86
75
  end
@@ -14,7 +14,7 @@ module RailsRestKit
14
14
 
15
15
  included do
16
16
  include RailsRestKit::Helpers::PermitterHelper
17
- include RailsRestKit::Helper::ResourceHelper
17
+ include RailsRestKit::Helpers::ResourceHelper
18
18
 
19
19
  define_restful_actions_and_callbacks
20
20
  end
@@ -74,34 +74,32 @@ module RailsRestKit
74
74
  end
75
75
 
76
76
  def default_destroy
77
+ resource = send("set_#{model_slug}")
78
+ run_callbacks(:destroy) do
79
+ resource.destroy
80
+ end
77
81
  end
78
82
 
79
- def self.define_restful_actions_and_callbacks
80
- # Get RESTful routes for this controller
81
- controller_routes = Rails.application.routes.routes.select do |route|
82
- route.defaults[:controller] == controller_name && route.defaults[:action].in?(RESTFUL_ACTIONS.keys)
83
- end
84
- controller_routes.each do |route|
85
- action = route.defaults[:action]
86
- lifecycle_hooks = RESTFUL_ACTIONS[action]
87
- # Define callbacks for the action
88
- define_callbacks(action)
89
- lifecycle_hooks.each do |hook|
90
- define_callbacks("#{action}_#{hook}")
91
- end
92
- %w[ before after around ].each do |callback|
93
- define_singleton_method("#{callback}_#{action}") do |*args, &block|
94
- set_callback(action, callback, &block)
83
+ class_methods do
84
+ def define_restful_actions_and_callbacks
85
+ RESTFUL_ACTIONS.each do |action, hooks|
86
+ define_callbacks(action)
87
+ hooks.each do |hook|
88
+ define_callbacks("#{action}_#{hook}")
95
89
  end
96
- lifecycle_hooks.each do |hook|
97
- define_singleton_method("#{callback}_#{action}_#{hook}") do |*args, &block|
90
+ %i[ before after around ].each do |callback|
91
+ define_singleton_method("#{callback}_#{action}") do |*args, &block|
98
92
  set_callback(action, callback, &block)
99
93
  end
94
+ hooks.each do |hook|
95
+ define_singleton_method("#{callback}_#{action}_#{hook}") do |*args, &block|
96
+ set_callback("#{action}_#{hook}", callback, &block)
97
+ end
98
+ end
99
+ end
100
+ define_method(action) do
101
+ send("default_#{action}")
100
102
  end
101
- end
102
- # Define the action method
103
- define_method(action) do
104
- send("default_#{action}")
105
103
  end
106
104
  end
107
105
  end
@@ -1,3 +1,3 @@
1
1
  module RailsRestKit
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,9 +1,9 @@
1
- require "rails-rest_kit/version"
2
- require "rails-rest_kit/helpers/resource_helper"
3
- require "rails-rest_kit/helpers/permitter_helper"
4
- require "rails-rest_kit/restful_controller_actions"
5
- require "rails-rest_kit/permitter"
6
- require "rails-rest_kit/engine" if defined?(Rails)
1
+ require "rails_rest_kit/version"
2
+ require "rails_rest_kit/helpers/resource_helper"
3
+ require "rails_rest_kit/helpers/permitter_helper"
4
+ require "rails_rest_kit/restful_controller_actions"
5
+ require "rails_rest_kit/permitter"
6
+ require "rails_rest_kit/engine" if defined?(Rails)
7
7
 
8
8
  module RailsRestKit
9
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-rest-kit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandon Woodruff
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-07-26 00:00:00.000000000 Z
11
+ date: 2025-07-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -62,16 +62,44 @@ dependencies:
62
62
  name: rspec
63
63
  requirement: !ruby/object:Gem::Requirement
64
64
  requirements:
65
- - - "~>"
65
+ - - ">="
66
66
  - !ruby/object:Gem::Version
67
67
  version: '3.0'
68
68
  type: :development
69
69
  prerelease: false
70
70
  version_requirements: !ruby/object:Gem::Requirement
71
71
  requirements:
72
- - - "~>"
72
+ - - ">="
73
73
  - !ruby/object:Gem::Version
74
74
  version: '3.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rspec-rails
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: 7.0.0
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: 7.0.0
89
+ - !ruby/object:Gem::Dependency
90
+ name: shoulda-matchers
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: 5.1.0
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: 5.1.0
75
103
  description: RailsRestKit provides a comprehensive solution for building RESTful Rails
76
104
  controllers with minimal boilerplate. It automatically generates standard CRUD actions
77
105
  (index, show, new, create, edit, update, destroy) with built-in lifecycle callbacks
@@ -84,6 +112,8 @@ executables: []
84
112
  extensions: []
85
113
  extra_rdoc_files: []
86
114
  files:
115
+ - MIT-LICENSE
116
+ - README.md
87
117
  - lib/rails_rest_kit.rb
88
118
  - lib/rails_rest_kit/engine.rb
89
119
  - lib/rails_rest_kit/helpers/permitter_helper.rb