active_form_objects 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/concerns/relations.rb +35 -0
- data/lib/concerns/savings.rb +71 -0
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: daff81f1e757efcd09b602052b01108e5975ec4afdbc23eb990d9fd4fad28067
|
4
|
+
data.tar.gz: bd751ee1875ac9ff54e70d107106cbc041e499dedd9bb8b44147c0f902ea44eb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 23909b529fe45a1de257f237698b6be281d1ef4d8ec8d7f09a588783ced45552e4373ca185e6787a23f3be2552a3a8c3b08477b14daef69dc8082ebd1e026a60
|
7
|
+
data.tar.gz: 3ed88b54b54be83db842ebb37565eecfe194bc12c970b01b26871ef0d95c0364705ea6b01e376ea6d584fb6877025c34ad6143e23902dc3cb6fb8c9c63ae80e7
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
require 'active_model'
|
3
|
+
|
4
|
+
module Relations
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
@@relation_keys = {}
|
8
|
+
|
9
|
+
class RelationValidator < ActiveModel::EachValidator
|
10
|
+
def validate_each(record, attribute, value)
|
11
|
+
record.errors.add(attribute, "#{attribute} or #{attribute}_id is required") unless record.try(attribute).present? || record.try("#{attribute}_id".to_sym).present?
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def affect_relation_values(params)
|
16
|
+
@relation_keys = @@relation_keys[self.class.name] || []
|
17
|
+
params.keys.each do |key|
|
18
|
+
params["#{key}_id".to_sym] = params[key.to_sym].id if @relation_keys.include?(key)
|
19
|
+
end
|
20
|
+
|
21
|
+
params
|
22
|
+
end
|
23
|
+
|
24
|
+
class_methods do
|
25
|
+
def relations(*params)
|
26
|
+
validates *params, relation: true
|
27
|
+
|
28
|
+
params.each do |param|
|
29
|
+
relation_key = "#{param}_id".to_sym
|
30
|
+
send(:attributes, param, relation_key)
|
31
|
+
@@relation_keys[name] = (@@relation_keys[name] || []) + [param]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
|
3
|
+
module Savings
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
@@associated_forms = {}
|
7
|
+
|
8
|
+
class_methods do
|
9
|
+
def associated(key, form, params = nil)
|
10
|
+
@@associated_forms[name] ||= []
|
11
|
+
@@associated_forms[name] << { name: key, form: form, params: params }
|
12
|
+
|
13
|
+
set_default value: {}, to: key
|
14
|
+
end
|
15
|
+
|
16
|
+
def save(&block)
|
17
|
+
define_method(:save!) do
|
18
|
+
with_rescue do
|
19
|
+
ActiveRecord::Base.transaction do
|
20
|
+
validate!
|
21
|
+
instance_eval(&block)
|
22
|
+
@resource.try(:save!)
|
23
|
+
end
|
24
|
+
@resource
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def save!
|
31
|
+
with_rescue do
|
32
|
+
if @@associated_forms[self.class.name].nil?
|
33
|
+
validate!
|
34
|
+
@resource.update!(@params)
|
35
|
+
@resource
|
36
|
+
else
|
37
|
+
save_associated_forms!
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def save
|
43
|
+
save!
|
44
|
+
rescue
|
45
|
+
false
|
46
|
+
end
|
47
|
+
|
48
|
+
def save_associated_forms!
|
49
|
+
(@@associated_forms[self.class.name] || []).each do |associated|
|
50
|
+
params = associated[:params].nil? ? @params[associated[:name]] : send(associated[:params])
|
51
|
+
resource_to_save = send("#{associated[:name]}_to_update") rescue nil
|
52
|
+
@TheForm = associated[:form]
|
53
|
+
|
54
|
+
saved_form = @TheForm.new(params, resource_to_save).save!
|
55
|
+
instance_variable_set("@#{associated[:name]}", saved_form)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def set_associated_forms
|
60
|
+
(@@associated_forms[self.class.name] || []).each do |form|
|
61
|
+
instance_variable_set("@#{form[:name]}_form", form[:form])
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def with_rescue
|
66
|
+
yield
|
67
|
+
rescue ActiveRecord::RecordInvalid => e
|
68
|
+
e.record.errors.add(e.record.class.name, '')
|
69
|
+
raise ActiveRecord::RecordInvalid.new(e.record)
|
70
|
+
end
|
71
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_form_objects
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michaël Villeneuve
|
@@ -45,6 +45,8 @@ extensions: []
|
|
45
45
|
extra_rdoc_files: []
|
46
46
|
files:
|
47
47
|
- lib/active_form_objects.rb
|
48
|
+
- lib/concerns/relations.rb
|
49
|
+
- lib/concerns/savings.rb
|
48
50
|
homepage: https://github.com/fidme/active_form_objects
|
49
51
|
licenses:
|
50
52
|
- MIT
|