Skip to content

Commit

Permalink
Merge 38061a7 into f95b56c
Browse files Browse the repository at this point in the history
  • Loading branch information
bluerogue251 committed Sep 4, 2014
2 parents f95b56c + 38061a7 commit c4bcafb
Show file tree
Hide file tree
Showing 22 changed files with 297 additions and 286 deletions.
52 changes: 9 additions & 43 deletions app/controllers/expenses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,67 +4,33 @@ def index
respond_to do |format|
format.html
format.json do
render json: ExpensesDatatable.new(view_context, current_user.id)
end
end
end

def review
respond_to do |format|
format.html
format.json do
render json: ReviewExpensesDatatable.new(view_context)
render json: ExpensesDatatable.new(view_context)
end
end
end

def approve
@expense = Expense.find(params[:id])
@expense.update!(status: "Approved")
render "update_status"
change_expense_status("Approved")
end

def reject
@expense = Expense.find(params[:id])
@expense.update!(status: "Rejected")
render "update_status"
change_expense_status("Rejected")
end

def pend
@expense = Expense.find(params[:id])
@expense.update!(status: "Pending")
render "update_status"
end

def create
@expense = current_user.expenses.build(expense_params)
@expense.save
render "create_or_update"
end

def destroy
find_expense
@expense.destroy!
end

def edit
find_expense
end

def update
find_expense
@expense.update(expense_params)
render "create_or_update"
change_expense_status("Pending")
end

private

def find_expense
@expense = current_user.expenses.find(params[:id])
@expense = Expense.find(params[:id])
end

def expense_params
params.require(:expense).permit(:date, :category_id, :description, :currency, :amount)
def change_expense_status(new_status)
find_expense
@expense.update!(status: new_status)
render "update_status"
end

end
41 changes: 41 additions & 0 deletions app/controllers/user/expenses_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class User::ExpensesController < ApplicationController
def index
respond_to do |format|
format.html
format.json do
render json: User::ExpensesDatatable.new(view_context, current_user.id)
end
end
end

def create
@expense = current_user.expenses.build(expense_params)
@expense.save
render "create_or_update"
end

def destroy
find_expense
@expense.destroy!
end

def edit
find_expense
end

def update
find_expense
@expense.update(expense_params)
render "create_or_update"
end

private

def find_expense
@expense = current_user.expenses.find(params[:id])
end

def expense_params
params.require(:expense).permit(:date, :category_id, :description, :currency, :amount)
end
end
53 changes: 36 additions & 17 deletions app/models/expenses_datatable.rb
Original file line number Diff line number Diff line change
@@ -1,59 +1,78 @@
class ExpensesDatatable
include DatatablesHelper
delegate :params, :link_to, :fa_icon, :number_with_precision, to: :view_context
attr_reader :view_context
delegate :params, :link_to, :fa_icon, :number_with_precision, to: :view_context

def initialize(view_context, current_user_id)
def initialize(view_context)
@view_context = view_context
@current_user_id = current_user_id
@display_records = get_records
end

private

def columns
%i(s_user_name s_date s_category_name s_description s_currency s_amount s_status s_user_name)
end

def current_user_id
@current_user_id
%w(s_user_name s_department_name s_job_title_name s_date s_category_name s_description s_currency s_amount s_status s_user_name s_user_name)
end

def total_record_count
Expense.search { with(:user_id, current_user_id) }.total
Expense.search.total
end

def get_records
c = sort_column
d = sort_direction
Expense.search do
with(:user_id, current_user_id)
fulltext params[:sSearch]
order_by(c, d)
paginate page: page, per_page: per
end
end

def data
@display_records.results.map do |expense|
get_records.results.map do |expense|
[
edit_link(expense),
expense.user_name,
expense.department_name,
expense.job_title_name,
expense.date,
expense.category_name,
expense.description,
expense.currency,
number_with_precision(expense.amount, precision: 2),
expense.status,
destroy_link(expense)
status_change_link_one(expense),
status_change_link_two(expense),
]
end
end

def edit_link(expense)
link_to fa_icon('pencil', text: 'edit'), [:edit, expense], remote: true, id: "edit_expense_#{expense.id}", class: "edit"
def status_change_link_one(expense)
expense.rejected? ? pend_link(expense) : reject_link(expense)
end

def status_change_link_two(expense)
expense.approved? ? pend_link(expense) : approve_link(expense)
end

def status_change_link(expense, type, icon)
link_to fa_icon(icon, text: type),
[type, expense],
method: :patch,
remote: true,
id: "#{type}_expense_#{expense.id}",
class: "status-link #{type}",
data: { confirm: "#{type} expense?" }
end

def pend_link(expense)
status_change_link(expense, :pend, "step-backward")
end

def reject_link(expense)
status_change_link(expense, :reject, "times")
end

def destroy_link(expense)
link_to fa_icon('times', text: 'delete'), expense, method: :delete, remote: true, id: "destroy_expense_#{expense.id}", class: "destroy", data: { confirm: "Delete expense?" }
def approve_link(expense)
status_change_link(expense, :approve, "check")
end
end
79 changes: 0 additions & 79 deletions app/models/review_expenses_datatable.rb

This file was deleted.

61 changes: 61 additions & 0 deletions app/models/user/expenses_datatable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
class User
class ExpensesDatatable
include DatatablesHelper
delegate :params, :link_to, :fa_icon, :number_with_precision, to: :view_context
attr_reader :view_context

def initialize(view_context, current_user_id)
@view_context = view_context
@current_user_id = current_user_id
@display_records = get_records
end

private

def columns
%i(s_user_name s_date s_category_name s_description s_currency s_amount s_status s_user_name)
end

def current_user_id
@current_user_id
end

def total_record_count
Expense.search { with(:user_id, current_user_id) }.total
end

def get_records
c = sort_column
d = sort_direction
Expense.search do
with(:user_id, current_user_id)
fulltext params[:sSearch]
order_by(c, d)
paginate page: page, per_page: per
end
end

def data
@display_records.results.map do |expense|
[
edit_link(expense),
expense.date,
expense.category_name,
expense.description,
expense.currency,
number_with_precision(expense.amount, precision: 2),
expense.status,
destroy_link(expense)
]
end
end

def edit_link(expense)
link_to fa_icon("pencil", text: "edit"), [:edit, :user, expense], remote: true, id: "edit_expense_#{expense.id}", class: "edit"
end

def destroy_link(expense)
link_to fa_icon("times", text: "delete"), [:user, expense], method: :delete, remote: true, id: "destroy_expense_#{expense.id}", class: "destroy", data: { confirm: "Delete expense?" }
end
end
end
7 changes: 5 additions & 2 deletions app/views/expenses/_table.html.erb
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
<table id="expenses" class="datatable hover" data-source="<%= expenses_url(format: :json)%>">
<table id="expenses" class="datatable hover" data-source="<%= expenses_path(format: :json) %>">
<thead>
<tr>
<td></td>
<td>User</td>
<td>Department</td>
<td>Job title</td>
<td>Date</td>
<td>Category</td>
<td>Description</td>
<td>Currency</td>
<td>Amount</td>
<td>Status</td>
<td></td>
<td></td>
</tr>
</thead>
<tbody>
Expand Down
6 changes: 2 additions & 4 deletions app/views/expenses/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<h1>My expenses</h1>

<h1>Review Expenses</h1>
<hr></hr>
<%= render "form", expense: Expense.new %>
<br></br>

<%= render "table" %>
4 changes: 0 additions & 4 deletions app/views/expenses/review.html.erb

This file was deleted.

Loading

0 comments on commit c4bcafb

Please sign in to comment.