-
Notifications
You must be signed in to change notification settings - Fork 13
/
base.rb
143 lines (120 loc) · 4.21 KB
/
base.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
module AssignableValues
module ActiveRecord
module Restriction
class Base
attr_reader :model, :property, :options, :values, :default
def initialize(model, property, options, &values)
@model = model
@property = property
@options = options
@values = values
ensure_values_given
setup_default if default?
define_assignable_values_method
setup_validation
end
def validate_record(record)
value = current_value(record)
unless allow_blank? && value.blank?
begin
assignable_values = assignable_values(record)
assignable_values.include?(value) or record.errors.add(property, I18n.t('errors.messages.inclusion'))
rescue DelegateUnavailable
# if the delegate is unavailable, the validation is skipped
end
end
end
def assignable_values(record)
assignable_values = []
old_value = previously_saved_value(record)
assignable_values << old_value if old_value.present?
assignable_values |= raw_assignable_values(record)
assignable_values = decorate_values(assignable_values)
assignable_values
end
def set_default(record)
if record.new_record? && record.send(property).nil?
default_value = default
default_value = record.instance_eval(&default_value) if default_value.is_a?(Proc)
record.send("#{property}=", default_value)
end
true
end
def humanize_string_value(value)
I18n.t("assignable_values.#{model.name.underscore}.#{property}.#{value}", :default => value.humanize)
end
private
def current_value(record)
record.send(property)
end
def previously_saved_value(record)
nil
end
def decorate_values(values)
values
end
def delegate_method
options[:through]
end
def delegate?
@options.has_key?(:through)
end
def default?
@options.has_key?(:default)
end
def allow_blank?
@options[:allow_blank]
end
def enhance_model(&block)
@model.class_eval(&block)
end
def setup_default
@default = options[:default]
restriction = self
enhance_model do
set_default_method = "set_default_#{restriction.property}"
define_method set_default_method do
restriction.set_default(self)
end
after_initialize set_default_method
end
end
def setup_validation
restriction = self
enhance_model do
validate_method = "validate_#{restriction.property}_assignable"
define_method validate_method do
restriction.validate_record(self)
end
validate validate_method
end
end
def define_assignable_values_method
restriction = self
enhance_model do
assignable_values_method = "assignable_#{restriction.property.to_s.pluralize}"
define_method assignable_values_method do
restriction.assignable_values(self)
end
end
end
def raw_assignable_values(record)
if delegate?
assignable_values_from_delegate(record)
else
record.instance_eval(&@values)
end.to_a
end
def assignable_values_from_delegate(record)
delegate = record.send(delegate_method) or raise DelegateUnavailable, "Cannot query a nil #{delegate_method} for assignable values"
delegate_query_method = "assignable_#{model.name.underscore}_#{property.to_s.pluralize}"
args = delegate.method(delegate_query_method).arity == 1 ? [record] : []
delegate.send(delegate_query_method, *args)
end
def ensure_values_given
@values or @options[:through] or raise NoValuesGiven, 'You must supply the list of assignable values by either a block or :through option'
end
end
end
end
end