aeonscope-btech_rest 0.5.2 → 0.5.3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +4 -0
- data/VERSION.yml +1 -1
- data/lib/actions.rb +32 -14
- metadata +1 -1
data/CHANGELOG.rdoc
CHANGED
@@ -40,4 +40,8 @@
|
|
40
40
|
= v0.5.x
|
41
41
|
|
42
42
|
* Fixed a bug in the disabled_actions class method where unique actions were returning as a null object.
|
43
|
+
* Added graceful handling of resources with no models as there are times where you can have a RESTful controller with no associated model.
|
44
|
+
* Renamed the render_new_or_edit method to render_new_or_edit_partial (see actions.rb).
|
45
|
+
* Added a render_action_partial method for generic rendering of action partials (see actions.rb).
|
46
|
+
|
43
47
|
|
data/VERSION.yml
CHANGED
data/lib/actions.rb
CHANGED
@@ -11,28 +11,31 @@ module BTech
|
|
11
11
|
records = parent.instance_eval("#{@resources.last[:parent_resource_method] || @resources.last[:name]}").paginate :page => params[:page], :per_page => 10
|
12
12
|
else
|
13
13
|
# Records for single resource.
|
14
|
-
records_name = get_model_name
|
15
|
-
|
14
|
+
records_name = get_model_name
|
15
|
+
if records_name
|
16
|
+
records_name = records_name.pluralize
|
17
|
+
records = @resources.last[:model].all.paginate(:page => params[:page], :per_page => 10)
|
18
|
+
end
|
16
19
|
end
|
17
|
-
instance_variable_set "@#{records_name}", records
|
20
|
+
instance_variable_set "@#{records_name}", records if records_name
|
18
21
|
end
|
19
22
|
|
20
23
|
# Default show action. Feel free to override.
|
21
24
|
def show
|
22
25
|
build_resources
|
23
|
-
|
26
|
+
render_action_partial @resources.last[:show_partial]
|
24
27
|
end
|
25
28
|
|
26
29
|
# Default new action. Feel free to override.
|
27
30
|
def new
|
28
31
|
build_resources
|
29
|
-
|
32
|
+
render_new_or_edit_partial
|
30
33
|
end
|
31
34
|
|
32
35
|
# Default edit action. Feel free to override.
|
33
36
|
def edit
|
34
37
|
build_resources
|
35
|
-
|
38
|
+
render_new_or_edit_partial
|
36
39
|
end
|
37
40
|
|
38
41
|
# Default create action. Feel free to override.
|
@@ -41,7 +44,7 @@ module BTech
|
|
41
44
|
if get_record.update_attributes params[get_model_symbol]
|
42
45
|
redirect_to build_resource_url(@resources)
|
43
46
|
else
|
44
|
-
|
47
|
+
render_new_or_edit_partial
|
45
48
|
end
|
46
49
|
end
|
47
50
|
|
@@ -51,7 +54,7 @@ module BTech
|
|
51
54
|
if get_record.update_attributes params[get_model_symbol]
|
52
55
|
redirect_to build_resource_url(@resources)
|
53
56
|
else
|
54
|
-
|
57
|
+
render_new_or_edit_partial
|
55
58
|
end
|
56
59
|
end
|
57
60
|
|
@@ -65,9 +68,20 @@ module BTech
|
|
65
68
|
|
66
69
|
protected
|
67
70
|
|
71
|
+
# Convenience method for rendering the action partials.
|
72
|
+
def render_action_partial partial
|
73
|
+
symbol = get_model_symbol
|
74
|
+
record = get_record
|
75
|
+
if symbol && record
|
76
|
+
render :partial => partial, :layout => true, :locals => {symbol => record, :resources => @resources}
|
77
|
+
else
|
78
|
+
render :partial => partial, :layout => true, :locals => {:resources => @resources}
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
68
82
|
# Convenience method for rendering the new or edit partial.
|
69
|
-
def
|
70
|
-
|
83
|
+
def render_new_or_edit_partial
|
84
|
+
render_action_partial @resources.last[:new_or_edit_partial]
|
71
85
|
end
|
72
86
|
|
73
87
|
# Builds the RESTful parent URL based on an array of resources.
|
@@ -104,17 +118,20 @@ module BTech
|
|
104
118
|
|
105
119
|
# Answers the name of the current model.
|
106
120
|
def get_model_name
|
107
|
-
@resources.last[:model]
|
121
|
+
model = @resources.last[:model]
|
122
|
+
model ? model.name.underscore : nil
|
108
123
|
end
|
109
124
|
|
110
125
|
# Answers the symbol of the current model.
|
111
126
|
def get_model_symbol
|
112
|
-
get_model_name
|
127
|
+
name = get_model_name
|
128
|
+
name ? name.to_sym : nil
|
113
129
|
end
|
114
130
|
|
115
131
|
# Answers the current record (a.k.a. the record of the last resource).
|
116
132
|
def get_record
|
117
|
-
|
133
|
+
name = get_model_name
|
134
|
+
name ? instance_variable_get("@#{name}") : nil
|
118
135
|
end
|
119
136
|
|
120
137
|
# Builds all resources for the controller(s).
|
@@ -126,7 +143,8 @@ module BTech
|
|
126
143
|
@resources.reverse!
|
127
144
|
end
|
128
145
|
# Convenience for accessing the current record.
|
129
|
-
|
146
|
+
name = get_model_name
|
147
|
+
instance_variable_set "@#{name}", @resources.last[:record] if name
|
130
148
|
end
|
131
149
|
|
132
150
|
# Convenience method for answering back a properly camelized controller name.
|