Skip to content
Browse files

Added transaction save through regular and modal version.

Added some js for the above.
Started work on update transaction regular and modal version.
  • Loading branch information...
1 parent 8a4bab4 commit b7f02317c81cedb89bafad67489beeffedb4b44e @ivik committed Nov 12, 2012
View
105 app/assets/javascripts/transactions.js
@@ -4,55 +4,90 @@
// $(lv_target).load(lv_url)})
$(document).ready(function() {
+
+ // in order to use same modal on multiple dynamic links
+ $('#updateTransactionModal').on('hidden', function() {
+ $(this).removeData('modal');
+ });
+
+ // new transaction modal footer button
+ $('#new_transaction_button').bind('click', function(event) {
+
+ document.forms["new_transaction"].submit();
+ });
+
+ // update transaction modal footer button
+ $('#update_transaction_button').bind('click', function(event) {
+
+ document.forms["update_transaction"].submit();
+ });
+
+
+ // get sub_category when category changes
$('#transaction_category').bind('change', function(event) {
$("#transaction_category option:selected").each(function () {
var category_id = $(this).val();
- //alert(category_id);
+ get_sub_categories_ajax(category_id);
+ });
+ });
+});
+
+// check if loaded category has sub categories
+if($('#transaction_category').length != 0) {
+
+ var category_id = $('#transaction_category').val()
+ if(category_id != "") {
- $.ajax({
- url: "/transactions/get_sub_categories",
- //url: "new",
- type: "get",
- data: "category_id="+category_id,
- dataType: 'json',
- async: false,
- success: function(sub_categories) {
+ get_sub_categories_ajax(category_id);
+ }
+}
- //alert(response);
+function get_sub_categories_ajax(category_id) {
- if(sub_categories.length != 0) {
+ if(category_id == "") {
+ return false;
+ }
- //alert("Ja");
+ $.ajax({
+ url: "/transactions/get_sub_categories",
+ //url: "new",
+ type: "get",
+ data: "category_id="+category_id,
+ dataType: 'json',
+ async: false,
+ success: function(sub_categories) {
- s_container = document.getElementById('sub_category_container');
+ //alert(response);
- var s = document.createElement("select");
- s.id = "sub_category_id";
- s.name = "transaction[sub_category]";
+ if(sub_categories.length != 0) {
- var o = document.createElement("option");
- o.text = "Please Select";
- o.value = "";
- s.appendChild(o);
+ s_container = document.getElementById('sub_category_container');
- for(var i=0; i<sub_categories.length; i++) {
+ var s = document.createElement("select");
+ s.id = "sub_category_id";
+ s.name = "transaction[sub_category]";
- var o = document.createElement("option");
- o.text = sub_categories[i]['name'];
- o.value = sub_categories[i]['id'];
- s.appendChild(o)
- }
+ var o = document.createElement("option");
+ o.text = "Please Select";
+ o.value = "";
+ s.appendChild(o);
- $("#sub_category_container").html('<label>Sub Categories</label>');
- s_container.appendChild(s);
- }
- else {
+ for(var i=0; i<sub_categories.length; i++) {
- alert("Nej");
- }
+ var o = document.createElement("option");
+ o.text = sub_categories[i]['name'];
+ o.value = sub_categories[i]['id'];
+ s.appendChild(o)
}
- });
- });
+
+ $("#sub_category_container").html('<label>Sub Categories</label>');
+ s_container.appendChild(s);
+ }
+ else {
+
+ //alert("No Sub Category, soon u can create one here!");
+ }
+ }
});
-});
+}
View
0 app/assets/javascripts/transactions.js.coffee
No changes.
View
4 app/controllers/static_pages_controller.rb
@@ -1,8 +1,8 @@
class StaticPagesController < ApplicationController
def home
if signed_in?
- @yearbooks = current_user.yearbooks
- @categories = current_user.categories.paginate(page: params[:page], per_page: 10)
+ @yearbooks = current_user.yearbooks.paginate(page: params[:page], per_page: 5)
+ @categories = current_user.categories.paginate(page: params[:page], per_page: 10)
#Get the last entered month
@month = @yearbooks.first.months.first
View
40 app/controllers/transactions_controller.rb
@@ -19,7 +19,12 @@ def show
def new
@transaction = Transaction.new
- render layout: false
+
+ @display_molad = params[:display_molad]
+ if @display_molad == "1"
+ render layout: false
+ end
+ #render layout: false
#render json: @transaction
end
@@ -43,8 +48,33 @@ def create
#Todo
# This needs to be done with nested Model Forms since category and subCategory needs to be saved in the transaction
# model as their own models and not IDs. --> Category(#69913318042000) expected, got String(#4906340)
+ #@transaction = Month.find(params[:id]).transactions.new(params[:transaction])
+
+ #@transaction = Transaction.new(date: params[:transaction][:date],
+ # month_id: params[:transaction][:month_id],
+ # category_id: params[:transaction][:category],
+ # sub_category_id: params[:transaction][:sub_category],
+ # amount: params[:transaction][:amount],
+ # is_expense: params[:transaction][:is_expense])
+
+ #params.o
+
+ month = Month.find(params[:id])
+ category = Category.find(params[:transaction][:category])
+
+ @transaction = Transaction.new(date: params[:transaction][:date],
+ month: month,
+ category: category,
+ amount: params[:transaction][:amount],
+ is_expense: params[:transaction][:is_expense])
+
+ if params[:transaction][:sub_category]
+ sub_category = SubCategory.find(params[:transaction][:sub_category])
+ @transaction.sub_category= sub_category
+ end
+
+ #@transaction.o
- @transaction = Month.find(params[:id]).transactions.new(params[:transaction])
if @transaction.save
flash[:success] = "Transaction created!"
@@ -56,6 +86,12 @@ def create
def edit
@transaction = Transaction.find(params[:id])
+ #@transaction.o
+
+ @display_molad = params[:display_molad]
+ if @display_molad == "1"
+ render layout: false
+ end
end
def update
View
2 app/models/transaction.rb
@@ -29,7 +29,7 @@ class Transaction < ActiveRecord::Base
validates :is_expense, :inclusion => {:in => [true, false]}
- default_scope order: "transactions.date desc"
+ default_scope order: "transactions.date desc, transactions.updated_at desc"
end
View
21 app/views/static_pages/_home_transactions.html.haml
@@ -1,8 +1,10 @@
%h4 Latest logs
.center
- = link_to "Create New - Standard", new_transaction_path(@month), {remote: true}
+ -#= link_to "Create New - Standard", new_transaction_path(@month), {remote: true}
+ = link_to "Create New - Standard", new_transaction_path(@month)
%br
- = link_to "Create New", new_transaction_path(@month), {"data-target" => "#newTransactionModal", "data-toggle" => "modal"}
+ = link_to "Create New", new_transaction_path(@month, display_molad: "1"), {"data-target" => "#newTransactionModal", "data-toggle" => "modal"}
+ /= link_to "Create New", "/months/12/transactions/new_modal", {"data-target" => "#newTransactionModal", "data-toggle" => "modal"}
%br
-#%a.btn{"data-target" => "#newTransactionModal", "data-toggle" => "modal", :href => "#{new_transaction_path(@month)}"} Create New
%br
@@ -21,7 +23,8 @@
%td
.center
-#= transaction.date
- = link_to transaction.date, edit_transaction_path(transaction)
+ -#= link_to transaction.date, edit_transaction_path(transaction)
+ = link_to transaction.date, edit_transaction_path(transaction, display_molad: "1"), {"data-target" => "#updateTransactionModal", "data-toggle" => "modal"}
%td
-#= link_to return_category_name(transaction.category_id), edit_transaction_path(transaction)
= return_category_name(transaction.category_id)
@@ -39,4 +42,14 @@
.modal-body
.modal-footer
%button.btn{"aria-hidden" => "true", "data-dismiss" => "modal"} Close
- %button.btn.btn-primary Save changes
+ %button#new_transaction_button.btn.btn-primary Create Transaction
+
+
+#updateTransactionModal.modal.hide.fade{"aria-hidden" => "true", "aria-labelledby" => "updateTransactionModalLabel", :role => "dialog", :tabindex => "-1"}
+ .modal-header
+ %button.close{"aria-hidden" => "true", "data-dismiss" => "modal", :type => "button"} ×
+ %h3#updateTransactionModalLabel Update Transaction
+ .modal-body
+ .modal-footer
+ %button.btn{"aria-hidden" => "true", "data-dismiss" => "modal"} Close
+ %button#update_transaction_button.btn.btn-primary Save Changes
View
29 app/views/static_pages/_home_yearbooks.html.haml
@@ -1,7 +1,28 @@
%h4
Yearbooks
-.center
- = link_to "Create New", new_yearbook_path
+= link_to "Create New", new_yearbook_path
- if @yearbooks.any?
- - @yearbooks.each do |yearbook|
- = link_to yearbook.name, yearbook
+ = will_paginate(@yearbooks)
+ %table.table.table-bordered.table-condensed
+ %thead
+ %tr
+ %th
+ .center Year
+ %th
+ .center Months
+ %th
+ %tbody
+ - @yearbooks.each do |yearbook|
+ %tr
+ %td
+ .center
+ = link_to yearbook.name, yearbook
+ %td
+ .center
+ - yearbook.months.each do |month|
+ = link_to return_month_name(month.name), month_path(month)
+ %td
+ .center
+ = link_to "edit", edit_yearbook_path(yearbook)
+ = link_to "delete", yearbook, method: :delete, data: { confirm: "You sure?" }
+ = will_paginate(@yearbooks)
View
15 app/views/transactions/edit.html.haml
@@ -1,8 +1,15 @@
- provide(:title, "Edit transaction")
-%h3 Edit transaction
-.row
- .span6.offset3
+- if @display_molad == "1"
+ %script{:src => "/assets/transactions.js?body=1", :type => "text/javascript"}
+ .span
= form_for(@transaction) do |f|
= render 'shared/error_messages', object: f.object
= render 'form', :f => f
- = f.submit "Save changes", class: "btn btn-large btn-primary"
+-else
+ %h3 Edit transaction
+ .row
+ .span6.offset3
+ = form_for(@transaction) do |f|
+ = render 'shared/error_messages', object: f.object
+ = render 'form', :f => f
+ = f.submit "Save Changes", class: "btn btn-large btn-primary"
View
15 app/views/transactions/new.html.haml
@@ -1,8 +1,15 @@
- provide(:title, 'New transaction')
-%h3 New transaction
-.row
- .span6.offset3
+- if @display_molad == "1"
+ %script{:src => "/assets/transactions.js?body=1", :type => "text/javascript"}
+ .span
= form_for(@transaction) do |f|
= render 'shared/error_messages', object: f.object
= render 'form', :f => f
- = f.submit "Create Transaction", class: "btn btn-large btn-primary"
+-else
+ %h3 New transaction
+ .row
+ .span6.offset3
+ = form_for(@transaction) do |f|
+ = render 'shared/error_messages', object: f.object
+ = render 'form', :f => f
+ = f.submit "Create Transaction", class: "btn btn-large btn-primary"
View
8 app/views/transactions/new.html.haml.old
@@ -0,0 +1,8 @@
+- provide(:title, 'New transaction')
+%h3 New transaction
+.row
+ .span6.offset3
+ = form_for(@transaction) do |f|
+ = render 'shared/error_messages', object: f.object
+ = render 'form', :f => f
+ = f.submit "Create Transaction", class: "btn btn-large btn-primary"

0 comments on commit b7f0231

Please sign in to comment.
Something went wrong with that request. Please try again.