basic_crud 0.1.0 → 0.1.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.
- checksums.yaml +4 -4
- data/README.md +13 -0
- data/lib/basic_crud/version.rb +1 -1
- data/lib/basic_crud.rb +21 -4
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bd4361924d1d993eb66ebea4283ca79fd5d3d5f3d3a53176dac79fcd93defa1c
|
4
|
+
data.tar.gz: 75d132c5330c070189c29e774ff7b7472795e61671ae8015def8e158c3b1c038
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 29122da93b7e313991bad8ddd8e2c32c312904ed6b92462adfe1f833321843d72d38f33a1eddc5e24a8771e6a18a087a86630a0591c1d1ffbe6f727332d8cfc0
|
7
|
+
data.tar.gz: e7b5127da9dfb9f86df9b19a08483f857639924953d3f5f7f5c2263709f48525ae2cd28f08115d19d4518bec53faf7ceef0d4dcf368350687a28a4f355ceeb42
|
data/README.md
CHANGED
@@ -24,11 +24,24 @@ In the (empty) controller, include basic_crud
|
|
24
24
|
```ruby
|
25
25
|
class ApplicationController < ActionController::Base
|
26
26
|
include BasicCrud
|
27
|
+
|
27
28
|
end
|
28
29
|
```
|
29
30
|
In this example, all controllers will inherit BasicCrud functionality.
|
30
31
|
You can also just include the Module into certain controllers.
|
31
32
|
|
33
|
+
Controllers must override the `restricted_params` method and permit params, e.g.:
|
34
|
+
```ruby
|
35
|
+
class ExampleController < ActionController::Base
|
36
|
+
include BasicCrud
|
37
|
+
|
38
|
+
def restricted_params
|
39
|
+
params.require(:example).permit(:name, :thingy)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
```
|
43
|
+
https://edgeguides.rubyonrails.org/action_controller_overview.html#strong-parameters
|
44
|
+
|
32
45
|
Objects are stored in `@record` or `@records` and accessible in the views.
|
33
46
|
|
34
47
|
If you need additional functionality, e.g. special logic for your index page, just override `def index .. end`.
|
data/lib/basic_crud/version.rb
CHANGED
data/lib/basic_crud.rb
CHANGED
@@ -4,6 +4,8 @@ module BasicCrud
|
|
4
4
|
def index
|
5
5
|
@records = model.all
|
6
6
|
|
7
|
+
yield if block_given?
|
8
|
+
|
7
9
|
respond_to do |format|
|
8
10
|
format.html { render :index }
|
9
11
|
format.json { render json: @records }
|
@@ -13,6 +15,8 @@ module BasicCrud
|
|
13
15
|
def show
|
14
16
|
@record = fetch_record_by_param
|
15
17
|
|
18
|
+
yield if block_given?
|
19
|
+
|
16
20
|
respond_to do |format|
|
17
21
|
format.html { render :show }
|
18
22
|
format.json { render json: @record }
|
@@ -21,18 +25,22 @@ module BasicCrud
|
|
21
25
|
|
22
26
|
def edit
|
23
27
|
@record = fetch_record_by_param
|
28
|
+
yield if block_given?
|
24
29
|
end
|
25
30
|
|
26
31
|
def new
|
27
32
|
@record = model.new
|
33
|
+
yield if block_given?
|
28
34
|
end
|
29
35
|
|
30
36
|
def create
|
31
37
|
@record = model.new(restricted_params)
|
38
|
+
@notice = "#{model} was successfully created."
|
39
|
+
yield if block_given?
|
32
40
|
|
33
41
|
respond_to do |format|
|
34
42
|
if @record.save
|
35
|
-
format.html { redirect_to action: :index, notice:
|
43
|
+
format.html { redirect_to action: :index, notice: @notice }
|
36
44
|
format.json { render @record, status: :created }
|
37
45
|
else
|
38
46
|
format.html { render :new }
|
@@ -43,9 +51,13 @@ module BasicCrud
|
|
43
51
|
|
44
52
|
def update
|
45
53
|
@record = fetch_record_by_param
|
54
|
+
@notice = "#{model} was successfully updated."
|
55
|
+
|
56
|
+
yield if block_given?
|
57
|
+
|
46
58
|
respond_to do |format|
|
47
59
|
if @record.update(restricted_params)
|
48
|
-
format.html { redirect_to @record, notice:
|
60
|
+
format.html { redirect_to @record, notice: @notice }
|
49
61
|
format.json { render @record, status: :ok }
|
50
62
|
else
|
51
63
|
format.html { render :edit }
|
@@ -56,9 +68,13 @@ module BasicCrud
|
|
56
68
|
|
57
69
|
def destroy
|
58
70
|
@record = fetch_record_by_param
|
71
|
+
@notice = "#{model} was successfully destroyed."
|
72
|
+
|
73
|
+
yield if block_given?
|
74
|
+
|
59
75
|
@record.destroy
|
60
76
|
respond_to do |format|
|
61
|
-
format.html { redirect_to action: :index, notice:
|
77
|
+
format.html { redirect_to action: :index, notice: @notice }
|
62
78
|
format.json { head :no_content }
|
63
79
|
end
|
64
80
|
end
|
@@ -75,6 +91,7 @@ module BasicCrud
|
|
75
91
|
|
76
92
|
# Override this method to allow model attributes
|
77
93
|
def restricted_params
|
78
|
-
params.require(self.controller_name.classify.underscore.to_sym).permit([])
|
94
|
+
#params.require(self.controller_name.classify.underscore.to_sym).permit([])
|
95
|
+
raise("No strong params set, override restricted_params method in your controller. E.g. params.require(:model).permit(:attribute1, :attribute2)")
|
79
96
|
end
|
80
97
|
end
|