path_utilities 0.2.2 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +3 -2
- data/lib/path_utilities/form.rb +30 -18
- data/lib/path_utilities/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 376c2224a5ecd56519af4e2fa97fc6ba87d1853b
|
4
|
+
data.tar.gz: a70641cb2463673f3df64b8a87a3bdfa980e85fd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 65ed40c98c4bc0492145d16adaf9e5d71eeaaa1381c027fbb7294ab8fd3f70317c7add39243d2888b739c07b69d3935278574529e301bd6c849e99e99929b797
|
7
|
+
data.tar.gz: dcd8705b8baea65b5b17456d4ef320506925ab7b5179986db6a00d3baae829cf484548ed253da67bd4820f37cd01fa2a50949660ef8aadbe37d8c205c29e8114
|
data/README.md
CHANGED
@@ -40,6 +40,8 @@ Add a form for validating it
|
|
40
40
|
class FormUser
|
41
41
|
include PathUtilities::Form
|
42
42
|
|
43
|
+
setup_model_name :user
|
44
|
+
|
43
45
|
properties [:login, :name, :password], :user
|
44
46
|
|
45
47
|
validates_uniqueness_of :login
|
@@ -54,8 +56,7 @@ class MyController < ApplicationController
|
|
54
56
|
before_action :build, on: [:new, :create]
|
55
57
|
|
56
58
|
def create
|
57
|
-
if @form.validate(params[:user])
|
58
|
-
@form.save
|
59
|
+
if @form.validate(params[:user]) && @form.save
|
59
60
|
redirect_to sign_in_path
|
60
61
|
else
|
61
62
|
render :new
|
data/lib/path_utilities/form.rb
CHANGED
@@ -20,6 +20,8 @@ module PathUtilities
|
|
20
20
|
|
21
21
|
attr_reader :models_mapping
|
22
22
|
|
23
|
+
delegate :id, :persisted?, :new_record?, to: :main_record
|
24
|
+
|
23
25
|
def initialize(models_mapping = {})
|
24
26
|
@models_mapping = models_mapping
|
25
27
|
validate_mapping!
|
@@ -34,19 +36,17 @@ module PathUtilities
|
|
34
36
|
end
|
35
37
|
|
36
38
|
def validate(params)
|
39
|
+
params = HashWithIndifferentAccess.new(params)
|
37
40
|
self.class.fields.keys.each do |field|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
+
any_changes = params[field] &&
|
42
|
+
params[field] != instance_model_for(field).send(field)
|
43
|
+
next unless any_changes
|
44
|
+
send("#{field}=", params[field])
|
41
45
|
end
|
42
46
|
|
43
47
|
valid?
|
44
48
|
end
|
45
49
|
|
46
|
-
def persisted?
|
47
|
-
false
|
48
|
-
end
|
49
|
-
|
50
50
|
def save
|
51
51
|
if valid?
|
52
52
|
sync
|
@@ -76,6 +76,10 @@ module PathUtilities
|
|
76
76
|
end
|
77
77
|
end
|
78
78
|
|
79
|
+
def main_record
|
80
|
+
models_mapping[self.class.model_name.name.to_sym]
|
81
|
+
end
|
82
|
+
|
79
83
|
def persist!
|
80
84
|
models_mapping.values.all?(&:save)
|
81
85
|
end
|
@@ -84,42 +88,50 @@ module PathUtilities
|
|
84
88
|
|
85
89
|
class_methods do
|
86
90
|
def properties(attributes, model)
|
87
|
-
|
91
|
+
@@attributes ||= {}
|
88
92
|
add_model(model)
|
89
93
|
attributes.each do |att|
|
90
94
|
already_define_attribute_warn(att, model) do
|
91
95
|
attribute att, String
|
92
96
|
end
|
93
97
|
|
94
|
-
|
98
|
+
@@attributes[att] = model.to_s.camelize
|
95
99
|
.safe_constantize.fields[att.to_s]
|
96
100
|
end
|
97
101
|
end
|
98
102
|
|
103
|
+
def setup_model_name(name)
|
104
|
+
@@model_name = ActiveModel::Name.new(self, nil, name.to_s)
|
105
|
+
end
|
106
|
+
|
107
|
+
def model_name
|
108
|
+
@@model_name || fail('setup_model_name not set in form class')
|
109
|
+
end
|
110
|
+
|
99
111
|
def fields
|
100
|
-
|
112
|
+
@@attributes
|
101
113
|
end
|
102
114
|
|
103
115
|
def already_define_attribute_warn(attr, new_model)
|
104
|
-
if
|
116
|
+
if fields[attr].nil?
|
105
117
|
yield
|
106
118
|
else
|
107
119
|
Rails.logger.warn "#{attr} param already defined " \
|
108
|
-
"for #{
|
120
|
+
"for #{fields[attr]} model"
|
109
121
|
|
110
|
-
return unless
|
122
|
+
return unless fields[attr] != new_model
|
111
123
|
Rails.logger.warn "#{attr} now is mapped to #{new_model}"
|
112
124
|
end
|
113
125
|
end
|
114
126
|
|
115
127
|
def models
|
116
|
-
|
128
|
+
@@models ||= []
|
117
129
|
end
|
118
130
|
|
119
131
|
def add_model(name)
|
120
|
-
|
121
|
-
return if
|
122
|
-
|
132
|
+
@@models ||= []
|
133
|
+
return if @@models.include?(name.to_sym)
|
134
|
+
@@models << name.to_sym
|
123
135
|
end
|
124
136
|
|
125
137
|
def validates_uniqueness_of(attribute, options = {})
|
@@ -140,7 +152,7 @@ module PathUtilities
|
|
140
152
|
end
|
141
153
|
|
142
154
|
def model_for(attr)
|
143
|
-
|
155
|
+
fields[attr] && fields[attr].options[:klass]
|
144
156
|
end
|
145
157
|
|
146
158
|
# mongoid-encrypted-fields gem compatibility method
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: path_utilities
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ricard Forniol Agustí
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-06-
|
11
|
+
date: 2015-06-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: byebug
|