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
|
# File 'lib/docx_manager_client/model_additions/belongs_to_docx_template.rb', line 5
def belongs_to_docx_template(name:, belongs_to:, template_type:)
raise DocxManagerClient::DependencyError.new("#{self.name} dose not have relation 'belongs_to :#{belongs_to}'") unless self.reflect_on_all_associations(:belongs_to).map(&:name).include?(belongs_to.to_sym)
attr_accessor :"#{name}_docx_file"
after_save :"post_#{name}_docx"
after_destroy :"delete_#{name}_docx"
define_method "#{name}_docx_path" do
status, response = DocxManagerClient::Connection.docx_server_api(:get, send("#{name}_docx_request_paht"))
docx_response_attr(status, response, :docx_url)
end
define_method "#{name}_pdf_path" do
status, response = DocxManagerClient::Connection.docx_server_api(:get, send("#{name}_docx_request_paht"))
docx_response_attr(status, response, :pdf_url)
end
define_method "#{name}_png_path" do
status, response = DocxManagerClient::Connection.docx_server_api(:get, send("#{name}_docx_request_paht"))
docx_response_attr(status, response, :png_url)
end
define_method "post_#{name}_docx" do
status, response = DocxManagerClient::Connection.docx_server_api(:post, send("#{name}_docx_request_paht"), send("#{name}_request_attributes"))
docx_response_attr(status, response)
end
define_method "delete_#{name}_docx" do
status, response = DocxManagerClient::Connection.docx_server_api(:delete, send("#{name}_docx_request_paht"), send("#{name}_request_attributes"))
docx_response_attr(status, response)
end
define_method :docx_response_attr do |status, response, attr = nil|
if status < 300
attr.nil? ? response.data : response.data.attributes.send(attr)
else
response.errors
end
end
define_method "#{name}_docx_request_paht" do
reference_record_id = send("#{belongs_to}_id")
reference_record_type = self.class.reflect_on_association(belongs_to).klass.name
record_id = id
record_type = self.class.name
"templates/#{template_type}/#{reference_record_type}/#{reference_record_id}/generated_documents/#{record_type}/#{record_id}"
end
define_method "#{name}_request_attributes" do
{ request_attributes: send("#{name}_docx_hash") }
end
define_method "#{name}_docx_hash" do
raise NotImplementedError,
"\e[1m\e[91m#{self.class}\e[0m belongs_to_docx_template :#{name} but didn't define \e[91m'#{name}_docx_hash'\e[0m"
end
private :docx_response_attr, "#{name}_docx_hash".to_sym, "#{name}_docx_request_paht".to_sym, "#{name}_request_attributes".to_sym
end
|