diff options
Diffstat (limited to 'app/controllers/concerns')
| -rw-r--r-- | app/controllers/concerns/iev_interfaces.rb | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/app/controllers/concerns/iev_interfaces.rb b/app/controllers/concerns/iev_interfaces.rb new file mode 100644 index 000000000..590482c7f --- /dev/null +++ b/app/controllers/concerns/iev_interfaces.rb @@ -0,0 +1,65 @@ +module IevInterfaces + extend ActiveSupport::Concern + + included do + before_action only: [:index] { set_date_time_params("started_at", DateTime) } + before_action :ransack_status_params, only: [:index] + respond_to :html + belongs_to :workbench + end + + def show + show! do + instance_variable_set "@#{collection_name.singularize}", resource.decorate(context: { + workbench: @workbench + }) + end + end + + def index + index! do |format| + format.html { + if collection.out_of_bounds? + redirect_to params.merge(:page => 1) + end + collection = decorate_collection(collection) + } + end + end + + protected + def collection + scope = parent.send(collection_name).where(type: "#{resource_class.parent.name}::Workbench") + + scope = self.ransack_period_range(scope: scope, error_message: t("#{collection_name}.filters.error_period_filter"), query: :where_started_at_in) + + @q = scope.search(params[:q]) + + unless instance_variable_get "@#{collection_name}" + coll = if sort_column && sort_direction + @q.result(distinct: true).order(sort_column + ' ' + sort_direction).paginate(page: params[:page], per_page: 10) + else + @q.result(distinct: true).order(:name).paginate(page: params[:page], per_page: 10) + end + instance_variable_set "@#{collection_name}", decorate_collection(coll) + end + instance_variable_get "@#{collection_name}" + end + + private + def ransack_status_params + if params[:q] + return params[:q].delete(:status_eq_any) if params[:q][:status_eq_any].empty? || ( (resource_class.status.values & params[:q][:status_eq_any]).length >= 4 ) + params[:q][:status_eq_any].push("new", "running") if params[:q][:status_eq_any].include?("pending") + params[:q][:status_eq_any].push("aborted", "canceled") if params[:q][:status_eq_any].include?("failed") + end + end + + def sort_column + parent.imports.column_names.include?(params[:sort]) ? params[:sort] : 'created_at' + end + + def sort_direction + %w[asc desc].include?(params[:direction]) ? params[:direction] : 'desc' + end +end |
