path_utilities 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 016c2c9cb836407b9f993c5fb039a63c49cad5b6
4
- data.tar.gz: fd2283f919f9caa809f002f15ff2bce71542636a
3
+ metadata.gz: 376c2224a5ecd56519af4e2fa97fc6ba87d1853b
4
+ data.tar.gz: a70641cb2463673f3df64b8a87a3bdfa980e85fd
5
5
  SHA512:
6
- metadata.gz: 234decbe01ceb25ea6468248427c99cd1019eec971a3694b8ee3e788de6fe90fa6fbcd92f75cc6092cdde9564483b6921daefcec5911b7a8353520b7fc925ca5
7
- data.tar.gz: a6e0670724b73f22a689e01a8c70f7ff47b195f7b694a281fbc7860350ba13d7fb23b105d2199a8473b15ae74e9582accbfa1ca98f96a6c6f2a98de9e3a95055
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
@@ -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
- if params[field].present?
39
- send("#{field}=", params[field])
40
- end
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
- @attributes ||= {}
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
- @attributes[att] = model.to_s.camelize
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
- @attributes
112
+ @@attributes
101
113
  end
102
114
 
103
115
  def already_define_attribute_warn(attr, new_model)
104
- if @attributes[attr].nil?
116
+ if fields[attr].nil?
105
117
  yield
106
118
  else
107
119
  Rails.logger.warn "#{attr} param already defined " \
108
- "for #{@attributes[attr]} model"
120
+ "for #{fields[attr]} model"
109
121
 
110
- return unless @attributes[attr] != new_model
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
- @models ||= []
128
+ @@models ||= []
117
129
  end
118
130
 
119
131
  def add_model(name)
120
- @models ||= []
121
- return if @models.include?(name.to_sym)
122
- @models << name.to_sym
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
- @attributes[attr] && @attributes[attr].options[:klass]
155
+ fields[attr] && fields[attr].options[:klass]
144
156
  end
145
157
 
146
158
  # mongoid-encrypted-fields gem compatibility method
@@ -1,3 +1,3 @@
1
1
  module PathUtilities
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.3"
3
3
  end
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.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-12 00:00:00.000000000 Z
11
+ date: 2015-06-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: byebug