select * from users where ...
Reward.by_status(status).
by_reason(reason).
where(:created_at => from_created_at..to_created_at).
by_campaigns(selected_campaigns)
User.where(:created_at => Date.today)
User.where(:created_at => Date.today.beginning_of_day..Date.today.end_of_day)
User.where(:created_at => -Infinity..1.month.ago.end_of_day)
def convert_date_to_timestamp(value)
if !value
value
elsif value.is_a?(Array)
[value.first.try(:beginning_of_day), value.last.try(:end_of_day)]
elsif value.is_a?(Range)
(value.first.beginning_of_day..value.last.end_of_day)
else
value.beginning_of_day..value.end_of_day
end
%table
%tr
%th Id
%th Email
%th Name
%th Creation Date
- @users.each do |user|
%tr
%td= user.id
%td= user.email
%td= user.name
%td= user.created_at.to_date
column(:id)
column(:email)
column(:name)
column(:created_at) do |model|
model.created_at.to_date
end
scope { User.includes(:profile).order("created_at desc") }
filter(:email, :string, options) do |value|
where("email ilike '%#{value}%'")
end
column(:created_at) do |user|
user.created_at.to_date
end
filter(:created_at, :date, :range => true, :default => (1.month.ago.to_date..Date.today))
filter(:campaign_type, :enum, :select => CAMPAIGN_TYPES) do |value|
where(:type => value)
end
filter(:tag_name, :enum, :select => :tag_names) do |value|
by_tag_name(value)
end
filter(:active, :eboolean) do |value|
value == "YES" ? active : inactive
end
column(:related_email, mandatory: true) do |offer|
offer.person.email
end
column(:short_link, mandatory: true) do |offer|
format(offer.short_url) do |value|
link_to("Claim", value)
end
end
column(:campaign, mandatory: true) do |offer|
campaign = offer.campaign
format(campaign.name) do |value|
link_to(value, site_campaign_path(campaign.site, campaign))
end
end
%w(share click visit referral).each do |action|
column(action, header: action.pluralize.humanize) do |offer|
offer.activities.by_actions(action).count
end
end
class UsersController
def index
@grid = UsersGrid.new(params[:users_grid])
@assets = @grid.assets.page(params[:page])
end
end
= datagrid_form_for @grid, :url => report_path
= datagrid_table @grid, @assets
grid = ProjectsGrid.new(params[:my_grid]) do |scope|
scope.where(:owner_id => current_user.id)
end
column(
:priority,
# suppose that models with null priority will be always on bottom
:order => "priority is not null desc, priority",
:order_desc => "prioritty is not null desc, priority desc"
)
column(:name) do |asset|
format(asset.name) do |value|
content_tag(:strong, value)
end
end
column(:profile_updated_at, :order => proc { |scope|
scope.join(:profile).order("profiles.updated_at")
}) do |model|
model.profile.updated_at.to_date
end
filter(:posts_count, :integer, :range => true, :default => [1, nil])
filter(:id, :integer, :multiple => true)
Grid.new(:id => "1,2").assets # => select * from <table> where id in (1,2)
rake datagrid:copy_partials
app/views/datagrid
├── _form.html.erb
├── _head.html.erb
├── _order_for.html.erb
├── _row.html.erb
└── _table.html.erb
column(:new_sales, :tooltip => "Amount of sales comming from referral programs")
<th class="<%= datagrid_column_classes(grid, column) %>">
<%= column.header %>
+ <% if text = column.options[:tooltip] %>
+
+ <% end %>
<%= datagrid_order_for(grid, column) if column.order && options[:order]%>
</th>
class DatagridActiveAdmin
def self.integrate_datagrid(context, grid_class)
context.config.filters = false
context.config.paginate = false
context.send :collection_action, :index do
datagrid = grid_class.new(params[grid_class.param_name])
respond_to do |f|
f.html do
render template: "admin/datagrid", locals: {datagrid: datagrid}, layout: 'active_admin'
end
f.csv do
send_data(@grid.to_csv, type: 'text/csv',
filename: "#{datagrid.class.to_s.underscore}-#{DateTime.now}.csv")
end
end
end
end
end
ActiveAdmin.register User do
DatagridActiveAdmin.integrate_datagrid(self, UsersGrid)
end
People don't like learning APIs. It is good learn less and do more.
# Bad DSL
2+2
# Good DSL
2.add :to => 2
column(:created_at, format: :date)
module DatagridExtension
def date_column(name)
column(name) do |model, grid|
I18n.localize(model.send(name).to_date, :locale => grid.current_locale)
end
end
end
class UsersGrid
include MyProjectDatagrid
def current_locale
describe User do
context do
before do
puts self.inspect
end
end
end
#<RSpec::Core::ExampleGroup::Nested_1:0x007fcc550a40a0 ... >
It is really hard to maintain a documentation
for 30+ methods
with 200+ options.