Class: Parliament::Utils::Helpers::RoleGroupingHelper
- Inherits:
-
Object
- Object
- Parliament::Utils::Helpers::RoleGroupingHelper
- Extended by:
- GroupingHelper
- Defined in:
- lib/parliament/utils/helpers/role_grouping_helper.rb
Defined Under Namespace
Classes: RoleGroupedObject
Class Method Summary collapse
-
.create_grouped_objects(data_hash, key) ⇒ Array
Creates new GroupingHelper::GroupedObject, for each set of Grom::Nodes that have been grouped (nodes) Each instance of GroupingHelper::GroupedObject is assigned properties of start_date, end_date, nodes and type Once object has been created, calls sort_grouped_nodes_by_date method to sort that object’s nodes by date.
-
.sort_grouped_nodes_by_date(grouped_object, current_nodes) ⇒ Instance object
Sorts an array of grouped Grom::Nodes by end date, placing the most current Grom::Node at the start of the array.
Methods included from GroupingHelper
create_sorted_array, group, group_data
Class Method Details
.create_grouped_objects(data_hash, key) ⇒ Array
Creates new GroupingHelper::GroupedObject, for each set of Grom::Nodes that have been grouped (nodes) Each instance of GroupingHelper::GroupedObject is assigned properties of start_date, end_date, nodes and type Once object has been created, calls sort_grouped_nodes_by_date method to sort that object’s nodes by date
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/parliament/utils/helpers/role_grouping_helper.rb', line 23 def self.create_grouped_objects(data_hash, key) grouped = [] grouped_object = RoleGroupingHelper::RoleGroupedObject.new grouped_object.nodes = data_hash[key] start_date = nil end_date = nil current_nodes = [] grouped_object.nodes.each do |node| # Find nodes that represents current roles current_nodes << node if node.try(:end_date).nil? || node.end_date.nil? # Find earliest start date and latest end date start_date = node.start_date if start_date.nil? || node.start_date < start_date end_date = node.end_date if current_nodes.empty? && (end_date.nil? || node.end_date > end_date) end # Set properties of the object # TODO: Might need to adjust to allow for multiple roles roles and memberships of a committee grouped_object.type = grouped_object.nodes.first.type grouped_object.start_date = start_date grouped_object.end_date = current_nodes.empty? ? end_date : nil # Add sorted object to the array sort_grouped_nodes_by_date(grouped_object, current_nodes) grouped << grouped_object end |
.sort_grouped_nodes_by_date(grouped_object, current_nodes) ⇒ Instance object
Sorts an array of grouped Grom::Nodes by end date, placing the most current Grom::Node at the start of the array
57 58 59 60 61 |
# File 'lib/parliament/utils/helpers/role_grouping_helper.rb', line 57 def self.sort_grouped_nodes_by_date(grouped_object, current_nodes) grouped_object.nodes -= current_nodes unless current_nodes.empty? grouped_object.nodes.sort_by!(&:end_date).reverse! grouped_object.nodes.unshift(current_nodes).flatten! unless current_nodes.empty? end |