aeonscope-btech_rest 0.2.0 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE.rdoc +1 -1
- data/README.rdoc +23 -25
- data/VERSION.yml +1 -1
- data/lib/actions.rb +24 -12
- metadata +2 -2
data/LICENSE.rdoc
CHANGED
data/README.rdoc
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
= Overview
|
2
2
|
|
3
|
-
Easily adhere to DRY principals and add default REST functionality to your controllers.
|
4
|
-
like this:
|
3
|
+
Easily adhere to DRY principals and add default REST functionality to your controllers. This means you can write code like this:
|
5
4
|
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
class Posts < ApplicationController
|
6
|
+
include BTech::Rest
|
7
|
+
end
|
9
8
|
|
10
|
-
|
9
|
+
Which would automatically yield the following REST actions:
|
11
10
|
|
12
11
|
* index
|
13
12
|
* show
|
@@ -17,27 +16,27 @@ like this:
|
|
17
16
|
* update
|
18
17
|
* destroy
|
19
18
|
|
20
|
-
How is that for being DRY[http://en.wikipedia.org/wiki/DRY]?
|
19
|
+
How is that for being {DRY}[http://en.wikipedia.org/wiki/DRY]? Read on to learn more.
|
21
20
|
|
22
21
|
= License
|
23
22
|
|
24
23
|
Copyright (c) 2008-2009 {Berserk Technologies}[http://www.berserktech.com].
|
25
|
-
See the included LICENSE[link:
|
24
|
+
See the included {LICENSE}[link:LICENSE.rdoc] file for more info.
|
26
25
|
|
27
26
|
= History
|
28
27
|
|
29
|
-
See the CHANGELOG[link:
|
28
|
+
See the {CHANGELOG}[link:CHANGELOG.rdoc] file for more info.
|
30
29
|
|
31
30
|
= Requirements
|
32
31
|
|
33
32
|
1. Knowledge of the {Representational State Transfer (REST)}[http://en.wikipedia.com/wiki/REST]. Download and read {RESTful Rails}[http://www.b-simple.de/documents] if you need further info.
|
34
|
-
2. mislav-will_paginate[http://github.com/mislav/will_paginate/tree/master] gem. This is automatically installed/updated for you unless the correct version is detected.
|
33
|
+
2. {mislav-will_paginate}[http://github.com/mislav/will_paginate/tree/master] gem. This is automatically installed/updated for you unless the correct version is detected.
|
35
34
|
|
36
35
|
= Installation
|
37
36
|
|
38
37
|
Type the following from the command line to install:
|
39
38
|
|
40
|
-
*
|
39
|
+
* *MacOS/Linux*: sudo gem install btech_rest
|
41
40
|
* *Windows*: gem install btech_rest
|
42
41
|
|
43
42
|
Type the following from the command line to setup:
|
@@ -52,24 +51,24 @@ As mentioned in the overview, simply add the following line of code to your cont
|
|
52
51
|
|
53
52
|
Example:
|
54
53
|
|
55
|
-
|
56
|
-
|
57
|
-
|
54
|
+
class Posts < ApplicationController
|
55
|
+
include BTech::Rest
|
56
|
+
end
|
58
57
|
|
59
58
|
To customize the RESTful behavior of your controller, use any combination of these three macros:
|
60
59
|
|
61
|
-
* belongs_to[link:
|
62
|
-
* resource_options[link:
|
63
|
-
* disabled_actions[link:
|
60
|
+
* {belongs_to}[link:lib/class_method.rb] - Enables resource nesting where a controller can belong to a parent controller. This behavior is similar to the ActiveRecord {belongs_to}[http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/belongs_to] macro.
|
61
|
+
* {resource_options}[link:lib/class_method.rb] - Allows you to customize the default behavior of your controller(s). There is a lot you can do with this, so follow the link and read the method comments to learn more.
|
62
|
+
* {disabled_actions}[link:lib/class_method.rb] - Allows you to disable any of the default REST actions. Follow the link to learn more.
|
64
63
|
|
65
64
|
Example:
|
66
65
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
66
|
+
class Comments < ApplicationController
|
67
|
+
include BTech::Rest
|
68
|
+
belongs_to :posts
|
69
|
+
resource_options :label => "My Wicked Comments"
|
70
|
+
disabled_actions :show, :destroy
|
71
|
+
end
|
73
72
|
|
74
73
|
Here is the breakdown, line-by-line, of the example shown above:
|
75
74
|
|
@@ -78,8 +77,7 @@ Here is the breakdown, line-by-line, of the example shown above:
|
|
78
77
|
3. Instead of using the default label "Comments", a customized label of "My Wicked Comments" is used instead.
|
79
78
|
4. The "show" and "destroy" actions are disabled which means only the following actions will work: index, new, edit, create, and update.
|
80
79
|
|
81
|
-
Using the post and comment controller relationship as defined above, we can break this relationship down even further.
|
82
|
-
The post (parent) resource would have the following values (in this case, all default values):
|
80
|
+
Using the post and comment controller relationship as defined above, we can break this relationship down even further. The post (parent) resource would have the following values (in this case, all default values):
|
83
81
|
|
84
82
|
* +parent_key+ = N/A
|
85
83
|
* +parent_value+ = N/A
|
data/VERSION.yml
CHANGED
data/lib/actions.rb
CHANGED
@@ -7,17 +7,20 @@ module BTech
|
|
7
7
|
if @resources.size > 1
|
8
8
|
# Records for a nested resource (act on the second-to-last parent).
|
9
9
|
parent = @resources[@resources.size - 2][:record]
|
10
|
-
|
10
|
+
records_name = parent.class.name.underscore.pluralize
|
11
|
+
records = parent.instance_eval("#{@resources.last[:parent_resource_method] || @resources.last[:name]}").paginate :page => params[:page], :per_page => 10
|
11
12
|
else
|
12
13
|
# Records for single resource.
|
13
|
-
|
14
|
+
records_name = @resources.last[:model].name.underscore.pluralize
|
15
|
+
records = @resources.last[:model].all.paginate(:page => params[:page], :per_page => 10)
|
14
16
|
end
|
17
|
+
instance_variable_set "@#{records_name}", records
|
15
18
|
end
|
16
19
|
|
17
20
|
# Default show action. Feel free to override.
|
18
21
|
def show
|
19
22
|
build_resources
|
20
|
-
render :partial => @resources.last[:show_partial], :layout => true, :locals => {:
|
23
|
+
render :partial => @resources.last[:show_partial], :layout => true, :locals => {model_to_sym(@resources.last[:model]) => get_record, :resources => @resources}
|
21
24
|
end
|
22
25
|
|
23
26
|
# Default new action. Feel free to override.
|
@@ -35,7 +38,7 @@ module BTech
|
|
35
38
|
# Default create action. Feel free to override.
|
36
39
|
def create
|
37
40
|
build_resources
|
38
|
-
if
|
41
|
+
if get_record.update_attributes params[model_to_sym(@resources.last[:model])]
|
39
42
|
redirect_to build_resource_url(@resources)
|
40
43
|
else
|
41
44
|
render_new_or_edit
|
@@ -45,7 +48,7 @@ module BTech
|
|
45
48
|
# Default update action. Feel free to override.
|
46
49
|
def update
|
47
50
|
build_resources
|
48
|
-
if
|
51
|
+
if get_record.update_attributes params[model_to_sym(@resources.last[:model])]
|
49
52
|
redirect_to build_resource_url(@resources)
|
50
53
|
else
|
51
54
|
render_new_or_edit
|
@@ -55,7 +58,7 @@ module BTech
|
|
55
58
|
# Default destroy action. Feel free to override.
|
56
59
|
def destroy
|
57
60
|
build_resources
|
58
|
-
|
61
|
+
get_record.destroy
|
59
62
|
@resources.last.delete :record
|
60
63
|
redirect_to build_resource_url(@resources)
|
61
64
|
end
|
@@ -64,7 +67,7 @@ module BTech
|
|
64
67
|
|
65
68
|
# Convenience method for rendering the new or edit partial.
|
66
69
|
def render_new_or_edit
|
67
|
-
render :partial => @resources.last[:new_or_edit_partial], :layout => true, :locals => {:
|
70
|
+
render :partial => @resources.last[:new_or_edit_partial], :layout => true, :locals => {model_to_sym(@resources.last[:model]) => get_record, :resources => @resources}
|
68
71
|
end
|
69
72
|
|
70
73
|
# Builds the RESTful parent URL based on an array of resources.
|
@@ -98,7 +101,11 @@ module BTech
|
|
98
101
|
end
|
99
102
|
|
100
103
|
private
|
101
|
-
|
104
|
+
|
105
|
+
def get_record
|
106
|
+
instance_variable_get "@#{@resources.last[:model].name.underscore}"
|
107
|
+
end
|
108
|
+
|
102
109
|
# Builds all resources for the controller(s).
|
103
110
|
def build_resources
|
104
111
|
@resources ||= []
|
@@ -108,12 +115,17 @@ module BTech
|
|
108
115
|
@resources.reverse!
|
109
116
|
end
|
110
117
|
# Convenience for holding the current record and for form manipulation.
|
111
|
-
|
112
|
-
end
|
113
|
-
|
118
|
+
instance_variable_set "@#{@resources.last[:model].name.underscore}", @resources.last[:record]
|
119
|
+
end
|
120
|
+
|
121
|
+
# Converts a model name to a symbol.
|
122
|
+
def model_to_sym model
|
123
|
+
model.name.underscore.to_sym
|
124
|
+
end
|
125
|
+
|
114
126
|
# Convenience method for answering back a properly camelized controller name.
|
115
127
|
def camelize_controller_name name
|
116
|
-
name = name.
|
128
|
+
name = name.gsub(/^(\w)/) {|c| c.capitalize}
|
117
129
|
name += "Controller" unless name.include? "Controller"
|
118
130
|
name
|
119
131
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: aeonscope-btech_rest
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brooke Kuhlmann
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-04-
|
12
|
+
date: 2009-04-10 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|