Related
I'm getting the following error when I'm trying to query another table in a model definition.
PG::UndefinedTable: ERROR: missing FROM-clause entry for table "miq_user_roles" LINE 1: ..." = $1 AND "service_templates"."display" = $2 AND "miq_user_... ^ [catalog/explorer]
Any idea how to resolve this?
scope :with_service_template_catalog_id, ->(cat_id) { where(:service_template_catalog_id => cat_id) }
scope :without_service_template_catalog_id, -> { where(:service_template_catalog_id => nil) }
scope :with_existent_service_template_catalog_id, -> { where.not(:service_template_catalog_id => nil) }
scope :displayed, -> { where(:display => true) }
scope :public_service_templates, -> { where("miq_user_roles.settings" => nil) }
Here is the full code:
class ServiceTemplate < ApplicationRecord
include SupportsFeatureMixin
DEFAULT_PROCESS_DELAY_BETWEEN_GROUPS = 120
GENERIC_ITEM_SUBTYPES = {
"custom" => N_("Custom"),
"vm" => N_("Virtual Machine"),
"playbook" => N_("Playbook"),
"hosted_database" => N_("Hosted Database"),
"load_balancer" => N_("Load Balancer"),
"storage" => N_("Storage")
}.freeze
SERVICE_TYPE_ATOMIC = 'atomic'.freeze
SERVICE_TYPE_COMPOSITE = 'composite'.freeze
RESOURCE_ACTION_UPDATE_ATTRS = [:dialog,
:dialog_id,
:fqname,
:configuration_template,
:configuration_template_id,
:configuration_template_type].freeze
include CustomActionsMixin
include ServiceMixin
include OwnershipMixin
include NewWithTypeStiMixin
include TenancyMixin
include ArchivedMixin
include CiFeatureMixin
include_concern 'Filter'
include_concern 'Copy'
validates :name, :presence => true
belongs_to :tenant
has_many :service_templates, :through => :service_resources, :source => :resource, :source_type => 'ServiceTemplate'
has_many :services
has_many :service_template_tenants, :dependent => :destroy
has_many :additional_tenants, :through => :service_template_tenants, :source => :tenant, :dependent => :destroy
has_one :picture, :dependent => :destroy, :as => :resource, :autosave => true
belongs_to :service_template_catalog
belongs_to :zone
belongs_to :currency, :inverse_of => false
has_many :dialogs, -> { distinct }, :through => :resource_actions
has_many :miq_schedules, :as => :resource, :dependent => :destroy
has_many :miq_requests, :as => :source, :dependent => :nullify
has_many :active_requests, -> { where(:request_state => MiqRequest::ACTIVE_STATES) }, :as => :source, :class_name => "MiqRequest"
virtual_column :type_display, :type => :string
virtual_column :template_valid, :type => :boolean
virtual_column :template_valid_error_message, :type => :string
virtual_column :archived, :type => :boolean
virtual_column :active, :type => :boolean
default_value_for :internal, false
default_value_for :service_type, SERVICE_TYPE_ATOMIC
default_value_for(:generic_subtype) { |st| 'custom' if st.prov_type == 'generic' }
virtual_has_one :config_info, :class_name => "Hash"
scope :with_service_template_catalog_id, ->(cat_id) { where(:service_template_catalog_id => cat_id) }
scope :without_service_template_catalog_id, -> { where(:service_template_catalog_id => nil) }
scope :with_existent_service_template_catalog_id, -> { where.not(:service_template_catalog_id => nil) }
scope :displayed, -> { where(:display => true) }
scope :public_service_templates, -> { where("miq_user_roles.settings" => nil) }
supports :order do
unsupported_reason_add(:order, 'Service template does not belong to a service catalog') unless service_template_catalog
unsupported_reason_add(:order, 'Service template is not configured to be displayed') unless display
end
alias orderable? supports_order?
alias validate_order supports_order?
def self.with_tenant(tenant_id)
tenant = Tenant.find(tenant_id)
where(:tenant_id => tenant.ancestor_ids + [tenant_id])
end
def self.with_additional_tenants
references(table_name, :tenants).includes(:service_template_tenants => :tenant)
end
def self.all_catalog_item_types
#all_catalog_item_types ||= begin
builtin_catalog_item_types = {
"generic" => N_("Generic"),
"generic_orchestration" => N_("Orchestration"),
}
ExtManagementSystem.subclasses_supporting(:catalog)
.flat_map(&:catalog_types)
.reduce(builtin_catalog_item_types, :merge)
end
end
def self.catalog_item_types
ems_classes = Rbac.filtered(ExtManagementSystem.all).collect(&:class).uniq.select { |ems| ems.supports?(:catalog) }
ci_types = Set.new(ems_classes.flat_map(&:catalog_types).reduce({}, :merge).keys)
ci_types.add('generic_orchestration') if Rbac.filtered(OrchestrationTemplate).exists?
ci_types.add('generic')
all_catalog_item_types.each.with_object({}) do |(key, description), hash|
hash[key] = {:description => description, :display => ci_types.include?(key)}
end
end
def self.create_catalog_item(options, auth_user)
transaction do
create_from_options(options).tap do |service_template|
config_info = options[:config_info].except(:provision, :retirement, :reconfigure)
workflow_class = MiqProvisionWorkflow.class_for_source(config_info[:src_vm_id])
if workflow_class
request = workflow_class.new(config_info, auth_user).make_request(nil, config_info)
service_template.add_resource(request)
end
service_template.create_resource_actions(options[:config_info])
end
end
end
def self.class_from_request_data(data)
request_type = data['prov_type']
if request_type.include?('generic_')
generic_type = request_type.split('generic_').last
"ServiceTemplate#{generic_type.camelize}".constantize
else
ServiceTemplate
end
end
def update_catalog_item(options, auth_user = nil)
config_info = validate_update_config_info(options)
unless config_info
update!(options)
return reload
end
transaction do
update_from_options(options)
update_service_resources(config_info, auth_user)
update_resource_actions(config_info)
save!
end
reload
end
def children
service_templates
end
def descendants
children.flat_map { |child| [child] + child.descendants }
end
def subtree
[self] + descendants
end
def vms_and_templates
[]
end
def destroy
if parent_services.present?
raise MiqException::MiqServiceError, _("Cannot delete a service that is the child of another service.")
end
service_resources.each do |sr|
rsc = sr.resource
rsc.destroy if rsc.kind_of?(MiqProvisionRequestTemplate)
end
super
end
def archive
raise _("Cannot archive while in use") unless active_requests.empty?
archive!
end
def retireable?
false
end
def request_class
ServiceTemplateProvisionRequest
end
def request_type
"clone_to_service"
end
def config_info
options[:config_info] || construct_config_info
end
def create_service(service_task, parent_svc = nil)
nh = attributes.dup
# Service#display was renamed to #visible in https://github.com/ManageIQ/manageiq-schema/pull/410
nh['visible'] = nh.delete('display') if nh.key?('display')
nh['options'][:dialog] = service_task.options[:dialog]
(nh.keys - Service.column_names + %w(created_at guid service_template_id updated_at id type prov_type)).each { |key| nh.delete(key) }
# Hide child services by default
nh['visible'] = false if parent_svc
# If visible is nil, set it to false
nh['visible'] ||= false
# convert template class name to service class name by naming convention
nh['type'] = self.class.name.sub('Template', '')
nh['initiator'] = service_task.options[:initiator] if service_task.options[:initiator]
service = Service.create!(nh) do |svc|
svc.service_template = self
set_ownership(svc, service_task.get_user)
service_resources.each do |sr|
nh = sr.attributes.dup
%w(id created_at updated_at service_template_id).each { |key| nh.delete(key) }
svc.add_resource(sr.resource, nh) unless sr.resource.nil?
end
end
service.tap do |svc|
if parent_svc
service_resource = ServiceResource.find_by(:id => service_task.options[:service_resource_id])
parent_svc.add_resource!(svc, service_resource)
end
end
end
def composite?
service_type.to_s.include?(self.class::SERVICE_TYPE_COMPOSITE)
end
def atomic?
service_type.to_s.include?(self.class::SERVICE_TYPE_ATOMIC)
end
def type_display
case service_type
when self.class::SERVICE_TYPE_ATOMIC then "Item"
when self.class::SERVICE_TYPE_COMPOSITE then "Bundle"
when nil then "Unknown"
else
service_type.to_s.capitalize
end
end
def create_tasks_for_service(service_task, parent_svc)
unless parent_svc
return [] unless self.class.include_service_template?(service_task,
service_task.source_id,
parent_svc)
end
svc = create_service(service_task, parent_svc)
service_task.destination = svc
create_subtasks(service_task, svc)
end
# default implementation to create subtasks from service resources
def create_subtasks(parent_service_task, parent_service)
tasks = []
service_resources.each do |child_svc_rsc|
scaling_min = child_svc_rsc.scaling_min
1.upto(scaling_min).each do |scaling_idx|
nh = parent_service_task.attributes.dup
%w(id created_on updated_on type state status message).each { |key| nh.delete(key) }
nh['options'] = parent_service_task.options.dup
nh['options'].delete(:child_tasks)
# Initial Options[:dialog] to an empty hash so we do not pass down dialog values to child services tasks
nh['options'][:dialog] = {}
next if child_svc_rsc.resource_type == "ServiceTemplate" &&
!self.class.include_service_template?(parent_service_task,
child_svc_rsc.resource.id,
parent_service)
new_task = parent_service_task.class.new(nh)
new_task.options.merge!(
:src_id => child_svc_rsc.resource.id,
:scaling_idx => scaling_idx,
:scaling_min => scaling_min,
:service_resource_id => child_svc_rsc.id,
:parent_service_id => parent_service.id,
:parent_task_id => parent_service_task.id,
)
new_task.state = 'pending'
new_task.status = 'Ok'
new_task.source = child_svc_rsc.resource
new_task.save!
new_task.after_request_task_create
parent_service_task.miq_request.miq_request_tasks << new_task
tasks << new_task
end
end
tasks
end
def set_ownership(service, user)
return if user.nil?
service.evm_owner = user
if user.current_group
$log.info("Setting Service Owning User to Name=#{user.name}, ID=#{user.id}, Group to Name=#{user.current_group.name}, ID=#{user.current_group.id}")
service.miq_group = user.current_group
else
$log.info("Setting Service Owning User to Name=#{user.name}, ID=#{user.id}")
end
end
def self.default_provisioning_entry_point(service_type)
if service_type == 'atomic'
'/Service/Provisioning/StateMachines/ServiceProvision_Template/CatalogItemInitialization'
else
'/Service/Provisioning/StateMachines/ServiceProvision_Template/CatalogBundleInitialization'
end
end
def self.default_retirement_entry_point
'/Service/Retirement/StateMachines/ServiceRetirement/Default'
end
def self.default_reconfiguration_entry_point
nil
end
def template_valid?
validate_template[:valid]
end
alias template_valid template_valid?
def template_valid_error_message
validate_template[:message]
end
def validate_template
missing_resources = service_resources.select { |sr| sr.resource.nil? }
if missing_resources.present?
missing_list = missing_resources.collect { |sr| "#{sr.resource_type}:#{sr.resource_id}" }.join(", ")
return {:valid => false,
:message => "Missing Service Resource(s): #{missing_list}"}
end
service_resources.detect do |s|
r = s.resource
r.respond_to?(:template_valid?) && !r.template_valid?
end.try(:resource).try(:validate_template) || {:valid => true, :message => nil}
end
def provision_action
resource_actions.find_by(:action => "Provision")
end
def update_resource_actions(ae_endpoints)
resource_action_list.each do |action|
resource_params = ae_endpoints[action[:param_key]]
resource_action = resource_actions.find_by(:action => action[:name])
# If the action exists in updated parameters
if resource_params
# And the resource action exists on the template already, update it
if resource_action
resource_action.update!(resource_params.slice(*RESOURCE_ACTION_UPDATE_ATTRS))
# If the resource action does not exist, create it
else
build_resource_action(resource_params, action)
end
elsif resource_action
# If the endpoint does not exist in updated parameters, but exists on the template, delete it
resource_action.destroy
end
end
end
def create_resource_actions(ae_endpoints)
ae_endpoints ||= {}
resource_action_list.each do |action|
ae_endpoint = ae_endpoints[action[:param_key]]
next unless ae_endpoint
build_resource_action(ae_endpoint, action)
end
save!
end
def self.create_from_options(options)
create!(options.except(:config_info).merge(:options => { :config_info => options[:config_info] }))
end
private_class_method :create_from_options
def provision_request(user, options = nil, request_options = {})
request_options[:provision_workflow] = true
request_options[:parent_id] = options.delete('param_parent_request_id') unless options['param_parent_request_id'].nil?
result = order(user, options, request_options)
raise result[:errors].join(", ") if result[:errors].any?
result[:request]
end
def picture=(value)
if value.kind_of?(Hash)
super(Picture.new(value))
else
super
end
end
def queue_order(user_id, options, request_options)
MiqQueue.submit_job(
:class_name => self.class.name,
:instance_id => id,
:method_name => "order",
:args => [user_id, options, request_options],
)
end
def order(user_or_id, options = nil, request_options = {}, schedule_time = nil)
user = user_or_id.kind_of?(User) ? user_or_id : User.find(user_or_id)
workflow = provision_workflow(user, options, request_options)
if schedule_time
require 'time'
time = Time.parse(schedule_time).utc
errors = workflow.validate_dialog
errors << unsupported_reason(:order)
return {:errors => errors} if errors.compact.present?
schedule = MiqSchedule.create!(
:name => "Order #{self.class.name} #{id} at #{time}",
:description => "Order #{self.class.name} #{id} at #{time}",
:sched_action => {:args => [user.id, options, request_options], :method => "queue_order"},
:resource => self,
:run_at => {
:interval => {:unit => "once"},
:start_time => time,
:tz => "UTC",
},
)
{:schedule => schedule}
else
workflow.submit_request
end
end
def provision_workflow(user, dialog_options = nil, request_options = {})
dialog_options ||= {}
request_options.delete(:provision_workflow) if request_options[:submit_workflow]
ra_options = request_options.slice(:initiator, :init_defaults, :provision_workflow, :submit_workflow).merge(:target => self)
ResourceActionWorkflow.new(dialog_options, user, provision_action, ra_options).tap do |wf|
wf.request_options = request_options
end
end
def add_resource(rsc, options = {})
super
adjust_service_type
end
def self.display_name(number = 1)
n_('Service Catalog Item', 'Service Catalog Items', number)
end
def my_zone
# Catalog items can specify a zone to run in.
# Catalog bundle are used for grouping catalog items and are therefore treated as zone-agnostic.
zone&.name if atomic?
end
private
def update_service_resources(config_info, auth_user = nil)
config_info = config_info.except(:provision, :retirement, :reconfigure)
workflow_class = MiqProvisionWorkflow.class_for_source(config_info[:src_vm_id])
if workflow_class
service_resources.find_by(:resource_type => 'MiqRequest').try(:destroy)
new_request = workflow_class.new(config_info, auth_user).make_request(nil, config_info)
add_resource!(new_request)
end
end
def build_resource_action(ae_endpoint, action)
fqname = ae_endpoint[:fqname] || self.class.send(action[:method], *action[:args]) || ""
build_options = {:action => action[:name],
:fqname => fqname,
:ae_attributes => {:service_action => action[:name]}}
build_options.merge!(ae_endpoint.slice(*RESOURCE_ACTION_UPDATE_ATTRS))
resource_actions.build(build_options)
end
def validate_update_config_info(options)
if options[:service_type] && options[:service_type] != service_type
raise _('service_type cannot be changed')
end
if options[:prov_type] && options[:prov_type] != prov_type
raise _('prov_type cannot be changed')
end
options[:config_info]
end
def resource_action_list
[
{:name => ResourceAction::PROVISION,
:param_key => :provision,
:method => 'default_provisioning_entry_point',
:args => [service_type]},
{:name => ResourceAction::RECONFIGURE,
:param_key => :reconfigure,
:method => 'default_reconfiguration_entry_point',
:args => []},
{:name => ResourceAction::RETIREMENT,
:param_key => :retirement,
:method => 'default_retirement_entry_point',
:args => []}
]
end
def update_from_options(params)
options[:config_info] = params[:config_info]
update!(params.except(:config_info))
end
def construct_config_info
config_info = {}
miq_request_resource = service_resources.find_by(:resource_type => 'MiqRequest')
config_info.merge!(miq_request_resource.resource.options.compact) if miq_request_resource
config_info.merge!(resource_actions_info)
end
def resource_actions_info
resource_actions.each_with_object({}) do |resource_action, config_info|
resource_options = resource_action.slice(:dialog_id, :configuration_template_type, :configuration_template_id).compact
resource_options[:fqname] = resource_action.fqname
config_info[resource_action.action.downcase.to_sym] = resource_options.symbolize_keys
end
end
def generic_custom_buttons
CustomButton.buttons_for("Service")
end
def adjust_service_type
self.service_type = service_resources.any? { |st| st.resource_type.in?(['Service', 'ServiceTemplate']) } ? self.class::SERVICE_TYPE_COMPOSITE : self.class::SERVICE_TYPE_ATOMIC
end
end
scope :public_service_templates, -> {
joins(:miq_user_roles).where(miq_user_roles: { settings: nil })
}
Assuming the table exists. You need to join on that table in order to query it. Note, the default joins is an inner join that will remove records that don't have an associated miq_user_roles.
This will change the query substantially. If a record has_many miq_user_roles you'll likely need to add a distinct or distinct on clause. If it's possible to have no miq_user_roles, then records without them will now dissappear when you call the scope, since the inner join didn't find any.
I have a serializer for a TimeEntry model that looks like this:
class TimeEntrySerializer < ActiveModel::Serializer
attributes :id, :description, :duration
has_one :project
end
And It works as expected when I just return all the records:
def index
#time_entries = current_user.time_entries.all
respond_to do |format|
format.html
format.json { render json: #time_entries }
end
end
However, I want to return the entries organized by day, something like this:
[
{ "2016-03-16" => [TimeEntry1, TimeEntry2, TimeEntry3] },
{ "2016-03-17" => [TimeEntry1, TimeEntry2] }
]
I do it like this form my model:
def self.get_entries_for_user(current_user)
current_user.time_entries
.group_by { |item| item.created_at.to_date.to_s }
.map { |day, entries| { day => entries } }
end
But now, the serializer is not working for the TimeEntry object, I'm not quite sure if it's actually supposed to work in this situation... I want to avoid having to format the data myself:
def self.get_entries_for_user(current_user)
current_user.time_entries
.group_by { |item| item.created_at.to_date.to_s }
.map do |day, entries|
{
day => entries.map do |entry|
{
:id => entry.id,
:description => entry.description,
:duration => entry.duration_ms,
:start_time => entry.time.begin,
:end_time => entry.time.end,
:project_id => entry.project.id
}
end
}
end
end
Is it possible to use the active_model_serializer for this situation? If not possible, how can I format the data more efficiently an avoid the nested map calls?
To call and be able to reuse the serializer:
options = {}
serialization = SerializableResource.new(resource, options)
serialization.to_json
serialization.as_json
So I used it like this:
def self.get_entries_for_user(current_user)
current_user.time_entries
.group_by { |item| item.created_at.to_date.to_s }
.map do |day, entries|
{
:day => day,
:entries => entries.map do |entry|
entry = ActiveModel::SerializableResource.new(entry)
entry.as_json
end
}
end
end
I'm having problems with the Podio_rails_sample. I've included my leadsController and leads.rb files. The line that gets hung up is field['config']['settings']['allowed_values'].
Line 25 is the problematic one:
NoMethodError in LeadsController#new
undefined method `[]' for nil:NilClass
Extracted source (around line #25):
23 app = Podio::Application.find(APP_ID)
24 field = app.fields.find { |field| field['external_id'] == 'status' }
25 field['config']['settings']['allowed_values']
26 end
27
28 def self.create_from_params(params)
Rails.root: c:/Sites/podio_rails_sample
app = Podio::Application.find(APP_ID)
field = app.fields.find { |field| field['external_id'] == 'status' }
field['config']['settings']['allowed_values']
end
def self.create_from_params(params)
Rails.root: c:/Sites/podio_rails_sample
-----------------------------------
class LeadsController < ApplicationController
before_filter :load_collections, :only => [:new, :edit]
def index
#leads = Lead.all
end
def new
#lead = Lead.new
end
def create
Lead.create_from_params(params['lead'])
redirect_to leads_path, :notice => 'Lead created'
end
def edit
#lead = Lead.find_basic(params[:id])
end
def update
Lead.update_from_params(params[:id], params['lead'])
redirect_to leads_path, :notice => 'Lead updated'
end
def destroy
Lead.delete(params[:id])
redirect_to leads_path, :notice => 'Lead deleted'
end
#protected
def load_collections
#lead_contacts = Lead.space_contacts
#sales_contacts = Lead.users
#statuses = Lead.statuses
end
end
-------------------------------------
- leads.rb file
class Lead < Podio::Item
APP_ID =12328033
SPACE_ID =3204114
# Find all items in the Leads app
def self.all
collection = self.find_all(APP_ID)
collection[:all]
end
# Find valid lead contacts in the space
def self.space_contacts
Podio::Contact.find_all_for_space(SPACE_ID, :order => 'contact', :limit => 12, :contact_type => 'space,connection', :exclude_self => false) rescue []
end
# Find valid sales contacts in the space
def self.users
Podio::Contact.find_all_for_space(SPACE_ID, :order => 'contact', :limit => 12, :contact_type => 'user', :exclude_self => false) rescue []
end
# Find valid statuses
def self.statuses
app = Podio::Application.find(APP_ID)
field = app.fields.find { |field| field['external_id'] == 'status' }
field['config']['settings']['allowed_values']
end
def self.create_from_params(params)
# raise fields.inspect
self.create(APP_ID, { :fields => fields_from_params(params) })
end
def self.update_from_params(id, params)
self.update(id, { :fields => fields_from_params(params) })
end
#
# Map the field values return by the Podio API to simple getters
#
def organization
field_values_by_external_id('company-or-organisation', :simple => true)
end
def lead_contact
field_values_by_external_id('contacts', :simple => true).try(:[], 'name')
end
def sales_contact
field_values_by_external_id('sales-contact', :simple => true).try(:[], 'name')
end
def potential_revenue_value
field_values_by_external_id('potential-revenue').try(:first).try(:[], 'value').to_i
end
def potential_revenue_currency
field_values_by_external_id('potential-revenue').try(:first).try(:[], 'currency')
end
def probability
field_values_by_external_id('probability-of-sale', :simple => true)
end
def status
field_values_by_external_id('status', :simple => true)
end
def followup_at
field_values_by_external_id('next-follow-up').try(:first).try(:[], 'start').try(:to_datetime)
end
protected
def field_values_by_external_id(external_id, options = {})
if self.fields.present?
field = self.fields.find { |field| field['external_id'] == external_id }
if field
values = field['values']
if options[:simple]
values.first['value']
else
values
end
else
nil
end
else
nil
end
end
def self.fields_from_params(params)
{
'company-or-organisation' => params[:organization],
'contacts' => (params[:lead_contact].present? ? params[:lead_contact].to_i : nil),
'sales-contact' => (params[:sales_contact].present? ? params[:sales_contact].to_i : nil),
'potential-revenue' => { :value => params['potential_revenue_value'], :currency => params['potential_revenue_currency'] },
'probability-of-sale' => params[:probability].to_i,
'status' => params[:status],
'next-follow-up' => DateTime.new(params['followup_at(1i)'].to_i, params['followup_at(2i)'].to_i, params['followup_at(3i)'].to_i).to_s(:db)
}.delete_if { |k, v| v.nil? }
end
end
I have a Rails controller that has 2 methods. Both Methods use some of the same variables and I'm wondering how I can refactor this into either a method in the Model of somewhere in the controller to make them more reusable than they are 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 do |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 would suggest to use a model class method for processing data. for 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 be moved to a model class method that returns charts. In the same way you can refactor your second method. Any kind of business logic and data proccessing should be handled in models
Moreover I can see there are too many unused local variables are there in controller. The controller should be thin as much as possible.
Use concept like decorator
module Chart
extend self
def service_analysis(freight_groups, freight_groups)
end
end
class ChartsController < ApplicationController
#chart = Chart.service_analysis(freight_groups, freight_groups)
end
notes: do not put calculation code in view, it's slow
If you decided to keep it within the controller, then try this:
class ChartsController < ApplicationController
before_filter :authenticate_user!, :company_id
before_filter :load_data, :only => [:service_level, weight_summary]
def service_level
freight_groups = #freight_details.group(:family).select("family, count(distinct package_id), sum(base_charge + discount)")
#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
packages = #freight_details.joins(:package, :invoice)
vol_data = {}
spend_data = {}
packages.group(:zone).select("zone, count(distinct package_id), sum(base_charge + discount)").each do |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
private
def load_data
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' })
end
end
The instance variable #freight_details will be available in both methods. The before_filter will execute the load_data method only for these two methods.
Good Luck!
I am fairly still new to ruby on rails and don't fully understand why I am getting the following error:
undefined local variable or method `user' for #<StatisticsController:0xb9a20d0>
The code:
class StatisticsController < ApplicationController
before_filter :authenticate, :only => [:index]
def index
#title = "Statistics"
#projects = Project.all
#data = []
Project.all.each do |project|
projdata = { 'name' => project.project_name.to_s,
'values' => [] }
['Pre-Sales','Project','Fault Fixing','Support' ].each do |taskname|
record = Effort.sum( :hours,
:joins => {:project_task => {:efforts => :user}},
:conditions => { "project_tasks.efforts.user_id" => user.id,
"project_tasks.project_id" => project.id,
"project_tasks.task_name" => taskname } )
projdata[ 'values' ].push( record )
end
#data.push( projdata )
end
end
end
Update
class StatisticsController < ApplicationController
before_filter :authenticate, :only => [:index]
def index
#title = "Statistics"
#projects = Project.all
#data = []
User.all.each do |user|
projdata = { 'name' => user.user_id.to_s,
'values' => [] }
['Pre-Sales','Project','Fault Fixing','Support' ].each do |taskname|
user = User.all
record = Effort.sum( :hours,
:joins => {:project_task => {:efforts => :user}},
:conditions => { "project_tasks.efforts.user_id" => user.id,
"project_tasks.project_id" => project.id,
"project_tasks.task_name" => taskname } )
projdata[ 'values'].push( record )
end
#data.push( projdata )
end
end
end
In string :conditions => { "project_tasks.efforts.user_id" => user.id, you call id for user object, but it is not instantiated in code above.
Your update doesn't loop over the users at all; user is now a collection of all the users. You need to iterate over the users if you want to get individual statistics for individual users.
Are you using devise? Use current_user instead of user.
Fix of your code:
User.all.each do |user|
projdata = { 'name' => user.user_id.to_s,
'values' => [] }
['Pre-Sales','Project','Fault Fixing','Support' ].each do |taskname|
record = Effort.sum( :hours,
:joins => {:project_task => {:efforts => :user}},
:conditions => { "project_tasks.efforts.user_id" => user.id,
"project_tasks.project_id" => project.id,
"project_tasks.task_name" => taskname } )
projdata[ 'values'].push( record )
end
#data.push( projdata )
end
So: removed the rogue user=User.all :)
Question: in 1 place you write user.user_id and in the other you write user.id. Is that correct?