Created
February 19, 2013 19:53
-
-
Save gogogarrett/4989278 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class TermsAndCondition < ActiveRecord::Base | |
| include CommonScopes | |
| include CheckBannedWords | |
| include ReachRoleExtensions | |
| belongs_to :program | |
| has_reach | |
| has_paper_trail | |
| is_hierarchable | |
| is_dynamically_translatable title: { required: true }, content: { required: true }, mcf_content: { required: false }, accept_text: { required: true }, decline_text: { required: true }, decline_message: { required: true } | |
| # This method takes in a reach selector hash and decides who should | |
| # have to resign the TOS. This hash is built in the controller(probably bad) | |
| # and then builds the queries in order to update those users accept_tos flag to false | |
| # | |
| # { | |
| # hierarchy_nodes: { | |
| # self_nodes: params["self_nodes"], | |
| # children_nodes: params["children_nodes"], | |
| # block_self_nodes: params["block-self_nodes"], | |
| # block_children_nodes: params["block-children_nodes"] | |
| # }, | |
| # role_nodes: params["role_nodes"], | |
| # user_nodes: params["user_nodes"] | |
| # } | |
| def check_reset_conditions(reset_reach) | |
| if reset_reach[:user_nodes].present? | |
| handle_user_nodes(reset_reach[:user_nodes].map(&:to_i)) | |
| end | |
| if reset_reach[:role_nodes].present? | |
| handle_role_nodes(reset_reach[:role_nodes].map(&:to_i)) | |
| end | |
| if reset_reach[:hierarchy_nodes].present? | |
| self_nodes = reset_reach[:hierarchy_nodes][:self_nodes] | |
| children_nodes = reset_reach[:hierarchy_nodes][:children_nodes] | |
| if self_nodes.present? | |
| handle_self_nodes(self_nodes.map(&:to_i)) | |
| end | |
| if children_nodes.present? | |
| handle_children_nodes(children_nodes.map(&:to_i)) | |
| end | |
| end | |
| end | |
| # Check through all the user ids and if the user's id is found | |
| # update their TOS flag to false | |
| def handle_user_nodes(user_ids) | |
| users = User.where("program_id = ? AND id in (?)", self.program_id, user_ids) | |
| users.update_all(accept_tos: false) | |
| end | |
| # Check through all the user's roles and if the role id is found | |
| # update their TOS flag to false | |
| def handle_role_nodes(role_ids) | |
| users = User.joins(:secondary_roles).where("users.program_id = ? AND secondary_roles.id in (?)", self.program_id, role_ids) | |
| users.update_all(accept_tos: false) | |
| end | |
| # this is when selecting "self" on a node | |
| # Check through all the user's hiearchy_node_id and if the id is found | |
| # update their TOS flag to false | |
| def handle_self_nodes self_nodes | |
| users = User.where("program_id = ? AND hierarchy_node_id in (?)", self.program_id, self_nodes) | |
| users.update_all(accept_tos: false) | |
| end | |
| # this is when selecting "self and children" on a node | |
| # ([1, 2, 3] & [1,2,3,6,70]).length > 0 | |
| # - user.self_and_anscestors in (children_nodes) | |
| # | |
| # Root (h-id: 1) | |
| # | | |
| # |_ Software (h-id: 2, p-id: 1) (select - self and children) | |
| # | | |
| # |_ Back-end (h-id: 3, p-id: 2) | |
| # | | |
| # |_ Front-end (h-id: 4, p-id; 2) | |
| # | | |
| # |_ Server (h-id: 5, p-id: 2) | |
| # | |
| # children_nodes = [2,3,4,5] | |
| def handle_children_nodes children_nodes | |
| user_ids = [] | |
| User.where(program_id: self.program_id).each do |user| | |
| user.hierarchy_node.self_and_ancestors.each do |h_node| | |
| if (children_nodes & [h_node.id]).length > 0 | |
| user_ids << user.id | |
| end | |
| end | |
| end | |
| User.where("id in (?)", user_ids).update_all(accept_tos: false) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment