Module: PolicyManager::Concerns::UserBehavior
- Extended by:
- ActiveSupport::Concern
- Defined in:
- app/models/policy_manager/concerns/user_behavior.rb
Instance Method Summary collapse
- #accept_policy_from(name) ⇒ Object
- #can_request_portability? ⇒ Boolean
- #confirm_all_policies! ⇒ Object
- #def(portability_schema) ⇒ Object
- #handle_policy_for(term) ⇒ Object
- #needs_policy_confirmation_for?(rule) ⇒ Boolean
- #pending_blocking_policies ⇒ Object
- #pending_policies ⇒ Object
- #policy_term_on(rule) ⇒ Object
- #policy_user_term_on(term) ⇒ Object
- #portability_collection_for(rule, page = nil) ⇒ Object
- #portability_member_for(rule) ⇒ Object
- #reject_all_policies! ⇒ Object
Instance Method Details
#accept_policy_from(name) ⇒ Object
132 133 134 135 136 137 138 |
# File 'app/models/policy_manager/concerns/user_behavior.rb', line 132 def accept_policy_from(name) term = PolicyManager::Config.rules.find{|o| o.name == name}.terms.published.last if term.present? user_term = self.handle_policy_for(term) user_term.accept! unless user_term.accepted? end end |
#can_request_portability? ⇒ Boolean
128 129 130 |
# File 'app/models/policy_manager/concerns/user_behavior.rb', line 128 def can_request_portability? self.portability_requests.select{|p| p.pending? || p.progress?}.blank? end |
#confirm_all_policies! ⇒ Object
87 88 89 90 91 92 |
# File 'app/models/policy_manager/concerns/user_behavior.rb', line 87 def confirm_all_policies! pending_policies.each do |c| term = c.terms.last self.handle_policy_for(term).accept! end end |
#def(portability_schema) ⇒ Object
59 60 61 62 63 |
# File 'app/models/policy_manager/concerns/user_behavior.rb', line 59 def def portability_schema PolicyManager::Config.portability_rules.map(&:name) end |
#handle_policy_for(term) ⇒ Object
122 123 124 125 126 |
# File 'app/models/policy_manager/concerns/user_behavior.rb', line 122 def handle_policy_for(term) self.user_terms.where(term_id: term).first_or_initialize do |member| member.term_id = term.id end end |
#needs_policy_confirmation_for?(rule) ⇒ Boolean
101 102 103 104 105 106 107 |
# File 'app/models/policy_manager/concerns/user_behavior.rb', line 101 def needs_policy_confirmation_for?(rule) term = policy_term_on(rule) user_term = policy_user_term_on(term) return true if user_term.blank? return true if user_term.rejected? term.created_at > user_term.created_at end |
#pending_blocking_policies ⇒ Object
81 82 83 84 85 |
# File 'app/models/policy_manager/concerns/user_behavior.rb', line 81 def pending_blocking_policies PolicyManager::Config.rules.select do |c| c.blocking && self.needs_policy_confirmation_for?(c.name) end end |
#pending_policies ⇒ Object
73 74 75 76 77 78 79 |
# File 'app/models/policy_manager/concerns/user_behavior.rb', line 73 def pending_policies # TODO: this seems to be a litle inefficient, # hint: try to do this in one query PolicyManager::Config.rules.select do |c| self.needs_policy_confirmation_for?(c.name) end end |
#policy_term_on(rule) ⇒ Object
109 110 111 112 113 114 115 |
# File 'app/models/policy_manager/concerns/user_behavior.rb', line 109 def policy_term_on(rule) category = PolicyManager::Config.rules.find{|o| o.name == rule} #binding.pry if category.blank? term = category.terms.where(state: "published").last raise "no term for #{rule} policy" if term.blank? term end |
#policy_user_term_on(term) ⇒ Object
117 118 119 120 |
# File 'app/models/policy_manager/concerns/user_behavior.rb', line 117 def policy_user_term_on(term) return if term.blank? self.user_terms.where(term_id: term.id).first end |
#portability_collection_for(rule, page = nil) ⇒ Object
69 70 71 |
# File 'app/models/policy_manager/concerns/user_behavior.rb', line 69 def portability_collection_for(rule, page = nil) self.send(rule.collection).paginate(page: page, per_page: rule.per) end |
#portability_member_for(rule) ⇒ Object
65 66 67 |
# File 'app/models/policy_manager/concerns/user_behavior.rb', line 65 def portability_member_for(rule) self.send(rule.member) end |
#reject_all_policies! ⇒ Object
94 95 96 97 98 99 |
# File 'app/models/policy_manager/concerns/user_behavior.rb', line 94 def reject_all_policies! pending_policies.each do |c| term = c.terms.last self.handle_policy_for(term).reject! end end |