-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathph_model.rb
64 lines (53 loc) · 1.85 KB
/
ph_model.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
require 'active_support/all'
require 'active_model'
require 'active_attr'
ActiveModel::Validations # ensure its loaded since we're patching it
module ActiveModel
module Validations
extend ActiveSupport::Autoload
autoload :TypeValidator
autoload :CollectionItemsValidator
end
end
# PayrollHero's mashup of ActiveModel and ActiveAttr with some of our own twists
module PhModel
extend ActiveSupport::Concern
extend ActiveSupport::Autoload
autoload :Concerns
autoload :ValidationFailed
include ActiveAttr::BasicModel
include ActiveAttr::AttributeDefaults
include ActiveAttr::QueryAttributes
include ActiveAttr::MassAssignment
include Concerns::InitializeCallback
include Concerns::AttributeRequiredValidation
include Concerns::AttributeTypeValidation
include Concerns::AttributeNestedValidation
include Concerns::AttributeOfArrayTypeInitialization
def as_json(*)
{}.tap do |hash|
self.class.attributes.each do |attribute_name, _info|
hash[attribute_name] = send(attribute_name).as_json
end
end
end
def inspect
attr_info = self.class.attributes.map { |attr_name, info| "#{attr_name}: #{self.send(attr_name).inspect}" }
"#<#{self.model_name} #{attr_info.join(', ')}>"
end
# Monkey patch #assign_attributes inside ActiveAttr::MassAssignment
# so that it doesn't blindly ignore attempting to assign attributes which do not
# exist
#
# TODO: try to submit something upstream to deal with this
def assign_attributes(new_attributes, options = {})
sanitized_new_attributes = sanitize_for_mass_assignment_if_sanitizer new_attributes, options
sanitized_new_attributes.each do |name, value|
writer = "#{name}="
# originally:
# send writer, value if respond_to? writer
send writer, value
end if sanitized_new_attributes
end
include Concerns::ValidatedFactory
end