aeonscope-btech_rest 0.4.2 → 0.4.4

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 CHANGED
@@ -23,5 +23,10 @@
23
23
 
24
24
  = v0.4.x
25
25
 
26
- * Cleaned up the summary and descriptions for the gem, README, and GitHub data.
27
- * Added partial UJS support for new/edit record actions. Nested fields are supported as well.
26
+ * Synchronized the summary and descriptions for the gem, README, and GitHub data.
27
+ * Added UJS support for nested new/edit actions.
28
+ * Added UJS support for nested delete actions.
29
+ * Changed the show_destroy_link helper so that the default link ID is suffixed with "_destroy" instead of "_link".
30
+ * Added a build_dom_id helper. Works the same as the dom_id found in Rails except that new records return a '_0' suffix instead of a "new_" prefix.
31
+ * Updated the show_destroy_link helper so that when a url option is not supplied an anchor link is used instead (derived from the record ID).
32
+ * Updated all edit and destroy resource helpers so that they accept the parent DOM ID for which to apply UJS manipulation too.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :minor: 4
3
- :patch: 2
3
+ :patch: 4
4
4
  :major: 0
data/lib/actions.rb CHANGED
@@ -125,7 +125,7 @@ module BTech
125
125
  add_resource controller_name, @resources
126
126
  @resources.reverse!
127
127
  end
128
- # Convenience for holding the current record and for form manipulation.
128
+ # Convenience for accessing the current record.
129
129
  instance_variable_set "@#{get_model_name}", @resources.last[:record]
130
130
  end
131
131
 
@@ -22,6 +22,13 @@ module ResourceHelper
22
22
  end
23
23
  content_for :breadcrumbs, content_tag(:div, :id => "breadcrumbs") {breadcrumbs.compact.join(" #{options[:separator]} ")}
24
24
  end
25
+
26
+ # Builds a DOM ID for a given record. Works the same as the dom_id helper found in Rails except that it returns a record ID with
27
+ # a "_0" suffix for new records instead of a "new_" prefix.
28
+ def build_dom_id record
29
+ name = record.class.name.underscore
30
+ record.new_record? ? name + "_0" : name + '_' + record.id.to_s
31
+ end
25
32
 
26
33
  # Builds the submit path for a new or edit form based on current controller action. This assumes
27
34
  # that the new/create and edit/update forms are always submitted to the same URL. Accepts the following parameters:
@@ -54,25 +61,29 @@ module ResourceHelper
54
61
  # * *label* - The link label. Defaults to "Delete".
55
62
  # * *url* - The REST destroy URL. Example: /posts/1. *NOTE:* The proper HTTP POST request will be handled by the JavaScript.
56
63
  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]
64
+ options.reverse_merge! :id => options[:parent_id].to_s + "_destroy"
65
+ options.reverse_merge! :label => "Delete", :class => "destroy", :url => '#' + options[:id].to_s
66
+ link_to options[:label], options[:url], :id => options[:id], :class => options[:class]
60
67
  end
61
68
 
62
- # Shows an unobtrusive jQuery link based on an array of resource hashes. See the show_destroy_link above
69
+ # Shows an unobtrusive jQuery link based on an array of resource hashes. See the show_destroy_link above
70
+ # for further details. Accepts the following arguments:
71
+ # * *resources* - The array of resource hashes.
72
+ def show_destroy_resource_link resources, parent_dom_id
73
+ show_destroy_link :parent_id => parent_dom_id, :url => build_resource_url(resources, :destroy)
74
+ end
75
+
76
+ # Shows an unobtrusive jQuery link based on an array of resource hashes. See the show_destroy_link above
63
77
  # for further details. Accepts the following arguments:
64
78
  # * *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)
79
+ def show_nested_destroy_resource_link resources, parent_dom_id
80
+ show_destroy_link(:id => parent_dom_id.to_s + "_nested-destroy", :class => "nested-destroy") unless resources.last[:record].new_record?
67
81
  end
68
82
 
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:
83
+ # Shows edit and delete links for resources. Accepts the following parameters:
72
84
  # * *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
85
+ # * *parent_dom_id* - The parent ID of the dom object for which the edit and destroy links belong to for UJS manipulation.
86
+ def show_edit_and_destroy_resource_links resources, parent_dom_id
87
+ [link_to("Edit", build_resource_url(resources, :edit)), show_destroy_resource_link(resources, parent_dom_id)].join(" | ")
77
88
  end
78
89
  end
@@ -1,3 +1,15 @@
1
+ // Chops off the last string segment per delimiter. If no delimiter is found in the string then the original string is
2
+ // returned instead. The following attributes are accepted:
3
+ // string = Required. The string to chop.
4
+ // delimiter = Optional. The delimiter used to chop up the string. Defaults to '_'.
5
+ function chop_last(string, delimiter) {
6
+ var chopped = string;
7
+ if (delimiter == undefined) {delimiter = '_';}
8
+ var endIndex = string.lastIndexOf(delimiter);
9
+ if (endIndex > 1) {chopped = string.slice(0, endIndex);}
10
+ return chopped;
11
+ }
12
+
1
13
  // Increments a number embedded in the text by one. If no number is found then the original text is returned.
