Skip to content

Instantly share code, notes, and snippets.

@beck03076
Created July 28, 2013 16:05
Show Gist options
  • Save beck03076/6099093 to your computer and use it in GitHub Desktop.
Save beck03076/6099093 to your computer and use it in GitHub Desktop.
class BranchesDatatable
delegate :image_tag,:edit_branch_path,:params, :h, :link_to, :number_to_currency, to: :@view
def initialize(view)
@view = view
end
def as_json(options = {})
{
sEcho: params[:sEcho].to_i,
iTotalRecords: Branch.count,
iTotalDisplayRecords: branches.total_entries,
aaData: data
}
end
private
def data
branches.map do |branch|
[ "<div class='main-name'>" +
link_to(branch.name,branch) +
"</div>",
(branch.country.name if !branch.country.nil?),
(branch.city.name if !branch.city.nil?),
(branch.desc if !branch.desc.nil?),
"<div class = 'tools dn'> <ul>" +
["<li>" +
link_to(image_tag("/assets/icons/view.png"),branch) +
"</li>", "<li>" +
link_to(image_tag("/assets/icons/edit.png"), edit_branch_path(branch) ) +
"</li>", "<li>" +
link_to(image_tag("/assets/icons/delete.png"),branch,method: :delete, data: { confirm: 'Are you sure?' }) +
"</li>"].join(" ") +
"</ul> </div>"
]
end
end
def branches
@branches ||= fetch_branches
end
def fetch_branches
branches = Branch.order("#{sort_column} #{sort_direction}")
branches = branches.page(page).per_page(per_page)
if params[:sSearch].present?
branches = branches.where("name like :search", search: "%#{params[:sSearch]}%")
end
branches
end
def page
params[:iDisplayStart].to_i/per_page + 1
end
def per_page
params[:iDisplayLength].to_i > 0 ? params[:iDisplayLength].to_i : 10
end
def sort_column
columns = %w[name ]
columns[params[:iSortCol_1].to_i]
end
def sort_direction
params[:sSortDir_0] == "desc" ? "desc" : "asc"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment