cruddy 0.3.0 → 0.3.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/lib/cruddy/controller.rb +132 -130
- data/lib/cruddy/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9cd6d5c002cf3bc7bb2f69dc6ac50a98b6baaf62
|
4
|
+
data.tar.gz: cad487e1435cbf4dfefb655c9cff0f61fc012406
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 16109537493706747f10f68d2d4efc93537c063017a87ef492eff8f37291fe728ceaafa0e475a65516f7c9f6c9398bba529cea5e7047fdf0bab30414cb63962f
|
7
|
+
data.tar.gz: ce88c9a91aa78877c81feb4ac31d0fb8cbaeb629abb98b367465582cf1a0fff6133c5952b2b581bb2728894d8f13b2ca5c7c393c594d6299ee8797dec0d7a27c
|
data/lib/cruddy/controller.rb
CHANGED
@@ -1,167 +1,169 @@
|
|
1
|
-
module Cruddy
|
2
|
-
module
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
1
|
+
module Cruddy
|
2
|
+
module Controller
|
3
|
+
module ClassMethods
|
4
|
+
# choose which actions the controller inherits
|
5
|
+
def actions(*methods)
|
6
|
+
methods_to_remove = [:index, :show, :new, :create, :edit, :update, :destroy].reject { |m| methods.include?(m) }
|
7
|
+
methods_to_remove.each do |method_to_remove|
|
8
|
+
undef_method method_to_remove
|
9
|
+
end
|
8
10
|
end
|
9
11
|
end
|
10
|
-
end
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
13
|
+
module InstanceMethods
|
14
|
+
# the class this controller manages
|
15
|
+
def resource_class
|
16
|
+
controller_name.classify.constantize
|
17
|
+
end
|
17
18
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
19
|
+
# the name of the resource collection
|
20
|
+
def resource_collection_name
|
21
|
+
controller_name
|
22
|
+
end
|
22
23
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
24
|
+
# the name of the singular resource
|
25
|
+
def resource_instance_name
|
26
|
+
controller_name.singularize
|
27
|
+
end
|
27
28
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
29
|
+
# get instance variable for singluar resource instance
|
30
|
+
def resource
|
31
|
+
instance_variable_get("@#{resource_instance_name}")
|
32
|
+
end
|
32
33
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
34
|
+
# set instance variable for singluar resource instance
|
35
|
+
def resource=(resource)
|
36
|
+
instance_variable_set("@#{resource_instance_name}", resource)
|
37
|
+
end
|
37
38
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
39
|
+
# get instance variable for collection of resource instances
|
40
|
+
def resources
|
41
|
+
instance_variable_get("@#{resource_collection_name}")
|
42
|
+
end
|
42
43
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
44
|
+
# set instance variable for collection of resource instances
|
45
|
+
def resources=(collection)
|
46
|
+
instance_variable_set("@#{resource_collection_name}", collection)
|
47
|
+
end
|
47
48
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
49
|
+
# require the resource and permit all params by default
|
50
|
+
def resource_params
|
51
|
+
params.require(resource_instance_name).permit!
|
52
|
+
end
|
52
53
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
54
|
+
# get the path to the collection of resources, including the namespace
|
55
|
+
def resource_collection_path
|
56
|
+
path_start = controller_path.gsub('/', '_')
|
57
|
+
"#{path_start}_path"
|
58
|
+
end
|
58
59
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
60
|
+
# get the path to a singular resource, including the namespace
|
61
|
+
def resource_instance_path
|
62
|
+
path_start = controller_path.gsub('/', '_').singularize
|
63
|
+
"#{path_start}_path"
|
64
|
+
end
|
64
65
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
66
|
+
# get the path to a singular resource
|
67
|
+
def resource_path(resource)
|
68
|
+
send("#{resource_instance_path}", resource)
|
69
|
+
end
|
69
70
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
71
|
+
# get the path to edit a singular resource
|
72
|
+
def edit_resource_path(resource)
|
73
|
+
send("edit_#{resource_instance_path}", resource)
|
74
|
+
end
|
74
75
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
76
|
+
# get the path to create a new singular resource
|
77
|
+
def new_resource_path
|
78
|
+
send("new_#{resource_instance_path}")
|
79
|
+
end
|
79
80
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
81
|
+
# get the path to index resources
|
82
|
+
def resources_path
|
83
|
+
send("#{resource_collection_path}")
|
84
|
+
end
|
84
85
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
86
|
+
# get the resource instance
|
87
|
+
def get_resource_instance
|
88
|
+
resource_class.find(params[:id])
|
89
|
+
end
|
89
90
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
91
|
+
# load the resource instance
|
92
|
+
def load_resource_instance
|
93
|
+
self.resource = get_resource_instance
|
94
|
+
end
|
94
95
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
96
|
+
# get the resource collection
|
97
|
+
def get_resource_collection
|
98
|
+
resource_class.all
|
99
|
+
end
|
99
100
|
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
101
|
+
# load the resource collection
|
102
|
+
def load_resource_collection
|
103
|
+
self.resources = get_resource_collection
|
104
|
+
end
|
104
105
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
106
|
+
# index action
|
107
|
+
def index
|
108
|
+
load_resource_collection
|
109
|
+
respond_with resources
|
110
|
+
end
|
110
111
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
112
|
+
# show action
|
113
|
+
def show
|
114
|
+
load_resource_instance
|
115
|
+
respond_with resource
|
116
|
+
end
|
116
117
|
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
118
|
+
# new action
|
119
|
+
def new
|
120
|
+
self.resource = resource_class.new
|
121
|
+
respond_with resource
|
122
|
+
end
|
122
123
|
|
123
|
-
|
124
|
-
|
125
|
-
|
124
|
+
# create action
|
125
|
+
def create
|
126
|
+
self.resource = resource_class.new(resource_params)
|
126
127
|
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
128
|
+
if resource.save
|
129
|
+
redirect_to resource_path(resource)
|
130
|
+
else
|
131
|
+
respond_with(resource) do |format|
|
132
|
+
format.html { render :new }
|
133
|
+
end
|
132
134
|
end
|
133
135
|
end
|
134
|
-
end
|
135
136
|
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
137
|
+
# edit action
|
138
|
+
def edit
|
139
|
+
load_resource_instance
|
140
|
+
respond_with resource
|
141
|
+
end
|
141
142
|
|
142
|
-
|
143
|
-
|
144
|
-
|
143
|
+
# update action
|
144
|
+
def update
|
145
|
+
load_resource_instance
|
145
146
|
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
147
|
+
if resource.update_attributes(resource_params)
|
148
|
+
redirect_to resource_path(resource)
|
149
|
+
else
|
150
|
+
respond_with(resource) do |format|
|
151
|
+
format.html { render :edit }
|
152
|
+
end
|
151
153
|
end
|
152
154
|
end
|
153
|
-
end
|
154
155
|
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
156
|
+
# destroy action
|
157
|
+
def destroy
|
158
|
+
load_resource_instance
|
159
|
+
resource.destroy
|
160
|
+
redirect_to resources_path
|
161
|
+
end
|
160
162
|
end
|
161
|
-
end
|
162
163
|
|
163
|
-
|
164
|
-
|
165
|
-
|
164
|
+
def self.included(receiver)
|
165
|
+
receiver.extend ClassMethods
|
166
|
+
receiver.send :include, InstanceMethods
|
167
|
+
end
|
166
168
|
end
|
167
169
|
end
|
data/lib/cruddy/version.rb
CHANGED