Metric Admin

This gem builds and edits metrics.

Updating

Bump version in version.rb Commit rake build rake release

Installation

Clone this repo to your local computer.

Add this line to your application's Gemfile:

gem 'metric_admin'

And then execute:

$ bundle install

Usage

Create a metric_admin.js file then add the following snippet:

$(function(){
  $(".list-group").sortable({
    update: function(event, ui) {
      var data = $(this).sortable('serialize');
      //alert(data);
      $.ajax({
        data: data,
        type: 'POST',
        url: '/metrics/sort'
      });
    }
  });
  // this excludes the div with class header from being sorted.
  $(".list-group").sortable({ cancel: '.header' });
});

Also ensure that you are allowing for the CSRF Meta tags:

$(function(){
  $.ajaxSetup({
    beforeSend: function( xhr ) {
      var token = $('meta[name="csrf-token"]').attr('content');
      if (token) xhr.setRequestHeader('X-CSRF-Token', token);
    }
  });
});

In your config/routes.rb, find the admin block of routes and add the following:

admin.connect '/metrics/sort', :action=>"sort", :controller => "metrics"

Your admin routes block should look like below:

#Admin routes
map.namespace :admin do |admin|
  admin.resources :metrics, :controller => 'metrics'
  admin.connect '/metrics/:id/evaluate.js', :action=>"evaluate", :controller => "metrics"
  admin.connect '/metrics/sort', :action=>"sort", :controller => "metrics"
  admin.connect '/metrics/:id/clone', :action=>"clone", :controller => "metrics"
  admin.connect '/metrics/:id/calculate', :action=>"calculate", :controller => "metrics"
  admin.connect '/admin/:controller/:action/:id', :action=>"destroy"
  admin.resources :resources, :controller => 'resources'
end

Make a new css file metric_admin.css in public/css and add the following:

div#simpleList {
  margin-bottom: 2em;
}

h2 {
  margin-left: 1.2em;
}

div.list-group {
  border: 1px solid #ccc;
  border-radius: 3px;
}

div.list-group-item < :first-child {
  border: none;
}

div.header {
  padding: 10px 0px 6px 10px;
  background-color: #d5d5d5;
}

div.list-group-item {
  border-top: 1px solid #ccc;
  padding: 16px 0px 6px 10px;
}

/*div.list-group-item a {
  padding: 5px 8px 3px 8px;
}*/

div.list-group-item-wrapper.odd {
  background-color: #ededed;
}

div.list-group-item-wrapper.even {
  background-color: #fff;
}

div.btn-toolbar {
  margin-top: -5px;
}

button.dropdown-toggle {
  padding: 12px;
  margin-right: 1em;
  outline: none;
}

div.btn-toolbar span.caret {
  display: inline-block !important;
  margin-top: 0px !important;
}

div.btn-toolbar ul {
  margin-left: -65px;
}

Link the metric_admin.js and the metric_admin.css in the admin view in layouts/admin.html.haml

Also ensure that you have linked in the admin layout the lastest jquery.ui.min.js file.

Ensure that the metric table has a sort column.

In the dashboard application modify app/models/metric.rb

attr_accessible :title, :name, :description, :logic, :status, :sort, :resource_type_id, :has_detail, :data_type, :metric_summaries_attributes

has_many :metric_summaries
accepts_nested_attributes_for :metric_summaries, :allow_destroy => true, :reject_if => lambda { |a| a[:title].blank? }

In config/routes.rb

admin.resources :metrics, :controller => 'metrics' do |m|
  m.resources :metric_summaries
end

Contributing

  1. Fork it ( https://github.com/analyticsforlawyers/afl_teamwork/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request