Sha256: 8abc4c1fa895cab60cf5d24c7bb2268dbefa61b7de2f56e06417f9c18a9a1b24

Contents?: true

Size: 1.73 KB

Versions: 5

Compression:

Stored size: 1.73 KB

Contents

require_dependency "prosperity/application_controller"

module Prosperity
  class GraphsController < ApplicationController
    before_action :get_graph, only: [:edit, :update, :show]
    def new
      @graph = Graph.new
    end

    def edit
      @metrics = MetricFinder.all.map(&:new)
      @options = @metrics.inject({}) do |h, metric|
        h[metric.id] = metric.options.keys
        h
      end
      @graph.graph_lines.build
    end

    def show
      render json: {
        title: @graph.title,
        extractors: @graph.graph_lines.map do |line|
          {
            key: line.extractor,
            url: data_metric_path(id: line.metric, 
                                  extractor: line.extractor, 
                                  option: line.option, 
                                  period: @graph.period, 
                                  start_time: start_time, 
                                  end_time: end_time),
          }
        end
      }
    end

    def update
      unless @graph.update_attributes(graph_params)
        set_error(@graph)
      end
      redirect_to action: :edit
    end

    def create
      @graph = Graph.new
      [:title, :period].each do |attr|
        @graph.send("#{attr}=", graph_params[attr])
      end

      if @graph.save
        redirect_to edit_graph_path(@graph)
      else
        set_error(@graph)
        render action: :new
      end
    end
    
    private

    def get_graph
      @graph = Graph.find(params[:id])
    end

    def graph_params
      if strong_params?
        params.require(:graph).
          permit(Graph::ATTR_ACCESSIBLE + [:graph_lines_attributes => (GraphLine::ATTR_ACCESSIBLE + [:id])])
      else
        params.fetch(:graph, {})     
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
prosperity-0.0.6 app/controllers/prosperity/graphs_controller.rb
prosperity-0.0.5 app/controllers/prosperity/graphs_controller.rb
prosperity-0.0.4 app/controllers/prosperity/graphs_controller.rb
prosperity-0.0.3 app/controllers/prosperity/graphs_controller.rb
prosperity-0.0.2 app/controllers/prosperity/graphs_controller.rb