Skip to content

Instantly share code, notes, and snippets.

@boie0025
Last active September 11, 2017 21:57
Show Gist options
  • Save boie0025/061ecfb72bf9d489843f454cdafd59f0 to your computer and use it in GitHub Desktop.
Save boie0025/061ecfb72bf9d489843f454cdafd59f0 to your computer and use it in GitHub Desktop.
ActiveAdmin Model Stubs - Form/Service objects
# Created by essentially answering all of the methods that were called on a PORO. Adjust as you see fit
module ActiveAdminModelStubs
extend ActiveSupport::Concern
# duck punch - ActiveAdmin uses these for index page. We don't have an index.
module ClassMethods
# Columns makes no sense in the context of a form.
def columns(*)
[]
end
def reorder(*)
self
end
def ransack(*)
self
end
def result(*)
self
end
def page(*)
self
end
def per(*)
self
end
def except(*)
self
end
def group_values(*)
self
end
def count(*)
self
end
def num_pages(*)
1
end
def total_pages(*)
1
end
def current_page(*)
1
end
def limit_value(*)
false
end
def total_count(*)
1
end
end
end
module SomeNamespace
class SomeProcessForm
include ActiveAdminModelStubs
include ActiveModel::Model
attr_accessor :attr_1, :attr_2
# We're just overriding this since we're overriding the display completely
def self.column_names
[]
end
def perform!
SomeServiceClass.new(attr_1: attr_1, attr_2: attr_2, opt_a: :some_static_option).perform!
end
end
end
ActiveAdmin.register SomeNamespace::SomeProcessForm do
menu label: 'Some Process', parent: 'Some Namespace'
permit_params :attr_1, :attr_2 # Make sure to update these to match the attributes as you see fit on your form object!
config.batch_actions = false
actions :all, except: [:new, :destroy, :edit]
controller do
def show
# A lot of times we redirect on show to something more relevant, like an instance of what the form manipulates
end
private
def collection
# Return a list of objects to show in the index
SomeModel.all.page(params[:page]).per(30) # Make sure to add the pagination methods!
end
end
index download_links: false do
column :attr_1
column :attr_2
column :some_other_derived_value do |obj|
# do something with the object
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment