Created
March 5, 2013 20:12
-
-
Save gogogarrett/5093844 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
| def can_receive_award_according_to_restrictions? user, award | |
| remaining_restriction_points(user) >= award[:points].to_i && | |
| remaining_restriction_game_tokens(user) >= award[:game_tokens].to_i && | |
| remaining_restriction_gift_tokens(user) >= award[:gift_tokens].to_i | |
| end | |
| currencies = ["points", "game_tokens", "gift_tokens"] | |
| currencies.each do |currency| | |
| define_method "remaining_restriction_#{currency}" do |user| | |
| currency_points = find_currency_of_submissions(user, currency) | |
| remaning = 0 | |
| case self.payout_type | |
| when "lifetime" | |
| awards = user.awards.includes(:approval).where("initiative_methodology_id = ? AND approvals.approval_status = 'approved'", self.id) | |
| user_points = (awards.length > 0) ? awards.map{|x| x.send(currency) }.sum : 0 | |
| remaning = currency_points - user_points | |
| when "absolute_duration", "relative_duration" | |
| time_frame = find_correct_time_frame(self.payout_type, "max_payouts") | |
| awards = user.awards.includes(:approval).where("initiative_methodology_id = ? AND | |
| approvals.approval_status = 'approved' AND | |
| disbursed_at >= ?", self.id, time_frame) | |
| user_points = (awards.length > 0) ? awards.map{|x| x.send(currency) }.sum : 0 | |
| remaning = currency_points - user_points | |
| when "set_time" | |
| option = self.restriction_options.max_payouts.option_for_date(DateTime.now) | |
| if option.any? | |
| awards = user.awards.includes(:approval).where("initiative_methodology_id = ? AND | |
| approvals.approval_status = 'approved' AND | |
| disbursed_at >= to_timestamp(?) AND disbursed_at <= to_timestamp(?)", self.id, option.start_set_time.beginning_of_day, option.end_set_time.end_of_day) | |
| user_points = (awards.length > 0) ? awards.map{|x| x.send(currency) }.sum : 0 | |
| remaning = totalPoints - points | |
| end | |
| end | |
| remaning = 0 if remaning < 0 || remaning.nil? | |
| remaning | |
| end | |
| end | |
| def find_currency_of_submissions user, currency | |
| total_amount = 0 | |
| if self.payout_type == "set_time" | |
| if total_amount == 0 | |
| self.restriction_options.max_payouts.each do |option| | |
| if (self.number_type == "set_time" && option.item_type = "max_payout") || (self.payout_type == "set_time" && option.item_type == "max_payout") | |
| total_amount = option.send(currency) if (Date.today.to_datetime).between?(option.start_set_time, option.end_set_time) | |
| end | |
| end | |
| end | |
| else | |
| total_amount = self.restriction_options.first.send(currency) | |
| end | |
| total_amount = 0 if total_amount.nil? | |
| total_amount | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment