Skip to content

Instantly share code, notes, and snippets.

@groony
Last active June 14, 2016 07:56
Show Gist options
  • Save groony/b8a8f15cc4f592fc8380 to your computer and use it in GitHub Desktop.
Save groony/b8a8f15cc4f592fc8380 to your computer and use it in GitHub Desktop.
module ExtendedActionItems
private
def add_default_action_items
super
add_default_toggle_filter_action_item
end
def add_default_toggle_filter_action_item
add_action_item :filter, only: :index do
link_to I18n.t('active_admin.filter_button'), '#', class: 'filter_button'
end
end
end
module ActiveAdmin
module ViewHelpers
include ApplicationHelper
end
class ResourceController < BaseController
before_action :patch_params, only: :index
after_action :store_location, only: :index
QUERY_DATE_FIELDS = %i(created_at updated_at last_visit_at)
def patch_params
return unless params[:q].present?
QUERY_DATE_FIELDS.each do |field|
field_lteq = "#{field}_lteq"
next unless params[:q][field_lteq.to_sym].present?
params[:q][field_lteq.to_sym] = send(field_lteq)
end
end
QUERY_DATE_FIELDS.each do |field|
field_lteq = "#{field}_lteq"
define_method(field_lteq) do
params[:q][field_lteq.to_sym].to_date.end_of_day
end
end
def store_location
session[:return_to] = request.get? ? request.original_url : request.referer
end
def redirect_back(*options)
tag_options = {}
options.first.each { |k, v| tag_options[k] = v } unless options.empty?
path = session.delete(:return_to) || :back
redirect_to path, tag_options
end
end
class Resource
prepend ExtendedActionItems
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment