-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from bluerogue251/refactor_controller
Refactor expenses controller
- Loading branch information
Showing
22 changed files
with
297 additions
and
286 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" %> |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.