module MetricAdmin class Admin::MetricsController < ApplicationController layout "admin" def index @metric_categories = ResourceType.all end def show @metric = Metric.find(params[:id]) @metricnote = MetricNote.new end def edit @metric_categories = ResourceType.all @metric = Metric.find(params[:id]) @resources = Resource.all @resource_type = ResourceType.find_by_id(@metric.resource_type_id) 1.times { @metric.metric_summaries.build } end def new @metric = Metric.new @metric.resource_type_id = params[:resource_type_id] @metric.resource_type = params[:resource_type] end def destroy if @metric = Metric.find(params[:id]) @metric.destroy flash[:success] = "Metric has been removed." redirect_to admin_metrics_url else flash[:alert] = "Metric has not been removed." redirect_to admin_metrics_url end end def create @metric = Metric.new(params[:metric]) @metric.has_detail = 1 @metric.data_type = "integer" if @metric.save flash[:success] = "#{@metric.title} has been created" redirect_to admin_metrics_url else flash[:notice] = "There was an error" render :action => :new, :status => :unprocessable_entity end end def sort params[:metric].each_with_index do |metric_id, i| @metric = Metric.find(metric_id) @metric.update_attribute(:sort, i) end render :text => "sorted" end def clone @metric_to_clone = Metric.find(params[:id]) @metric = @metric_to_clone.clone @metric.title = "Copy of #{@metric.title}" @metric.sort = Metric.maximum(:sort, :conditions=>["resource_type_id = ?", @metric_to_clone.resource_type_id ]).to_i + 1 if @metric.save flash[:notice] = "#{@metric.title} has been cloned" redirect_to edit_admin_metric_url(@metric) else flash[:notice] = "There was an error" render :action => :new, :status => :unprocessable_entity end end def calculate CalculateMetrics.calculate_metrics_for_id(params[:id]) flash[:notice] = "Completed calculating" redirect_to admin_metrics_url end def evaluate metric_logic = params["logic"].gsub(":staff", params[:logic_staff]) error = {} res = Thread.new{ require 'rubygems' require 'active_support' begin @has_error = false ActiveRecord::Base.establish_connection "needles_#{RAILS_ENV}" @result, @results = eval(metric_logic) ActiveRecord::Base.verify_active_connections! [@result,@results] rescue Exception => e logger.info "Error Message: #{e.message}" @has_error = true @errors = [e.message, e.backtrace.join("\r\n")].join("\r\n") end } @result, @results = res.value @results = @results respond_to do |format| format.js end end # PUT /admin/metrics/id #---------------------------------------------------------------------------- def update @metric = Metric.find_by_id(params[:metric][:id]) if @metric.update_attributes(params[:metric]) flash[:notice] = "#{@metric.title} has been updated" # CalculateMetrics.calculate_metrics_for_id(@metric.id) redirect_to :action => :edit else render :action => :index, :status => :unprocessable_entity end end end end