Skip to content

Instantly share code, notes, and snippets.

@martinrehfeld
Created February 11, 2009 20:50
Show Gist options
  • Save martinrehfeld/62257 to your computer and use it in GitHub Desktop.
Save martinrehfeld/62257 to your computer and use it in GitHub Desktop.
Diff-howto on belongs_to associations with Ext Scaffold Generator
diff --git a/app/controllers/non_namespaced_post_resources_controller.rb b/app/controllers/non_namespaced_post_resources_controller.rb
index 1c0d143..0dc0b74 100644
--- a/app/controllers/non_namespaced_post_resources_controller.rb
+++ b/app/controllers/non_namespaced_post_resources_controller.rb
@@ -12,7 +12,7 @@ class NonNamespacedPostResourcesController < ApplicationController
def index
respond_to do |format|
format.html # index.html.erb (no data required)
- format.ext_json { render :json => find_non_namespaced_post_resources.to_ext_json(:class => NonNamespacedPostResource, :count => NonNamespacedPostResource.count(options_from_search(NonNamespacedPostResource))) }
+ format.ext_json { render :json => find_non_namespaced_post_resources.to_ext_json(:methods => :author_name, :class => NonNamespacedPostResource, :count => NonNamespacedPostResource.count(options_from_search(NonNamespacedPostResource))) }
end
end
diff --git a/app/models/author.rb b/app/models/author.rb
new file mode 100644
index 0000000..e47dd2d
--- /dev/null
+++ b/app/models/author.rb
@@ -0,0 +1,2 @@
+class Author < ActiveRecord::Base
+end
diff --git a/app/models/non_namespaced_post_resource.rb b/app/models/non_namespaced_post_resource.rb
index 6f4c652..aea6767 100644
--- a/app/models/non_namespaced_post_resource.rb
+++ b/app/models/non_namespaced_post_resource.rb
@@ -1,3 +1,9 @@
class NonNamespacedPostResource < ActiveRecord::Base
set_table_name "to_be_deleted_post_resources"
+
+ belongs_to :author
+
+ def author_name
+ author && author.name
+ end
end
diff --git a/app/views/non_namespaced_post_resources/index.html.erb b/app/views/non_namespaced_post_resources/index.html.erb
index e57e42a..3255d19 100644
--- a/app/views/non_namespaced_post_resources/index.html.erb
+++ b/app/views/non_namespaced_post_resources/index.html.erb
@@ -9,6 +9,7 @@
var vp = new Ext.Viewport({
layout: 'fit',
items: new ExtScaffold.NonNamespacedPostResource({
+ authorNamesStore: <%= Author.all.collect {|a| [a.id, a.name] }.to_json %>,
url: '<%= non_namespaced_post_resources_path %>'
<%= ",baseParams: { #{request_forgery_protection_token}: '#{form_authenticity_token}' }" if protect_against_forgery? %>
})
diff --git a/db/migrate/20081108072928_create_authors.rb b/db/migrate/20081108072928_create_authors.rb
new file mode 100644
index 0000000..9585228
--- /dev/null
+++ b/db/migrate/20081108072928_create_authors.rb
@@ -0,0 +1,13 @@
+class CreateAuthors < ActiveRecord::Migration
+ def self.up
+ create_table :authors do |t|
+ t.string :name
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :authors
+ end
+end
diff --git a/db/migrate/20081108073107_add_author_to_to_be_deleted_post_resource.rb b/db/migrate/20081108073107_add_author_to_to_be_deleted_post_resource.rb
new file mode 100644
index 0000000..eace078
--- /dev/null
+++ b/db/migrate/20081108073107_add_author_to_to_be_deleted_post_resource.rb
@@ -0,0 +1,9 @@
+class AddAuthorToToBeDeletedPostResource < ActiveRecord::Migration
+ def self.up
+ add_column :to_be_deleted_post_resources, :author_id, :integer
+ end
+
+ def self.down
+ remove_column :to_be_deleted_post_resources, :author_id
+ end
+end
diff --git a/public/javascripts/ext_scaffold.js b/public/javascripts/ext_scaffold.js
index 742fd8c..bb0011d 100644
--- a/public/javascripts/ext_scaffold.js
+++ b/public/javascripts/ext_scaffold.js
@@ -53,7 +53,13 @@ ExtScaffold.FormPanel = Ext.extend(Ext.FormPanel, {
item.setDisabled(fieldsDisabled);
});
},
-
+
+ clearAllStoreFilters: function() {
+ this.getForm().items.each(function(item) {
+ if (item.store && item.store.clearFilter) item.store.clearFilter(true);
+ });
+ },
+
setFormMode: function(mode) {
if (mode === 'new') this.getForm().reset(); // empty all fields
diff --git a/public/javascripts/ext_scaffold/non_namespaced_post_resource.js b/public/javascripts/ext_scaffold/non_namespaced_post_resource.js
index 3a6134e..15c1152 100644
--- a/public/javascripts/ext_scaffold/non_namespaced_post_resource.js
+++ b/public/javascripts/ext_scaffold/non_namespaced_post_resource.js
@@ -143,6 +143,8 @@ ExtScaffold.NonNamespacedPostResource = Ext.extend(Ext.Panel, {
,{ name: 'non_namespaced_post_resource[visible_from]', mapping: 'non_namespaced_post_resource.visible_from', type: 'date', dateFormat: 'c' }
,{ name: 'non_namespaced_post_resource[expires]', mapping: 'non_namespaced_post_resource.expires', type: 'date', dateFormat: 'Y-m-d' }
,{ name: 'non_namespaced_post_resource[rating]', mapping: 'non_namespaced_post_resource.rating', type: 'float' }
+ ,{ name: 'non_namespaced_post_resource[author_id]', mapping: 'non_namespaced_post_resource.author_id', type: 'int' }
+ ,{ name: 'virtual_attributes[author_name]', mapping: 'non_namespaced_post_resource.author_name' }
]),
remoteSort: true, // turn on server-side sorting
sortInfo: {field: 'id', direction: 'ASC'}
@@ -156,6 +158,7 @@ ExtScaffold.NonNamespacedPostResource = Ext.extend(Ext.Panel, {
,{ header: scaffoldPanel.labels['non_namespaced_post_resource[visible_from]'], dataIndex: 'non_namespaced_post_resource[visible_from]' }
,{ header: scaffoldPanel.labels['non_namespaced_post_resource[expires]'], dataIndex: 'non_namespaced_post_resource[expires]' }
,{ header: scaffoldPanel.labels['non_namespaced_post_resource[rating]'], dataIndex: 'non_namespaced_post_resource[rating]' }
+ ,{ header: 'Author', dataIndex: 'virtual_attributes[author_name]' }
]);
cm.defaultSortable = true; // all fields are sortable by default
@@ -216,8 +219,11 @@ ExtScaffold.NonNamespacedPostResource = Ext.extend(Ext.Panel, {
listeners: {
// populate form fields when a row is selected
'rowselect': function(sm, row, rec) {
+ var fp = scaffoldPanel.getFormPanel();
+
scaffoldPanel.selectedRecordId = rec.data.id;
- scaffoldPanel.getFormPanel().getForm().loadRecord(rec);
+ fp.clearAllStoreFilters();
+ fp.getForm().loadRecord(rec);
}
}
}),
@@ -311,7 +317,8 @@ ExtScaffold.NonNamespacedPostResource = Ext.extend(Ext.Panel, {
{ fieldLabel: scaffoldPanel.labels['non_namespaced_post_resource[published]'], name: 'non_namespaced_post_resource[published]', xtype: 'checkbox', inputValue: '1', width: 20 }, { xtype: 'hidden', name: 'non_namespaced_post_resource[published]', value: '0' },
{ fieldLabel: scaffoldPanel.labels['non_namespaced_post_resource[visible_from]'], name: 'non_namespaced_post_resource[visible_from]', xtype: 'xdatetime' },
{ fieldLabel: scaffoldPanel.labels['non_namespaced_post_resource[expires]'], name: 'non_namespaced_post_resource[expires]', xtype: 'datefield' },
- { fieldLabel: scaffoldPanel.labels['non_namespaced_post_resource[rating]'], name: 'non_namespaced_post_resource[rating]', xtype: 'numberfield' }
+ { fieldLabel: scaffoldPanel.labels['non_namespaced_post_resource[rating]'], name: 'non_namespaced_post_resource[rating]', xtype: 'numberfield' },
+ { fieldLabel: 'Author', name:'virtual_attributes[author_name]', hiddenName: 'non_namespaced_post_resource[author_id]', xtype: 'combo', store: scaffoldPanel.authorNamesStore, triggerAction: 'all', forceSelection: true }
],
onOk: function() {
@@ -364,6 +371,7 @@ ExtScaffold.NonNamespacedPostResource = Ext.extend(Ext.Panel, {
fp.setFormMode('show');
if (sm.hasSelection()) {
+ fp.clearAllStoreFilters();
fp.getForm().loadRecord(sm.getSelected()); // reload previous record version
} else {
fp.getForm().reset();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment