Skip to content

Instantly share code, notes, and snippets.

@JackPott
Last active January 3, 2016 19:48
Show Gist options
  • Save JackPott/8510513 to your computer and use it in GitHub Desktop.
Save JackPott/8510513 to your computer and use it in GitHub Desktop.
This doesn't feel very Ruby? Basically groups and counts the total number of connectors used in project and is displays a report
<h1>Reports#connectors_total</h1>
<p>Find me in app/views/reports/connectors_total.html.erb</p>
<table>
<tr>
<th>Connector</th>
<th class=align-right>Total</th>
<th class=align-right>Total cost</th>
<th>Order code</th>
</tr>
<% @connectors_total.each do |connector| %>
<tr>
<td><%= connector[:connector_type][:name] %></td>
<td class=align-right><%= connector[:count] %></td>
<td class=align-right><%= number_to_currency((connector[:total_cost]), unit: "£") %></td>
<td><%= connector[:connector_type][:part_code] %>
</tr>
<% end %>
</table>
# Each termination has a connector
class Termination < ActiveRecord::Base
# Returns an array of connectors
def self.connectors_total(project)
Termination.where(project: project).group(:connector_type_id).count(:connector_type_id).map do |id, value|
connector_type = ConnectorType.find(id)
{ connector_type: connector_type, count: value, total_cost: value*connector_type.cost }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment