ajax_scaffold_generator 3.1.5 → 3.1.6

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.
@@ -1,126 +1,126 @@
1
- /*
2
- AjaxScaffoldGenerator version 3.1.0
3
- (c) 2006 Richard White <rrwhite@gmail.com>
4
-
5
- AjaxScaffoldGenerator is freely distributable under the terms of an MIT-style license.
6
-
7
- For details, see the AjaxScaffoldGenerator web site: http://www.ajaxscaffold.com/
8
- */
9
-
10
- /*
11
- * The following is a cross browser way to move around <tr> elements in a <table> or <tbody>
12
- */
13
-
14
- var Abstract = new Object();
15
- Abstract.Table = function() {};
16
- Abstract.Table.prototype = {
17
- tagTest: function(element, tagName) {
18
- return $(element).tagName.toLowerCase() == tagName.toLowerCase();
19
- }
20
- };
21
-
22
- Abstract.TableRow = function() {};
23
- Abstract.TableRow.prototype = Object.extend(new Abstract.Table(), {
24
- initialize: function(targetTableRow, sourceTableRow) {
25
- try {
26
- var sourceTableRow = $(sourceTableRow);
27
- var targetTableRow = $(targetTableRow);
28
-
29
- if (targetTableRow == null || !this.tagTest(targetTableRow,'tr')
30
- || sourceTableRow == null || !this.tagTest(sourceTableRow,'tr')) {
31
- throw("TableRow: both parameters must be a <tr> tag.");
32
- }
33
-
34
- var tableOrTbody = this.findParentTableOrTbody(targetTableRow);
35
-
36
- var newRow = tableOrTbody.insertRow(this.getNewRowIndex(targetTableRow) - this.getRowOffset(tableOrTbody));
37
- newRow.parentNode.replaceChild(sourceTableRow, newRow);
38
-
39
- } catch (e) {
40
- alert(e);
41
- }
42
- },
43
- getRowOffset: function(tableOrTbody) {
44
- //If we are inserting into a tablebody we would need figure out the rowIndex of the first
45
- // row in that tbody and subtract that offset from the new row index
46
- var rowOffset = 0;
47
- if (this.tagTest(tableOrTbody,'tbody')) {
48
- rowOffset = tableOrTbody.rows[0].rowIndex;
49
- }
50
- return rowOffset;
51
- },
52
- findParentTableOrTbody: function(element) {
53
- var element = $(element);
54
- // Completely arbitrary value
55
- var maxSearchDepth = 3;
56
- var currentSearchDepth = 1;
57
- var current = element;
58
- while (currentSearchDepth <= maxSearchDepth) {
59
- current = current.parentNode;
60
- if (this.tagTest(current, 'tbody') || this.tagTest(current, 'table')) {
61
- return current;
62
- }
63
- currentSearchDepth++;
64
- }
65
- }
66
- });
67
-
68
- var TableRow = new Object();
69
-
70
- TableRow.MoveBefore = Class.create();
71
- TableRow.MoveBefore.prototype = Object.extend(new Abstract.TableRow(), {
72
- getNewRowIndex: function(target) {
73
- return target.rowIndex;
74
- }
75
- });
76
-
77
- TableRow.MoveAfter = Class.create();
78
- TableRow.MoveAfter.prototype = Object.extend(new Abstract.TableRow(), {
79
- getNewRowIndex: function(target) {
80
- return target.rowIndex+1;
81
- }
82
- });
83
-
84
- /*
85
- * The following are simple utility methods
86
- */
87
-
88
- var AjaxScaffold = {
89
- stripe: function(tableBody) {
90
- var even = false;
91
- var tableBody = $(tableBody);
92
- var tableRows = tableBody.getElementsByTagName("tr");
93
- var length = tableBody.rows.length;
94
-
95
- for (var i = 0; i < length; i++) {
96
- var tableRow = tableBody.rows[i];
97
- //Make sure to skip rows that are create or edit rows or messages
98
- if (!Element.hasClassName(tableRow, "create")
99
- && !Element.hasClassName(tableRow, "update")) {
100
-
101
- if (even) {
102
- Element.addClassName(tableRow, "even");
103
- } else {
104
- Element.removeClassName(tableRow, "even");
105
- }
106
- even = !even;
107
- }
108
- }
109
- },
110
- displayMessageIfEmpty: function(tableBody, emptyMessageElement) {
111
- // Check to see if this was the last element in the list
112
- if ($(tableBody).rows.length == 0) {
113
- Element.show($(emptyMessageElement));
114
- }
115
- },
116
- removeSortClasses: function(scaffoldId) {
117
- $$('#' + scaffoldId + ' td.sorted').each(function(element) {
118
- Element.removeClassName(element, "sorted");
119
- });
120
- $$('#' + scaffoldId + ' th.sorted').each(function(element) {
121
- Element.removeClassName(element, "sorted");
122
- Element.removeClassName(element, "asc");
123
- Element.removeClassName(element, "desc");
124
- });
125
- }
1
+ /*
2
+ AjaxScaffoldGenerator version 3.1.0
3
+ (c) 2006 Richard White <rrwhite@gmail.com>
4
+
5
+ AjaxScaffoldGenerator is freely distributable under the terms of an MIT-style license.
6
+
7
+ For details, see the AjaxScaffoldGenerator web site: http://www.ajaxscaffold.com/
8
+ */
9
+
10
+ /*
11
+ * The following is a cross browser way to move around <tr> elements in a <table> or <tbody>
12
+ */
13
+
14
+ var Abstract = new Object();
15
+ Abstract.Table = function() {};
16
+ Abstract.Table.prototype = {
17
+ tagTest: function(element, tagName) {
18
+ return $(element).tagName.toLowerCase() == tagName.toLowerCase();
19
+ }
20
+ };
21
+
22
+ Abstract.TableRow = function() {};
23
+ Abstract.TableRow.prototype = Object.extend(new Abstract.Table(), {
24
+ initialize: function(targetTableRow, sourceTableRow) {
25
+ try {
26
+ var sourceTableRow = $(sourceTableRow);
27
+ var targetTableRow = $(targetTableRow);
28
+
29
+ if (targetTableRow == null || !this.tagTest(targetTableRow,'tr')
30
+ || sourceTableRow == null || !this.tagTest(sourceTableRow,'tr')) {
31
+ throw("TableRow: both parameters must be a <tr> tag.");
32
+ }
33
+
34
+ var tableOrTbody = this.findParentTableOrTbody(targetTableRow);
35
+
36
+ var newRow = tableOrTbody.insertRow(this.getNewRowIndex(targetTableRow) - this.getRowOffset(tableOrTbody));
37
+ newRow.parentNode.replaceChild(sourceTableRow, newRow);
38
+
39
+ } catch (e) {
40
+ alert(e);
41
+ }
42
+ },
43
+ getRowOffset: function(tableOrTbody) {
44
+ //If we are inserting into a tablebody we would need figure out the rowIndex of the first
45
+ // row in that tbody and subtract that offset from the new row index
46
+ var rowOffset = 0;
47
+ if (this.tagTest(tableOrTbody,'tbody')) {
48
+ rowOffset = tableOrTbody.rows[0].rowIndex;
49
+ }
50
+ return rowOffset;
51
+ },
52
+ findParentTableOrTbody: function(element) {
53
+ var element = $(element);
54
+ // Completely arbitrary value
55
+ var maxSearchDepth = 3;
56
+ var currentSearchDepth = 1;
57
+ var current = element;
58
+ while (currentSearchDepth <= maxSearchDepth) {
59
+ current = current.parentNode;
60
+ if (this.tagTest(current, 'tbody') || this.tagTest(current, 'table')) {
61
+ return current;
62
+ }
63
+ currentSearchDepth++;
64
+ }
65
+ }
66
+ });
67
+
68
+ var TableRow = new Object();
69
+
70
+ TableRow.MoveBefore = Class.create();
71
+ TableRow.MoveBefore.prototype = Object.extend(new Abstract.TableRow(), {
72
+ getNewRowIndex: function(target) {
73
+ return target.rowIndex;
74
+ }
75
+ });
76
+
77
+ TableRow.MoveAfter = Class.create();
78
+ TableRow.MoveAfter.prototype = Object.extend(new Abstract.TableRow(), {
79
+ getNewRowIndex: function(target) {
80
+ return target.rowIndex+1;
81
+ }
82
+ });
83
+
84
+ /*
85
+ * The following are simple utility methods
86
+ */
87
+
88
+ var AjaxScaffold = {
89
+ stripe: function(tableBody) {
90
+ var even = false;
91
+ var tableBody = $(tableBody);
92
+ var tableRows = tableBody.getElementsByTagName("tr");
93
+ var length = tableBody.rows.length;
94
+
95
+ for (var i = 0; i < length; i++) {
96
+ var tableRow = tableBody.rows[i];
97
+ //Make sure to skip rows that are create or edit rows or messages
98
+ if (!Element.hasClassName(tableRow, "create")
99
+ && !Element.hasClassName(tableRow, "update")) {
100
+
101
+ if (even) {
102
+ Element.addClassName(tableRow, "even");
103
+ } else {
104
+ Element.removeClassName(tableRow, "even");
105
+ }
106
+ even = !even;
107
+ }
108
+ }
109
+ },
110
+ displayMessageIfEmpty: function(tableBody, emptyMessageElement) {
111
+ // Check to see if this was the last element in the list
112
+ if ($(tableBody).rows.length == 0) {
113
+ Element.show($(emptyMessageElement));
114
+ }
115
+ },
116
+ removeSortClasses: function(scaffoldId) {
117
+ $$('#' + scaffoldId + ' td.sorted').each(function(element) {
118
+ Element.removeClassName(element, "sorted");
119
+ });
120
+ $$('#' + scaffoldId + ' th.sorted').each(function(element) {
121
+ Element.removeClassName(element, "sorted");
122
+ Element.removeClassName(element, "asc");
123
+ Element.removeClassName(element, "desc");
124
+ });
125
+ }
126
126
  }
@@ -1,135 +1,135 @@
1
- class <%= controller_class_name %>Controller < ApplicationController
2
- include AjaxScaffold::Controller
3
-
4
- after_filter :clear_flashes
5
- before_filter :update_params_filter
6
-
7
- def update_params_filter
8
- update_params :default_scaffold_id => "<%= singular_name %>", :default_sort => nil, :default_sort_direction => "asc"
9
- end
10
- <% unless suffix -%>
11
- def index
12
- redirect_to :action => 'list'
13
- end
14
- <% end -%>
15
- def return_to_main
16
- # If you have multiple scaffolds on the same view then you will want to change this to
17
- # to whatever controller/action shows all the views
18
- # (ex: redirect_to :controller => 'AdminConsole', :action => 'index')
19
- redirect_to :action => 'list'
20
- end
21
-
22
- def list
23
- end
24
-
25
- # All posts to change scaffold level variables like sort values or page changes go through this action
26
- def component_update
27
- @show_wrapper = false # don't show the outer wrapper elements if we are just updating an existing scaffold
28
- if request.xhr?
29
- # If this is an AJAX request then we just want to delegate to the component to rerender itself
30
- component
31
- else
32
- # If this is from a client without javascript we want to update the session parameters and then delegate
33
- # back to whatever page is displaying the scaffold, which will then rerender all scaffolds with these update parameters
34
- return_to_main
35
- end
36
- end
37
-
38
- def component
39
- @show_wrapper = true if @show_wrapper.nil?
40
- @sort_sql = <%= model_name %>.scaffold_columns_hash[current_sort(params)].sort_sql rescue nil
41
- @sort_by = @sort_sql.nil? ? "#{<%= model_name %>.table_name}.#{<%= model_name %>.primary_key} asc" : @sort_sql + " " + current_sort_direction(params)
42
- @paginator, @<%= plural_name %> = paginate(:<%= plural_name %>, :order => @sort_by, :per_page => default_per_page)
43
-
44
- render :action => "component", :layout => false
45
- end
46
-
47
- def new
48
- @<%= singular_name %> = <%= model_name %>.new
49
- @successful = true
50
-
51
- return render(:action => 'new.rjs') if request.xhr?
52
-
53
- # Javascript disabled fallback
54
- if @successful
55
- @options = { :action => "create" }
56
- render :partial => "new_edit", :layout => true
57
- else
58
- return_to_main
59
- end
60
- end
61
-
62
- def create
63
- begin
64
- @<%= singular_name %> = <%= model_name %>.new(params[:<%= singular_name %>])
65
- @successful = @<%= singular_name %>.save
66
- rescue
67
- flash[:error], @successful = $!.to_s, false
68
- end
69
-
70
- return render(:action => 'create.rjs') if request.xhr?
71
- if @successful
72
- return_to_main
73
- else
74
- @options = { :scaffold_id => params[:scaffold_id], :action => "create" }
75
- render :partial => 'new_edit', :layout => true
76
- end
77
- end
78
-
79
- def edit
80
- begin
81
- @<%= singular_name %> = <%= model_name %>.find(params[:id])
82
- @successful = !@<%= singular_name %>.nil?
83
- rescue
84
- flash[:error], @successful = $!.to_s, false
85
- end
86
-
87
- return render(:action => 'edit.rjs') if request.xhr?
88
-
89
- if @successful
90
- @options = { :scaffold_id => params[:scaffold_id], :action => "update", :id => params[:id] }
91
- render :partial => 'new_edit', :layout => true
92
- else
93
- return_to_main
94
- end
95
- end
96
-
97
- def update
98
- begin
99
- @<%= singular_name %> = <%= model_name %>.find(params[:id])
100
- @successful = @<%= singular_name %>.update_attributes(params[:<%= singular_name %>])
101
- rescue
102
- flash[:error], @successful = $!.to_s, false
103
- end
104
-
105
- return render(:action => 'update.rjs') if request.xhr?
106
-
107
- if @successful
108
- return_to_main
109
- else
110
- @options = { :action => "update" }
111
- render :partial => 'new_edit', :layout => true
112
- end
113
- end
114
-
115
- def destroy
116
- begin
117
- @successful = <%= model_name %>.find(params[:id]).destroy
118
- rescue
119
- flash[:error], @successful = $!.to_s, false
120
- end
121
-
122
- return render(:action => 'destroy.rjs') if request.xhr?
123
-
124
- # Javascript disabled fallback
125
- return_to_main
126
- end
127
-
128
- def cancel
129
- @successful = true
130
-
131
- return render(:action => 'cancel.rjs') if request.xhr?
132
-
133
- return_to_main
134
- end
135
- end
1
+ class <%= controller_class_name %>Controller < ApplicationController
2
+ include AjaxScaffold::Controller
3
+
4
+ after_filter :clear_flashes
5
+ before_filter :update_params_filter
6
+
7
+ def update_params_filter
8
+ update_params :default_scaffold_id => "<%= singular_name %>", :default_sort => nil, :default_sort_direction => "asc"
9
+ end
10
+ <% unless suffix -%>
11
+ def index
12
+ redirect_to :action => 'list'
13
+ end
14
+ <% end -%>
15
+ def return_to_main
16
+ # If you have multiple scaffolds on the same view then you will want to change this to
17
+ # to whatever controller/action shows all the views
18
+ # (ex: redirect_to :controller => 'AdminConsole', :action => 'index')
19
+ redirect_to :action => 'list'
20
+ end
21
+
22
+ def list
23
+ end
24
+
25
+ # All posts to change scaffold level variables like sort values or page changes go through this action
26
+ def component_update
27
+ @show_wrapper = false # don't show the outer wrapper elements if we are just updating an existing scaffold
28
+ if request.xhr?
29
+ # If this is an AJAX request then we just want to delegate to the component to rerender itself
30
+ component
31
+ else
32
+ # If this is from a client without javascript we want to update the session parameters and then delegate
33
+ # back to whatever page is displaying the scaffold, which will then rerender all scaffolds with these update parameters
34
+ return_to_main
35
+ end
36
+ end
37
+
38
+ def component
39
+ @show_wrapper = true if @show_wrapper.nil?
40
+ @sort_sql = <%= model_name %>.scaffold_columns_hash[current_sort(params)].sort_sql rescue nil
41
+ @sort_by = @sort_sql.nil? ? "#{<%= model_name %>.table_name}.#{<%= model_name %>.primary_key} asc" : @sort_sql + " " + current_sort_direction(params)
42
+ @paginator, @<%= plural_name %> = paginate(:<%= plural_name %>, :order => @sort_by, :per_page => default_per_page)
43
+
44
+ render :action => "component", :layout => false
45
+ end
46
+
47
+ def new
48
+ @<%= singular_name %> = <%= model_name %>.new
49
+ @successful = true
50
+
51
+ return render(:action => 'new.rjs') if request.xhr?
52
+
53
+ # Javascript disabled fallback
54
+ if @successful
55
+ @options = { :action => "create" }
56
+ render :partial => "new_edit", :layout => true
57
+ else
58
+ return_to_main
59
+ end
60
+ end
61
+
62
+ def create
63
+ begin
64
+ @<%= singular_name %> = <%= model_name %>.new(params[:<%= singular_name %>])
65
+ @successful = @<%= singular_name %>.save
66
+ rescue
67
+ flash[:error], @successful = $!.to_s, false
68
+ end
69
+
70
+ return render(:action => 'create.rjs') if request.xhr?
71
+ if @successful
72
+ return_to_main
73
+ else
74
+ @options = { :scaffold_id => params[:scaffold_id], :action => "create" }
75
+ render :partial => 'new_edit', :layout => true
76
+ end
77
+ end
78
+
79
+ def edit
80
+ begin
81
+ @<%= singular_name %> = <%= model_name %>.find(params[:id])
82
+ @successful = !@<%= singular_name %>.nil?
83
+ rescue
84
+ flash[:error], @successful = $!.to_s, false
85
+ end
86
+
87
+ return render(:action => 'edit.rjs') if request.xhr?
88
+
89
+ if @successful
90
+ @options = { :scaffold_id => params[:scaffold_id], :action => "update", :id => params[:id] }
91
+ render :partial => 'new_edit', :layout => true
92
+ else
93
+ return_to_main
94
+ end
95
+ end
96
+
97
+ def update
98
+ begin
99
+ @<%= singular_name %> = <%= model_name %>.find(params[:id])
100
+ @successful = @<%= singular_name %>.update_attributes(params[:<%= singular_name %>])
101
+ rescue
102
+ flash[:error], @successful = $!.to_s, false
103
+ end
104
+
105
+ return render(:action => 'update.rjs') if request.xhr?
106
+
107
+ if @successful
108
+ return_to_main
109
+ else
110
+ @options = { :action => "update" }
111
+ render :partial => 'new_edit', :layout => true
112
+ end
113
+ end
114
+
115
+ def destroy
116
+ begin
117
+ @successful = <%= model_name %>.find(params[:id]).destroy
118
+ rescue
119
+ flash[:error], @successful = $!.to_s, false
120
+ end
121
+
122
+ return render(:action => 'destroy.rjs') if request.xhr?
123
+
124
+ # Javascript disabled fallback
125
+ return_to_main
126
+ end
127
+
128
+ def cancel
129
+ @successful = true
130
+
131
+ return render(:action => 'cancel.rjs') if request.xhr?
132
+
133
+ return_to_main
134
+ end
135
+ end
data/templates/form.rhtml CHANGED
@@ -1,5 +1,5 @@
1
1
  <fieldset>
2
- <div class="row">
3
- <%= template_for_inclusion %>
4
- </div>
2
+ <div class="row">
3
+ <%= template_for_inclusion %>
4
+ </div>
5
5
  </fieldset>
@@ -5,7 +5,7 @@ require '<%= base_controller_file_path %>_controller'
5
5
  class <%= controller_class_name %>Controller; def rescue_action(e) raise e end; end
6
6
 
7
7
  class <%= controller_class_name %>ControllerTest < Test::Unit::TestCase
8
- fixtures :<%= table_name %>
8
+ fixtures :<%= plural_name %>
9
9
 
10
10
  NEW_<%= singular_name.upcase %> = {} # e.g. {:name => 'Test <%= class_name %>', :description => 'Dummy'}
11
11
  REDIRECT_TO_MAIN = {:action => 'list'} # put hash or string redirection that you normally expect
@@ -15,7 +15,7 @@ class <%= controller_class_name %>ControllerTest < Test::Unit::TestCase
15
15
  @request = ActionController::TestRequest.new
16
16
  @response = ActionController::TestResponse.new
17
17
  # Retrieve fixtures via their name
18
- # @first = <%= table_name %>(:first)
18
+ # @first = <%= plural_name %>(:first)
19
19
  @first = <%= class_name %>.find_first
20
20
  end
21
21
 
@@ -23,8 +23,8 @@ class <%= controller_class_name %>ControllerTest < Test::Unit::TestCase
23
23
  get :component
24
24
  assert_response :success
25
25
  assert_template '<%= base_controller_file_path %>/component'
26
- <%= table_name %> = check_attrs(%w(<%= table_name %>))
27
- assert_equal <%= class_name %>.find(:all).length, <%= table_name %>.length, "Incorrect number of <%= table_name %> shown"
26
+ <%= plural_name %> = check_attrs(%w(<%= plural_name %>))
27
+ assert_equal <%= class_name %>.find(:all).length, <%= plural_name %>.length, "Incorrect number of <%= plural_name %> shown"
28
28
  end
29
29
 
30
30
  def test_component_update
@@ -37,8 +37,8 @@ class <%= controller_class_name %>ControllerTest < Test::Unit::TestCase
37
37
  xhr :get, :component_update
38
38
  assert_response :success
39
39
  assert_template '<%= base_controller_file_path %>/component'
40
- <%= table_name %> = check_attrs(%w(<%= table_name %>))
41
- assert_equal <%= class_name %>.find(:all).length, <%= table_name %>.length, "Incorrect number of <%= table_name %> shown"
40
+ <%= plural_name %> = check_attrs(%w(<%= plural_name %>))
41
+ assert_equal <%= class_name %>.find(:all).length, <%= plural_name %>.length, "Incorrect number of <%= plural_name %> shown"
42
42
  end
43
43
 
44
44
  def test_create
Binary file
Binary file