forked from otwcode/otwarchive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gift_exchange.rb
83 lines (66 loc) · 3.16 KB
/
gift_exchange.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
class GiftExchange < ActiveRecord::Base
PROMPT_TYPES = %w(requests offers)
include ChallengeCore
override_datetime_setters
belongs_to :collection
has_one :collection, :as => :challenge
attr_protected :signup_instructions_general_sanitizer_version
attr_protected :signup_instructions_requests_sanitizer_version
attr_protected :signup_instructions_offers_sanitizer_version
# limits the kind of prompts users can submit
belongs_to :prompt_restriction, :class_name => "PromptRestriction", :dependent => :destroy
accepts_nested_attributes_for :prompt_restriction
belongs_to :request_restriction, :class_name => "PromptRestriction", :dependent => :destroy
accepts_nested_attributes_for :request_restriction
belongs_to :offer_restriction, :class_name => "PromptRestriction", :dependent => :destroy
accepts_nested_attributes_for :offer_restriction
belongs_to :potential_match_settings, :dependent => :destroy
accepts_nested_attributes_for :potential_match_settings
validates_length_of :signup_instructions_general, :signup_instructions_requests, :signup_instructions_offers, {
:allow_blank => true,
:maximum => ArchiveConfig.INFO_MAX, :too_long => ts("must be less than %{max} letters long.", :max => ArchiveConfig.INFO_MAX)
}
PROMPT_TYPES.each do |type|
%w(required allowed).each do |setting|
prompt_limit_field = "#{type}_num_#{setting}"
validates_numericality_of prompt_limit_field, :only_integer => true, :less_than_or_equal_to => ArchiveConfig.PROMPTS_MAX, :greater_than_or_equal_to => 0
end
end
before_validation :update_allowed_values
def update_allowed_values
%w(request offer).each do |prompt_type|
required = eval("#{prompt_type}s_num_required")
allowed = eval("#{prompt_type}s_num_allowed")
if required > allowed
eval("#{prompt_type}s_num_allowed = required")
end
end
end
#FIXME hack because time zones are being html encoded. couldn't figure out why.
before_save :fix_time_zone
after_save :copy_tag_set_from_offer_to_request
def copy_tag_set_from_offer_to_request
if self.offer_restriction
self.request_restriction.set_owned_tag_sets(self.offer_restriction.owned_tag_sets)
# copy the tag-set-based restriction settings
self.request_restriction.character_restrict_to_fandom = self.offer_restriction.character_restrict_to_fandom
self.request_restriction.relationship_restrict_to_fandom = self.offer_restriction.relationship_restrict_to_fandom
self.request_restriction.character_restrict_to_tag_set = self.offer_restriction.character_restrict_to_tag_set
self.request_restriction.relationship_restrict_to_tag_set = self.offer_restriction.relationship_restrict_to_tag_set
self.request_restriction.save
end
end
# override core
def allow_name_change?
false
end
def topmost_tag_type
self.request_restriction.topmost_tag_type
end
def user_allowed_to_see_requests_summary?(user)
self.collection.user_is_maintainer?(user) || self.requests_summary_visible?
end
def user_allowed_to_see_prompt?(user, prompt)
self.collection.user_is_maintainer?(user) || prompt.pseud.user == user
end
end