Refactoring

git-svn-id: https://dev.finn.de/svn/cockpit/branches/reporting_merge@1269 7926756e-e54e-46e6-9721-ed318f58905e
pull/6827/head
t.felgentreff 15 years ago
parent 69590757f2
commit 44fb55868c
  1. 54
      app/controllers/cost_reports_controller.rb
  2. 4
      app/models/cost_query.rb
  3. 1
      app/views/cost_reports/_cost_report_table.rhtml
  4. 2
      app/views/cost_reports/index.rhtml

@ -1,6 +1,6 @@
class CostReportsController < ApplicationController class CostReportsController < ApplicationController
before_filter :find_optional_project, :only => [:index] before_filter :find_optional_project, :only => [:index]
before_filter :set_query, :only => [:index] before_filter :query, :only => [:index]
helper :reporting helper :reporting
include ReportingHelper include ReportingHelper
@ -9,11 +9,6 @@ class CostReportsController < ApplicationController
render :layout => !request.xhr? render :layout => !request.xhr?
end end
def set_query
@query = query
@walker = walker(@query)
end
## ##
# Determines if the request contains filters to set # Determines if the request contains filters to set
def set_filter? def set_filter?
@ -21,47 +16,46 @@ class CostReportsController < ApplicationController
end end
## ##
# Determines whether the session-saved query can to be used # Find a query to search on and put it in the session
def session_query? def query_parameters
!session[:cost_query].nil? filters = http_query_parameters if set_filter?
filters ||= session[:cost_query]
filters ||= default_query_parameters
session[:cost_query] = filters
end end
# FIXME: Remove € symbol ##
def walker(query) # Extract active filters from the http params
walker = CostQuery::Walker.new(query) def http_query_parameters
walker params[:fields].inject({}) do |hash, field|
hash[:operators][field] = params[:operators][field]
hash[:values][field] = params[:values][field]
end
end end
## ##
# Find a query to search on and put it in the session # Set a default query to cut down initial load time
def query_params def default_query_parameters
filters = session[:cost_query] if session_query? hash = {:operators => {:user_id => "=", :tweek => "="},
if set_filter?
filters = {}
filters[:operators] = Hash[*params[:operators].select do |filter, op|
params[:fields].include? filter.to_s
end.flatten]
filters[:values] = params[:values]
end
filters ||= {:operators => {:user_id => "=", :tweek => "="},
:values => {:user_id => [User.current.id], :tweek => [Date.today.cweek]}} :values => {:user_id => [User.current.id], :tweek => [Date.today.cweek]}}
session[:cost_query] = filters if @project
filters hash[:operators].merge! :project_id => "="
hash[:values].merge! :project_id => [@project.id]
end
hash
end end
## ##
# Build the query from the current request and save it to # Build the query from the current request and save it to
# the session. # the session.
def query def query
filters = query_params filters = query_parameters
CostQuery.new.tap do |q| @query = CostQuery.new.tap do |q|
filters[:operators].each do |filter, operator| filters[:operators].each do |filter, operator|
unless filters[:values][filter].nil?
q.filter(filter.to_sym, q.filter(filter.to_sym,
:operator => operator, :operator => operator,
:values => filters[:values][filter]) :values => filters[:values][filter])
end end
end
end. end.
column(:tweek).column(:tyear). column(:tweek).column(:tyear).
row(:project_id).row(:user_id) row(:project_id).row(:user_id)

@ -20,6 +20,10 @@ class CostQuery < ActiveRecord::Base
@transformer ||= CostQuery::Transformer.new self @transformer ||= CostQuery::Transformer.new self
end end
def walker
@walker ||= CostQuery::Walker.new self
end
def add_chain(type, name, options) def add_chain(type, name, options)
chain type.const_get(name.to_s.camelcase), options chain type.const_get(name.to_s.camelcase), options
@transformer, @table = nil, nil @transformer, @table = nil, nil

@ -17,7 +17,6 @@
walker.for_cell do |result| walker.for_cell do |result|
"<td>#{result.real_costs} €</td>" "<td>#{result.real_costs} €</td>"
end end
%> %>
<table style='text-align: center' border='1'> <table style='text-align: center' border='1'>

@ -38,4 +38,4 @@
<% end %> <% end %>
<h1>Die sch&ouml;ne Tabelle:</h1> <h1>Die sch&ouml;ne Tabelle:</h1>
<%= render :partial => "cost_report_table", :locals => {:query => @query, :walker => @walker} %> <%= render :partial => "cost_report_table", :locals => {:query => @query, :walker => @query.walker} %>

Loading…
Cancel
Save