Permalink
Browse files

CRUD completo de Rotinas

  • Loading branch information...
1 parent 2570989 commit 00e4516352292351763b4fc8c098e6361adafec6 @cmilfont committed Dec 20, 2012
View
@@ -34,4 +34,4 @@ gem 'jquery-rails'
# gem 'capistrano'
# To use debugger
-# gem 'debugger'
+gem 'debugger'
View
@@ -37,6 +37,14 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.4.0)
+ columnize (0.3.6)
+ debugger (1.2.2)
+ columnize (>= 0.3.1)
+ debugger-linecache (~> 1.1.1)
+ debugger-ruby_core_source (~> 1.1.5)
+ debugger-linecache (1.1.2)
+ debugger-ruby_core_source (>= 1.1.1)
+ debugger-ruby_core_source (1.1.5)
erubis (2.7.0)
execjs (1.4.0)
multi_json (~> 1.0)
@@ -104,6 +112,7 @@ PLATFORMS
DEPENDENCIES
coffee-rails (~> 3.2.1)
+ debugger
jquery-rails
mysql2
rails (= 3.2.8)
@@ -1,3 +1,18 @@
+Ext.onReady(function(){
+
+ Ext.Ajax.extraParams = {
+ authenticity_token : Ext.select("meta[name='csrf-token']").first().getAttribute('content')
+ }
+
+ // Ext.Ajax.on('requestexception', function (conn, response, options) {
+ // if (response.status === 401 || response.status === 401) {
+ // resp = JSON.parse(response.responseText)
+ // Ext.Msg.alert('Erro', resp.message);
+ // }
+ // });
+
+});
+
Ext.application({
name: 'Workout',
paths: {"Ext.ux":"/assets/ux"},
@@ -3,6 +3,17 @@ Ext.define('Workout.model.Rotina', {
proxy: {
type: 'rest', format: 'json', url: '/rotinas'
},
+ fields: ['titulo', 'exercicio', {name:'id', mapping:'id'}],
+ hasMany: [
+ {model: 'Workout.model.Item', name: 'itens', associationKey: 'itens'}
+ ]
+});
+
+Ext.define('Workout.model.RotinaTree', {
+ extend: 'Ext.data.Model',
+ proxy: {
+ type: 'rest', format: 'json', url: '/rotinas'
+ },
fields: ['id', 'titulo', 'exercicio'],
hasMany: [
{model: 'Workout.model.Item', name: 'itens', associationKey: 'itens'}
@@ -1,7 +1,7 @@
Ext.define('Workout.store.RotinaStore', {
alias: 'store.rotinastore',
extend: 'Ext.data.TreeStore',
- model: "Workout.model.Rotina",
+ model: "Workout.model.RotinaTree",
root: "Workout.model.Rotina",
constructor: function() {
this.callParent(arguments);
@@ -10,19 +10,20 @@ Ext.define('Workout.view.rotina.Form', {
frame:true,
salvarCallback: Ext.emptyFn,
scopeSalvarCallback: null,
- items: [{name: "titulo", xtype: "textfield", fieldLabel: "Titulo", allowBlank: false }],
+ model: null,
+ constructor: function(config) {
+ this.callParent(arguments);
+ if(config.model) this.getForm().loadRecord(config.model);
+ },
+ items: [
+ {xtype: "hiddenfield", name: "id"},
+ {name: "titulo", xtype: "textfield", fieldLabel: "Titulo", allowBlank: false }
+ ],
buttons: [{ text: "Salvar", formBind:true, handler: function(){
var panel = this.up("form");
var form = panel.getForm();
if( form.isValid() ) {
- var model = Ext.create('Workout.model.Rotina', form.getValues());
- model.save({
- callback: function(rec){
- console.log("SAlvou", rec);
- //panel.salvarCallback(model);
- panel.salvarCallback.call( panel.scopeSalvarCallback, model )
- }
- })
+ panel.salvarCallback.call( panel.scopeSalvarCallback, form.getValues() );
}
}}]
});
@@ -15,8 +15,8 @@ Ext.define('Workout.view.rotina.List', {
constructor: function() {
this.tbar = [
{ xtype: 'button', text: 'Cadastrar' , pressed: true, handler: this.abrirJanela, scope: this},
- { xtype: 'button', text: 'Editar', itemId: 'editar', disabled: true, pressed: true },
- { xtype: 'button', text: 'Excluir', itemId: 'excluir', disabled: true, pressed: true }
+ { xtype: 'button', text: 'Editar', itemId: 'editar', disabled: true, pressed: true, handler: this.abrirJanela, scope: this },
+ { xtype: 'button', text: 'Excluir', itemId: 'excluir', disabled: true, pressed: true, handler: this.excluir, scope: this }
];
this.callParent(arguments);
this.on({
@@ -25,17 +25,51 @@ Ext.define('Workout.view.rotina.List', {
scope: this
});
},
- abrirJanela: function() {
+ excluir: function() {
+ Ext.Msg.show({
+ title:'Excluir Rotina',
+ msg: 'Deseja realmente excluir essa rotina de treinos?',
+ buttons: Ext.Msg.YESNO,
+ scope: this,
+ fn: function(btn){
+ if(btn == "yes")
+ this.getSelectionModel().getLastSelected().destroy();
+ }
+ });
+ },
+ abrirJanela: function(button) {
+ var model = null;
+ if(button.text != "Cadastrar") model = this.getSelectionModel().getLastSelected();
Ext.create("Ext.window.Window", {
title: "Criar Rotina",
items: [{
- xtype: "rotinaform", salvarCallback: this.adicionar, scopeSalvarCallback: this
+ xtype: "rotinaform",
+ salvarCallback: this.adicionar,
+ scopeSalvarCallback: this,
+ model: model
}]
}).show();
},
- adicionar: function(model) {
- model.set("leaf", true);
- this.getRootNode().appendChild(model);
+ adicionar: function(json) {
+ var id = json["id"];
+ var model = this.getStore().getNodeById( id );
+ if( !model ) {
+ model = Ext.create('Workout.model.Rotina', json);
+ model.save({
+ scope: this,
+ callback: function(rec){
+ this.getRootNode().appendChild({
+ leaf: true,
+ id: id,
+ titulo: model.get("titulo"),
+ exercicio: ""
+ });
+ }
+ });
+ } else {
+ model.set("titulo", json.titulo);
+ }
+
},
desabilitarAoSelecionar: function() {
this.down("#editar").disable();
@@ -19,8 +19,16 @@ def create
def update
@rotina = Rotina.find params[:id]
- @rotina.update_attributes params[:rotina]
- respond_with @rotina, :include => {:itens => {:include => :exercicio}}
+ @rotina.titulo = params[:titulo]
+ @rotina.save
+ #respond_with @rotina, :include => {:itens => {:include => :exercicio}}
+ render :json => @rotina.to_json(:include => {:itens => {:include => :exercicio}})
+ end
+
+ def destroy
+ @rotina = Rotina.find params[:id]
+ @rotina.destroy
+ respond_with @rotina
end
end
View
@@ -1,5 +1,5 @@
class Rotina < ActiveRecord::Base
attr_accessible :titulo
has_many :itens
- validates_presence_of :titulo
+ #validates_presence_of :titulo
end

0 comments on commit 00e4516

Please sign in to comment.