ruby - Share Variables between Methods in a Rails Controller -


i have rails controller has 2 methods. both methods use of same variables , i'm wondering how can refactor either method in model of somewhere in controller make them more reusable now.

class chartscontroller < applicationcontroller    before_filter :authenticate_user!, :company_id    def service_level     latest_date = invoice.where(:account_id => @company.accounts).maximum(:invc_date)     invoices_filter = { :invoices => { :invc_date => (latest_date - 3.months)..latest_date } }      invoices = invoice.where({:account_id => @company.accounts}.merge(invoices_filter))     details = invoicedetail.joins(:type).where(:invoice_id => invoices)     freight_details = details.where(:invoice_detail_types => { :category => 'freight' })      freight_groups = freight_details.group(:family).select("family, count(distinct package_id), sum(base_charge + discount)")       vol_data = {}     spend_data = {}      @charts = {}       @charts[:service_analysis] = {       :vol_data => hash[freight_groups.map { |row| [invoicedetailfamily[row.family].name, row.count.to_i] }],       :spend_data => hash[freight_groups.map { |row| [invoicedetailfamily[row.family].name, row.sum.to_f] }]     }      render partial: 'service_level'   end    def weight_summary      latest_date = invoice.where(:account_id => @company.accounts).maximum(:invc_date)     invoices_filter = { :invoices => { :invc_date => (latest_date - 3.months)..latest_date } }      invoices = invoice.where({:account_id => @company.accounts}.merge(invoices_filter))     details = invoicedetail.joins(:type).where(:invoice_id => invoices)     freight_details = details.where(:invoice_detail_types => { :category => 'freight' })     packages = freight_details.joins(:package, :invoice)        vol_data = {}     spend_data = {}     packages.group(:zone).select("zone, count(distinct package_id), sum(base_charge + discount)").each |row|       case row.zone         when '02'..'08', '002'..'008', '102'..'108', '132'..'138', '202'..'208', '242'..'248', '302'..'308'           zg = row.zone[-1]         when '09'..'17', '124'..'126', '224'..'226'           zg = 'ak/hi/pr'         else           zg = 'import/export'       end       vol_data[zg] = (vol_data[zg] || 0) + row.count.to_i       spend_data[zg] = (spend_data[zg] || 0) + row.sum.to_f     end     @charts = {}      @charts[:weight_analysis] = {       :vol_data => hash[(vol_data.sort_by {|key, value| key.scan(/\d+/)[0].to_i})],       :spend_data => hash[(spend_data.sort_by {|key, value| key.scan(/\d+/)[0].to_i})]     }       render partial: 'weight_summary'   end end 

i suggest use model class method processing data. example

freight_details = details.where(:invoice_detail_types => { :category => 'freight' })  freight_groups = freight_details.group(:family).select("family, count(distinct package_id), sum(base_charge + discount)")   vol_data = {} spend_data = {}  @charts = {}   @charts[:service_analysis] = {   :vol_data => hash[freight_groups.map { |row| [invoicedetailfamily[row.family].name, row.count.to_i] }],   :spend_data => hash[freight_groups.map { |row| [invoicedetailfamily[row.family].name, row.sum.to_f] }] } 

could moved model class method returns charts. in same way can refactor second method. kind of business logic , data proccessing should handled in models

moreover can see there many unused local variables there in controller. controller should thin as possible.


Comments

Popular posts from this blog

android - Inheriting from Theme.AppCompat* -

broadcastreceiver - android BOOT_COMPLETED not received if not activity intent-filter -

basic authentication with http post params android -