2
14
  function increment_text(text) {
3
15
  var match = text.match(/\d+/);
@@ -35,38 +47,22 @@ function increment_input_name(input) {
35
47
  $(input).attr("name", name);
36
48
  };
37
49
 
50
+ // Generates a hidden input field based off original input field data that instructs ActiveRecord to delete
51
+ // a record based on the ID of the input field.
52
+ function generate_destroy_input(input) {
53
+ $(input).attr("id", chop_last($(input).attr("id")) + "__delete");
54
+ $(input).attr("name", chop_last($(input).attr("name"), '[') + "[_delete]");
55
+ $(input).attr("type", "hidden");
56
+ $(input).attr("value", 1);
57
+ return input;
58
+ }
59
+
38
60
  // UJS
39
61
  $(document).ready(function(){
40
- // Destroy
41
- $("a.destroy").click(function(){
42
- var result = confirm("Are you sure you want to delete this?");
43
- if (result) {
44
- try {
45
- var id = $(this).attr("id");
46
- // Parent ID is determined from link ID. Example: post_3_link (link ID) yields post_3 (parent ID).
47
- var endIndex = id.lastIndexOf('_');
48
- if (endIndex > 1) {
49
- var parent_id = '#' + id.slice(0, endIndex);
50
- $(parent_id).animate({backgroundColor: "#FF0000"}, 500);
51
- $(parent_id).fadeOut(500);
52
- } else {
53
- throw "Invalid ID";
54
- }
55
- // Finally, call the destroy action.
56
- $.post($(this).attr("href"), "_method=delete");
57
- } catch (e) {
58
- alert("Error: " + e + ". Check that parent ID is defined and/or the link ID includes parent ID as part of the link ID.");
59
- }
60
- }
61
- // Ensure the default event does not fire.
62
- return false;
63
- });
64
-
65
- // New
66
- $("a.new").click(function(){
62
+ // Nested New
63
+ $("a.nested-new").click(function(){
67
64
  var id = $(this).attr("id");
68
- var endIndex = id.lastIndexOf('_');
69
- var parent_id = '#' + id.slice(0, endIndex);
65
+ var parent_id = '#' + chop_last(id);
70
66
  var clone = $(parent_id + " tr:last").clone(true);
71
67
  var clone_id = clone.attr("id");
72
68
  // Clone for new action.
@@ -87,5 +83,51 @@ $(document).ready(function(){
87
83
  $(this).attr("value", '');
88
84
  return this;
89
85
  });
86
+ // Ensure default event does not fire.
87
+ return false;
90
88
  });
89
+
90
+ // Nested Destroy
91
+ $("a.nested-destroy").click(function(){
92
+ try {
93
+ var id = $(this).attr("id");
94
+ var parent_id = chop_last(id);
95
+ if (parent_id != id) {
96
+ parent_id = '#' + parent_id;
97
+ $(parent_id).prepend(generate_destroy_input($(parent_id + " input:first").clone()));
98
+ $(parent_id).animate({backgroundColor: "#FF0000"}, 500);
99
+ $(parent_id).fadeOut(500);
100
+ } else {
101
+ throw "Invalid ID";
102
+ }
103
+ } catch (e) {
104
+ alert("Error: " + e + ". Check that parent ID is defined and/or the link ID includes parent ID as part of the link ID.");
105
+ }
106
+ // Ensure default event does not fire.
107
+ return false;
108
+ });
109
+
110
+ // Destroy
111
+ $("a.destroy").click(function(){
112
+ var result = confirm("Are you sure you want to delete this?");
113
+ if (result) {
114
+ try {
115
+ var id = $(this).attr("id");
116
+ var parent_id = chop_last(id);
117
+ if (parent_id != id) {
118
+ parent_id = '#' + parent_id;
119
+ $(parent_id).animate({backgroundColor: "#FF0000"}, 500);
120
+ $(parent_id).fadeOut(500);
121
+ } else {
122
+ throw "Invalid ID";
123
+ }
124
+ // Finally, call the destroy action.
125
+ $.post($(this).attr("href"), "_method=delete");
126
+ } catch (e) {
127
+ alert("Error: " + e + ". Check that parent ID is defined and/or the link ID includes parent ID as part of the link ID.");
128
+ }
129
+ }
130
+ // Ensure default event does not fire.
131
+ return false;
132
+ });
91
133
  });
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.4.2
4
+ version: 0.4.4
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-14 00:00:00 -07:00
12
+ date: 2009-04-15 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency