Mike
Mike

Reputation: 159

Submitting ActiveAdmin form to non-ActiveRecord methods

In ActiveAdmin, I have methods that measure what features users are using. These methods are specific to ActiveAdmin, and will not be used in the app. The method could, for example, tell us how many daily_reports have been created across all companies from July 5, 2013 to September 15, 2013. The method returns the data as a hash.

In the ActiveAdmin interface, I need a form. In this form, I will specify the time range of the query, the 'created or updated' option, and specific data types to track. I'd like the form to look something like this:

ActiveAdmin.register_page "Metrics" do

form do |f|

    # radio buttons for yearly, monthly...etc. calls metrics_histogram_by_relative
    # one option is 'range'. When selected, it calls metrics_histogram_by_range
        # renders a small range text box 

    # radio buttons for created or updated

    # check boxes for data types

    # check boxes for companies, default: all

    # f.submit

end

The range method's header looks like this:

def metrics_histogram_by_range(start_day, end_day, updated_or_created, 
                           data_types=[:itbs, :projects, :daily_report_entries, :schedules]),
                           companies = Company.find_each)

The relative time method header looks like this:

def metrics_histogram_by_relative(time_span, updated_or_created, 
                              data_types=[:itbs, :projects, :daily_report_entries, :schedules],
                              companies = Company.find_each))

Where 'time_span' will be a string, 'yearly', 'monthly', 'weekly', or 'daily'.

I don't know how to wire up the form so that the inputs will submit their data to these methods, and get the response. The form would need to submit to metrics_histogram_by_range if the range radio button is selected. Otherwise it would submit to metrics_histogram_by_relative. Is there a way to do this, or will I need to combine these methods?

Upvotes: 0

Views: 441

Answers (1)

taki12345
taki12345

Reputation: 125

You have to override the default ActiveAdmin create method. Insert a similar code to this after your form block:

controller do
  def create
    if permitted_params[:metrics][:radios] == "radio_relative"
      # Call metrics_histogram_by_relative(). You can access the form values in the array: permitted_params[:metrics]
    else 
      # Call metrics_histogram_by_range()
    end

    # You can do changes in Metrics using the global variable.
    @metrics = Metrics.new
    @metrics.companies = "all"      # The other attributes not changed here will set up by the filled form.

    create! do |succes, failure|
    end
  end
end

Upvotes: 1

Related Questions