mardi 23 juin 2015

Advanced search in active scaffold

I have two models - Benefit and UserBenefit.

Benefit:

:id [integer]
:created_at [datetime]
:expiration_date [datetime]

UserBenefit:

:id [integer]
:user_id [integer]
:benefit_id [integer]
:active [boolean]
:updated_at [datetime]

I would like to create custom search in active scaffold for Benefit model where will be possible to select month and year and list all Benefits which are active in selected time.
Active time of benefit is from created_at to expiration_date.

So far I have created custom method in Benefit model to return all months with year when benefit is active.

  def dates
    d1 = self.created_at
    d2 = self.expiration_date
    m = []
    while d1 <= d2.at_end_of_month
      m << d1.strftime('%b %Y')
      d1 = d1 + 1.month
    end
    m
  end

Then I have created controller for active scaffold:

class Admin::BenefitsController < AdminController
  active_scaffold :benefit do |config|
    config.columns = :id, :expiration_date, :created_at, :dates
    config.actions = [:list, :field_search, :export, :show, :update, :create]
    config.field_search.columns = :dates
    config.list.per_page = 50
  end
end

and helper for search:

module BenefitHelper
  def benefit_dates_search_column(record, input_name)
    select :record, :dates, options_for_select(Date::ABBR_MONTHNAMES.compact.each_with_index.collect{|m, i| [m, i+1]}), {:include_blank => as_(:_select_)}, input_name
  end
end

There is a moment when problems begins. This select doesn't appear in search and the second thing is that I have no idea how to add year selection.

After this I would like also to list how many Users where active in selected benefits.
Active users - UserBenefit.updated_at - equals to selected date and UserBenefit.active = true

There is the last problem. There is an easy way to add additional column which will count all active users for each Benefit, but how can I update it depending on selected date in search?

Aucun commentaire:

Enregistrer un commentaire