aeonscope-btech_rest 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.
- data/CHANGELOG.rdoc +5 -0
- data/LICENSE.rdoc +20 -0
- data/README.rdoc +114 -0
- data/VERSION.yml +4 -0
- data/lib/actions.rb +195 -0
- data/lib/btech_rest.rb +14 -0
- data/lib/class_methods.rb +37 -0
- data/lib/resource_helper.rb +78 -0
- data/test/btech_rest_test.rb +7 -0
- data/test/test_helper.rb +10 -0
- metadata +74 -0
data/CHANGELOG.rdoc
ADDED
data/LICENSE.rdoc
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008, 2009 Berserk Technologies
|
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.rdoc
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
= Overview
|
2
|
+
|
3
|
+
Easily adhere to DRY principals and add default REST functionality to your controllers. This means you can write code
|
4
|
+
like this:
|
5
|
+
|
6
|
+
class Posts < ApplicationController
|
7
|
+
include BTech::Rest
|
8
|
+
end
|
9
|
+
|
10
|
+
...and you will automatically have the following REST actions:
|
11
|
+
|
12
|
+
* index
|
13
|
+
* show
|
14
|
+
* new
|
15
|
+
* edit
|
16
|
+
* create
|
17
|
+
* update
|
18
|
+
* destroy
|
19
|
+
|
20
|
+
How is that for being DRY[http://en.wikipedia.org/wiki/DRY]? Read on to learn more.
|
21
|
+
|
22
|
+
= License
|
23
|
+
|
24
|
+
Copyright (c) 2008-2009 {Berserk Technologies}[http://www.berserktech.com].
|
25
|
+
See the included LICENSE[link:files/LICENSE_rdoc.html] file for more info.
|
26
|
+
|
27
|
+
= History
|
28
|
+
|
29
|
+
See the CHANGELOG[link:files/CHANGELOG_rdoc.html] file for more info.
|
30
|
+
|
31
|
+
= Requirements
|
32
|
+
|
33
|
+
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.
|
35
|
+
|
36
|
+
= Installation
|
37
|
+
|
38
|
+
Type the following from the command line to install:
|
39
|
+
|
40
|
+
* <b>MacOS/Linux</b>: sudo gem install btech_rest
|
41
|
+
* *Windows*: gem install btech_rest
|
42
|
+
|
43
|
+
Type the following from the command line to setup:
|
44
|
+
|
45
|
+
* script/generate rest_setup
|
46
|
+
|
47
|
+
= Usage
|
48
|
+
|
49
|
+
As mentioned in the overview, simply add the following line of code to your controller(s) to make them RESTful:
|
50
|
+
|
51
|
+
include BTech::Rest
|
52
|
+
|
53
|
+
Example:
|
54
|
+
|
55
|
+
class Posts < ApplicationController
|
56
|
+
include BTech::Rest
|
57
|
+
end
|
58
|
+
|
59
|
+
To customize the RESTful behavior of your controller, use any combination of these three macros:
|
60
|
+
|
61
|
+
* belongs_to[link:classes/BTech/Rest/ClassMethods.html] - Enables resource nesting where a controller can belong to a parent controller (i.e. http://www.example.com/posts/1/comments/2). This behavior is similar to the ActiveRecord belongs_to[http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/belongs_to/] macro.
|
62
|
+
* resource_options[link:classes/BTech/Rest/ClassMethods.html] - 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.
|
63
|
+
* disabled_actions[link:classes/BTech/Rest/ClassMethods.html] - Allows you to disable any of the default REST actions. Follow the link to learn more.
|
64
|
+
|
65
|
+
Example:
|
66
|
+
|
67
|
+
class Comments < ApplicationController
|
68
|
+
include BTech::Rest
|
69
|
+
belongs_to :posts
|
70
|
+
resource_options :label => "My Wicked Comments"
|
71
|
+
disabled_actions :show, :destroy
|
72
|
+
end
|
73
|
+
|
74
|
+
Here is the breakdown, line-by-line, of the example shown above:
|
75
|
+
|
76
|
+
1. Enables restful behavior.
|
77
|
+
2. Identifies the controller as a nested resource of posts.
|
78
|
+
3. Instead of using the default label "Comments", a customized label of "My Wicked Comments" is used instead.
|
79
|
+
4. The "show" and "destroy" actions are disabled which means only the following actions will work: index, new, edit, create, and update.
|
80
|
+
|
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):
|
83
|
+
|
84
|
+
* +parent_key+ = N/A
|
85
|
+
* +parent_value+ = N/A
|
86
|
+
* +parent_resource_method+ = N/A
|
87
|
+
* +name+ = "posts"
|
88
|
+
* +label+ = "Posts"
|
89
|
+
* +controller+ = PostsController
|
90
|
+
* +model+ = Post
|
91
|
+
* +record+ = #<Post id: 1, label: "Test", content: "Test", created_at: "2008-10-31 23:59:28", updated_at: "2008-10-31 23:59:28">
|
92
|
+
* +namespaces+ = []
|
93
|
+
* +show_partial+ = "/posts/show"
|
94
|
+
* +new_or_edit_partial+ = "/posts/new_or_edit"
|
95
|
+
|
96
|
+
The comment (child) resource would have the following values:
|
97
|
+
|
98
|
+
* +parent_key+ = post_id
|
99
|
+
* +parent_value+ = 1
|
100
|
+
* +parent_resource_method+ = N/A
|
101
|
+
* +name+ = "comments"
|
102
|
+
* +label+ = "My Wicked Comments"
|
103
|
+
* +controller+ = CommentsController
|
104
|
+
* +model+ = Comment
|
105
|
+
* +record+ = #<Post id: 1, post_id: nil, label: "Test", content: "Test", created_at: "2008-10-31 23:59:28", updated_at: "2008-10-31 23:59:28">
|
106
|
+
* +namespaces+ = []
|
107
|
+
* +show_partial+ = "/comments/show"
|
108
|
+
* +new_or_edit_partial+ = "/comments/new_or_edit"
|
109
|
+
|
110
|
+
= Contact/Feedback/Issues
|
111
|
+
|
112
|
+
* {Berserk Technologies}[http://www.berserktech.com] - Company web site.
|
113
|
+
* Aeonscope[http://www.aeonscope.net] - Personal web site.
|
114
|
+
* Twitter[http://www.twitter.com/Aeonscope] - Short bursts of insight and/or noise.
|
data/VERSION.yml
ADDED
data/lib/actions.rb
ADDED
@@ -0,0 +1,195 @@
|
|
1
|
+
module BTech
|
2
|
+
module Rest
|
3
|
+
module Actions
|
4
|
+
# Default index action. Feel free to override.
|
5
|
+
def index
|
6
|
+
build_resources
|
7
|
+
if @resources.size > 1
|
8
|
+
# Records for a nested resource (act on the second-to-last parent).
|
9
|
+
parent = @resources[@resources.size - 2][:record]
|
10
|
+
@records = parent.instance_eval("#{@resources.last[:parent_resource_method] || @resources.last[:name]}").paginate :page => params[:page], :per_page => 10
|
11
|
+
else
|
12
|
+
# Records for single resource.
|
13
|
+
@records = @resources.last[:model].paginate :page => params[:page], :per_page => 10
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# Default show action. Feel free to override.
|
18
|
+
def show
|
19
|
+
build_resources
|
20
|
+
render :partial => @resources.last[:show_partial], :layout => true, :locals => {:record => @record, :resources => @resources}
|
21
|
+
end
|
22
|
+
|
23
|
+
# Default new action. Feel free to override.
|
24
|
+
def new
|
25
|
+
build_resources
|
26
|
+
render_new_or_edit
|
27
|
+
end
|
28
|
+
|
29
|
+
# Default edit action. Feel free to override.
|
30
|
+
def edit
|
31
|
+
build_resources
|
32
|
+
render_new_or_edit
|
33
|
+
end
|
34
|
+
|
35
|
+
# Default create action. Feel free to override.
|
36
|
+
def create
|
37
|
+
build_resources
|
38
|
+
if @record.update_attributes params[:record]
|
39
|
+
redirect_to build_resource_url(@resources)
|
40
|
+
else
|
41
|
+
render_new_or_edit
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
# Default update action. Feel free to override.
|
46
|
+
def update
|
47
|
+
build_resources
|
48
|
+
if @record.update_attributes params[:record]
|
49
|
+
redirect_to build_resource_url(@resources)
|
50
|
+
else
|
51
|
+
render_new_or_edit
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# Default destroy action. Feel free to override.
|
56
|
+
def destroy
|
57
|
+
build_resources
|
58
|
+
@record.destroy
|
59
|
+
@resources.last.delete :record
|
60
|
+
redirect_to build_resource_url(@resources)
|
61
|
+
end
|
62
|
+
|
63
|
+
protected
|
64
|
+
|
65
|
+
# Convenience method for rendering the new or edit partial.
|
66
|
+
def render_new_or_edit
|
67
|
+
render :partial => @resources.last[:new_or_edit_partial], :layout => true, :locals => {:record => @record, :resources => @resources}
|
68
|
+
end
|
69
|
+
|
70
|
+
# Builds the RESTful parent URL based on an array of resources.
|
71
|
+
def build_parent_url resources = []
|
72
|
+
namespaces = resources.last[:namespaces]
|
73
|
+
url = namespaces && !namespaces.empty? ? '/' + resources.last[:namespaces].join('/') : nil
|
74
|
+
if resources.size > 1
|
75
|
+
resources.slice(0...resources.size - 1).each do |resource|
|
76
|
+
url = [url, resource[:name], resource[:parent_id]].join('/')
|
77
|
+
end
|
78
|
+
end
|
79
|
+
url
|
80
|
+
end
|
81
|
+
|
82
|
+
# A convenience method for builing RESTful URLs based on an array of resources with the
|
83
|
+
# option to override the default action. This is also defined as a helper method (see base.rb).
|
84
|
+
def build_resource_url resources = [], action = :index
|
85
|
+
name = resources.last[:name]
|
86
|
+
id = resources.last[:record].id if resources.last[:record]
|
87
|
+
parent_url = build_parent_url(resources)
|
88
|
+
url = parent_url ? [parent_url, name].join('/') : '/' + name
|
89
|
+
case action
|
90
|
+
when :show then url = [url, id].compact.join('/')
|
91
|
+
when :new then url = [url, "new"].compact.join('/')
|
92
|
+
when :edit then url = [url, id, "edit"].compact.join('/')
|
93
|
+
when :update then url = [url, id].compact.join('/') if name == name.pluralize
|
94
|
+
when :destroy then url = [url, id].compact.join('/') if name == name.pluralize
|
95
|
+
end
|
96
|
+
logger.debug "Resource URL: #{url}"
|
97
|
+
url
|
98
|
+
end
|
99
|
+
|
100
|
+
private
|
101
|
+
|
102
|
+
# Builds all resources for the controller(s).
|
103
|
+
def build_resources
|
104
|
+
@resources ||= []
|
105
|
+
if @resources.empty?
|
106
|
+
controller_name = self.class.name
|
107
|
+
add_resource controller_name, @resources
|
108
|
+
@resources.reverse!
|
109
|
+
end
|
110
|
+
# Convenience for holding the current record and for form manipulation.
|
111
|
+
@record = @resources.last[:record]
|
112
|
+
end
|
113
|
+
|
114
|
+
# Convenience method for answering back a properly camelized controller name.
|
115
|
+
def camelize_controller_name name
|
116
|
+
name = name.capitalize.gsub "controller", "Controller"
|
117
|
+
name += "Controller" unless name.include? "Controller"
|
118
|
+
name
|
119
|
+
end
|
120
|
+
|
121
|
+
# Answers the child or parent controller namespaces (array) and name (string).
|
122
|
+
# Accepts the following argruments:
|
123
|
+
# * *full_name* - The fully namespaced controller (i.e. PostsController) or parent name (i.e. protected_pages).
|
124
|
+
# Usage:
|
125
|
+
# * Input: Public::PostsController, Output: [Public], "PostsController"
|
126
|
+
# * Input: member_manage_posts, Output: [Member, Manage], "PostsController"
|
127
|
+
# * Input: posts, Output: nil, "PostsController"
|
128
|
+
def get_controller_namespaces_and_name full_name
|
129
|
+
if full_name
|
130
|
+
delimiter = full_name.include?("Controller") ? "::" : '_'
|
131
|
+
if full_name.include? delimiter
|
132
|
+
namespaces = full_name.split delimiter
|
133
|
+
name = camelize_controller_name namespaces.pop
|
134
|
+
return namespaces.collect(&:capitalize), name
|
135
|
+
else
|
136
|
+
return nil, camelize_controller_name(full_name)
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
# Recursively walks the controller belongs_to hierarchy (if any) adding new resources along the way.
|
142
|
+
def add_resource controller_name, resources
|
143
|
+
logger.debug "Adding resource '#{controller_name}' to resources (size = #{resources.size})."
|
144
|
+
resource = configure_resource controller_name
|
145
|
+
if resource
|
146
|
+
resources << resource
|
147
|
+
# Recursively add parents (if any).
|
148
|
+
if resource[:controller].methods.include? "parent_resource"
|
149
|
+
add_resource resource[:controller].parent_resource, resources
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
# Configures a resource via belongs_to name refrence in controller. By default the controller and model
|
155
|
+
# are assumed to use the same root name regardless of singular or plural context.
|
156
|
+
def configure_resource controller_name
|
157
|
+
namespaces, name = get_controller_namespaces_and_name controller_name
|
158
|
+
controller = [namespaces, name].compact.join("::").constantize
|
159
|
+
name = name.chomp("Controller").underscore
|
160
|
+
parent_key = name.singularize + "_id"
|
161
|
+
resource = controller.resource_options || {}
|
162
|
+
resource.reverse_merge! :parent_key => parent_key,
|
163
|
+
:parent_id => params[parent_key],
|
164
|
+
:name => name,
|
165
|
+
:controller => controller,
|
166
|
+
:label => name.capitalize,
|
167
|
+
:namespaces => (namespaces.collect(&:downcase) if namespaces),
|
168
|
+
:show_partial => '/' + [namespaces, name, "show"].compact.join('/').downcase,
|
169
|
+
:new_or_edit_partial => '/' + [namespaces, name, "new_or_edit"].compact.join('/').downcase
|
170
|
+
begin
|
171
|
+
resource.reverse_merge! :model => name.singularize.camelize.constantize
|
172
|
+
rescue NameError
|
173
|
+
logger.warn "Unable to constantize: " + name
|
174
|
+
end
|
175
|
+
add_record resource
|
176
|
+
end
|
177
|
+
|
178
|
+
# Adds the current record to the resource based on position in chain.
|
179
|
+
def add_record resource
|
180
|
+
if resource[:model]
|
181
|
+
if params.include? resource[:parent_key]
|
182
|
+
# Nested parent.
|
183
|
+
resource[:record] = resource[:model].find resource[:parent_id] if resource[:parent_id]
|
184
|
+
else
|
185
|
+
# Single resource and/or end of nested chain.
|
186
|
+
resource[:record] = params[:id] ? resource[:model].find(params[:id]) : resource[:model].new
|
187
|
+
end
|
188
|
+
else
|
189
|
+
logger.error "Invalid model. Check that your controller and model are of the same name, otherwise specify the model as a resource option."
|
190
|
+
end
|
191
|
+
resource
|
192
|
+
end
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|
data/lib/btech_rest.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/class_methods.rb"
|
2
|
+
require File.dirname(__FILE__) + "/actions.rb"
|
3
|
+
require File.dirname(__FILE__) + "/resource_helper.rb"
|
4
|
+
|
5
|
+
module BTech
|
6
|
+
module Rest
|
7
|
+
def self.included base
|
8
|
+
base.extend ClassMethods
|
9
|
+
base.helper "resource"
|
10
|
+
base.helper_method :build_resource_url
|
11
|
+
end
|
12
|
+
include Actions
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module BTech
|
2
|
+
module Rest
|
3
|
+
module ClassMethods
|
4
|
+
# Allows an object to belong to a parent object, thus creating a nested hierarchy. This behaves in a similar
|
5
|
+
# fashion to the belongs_to ActiveRecord macro. Accepts the following parameters:
|
6
|
+
# * *parent* - The parent symbol (including namespaces delimited by underscores). Be sure to reflect singular or plural forms on the name. Example: Public::PostsController, Result: :public_posts
|
7
|
+
def belongs_to parent
|
8
|
+
@parent_resource = parent.to_s
|
9
|
+
class_eval "class << self; attr_accessor :parent_resource end"
|
10
|
+
end
|
11
|
+
|
12
|
+
# Allows one to specify the various options for a resource. The following options are accepted:
|
13
|
+
# * *parent_key* - The ID key of the parent resource (for nested resources only). Defaults to: <belongs_to name>_id
|
14
|
+
# * *parent_value* - The ID value of the parent resource (for nested resources only). Defaults to the record id.
|
15
|
+
# * *parent_resource_method* - The instance method to call for acquiring child resource(s) of the parent (for nested resources only). Example: A post with many comments would be post.comments. Defaults to child resource name.
|
16
|
+
# * *name* - The resource name. Defaults to the controller name.
|
17
|
+
# * *label* - The resource label. Defaults to the controller name with capitalization.
|
18
|
+
# * *controller* - The controller class. Defaults to the current class. You should not need to change this.
|
19
|
+
# * *model* - The model class. Defaults to a model of the same name as the controller (minus namespace, suffix, and pluralization of course).
|
20
|
+
# * *record* - The record (new or found) related to the resource.
|
21
|
+
# * *namespaces* - The namespaces (if any) for routing. Defaults to whatever namespace your controller is using.
|
22
|
+
# * *show_partial* - The show partial. Defaults to "/<namspace(s)>/<controller name>/_show".
|
23
|
+
# * *new_or_edit_partial* - The new or edit partial. Defaults to "/<namspace(s)>/<controller name>/_new_or_edit".
|
24
|
+
def resource_options options = {}
|
25
|
+
@resource_options = options
|
26
|
+
class_eval "class << self; attr_reader :resource_options end"
|
27
|
+
end
|
28
|
+
|
29
|
+
# Allows one to disable any number of default actions. Accepts the following parameters:
|
30
|
+
# * *actions* - A variable list of REST action symbols you wish to disable. You may use any of the following: index, show, new, create, edit, update, and/or destroy.
|
31
|
+
def disabled_actions *actions
|
32
|
+
actions.uniq!
|
33
|
+
actions.each {|action| undef_method(action)}
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
module ResourceHelper
|
2
|
+
# Displays breadcrumb navigation based on an array of hashes. Each resource
|
3
|
+
# hash is expected to have the following keys:
|
4
|
+
# * *id* - The resource ID (a.k.a the model ID).
|
5
|
+
# * *label* - The link label for display, otherwise the name is used instead.
|
6
|
+
# * *name* - The name of the resource (a.k.a. the controller). Also used as the link lable unless a label has been given.
|
7
|
+
# An optional hash of key/values can be supplied, here is what is allowed:
|
8
|
+
# * *separator* - The breadcrumb separator link, defaults to: '>'
|
9
|
+
def resource_breadcrumbs resources = [], options = {}
|
10
|
+
# Set defaults.
|
11
|
+
breadcrumbs = []
|
12
|
+
options.reverse_merge! :separator => "»"
|
13
|
+
# Process resources.
|
14
|
+
if resources.size > 0
|
15
|
+
url = '/' + resources.first[:namespaces].join('/')
|
16
|
+
parent_id = nil
|
17
|
+
resources.each do |resource|
|
18
|
+
url = [url, parent_id, resource[:name].pluralize].compact.join('/')
|
19
|
+
parent_id = resource[:parent_id]
|
20
|
+
breadcrumbs << link_to(resource[:label], url)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
content_for :breadcrumbs, content_tag(:div, :id => "breadcrumbs") {breadcrumbs.compact.join(" #{options[:separator]} ")}
|
24
|
+
end
|
25
|
+
|
26
|
+
# Builds the submit path for a new or edit form based on current controller action. This assumes
|
27
|
+
# that the new/create and edit/update forms are always submitted to the same URL. Accepts the following parameters:
|
28
|
+
# * *resources* - The array of resource hashes.
|
29
|
+
# * *html_options* - The html options for a form (same as form_for[http://apidock.com/rails/ActionView/Helpers/FormHelper/form_for] html options).
|
30
|
+
def build_resource_form_submit resources = [], html_options = {}
|
31
|
+
case params[:action]
|
32
|
+
# New/create flow.
|
33
|
+
when "new" then return :url => build_resource_url(resources, :create), :html => html_options
|
34
|
+
# Validate/create flow.
|
35
|
+
when "create" then return :url => build_resource_url(resources, :create), :html => html_options
|
36
|
+
# Edit/update flow.
|
37
|
+
when "edit" then return :url => build_resource_url(resources, :update), :html => html_options.merge({:method => :put})
|
38
|
+
# Validate/update flow.
|
39
|
+
when "update" then return :url => build_resource_url(resources, :update), :html => html_options.merge({:method => :put})
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# Builds a more descriptive label based on the current controller action. Accepts the following parameters:
|
44
|
+
# * *label* - The action label.
|
45
|
+
def build_action_label label
|
46
|
+
[params[:action].capitalize, label.singularize].compact.join ' '
|
47
|
+
end
|
48
|
+
|
49
|
+
# Shows an unobtrusive jQuery link where the UJS event is attached to the link via the "destroy" class.
|
50
|
+
# If JavaScript is disabled then the _show_ action will be called instead of the _destroy_ action. Accepts
|
51
|
+
# the following hash arguments:
|
52
|
+
# * *parent_id* - The ID of the parent element for which the link is a child of. Example: post_1. *NOTE:* The parent ID take precidence over the link ID if defined.
|
53
|
+
# * *id* - The link ID. Example: post_1_link. *NOTE:* The link ID _must_ include the parent ID.
|
54
|
+
# * *label* - The link label. Defaults to "Delete".
|
55
|
+
# * *url* - The REST destroy URL. Example: /posts/1. *NOTE:* The proper HTTP POST request will be handled by the JavaScript.
|
56
|
+
def show_destroy_link options = {}
|
57
|
+
options.reverse_merge! :label => "Delete", :class => "destroy"
|
58
|
+
options[:id] = options[:parent_id] + "_link" if options[:parent_id]
|
59
|
+
content_tag :a, options[:label], :href => options[:url], :id => options[:id], :class => options[:class]
|
60
|
+
end
|
61
|
+
|
62
|
+
# Shows an unobtrusive jQuery link based on an array of resource hashes. See the show_destroy_link above
|
63
|
+
# for further details. Accepts the following arguments:
|
64
|
+
# * *resources* - The array of resource hashes.
|
65
|
+
def show_destroy_resource_link resources
|
66
|
+
show_destroy_link :parent_id => resources.last[:parent_id], :url => build_resource_url(resources, :destroy)
|
67
|
+
end
|
68
|
+
|
69
|
+
# Shows edit and delete links for resources. *NOTE*: Normally the record being edited is not known when
|
70
|
+
# resources are first assembled. Make sure to add the record to the last resource prior to calling this
|
71
|
+
# method for correct results. Accepts the following parameters:
|
72
|
+
# * *resources* - The array of resource hashes.
|
73
|
+
def show_edit_and_destroy_resource_links resources
|
74
|
+
html = link_to "Edit", build_resource_url(resources, :edit)
|
75
|
+
html += " | "
|
76
|
+
html += show_destroy_resource_link resources
|
77
|
+
end
|
78
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: aeonscope-btech_rest
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brooke Kuhlmann
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-04-05 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: mislav-will_paginate
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.3.7
|
24
|
+
version:
|
25
|
+
description:
|
26
|
+
email: aeonscope@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- README.rdoc
|
33
|
+
- LICENSE.rdoc
|
34
|
+
files:
|
35
|
+
- CHANGELOG.rdoc
|
36
|
+
- LICENSE.rdoc
|
37
|
+
- README.rdoc
|
38
|
+
- VERSION.yml
|
39
|
+
- lib/actions.rb
|
40
|
+
- lib/btech_rest.rb
|
41
|
+
- lib/class_methods.rb
|
42
|
+
- lib/resource_helper.rb
|
43
|
+
- test/btech_rest_test.rb
|
44
|
+
- test/test_helper.rb
|
45
|
+
has_rdoc: true
|
46
|
+
homepage: http://github.com/aeonscope/btech-rest
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options:
|
49
|
+
- CHANGELOG.rdoc
|
50
|
+
- --inline-source
|
51
|
+
- --charset=UTF-8
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: 1.8.6
|
59
|
+
version:
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: "0"
|
65
|
+
version:
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 1.2.0
|
70
|
+
signing_key:
|
71
|
+
specification_version: 2
|
72
|
+
summary: Easily adhere to DRY principals and add default REST functionality to your controllers.
|
73
|
+
test_files: []
|
74
|
+
|