From 3c01a830413fe414d7949da79c619dc5e9166b6d Mon Sep 17 00:00:00 2001 From: Atchyut Date: Tue, 31 May 2016 12:44:15 +0530 Subject: [PATCH] Added validations, Added content in Home page --- app/controllers/categories_controller.rb | 3 +++ app/controllers/expenses_controller.rb | 2 +- app/models/category.rb | 3 +++ app/models/expense.rb | 2 ++ app/models/user.rb | 20 ++++++++++++++++++++ app/views/expenses/_form.html.erb | 3 ++- app/views/static_pages/home.html.erb | 14 +++++++++++++- 7 files changed, 44 insertions(+), 3 deletions(-) diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb index e251d92..daf6bc9 100644 --- a/app/controllers/categories_controller.rb +++ b/app/controllers/categories_controller.rb @@ -30,6 +30,9 @@ def update end def destroy + if @category.destroy + redirect_to categories_path + end end def index diff --git a/app/controllers/expenses_controller.rb b/app/controllers/expenses_controller.rb index 1bca22f..372fe2e 100644 --- a/app/controllers/expenses_controller.rb +++ b/app/controllers/expenses_controller.rb @@ -13,7 +13,7 @@ def create redirect_to expenses_path flash[:success] = "New expence has been created" else - render Expense.new + redirect_to new_expense_path end end diff --git a/app/models/category.rb b/app/models/category.rb index db5b988..bf5b0e6 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -1,4 +1,7 @@ class Category < ActiveRecord::Base belongs_to :user has_many :expenses + + validates :name , uniqueness: true + validates :name , presence: true end diff --git a/app/models/expense.rb b/app/models/expense.rb index 3975f17..057aca7 100644 --- a/app/models/expense.rb +++ b/app/models/expense.rb @@ -1,4 +1,6 @@ class Expense < ActiveRecord::Base belongs_to :category belongs_to :user + + validates :amount, :category, :date, :description, presence: true end diff --git a/app/models/user.rb b/app/models/user.rb index 79709a5..bd0cb96 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -6,4 +6,24 @@ class User < ActiveRecord::Base has_many :categories has_many :expenses, through: :categories + + after_create :create_default_categories + + DEFAULT_CATEGORIES = [ + {name: 'Phone bill'}, + {name: 'Entertainment'}, + {name: 'Rent'}, + {name: 'Travel'}, + {name: 'Parties'}, + {name: 'Gifts'}, + {name: 'Phone bill'}, + {name: 'Miscellaneous'} + ] + + def create_default_categories + DEFAULT_CATEGORIES.each do |default_attrs| + self.categories.build(default_attrs) + end + end + end diff --git a/app/views/expenses/_form.html.erb b/app/views/expenses/_form.html.erb index 6709233..9c82808 100644 --- a/app/views/expenses/_form.html.erb +++ b/app/views/expenses/_form.html.erb @@ -27,9 +27,10 @@
<%= f.label :category, "Category:" %>
-
+
<%= f.collection_select(:category_id, current_user.categories, :id, :name, {}, { :class => "select_box selectpicker picker"}) %> + <%= link_to "+ Add Category", new_category_path %>
diff --git a/app/views/static_pages/home.html.erb b/app/views/static_pages/home.html.erb index 3c41c6f..f2f634f 100644 --- a/app/views/static_pages/home.html.erb +++ b/app/views/static_pages/home.html.erb @@ -1,4 +1,16 @@

Expence Manager

+

Manage all your expences with hassle free

+
+<%= link_to "+ Add Expense", new_expense_path, class: "btn btn-small btn-success" %> +<%= link_to "+ Add Category", new_category_path, class: "btn btn-small btn-success" %> +
-

Manage all your expences with hassle free

\ No newline at end of file +

Benefits:

+