User Grid (basic) Time Sheets Grid (advanced)

Developer Project Report hours
Gonzalo Blanda animi aperiam tenetur 49.0
Gonzalo Blanda doloribus hic corporis 45.0
Gonzalo Blanda eum amet quia 74.0
Gonzalo Blanda odio velit repudiandae 84.0
Gonzalo Blanda perspiciatis totam perferendis 79.0
Gonzalo Blanda quibusdam sed sit 79.0
Gonzalo Blanda repudiandae officiis qui 34.0
Gonzalo Blanda saepe hic temporibus 104.0
Gonzalo Blanda suscipit consectetur beatae 109.0
Gonzalo Blanda vitae eum pariatur 78.0
Grace Kris animi aperiam tenetur 42.0
Grace Kris doloribus hic corporis 79.0
Grace Kris eum amet quia 34.0
Grace Kris odio velit repudiandae 51.0
Grace Kris perspiciatis totam perferendis 53.0
Grace Kris quibusdam sed sit 47.0
Grace Kris repudiandae officiis qui 37.0
Grace Kris saepe hic temporibus 65.0
Grace Kris suscipit consectetur beatae 42.0
Grace Kris vitae eum pariatur 72.0
Gwendolyn Satterfield animi aperiam tenetur 90.0
Gwendolyn Satterfield doloribus hic corporis 47.0
Gwendolyn Satterfield eum amet quia 87.0
Gwendolyn Satterfield odio velit repudiandae 68.0
Gwendolyn Satterfield perspiciatis totam perferendis 93.0

Grid:

class TimeEntriesGrid < BaseGrid

  #
  # Scope
  #

  scope do
    User.select(
      "users.name",
      "projects.name as project_name",
      "accounts.name as account_name",
      "sum(time_entries.hours) as report_hours"
    ).joins(
      time_entries: {project: :account}
    ).group("projects.name", "users.name", "accounts.name").
    order("users.name")
  end


  #
  # Filters
  #

  filter(
    :project_id, :enum,
    :select => :project_id_select,
    :multiple => true,
    :include_blank => false
  ) do |value|
    self.where(:time_entries => {:project_id => value})
  end


  filter(
    :year, :enum,
    :select => :year_select,
    :include_blank => false,
    :default => lambda {Date.today.year}
  ) do |value|
    self.where([
      "extract(year from time_entries.date) = ?",
      value.to_i
    ])
  end

  filter(
    :month, :enum,
    :select => :month_select,
    :include_blank => false,
    :default => lambda {Date.today.month}
  ) do |value|

    self.where([
      "extract(month from time_entries.date) = ?",
      value.to_i
    ])
  end


  #
  # Columns
  #

  column(:user_name, :header => "Developer", :order => "users.name") do
    self.name
  end
  column(:project_name, :header => "Project", :order => "projects.name")
  column(
    :account_name,
    :header => "Company",
    :order => "accounts.name",
  ) do |model|
    format(model.account_name) do
      render :partial => "time_entries/company", :locals => {:model => model}
    end
  end

  column(:report_hours, order: "sum(time_entries.hours)")

  protected

  def year_select
    TimeEntry.all.any? ? (TimeEntry.minimum(:date).year..TimeEntry.maximum(:date).year) : []
  end
  def month_select
    Date::MONTHNAMES[1..12].enum_for(:each_with_index).collect {|name, index| [name, index + 1]}
  end

  def project_id_select
    Project.all.map {|p| [p.name, p.id]}
  end
end

Controller:

class TimeEntriesController < ApplicationController

  def index
    @time_entries_grid = TimeEntriesGrid.new(params[:g]) do |scope|
      scope.page(params[:page] )
    end
  end
end

View:

.grid-index-grid
  %div{class: 'overflow-x-auto p-4'}
    = datagrid_form_for @time_entries_grid, url: time_entries_path
    %br/
    = datagrid_table(@time_entries_grid)
    = paginate @time_entries_grid.assets, window: 2
  %div
    = render :partial => "shared/source", :object => @time_entries_grid