/
application_form.rb
46 lines (37 loc) · 1.37 KB
/
application_form.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
class ApplicationForm < ApplicationStruct
class << self
attr_reader :default_action, :default_method
def action(url, method: nil)
@default_action = url
@default_method = method
end
def model_name
@_model_name ||= ActiveModel::Name.new(self, nil, name.demodulize)
end
def ===(other) = other.respond_to?(:key?) ? other.key?(param_key) : super
def param_key = model_name.param_key
def attribute_names = portrayal.keywords.without(:action, :method)
def from_params(params, **kw) = new(**filter_params(params), **kw)
def public_attribute_names
attribute_names.select { |n| public_method_defined?(n) }
end
def filter_params(params)
params
.require(param_key)
.permit(*public_attribute_names)
.to_hash
.transform_keys(&:to_sym)
end
end
keyword :action, default: proc { self.class.default_action }
keyword :method, default: proc { self.class.default_method }
def attributes = super.slice(*self.class.attribute_names)
def persisted? = false
def plural_error = "error".pluralize(errors.size)
def with = {model: self, url: action, method: method}
def as_json(*args)
options = args.extract_options!
methods = Array(options[:methods]) | %i[action method plural_error errors]
super(**options, methods: methods)
end
end